Ejemplo n.º 1
0
        public OverlayMenu(OverlayCanvas canvas)
        {
            m_Canvas = canvas;
            name     = ussClassName;
            if (s_TreeAsset == null)
            {
                s_TreeAsset = EditorGUIUtility.Load(k_UxmlPath) as VisualTreeAsset;
            }

            if (s_TreeAsset != null)
            {
                s_TreeAsset.CloneTree(this);
            }

            AddToClassList(ussClassName);

            m_Toggle = this.Q <Toggle>("overlay-toggle");
            m_Toggle.RegisterCallback <ChangeEvent <bool> >((evt) =>
            {
                canvas.SetOverlaysEnabled(evt.newValue);
            });

            m_ListRoot                 = this.Q <ListView>("OverlayList");
            m_ListRoot.makeItem        = CreateListItem;
            m_ListRoot.bindItem        = BindListItem;
            m_ListRoot.itemsSource     = m_OverlayToShow;
            m_ListRoot.fixedItemHeight = 20;
            m_ListRoot.selectionType   = SelectionType.None;

            m_Dropdown          = this.Q <Button>("preset-dropdown");
            m_Dropdown.clicked += DropdownOnClicked;

            m_DropdownText = this.Q <TextElement>(classes: "preset-dropdown-text");
            var lastSavedPreset = canvas.lastAppliedPresetName;

            m_DropdownText.text = string.IsNullOrEmpty(lastSavedPreset) ? L10n.Tr("Window Preset") : lastSavedPreset;

            canvas.afterOverlaysInitialized += () =>
            {
                m_DropdownText.text = canvas.lastAppliedPresetName;
            };

            RegisterCallback <FocusOutEvent>(evt =>
            {
                if (s_KeepAlive)
                {
                    var focusController  = evt.relatedTarget?.focusController;
                    s_DelayUntilCanHide += () => CheckIfShouldHide(focusController?.focusedElement as VisualElement);
                }
                else
                {
                    CheckIfShouldHide(evt.relatedTarget as VisualElement);
                }
            });

            canvas.overlaysEnabledChanged += OnOverlayEnabledChanged;

            focusable = true;
            Hide();
        }
Ejemplo n.º 2
0
        public OverlayPopup(Overlay overlay)
        {
            name = "overlay-popup";
            Overlay.treeAsset.CloneTree(this);

            this.Q(Overlay.k_CollapsedContent)?.RemoveFromHierarchy();
            this.Q(null, Overlay.k_Header)?.RemoveFromHierarchy();

            focusable   = true;
            pickingMode = PickingMode.Position;
            AddToClassList(Overlay.ussClassName);
            style.position = Position.Absolute;

            var root = this.Q("overlay-content");

            root.renderHints = RenderHints.ClipWithScissors;
            root.Add(overlay.CreatePanelContent());

            RegisterCallback <MouseEnterEvent>(evt => m_CursorIsOverPopup = true);
            RegisterCallback <MouseLeaveEvent>(evt => m_CursorIsOverPopup = false);

            RegisterCallback <GeometryChangedEvent>(evt =>
            {
                var proposed  = overlay.collapsedButtonRect;
                proposed.size = evt.newRect.size;
                var placement = OverlayCanvas.ClampRectToBounds(overlay.canvas.windowRoot.worldBound, proposed);

                if (!Mathf.Approximately(proposed.position.x, placement.position.x))
                {
                    this.EnableInClassList(k_Clamped, true);
                }

                var canvasWorld = overlay.canvas.rootVisualElement.worldBound;

                if (overlay.layout == Layout.HorizontalToolbar)
                {
                    this.EnableInClassList(k_FromHorizontal, true);
                }
                else if (overlay.layout == Layout.VerticalToolbar)
                {
                    this.EnableInClassList(k_FromVertical, true);
                }

                if (!overlay.isInToolbar)
                {
                    this.EnableInClassList(k_OutsideToolbar, true);
                    var overlayWorldBound = overlay.rootVisualElement.worldBound;

                    var rightPlacement = overlayWorldBound.x + overlayWorldBound.width;
                    var rightSideSpace = canvasWorld.xMax - rightPlacement;

                    var xAdjusted = placement.position.x;
                    if (rightSideSpace >= placement.width)
                    {
                        xAdjusted = rightPlacement;
                    }
                    else
                    {
                        var leftSideSpace = placement.x - overlay.canvas.rootVisualElement.worldBound.x;
                        if (leftSideSpace >= placement.width)
                        {
                            xAdjusted = overlayWorldBound.x - placement.width;
                        }
                        else // If neither side has enough space, show the popup on the widest one
                        {
                            if (rightSideSpace > leftSideSpace)
                            {
                                xAdjusted = overlayWorldBound.x + overlayWorldBound.width;
                            }
                            else
                            {
                                xAdjusted = overlayWorldBound.x - placement.width;
                            }
                        }
                    }
                    placement.position = new Vector2(xAdjusted, placement.position.y);
                }

                transform.position = placement.position - canvasWorld.position;
            });
        }