Beispiel #1
0
        /// <summary>
        /// Handler for auto popup item destroy event.
        /// </summary>
        /// <param name="item">Popup menu item.</param>
        public static void OnAutoPopupItemDestroy(AutoPopupItemScript item)
        {
            DebugEx.VerboseFormat("PopupMenuAreaScript.OnAutoPopupItemDestroy(item = {0})", item);

            if (sInstance != null)
            {
                if (sInstance.mPopupMenus.Count > 0)
                {
                    if (sInstance.mAutoPopupItem == item)
                    {
                        sInstance.mAutoPopupItem = null;
                        sInstance.mTimer.Stop();
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Handler for auto popup item exit event.
        /// </summary>
        /// <param name="item">Popup menu item.</param>
        public static void OnAutoPopupItemExit(AutoPopupItemScript item)
        {
            DebugEx.VerboseFormat("PopupMenuAreaScript.OnAutoPopupItemExit(item = {0})", item);

            if (sInstance != null)
            {
                if (sInstance.mPopupMenus.Count > 0)
                {
                    sInstance.mAutoPopupItem = null;
                    sInstance.mTimer.Stop();
                }
            }
            else
            {
                DebugEx.Error("There is no PopupMenuAreaScript instance");
            }
        }
 public static void OnAutoPopupItemDestroy(AutoPopupItemScript item)
 {
     if (instance != null)
     {
         if (instance.mPopupMenus.Count > 0)
         {
             if (instance.mAutoPopupItem == item)
             {
                 instance.mAutoPopupItem = null;
                 instance.StopTimer();
             }
         }
     }
     else
     {
         Debug.LogError("There is no PopupMenuAreaScript instance");
     }
 }
Beispiel #4
0
        /// <summary>
        /// Script starting callback.
        /// </summary>
        void Start()
        {
            DebugEx.Verbose("PopupMenuAreaScript.Start()");

            if (sInstance == null)
            {
                sInstance = this;
            }
            else
            {
                DebugEx.Error("Two instances of PopupMenuAreaScript not supported");
            }

            mPopupMenus    = new List <PopupMenu>();
            mAutoPopupItem = null;
            mTimer         = new Timer(OnAutoPopupTimeout);

            enabled = false;
        }
Beispiel #5
0
        /// <summary>
        /// Show popup menu at the specified coordinates.
        /// </summary>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="left">Left edge for button of parent popup if present.</param>
        /// <param name="bottom">Bottom edge for button of parent popup if present.</param>
        public void Show(float x, float y, float left = -1, float bottom = -1)
        {
            DebugEx.VerboseFormat("PopupMenu.Show(x = {0}, y = {1}, left = {2}, bottom = {3})", x, y, left, bottom);

            PopupMenuAreaScript.RegisterPopupMenu(this);

            //***************************************************************************
            // PopupMenu GameObject
            //***************************************************************************
            #region PopupMenu GameObject
            mGameObject = new GameObject("PopupMenu");
            Utils.InitUIObject(mGameObject, PopupMenuAreaScript.geometry);

            //===========================================================================
            // RectTransform Component
            //===========================================================================
            #region RectTransform Component
            RectTransform popupMenuTransform = mGameObject.AddComponent <RectTransform>();
            #endregion

            //===========================================================================
            // CanvasRenderer Component
            //===========================================================================
            #region CanvasRenderer Component
            mGameObject.AddComponent <CanvasRenderer>();
            #endregion

            //===========================================================================
            // Image Component
            //===========================================================================
            #region Image Component
            Image popupMenuImage = mGameObject.AddComponent <Image>();

            popupMenuImage.sprite = Assets.Common.Popups.Textures.popupBackground.sprite;
            popupMenuImage.type   = Image.Type.Sliced;
            #endregion

            //***************************************************************************
            // ScrollArea GameObject
            //***************************************************************************
            #region ScrollArea GameObject
            GameObject scrollArea = new GameObject("ScrollArea");
            Utils.InitUIObject(scrollArea, mGameObject.transform);

            //===========================================================================
            // RectTransform Component
            //===========================================================================
            #region RectTransform Component
            RectTransform scrollAreaTransform = scrollArea.AddComponent <RectTransform>();
            Utils.AlignRectTransformStretchStretch(
                scrollAreaTransform
                , popupMenuImage.sprite.border.x
                , popupMenuImage.sprite.border.w
                , popupMenuImage.sprite.border.z
                , popupMenuImage.sprite.border.y
                );
            #endregion

            //***************************************************************************
            // Content GameObject
            //***************************************************************************
            #region Content GameObject
            GameObject scrollAreaContent = new GameObject("Content");
            Utils.InitUIObject(scrollAreaContent, scrollArea.transform);

            //===========================================================================
            // RectTransform Component
            //===========================================================================
            #region RectTransform Component
            RectTransform scrollAreaContentTransform = scrollAreaContent.AddComponent <RectTransform>();
            #endregion

            float contentWidth  = 0f;
            float contentHeight = 0f;

            //===========================================================================
            // Fill content
            //===========================================================================
            #region Fill content
            // Create menu item buttons
            ReadOnlyCollection <TreeNode <CustomMenuItem> > menuItems = mItems.children;

            foreach (TreeNode <CustomMenuItem> menuItem in menuItems)
            {
                if (menuItem.data.visible)
                {
                    if (menuItem.data is MenuSeparatorItem)
                    {
                        //***************************************************************************
                        // Separator GameObject
                        //***************************************************************************
                        #region Separator GameObject
                        GameObject menuSeparator = new GameObject("Separator");
                        Utils.InitUIObject(menuSeparator, scrollAreaContent.transform);

                        //===========================================================================
                        // RectTransform Component
                        //===========================================================================
                        #region RectTransform Component
                        float separatorHeight = 8;

                        RectTransform menuItemSeparatorTransform = menuSeparator.AddComponent <RectTransform>();

                        Utils.AlignRectTransformTopStretch(menuItemSeparatorTransform, separatorHeight, contentHeight, 28f);

                        contentHeight += separatorHeight;
                        #endregion

                        //===========================================================================
                        // Image Component
                        //===========================================================================
                        #region Image Component
                        Image image = menuSeparator.AddComponent <Image>();

                        image.sprite = Assets.Common.Popups.Textures.separator.sprite;
                        #endregion
                        #endregion
                    }
                    else
                    if (menuItem.data is MenuItem)
                    {
                        MenuItem item = menuItem.data as MenuItem;

                        bool hasChildren = menuItem.HasChildren();
                        bool enabled     = item.enabled && (hasChildren || item.onClick != null);

                        //***************************************************************************
                        // Button GameObject
                        //***************************************************************************
                        #region Button GameObject
                        GameObject menuItemButton = new GameObject(item.name);
                        Utils.InitUIObject(menuItemButton, scrollAreaContent.transform);

                        //===========================================================================
                        // RectTransform Component
                        //===========================================================================
                        #region RectTransform Component
                        RectTransform menuItemButtonTransform = menuItemButton.AddComponent <RectTransform>();
                        #endregion

                        //===========================================================================
                        // CanvasRenderer Component
                        //===========================================================================
                        #region CanvasRenderer Component
                        menuItemButton.AddComponent <CanvasRenderer>();
                        #endregion

                        //===========================================================================
                        // Image Component
                        //===========================================================================
                        #region Image Component
                        Image menuItemButtonImage = menuItemButton.AddComponent <Image>();

                        menuItemButtonImage.sprite = Assets.Common.Popups.Textures.button.sprite;
                        menuItemButtonImage.type   = Image.Type.Sliced;
                        #endregion

                        //===========================================================================
                        // Button Component
                        //===========================================================================
                        #region Button Component
                        Button menuItemButtonButton = menuItemButton.AddComponent <Button>();

                        menuItemButtonButton.targetGraphic = menuItemButtonImage;
                        menuItemButtonButton.transition    = Selectable.Transition.SpriteSwap;

                        if (enabled)
                        {
                            menuItemButtonButton.spriteState = Assets.Common.Popups.SpriteStates.button.spriteState;
                            menuItemButtonButton.navigation  = Internal.PopupMenuCommon.defaultNavigation;

                            if (hasChildren)
                            {
                                TreeNode <CustomMenuItem> currentMenuItem = menuItem;

                                menuItemButtonButton.onClick.AddListener(() => OnShowMenuSubItems(currentMenuItem));

                                //===========================================================================
                                // AutoPopupItemScript Component
                                //===========================================================================
                                #region AutoPopupItemScript Component
                                AutoPopupItemScript menuItemButtonAutoPopup = menuItemButton.AddComponent <AutoPopupItemScript>();

                                menuItemButtonAutoPopup.delay = AUTO_POPUP_DELAY;
                                #endregion
                            }
                            else
                            {
                                menuItemButtonButton.onClick.AddListener(item.onClick);

                                if (item.radioGroup != null)
                                {
                                    menuItemButtonButton.onClick.AddListener(() => OnSelectItem(item));
                                }

                                menuItemButtonButton.onClick.AddListener(PopupMenuAreaScript.DestroyAll);
                            }
                        }
                        else
                        {
                            menuItemButtonButton.spriteState = Assets.Common.Popups.SpriteStates.buttonDisabled.spriteState;
                            menuItemButtonButton.navigation  = Internal.PopupMenuCommon.noneNavigation;
                        }
                        #endregion
                        #endregion

                        //***************************************************************************
                        // Text GameObject
                        //***************************************************************************
                        #region Text GameObject
                        GameObject menuItemText = new GameObject("Text");
                        Utils.InitUIObject(menuItemText, menuItemButton.transform);

                        //===========================================================================
                        // RectTransform Component
                        //===========================================================================
                        #region RectTransform Component
                        RectTransform menuItemTextTransform = menuItemText.AddComponent <RectTransform>();
                        Utils.AlignRectTransformStretchStretch(menuItemTextTransform, Assets.Common.Popups.Textures.background.sprite.border.x + 4f);
                        #endregion

                        //===========================================================================
                        // Text Component
                        //===========================================================================
                        #region Text Component
                        Text menuItemTextText = menuItemText.AddComponent <Text>();

                        if (enabled)
                        {
                            Assets.Common.Popups.TextStyles.button.Apply(menuItemTextText);
                        }
                        else
                        {
                            Assets.Common.Popups.TextStyles.buttonDisabled.Apply(menuItemTextText);
                        }

                        menuItemTextText.text = item.text;
                        #endregion
                        #endregion

                        float buttonWidth  = menuItemTextText.preferredWidth + Assets.Common.Popups.Textures.background.sprite.border.x + 16f;
                        float buttonHeight = 22f; // TODO: [Trivial] Report request for prefferedHeight for specified width

                        Utils.AlignRectTransformTopStretch(menuItemButtonTransform, buttonHeight, contentHeight);

                        if (buttonWidth > contentWidth)
                        {
                            contentWidth = buttonWidth;
                        }

                        contentHeight += buttonHeight;
                    }
                    else
                    {
                        DebugEx.Error("Unknown menu item type");
                    }
                }
            }

            //***************************************************************************
            // Shortcuts
            //***************************************************************************
            #region Shortcuts
            float shortcutWidth = 0f;
            int   index         = 0;

            for (int i = 0; i < menuItems.Count; ++i)
            {
                if (menuItems[i].data.visible)
                {
                    if (menuItems[i].data is MenuItem)
                    {
                        MenuItem item     = menuItems[i].data as MenuItem;
                        string   shortcut = item.shortcut;

                        if (shortcut != null)
                        {
                            Transform menuItemButtonTransform = scrollAreaContentTransform.GetChild(index);

                            //***************************************************************************
                            // Text GameObject
                            //***************************************************************************
                            #region Text GameObject
                            GameObject menuItemText = menuItemButtonTransform.GetChild(0).gameObject; // Button/Text
                            GameObject shortcutText = UnityEngine.Object.Instantiate(menuItemText) as GameObject;

                            Utils.InitUIObject(shortcutText, menuItemButtonTransform);
                            shortcutText.name = "Shortcut";

                            //===========================================================================
                            // Text Component
                            //===========================================================================
                            #region Text Component
                            Text text = shortcutText.GetComponent <Text>();

                            text.text      = shortcut;
                            text.alignment = TextAnchor.MiddleRight;

                            float shortcutTextWidth = text.preferredWidth;

                            if (shortcutTextWidth > shortcutWidth)
                            {
                                shortcutWidth = shortcutTextWidth;
                            }
                            #endregion
                            #endregion
                        }
                    }

                    ++index;
                }
            }

            if (shortcutWidth > 0f)
            {
                index = 0;

                for (int i = 0; i < menuItems.Count; ++i)
                {
                    if (menuItems[i].data.visible)
                    {
                        if (menuItems[i].data is MenuItem)
                        {
                            MenuItem item     = menuItems[i].data as MenuItem;
                            string   shortcut = item.shortcut;

                            if (shortcut != null)
                            {
                                Transform menuItemButtonTransform = scrollAreaContentTransform.GetChild(index);

                                //***************************************************************************
                                // Text GameObject
                                //***************************************************************************
                                #region Text GameObject
                                GameObject menuItemText = menuItemButtonTransform.GetChild(0).gameObject; // Button/Text
                                GameObject shortcutText = menuItemButtonTransform.GetChild(1).gameObject; // Button/Text

                                //===========================================================================
                                // RectTransform Component
                                //===========================================================================
                                #region RectTransform Component
                                RectTransform menuItemTextTransform = menuItemText.transform as RectTransform;

                                menuItemTextTransform.offsetMax = new Vector2(-shortcutWidth, 0f);
                                #endregion

                                //===========================================================================
                                // RectTransform Component
                                //===========================================================================
                                #region RectTransform Component
                                RectTransform shortcutTextTransform = shortcutText.transform as RectTransform;

                                Utils.AlignRectTransformStretchRight(shortcutTextTransform, shortcutWidth, 4);
                                #endregion
                                #endregion
                            }
                        }

                        ++index;
                    }
                }

                contentWidth += shortcutWidth + 8;
            }
            #endregion

            //***************************************************************************
            // Arrow
            //***************************************************************************
            #region Arrow
            float arrowWidth = 0f;

            for (int i = 0; i < menuItems.Count; ++i)
            {
                if (menuItems[i].data.visible)
                {
                    if (menuItems[i].data is MenuItem)
                    {
                        if (menuItems[i].HasChildren())
                        {
                            arrowWidth = 16f;

                            break;
                        }
                    }
                }
            }

            if (arrowWidth > 0f)
            {
                index = 0;

                for (int i = 0; i < menuItems.Count; ++i)
                {
                    if (menuItems[i].data.visible)
                    {
                        if (menuItems[i].data is MenuItem)
                        {
                            Transform menuItemButtonTransform = scrollAreaContentTransform.GetChild(index);

                            //***************************************************************************
                            // Text GameObject
                            //***************************************************************************
                            #region Text GameObject
                            GameObject menuItemText = menuItemButtonTransform.GetChild(0).gameObject; // Button/Text
                            GameObject shortcutText = null;

                            if (menuItemButtonTransform.childCount > 1)
                            {
                                shortcutText = menuItemButtonTransform.GetChild(1).gameObject; // Button/Text
                            }

                            //===========================================================================
                            // RectTransform Component
                            //===========================================================================
                            #region RectTransform Component
                            RectTransform menuItemTextTransform = menuItemText.transform as RectTransform;

                            menuItemTextTransform.offsetMax = new Vector2(menuItemTextTransform.offsetMax.x - arrowWidth, 0f);
                            #endregion

                            if (shortcutText != null)
                            {
                                //===========================================================================
                                // RectTransform Component
                                //===========================================================================
                                #region RectTransform Component
                                RectTransform shortcutTextTransform = shortcutText.transform as RectTransform;

                                shortcutTextTransform.offsetMin = new Vector2(shortcutTextTransform.offsetMin.x - arrowWidth, 0f);
                                shortcutTextTransform.offsetMax = new Vector2(shortcutTextTransform.offsetMax.x - arrowWidth, 0f);
                                #endregion
                            }
                            #endregion

                            if (menuItems[i].HasChildren())
                            {
                                //***************************************************************************
                                // Image GameObject
                                //***************************************************************************
                                #region Image GameObject
                                GameObject arrow = new GameObject("Arrow");
                                Utils.InitUIObject(arrow, menuItemButtonTransform);

                                //===========================================================================
                                // RectTransform Component
                                //===========================================================================
                                #region RectTransform Component
                                RectTransform arrowTransform = arrow.AddComponent <RectTransform>();

                                Utils.AlignRectTransformStretchRight(arrowTransform, arrowWidth, 4, 3, 3);
                                #endregion

                                //===========================================================================
                                // CanvasRenderer Component
                                //===========================================================================
                                #region CanvasRenderer Component
                                arrow.AddComponent <CanvasRenderer>();
                                #endregion

                                //===========================================================================
                                // Image Component
                                //===========================================================================
                                #region Image Component
                                Image arrowImage = arrow.AddComponent <Image>();

                                arrowImage.sprite = Assets.Common.Popups.Textures.arrow.sprite;
                                arrowImage.type   = Image.Type.Sliced;
                                #endregion
                                #endregion
                            }
                        }

                        ++index;
                    }
                }

                contentWidth += arrowWidth + 8;
            }
            #endregion

            //***************************************************************************
            // Checkbox
            //***************************************************************************
            #region Checkbox
            float checkboxWidth = 22f;
            index = 0;

            for (int i = 0; i < menuItems.Count; ++i)
            {
                if (menuItems[i].data.visible)
                {
                    if (menuItems[i].data is MenuItem)
                    {
                        MenuItem item = menuItems[i].data as MenuItem;

                        if (item.radioGroup != null && item.radioGroup.selectedItem == item)
                        {
                            Transform menuItemButtonTransform = scrollAreaContentTransform.GetChild(index);

                            //***************************************************************************
                            // Image GameObject
                            //***************************************************************************
                            #region Image GameObject
                            GameObject checkbox = new GameObject("Checkbox");
                            Utils.InitUIObject(checkbox, menuItemButtonTransform);

                            //===========================================================================
                            // RectTransform Component
                            //===========================================================================
                            #region RectTransform Component
                            RectTransform checkboxTransform = checkbox.AddComponent <RectTransform>();

                            Utils.AlignRectTransformStretchLeft(checkboxTransform, checkboxWidth);
                            #endregion

                            //===========================================================================
                            // CanvasRenderer Component
                            //===========================================================================
                            #region CanvasRenderer Component
                            checkbox.AddComponent <CanvasRenderer>();
                            #endregion

                            //===========================================================================
                            // Image Component
                            //===========================================================================
                            #region Image Component
                            Image checkboxImage = checkbox.AddComponent <Image>();

                            checkboxImage.sprite = Assets.Common.Popups.Textures.checkbox.sprite;
                            checkboxImage.type   = Image.Type.Sliced;
                            #endregion
                            #endregion
                        }
                    }

                    ++index;
                }
            }
            #endregion
            #endregion

            Utils.AlignRectTransformTopStretch(scrollAreaContentTransform, contentHeight);
            #endregion

            //===========================================================================
            // ScrollRect Component
            //===========================================================================
            #region ScrollRect Component
            ScrollRect scrollAreaScrollRect = scrollArea.AddComponent <ScrollRect>();

            scrollAreaScrollRect.content    = scrollAreaContentTransform;
            scrollAreaScrollRect.horizontal = false;
            #endregion

            //===========================================================================
            // CanvasRenderer Component
            //===========================================================================
            #region CanvasRenderer Component
            scrollArea.AddComponent <CanvasRenderer>();
            #endregion

            //===========================================================================
            // Image Component
            //===========================================================================
            #region Image Component
            Image scrollAreaImage = scrollArea.AddComponent <Image>();

            scrollAreaImage.sprite = Assets.Common.Popups.Textures.background.sprite;
            scrollAreaImage.type   = Image.Type.Sliced;
            #endregion

            //===========================================================================
            // Mask Component
            //===========================================================================
            #region Mask Component
            scrollArea.AddComponent <Mask>();
            #endregion
            #endregion

            float popupMenuWidth  = contentWidth + 11;
            float popupMenuHeight = contentHeight + 11;

            Utils.FitRectTransformToScreen(popupMenuTransform, popupMenuWidth, popupMenuHeight, x, y, left, bottom, SHADOW_WIDTH, SHADOW_WIDTH);
            #endregion
        }
        /// <summary>
        /// Script starting callback.
        /// </summary>
        void Start()
        {
            if (instance == null)
            {
                instance = this;
            }
            else
            {
                Debug.LogError("Two instances of PopupMenuAreaScript not supported");
            }

            mPopupMenus    = new List<PopupMenu>();
            mAutoPopupItem = null;
            mRemainingTime = TIMER_NOT_ACTIVE;

            enabled = false;
        }
 public static void OnAutoPopupItemEnter(AutoPopupItemScript item)
 {
     if (instance != null)
     {
         if (instance.mPopupMenus.Count > 0)
         {
             instance.mAutoPopupItem = item;
             instance.StartTimer(instance.mAutoPopupItem.delay);
         }
     }
     else
     {
         Debug.LogError("There is no PopupMenuAreaScript instance");
     }
 }