コード例 #1
0
    // HACK for finding canvas and drawing on it
    // Hardcoded for 3.2.4
    void FindViewportControl()
    {
        // Create temp control to get spatial viewport
        Control ctrl = new Control();

        AddControlToContainer(CustomControlContainer.SpatialEditorMenu, ctrl);

        // Try to get main viewport node. Must be `SpatialEditor`
        Control spatial_editor = ctrl.GetParent().GetParent <Control>();

        // Remove and destroy temp control
        RemoveControlFromContainer(CustomControlContainer.SpatialEditorMenu, ctrl);
        ctrl.QueueFree();

        spatial_viewport = null;
        if (spatial_editor.GetClass() == "SpatialEditor")
        {
            // Try to recursively find `SpatialEditorViewport`
            Func <Control, int, Control> get = null;
            get = (c, level) =>
            {
                if (c.GetClass() == "SpatialEditorViewport")
                {
                    return(c);
                }

                // 4 Levels must be enough for 3.2.4
                if (level < 4)
                {
                    foreach (var o in c.GetChildren())
                    {
                        if (o is Control ch)
                        {
                            var res = get(ch, level + 1);
                            if (res != null)
                            {
                                return(res);
                            }
                        }
                    }
                }

                return(null);
            };

            spatial_viewport = get(spatial_editor, 0)?.GetChild <ViewportContainer>(0);
        }

        if (spatial_viewport != null)
        {
            spatial_viewport.SetMeta("UseParentSize", true);
            spatial_viewport.Update();
        }
    }