Exemple #1
0
        private void SpreadMode(bool active, ScreenManager screenMgr)
        {
            RectTransform rectTScreenMgr = screenMgr.GetComponent <RectTransform>();

            var screenList = screenMgr.GetComponentsInChildren <BaseScreen>(true);

            if (active)
            {
                // Spread
                float screenCountX = 0, screenCountY = 0;
                for (int i = 0; i < screenList.Length; i++)
                {
                    BaseScreen screen = screenList[i];
                    screen.gameObject.SetActive(true);
                    RectTransform rectT = screen.GetComponent <RectTransform>();
                    if (rectT != null)
                    {
                        screenCountX += (rectTScreenMgr.rect.xMin - rectT.rect.xMin);
                        screenCountY += (rectTScreenMgr.rect.yMin - rectT.rect.yMin);

                        rectT.anchoredPosition = new Vector2(screenCountX, screenCountY);

                        screenCountY -= (rectTScreenMgr.rect.yMax - rectT.rect.yMax);

                        screenCountX += rectTScreenMgr.rect.width - (rectTScreenMgr.rect.xMax - rectT.rect.xMax);

                        if (i != 0 && (i % 4) == 0)
                        {
                            screenCountX  = 0;
                            screenCountY += rectT.rect.height;
                        }
                    }
                }
            }
            else
            {
                // Unspread
                for (int i = 0; i < screenList.Length; i++)
                {
                    BaseScreen screen = screenList[i];
                    screen.gameObject.SetActive(false);
                    RectTransform rectT = screen.GetComponent <RectTransform>();
                    if (rectT != null)
                    {
                        rectT.anchoredPosition = Vector2.zero;
                    }
                }
                if (screenMgr.defaultScreen != null)
                {
                    screenMgr.defaultScreen.gameObject.SetActive(true);
                }
            }
        }
        private static int CompareBaseScreens(BaseScreen x, BaseScreen y)
        {
            int result = 1;

            if (x != null && x is BaseScreen &&
                y != null && y is BaseScreen)
            {
                BaseScreen screenX = (BaseScreen)x;
                BaseScreen screenY = (BaseScreen)y;
                result = screenX.CompareTo(screenY);
            }
            return(result);
        }
        /// <summary>
        /// Hides all screns of specific type ( or removes them when they're a copy )
        /// </summary>
        public void HideAll()
        {
            Debug("HIDE ALL");

            foreach (var item in screenQueue)
            {
                if (item == Current)
                {
                    continue;
                }
                item.selectedObject = null;
                item.OnDeactivated(true, true);
            }

            screenToKill     = Current;
            screenQueueDirty = true;
        }
Exemple #4
0
        /// <summary>
        /// Clears navigation settings and removes CancelTrigger class from all selectables
        /// </summary>
        /// <param name="ScreenMgr"></param>
        /// <param name="children"></param>
        private void ClearNavigation(ScreenManager ScreenMgr, BaseScreen[] children)
        {
            for (int i = 0; i < children.Length; i++)
            {
                children[i].gameObject.SetActive(false);
            }

            List <Selectable> selectables = new List <Selectable>();

            for (int i = 0; i < children.Length; i++)
            {
                // Transform parent = children[i];
                Transform parent = children[i].transform;
                // BaseScreen screen = parent.GetComponent<BaseScreen>();
                BaseScreen screen = children[i];
                if (!screen.generateNavigation)
                {
                    continue;
                }

                parent.gameObject.SetActive(true);

                selectables.Clear();
                parent.GetComponentsInChildren <Selectable>(selectables);
                foreach (Selectable selectableUI in selectables)
                {
                    selectableUI.navigation = new Navigation()
                    {
                        mode = Navigation.Mode.Automatic
                    };
                    CancelTrigger sctrigger = selectableUI.gameObject.GetComponent <CancelTrigger>();

                    if (sctrigger != null)
                    {
                        DestroyImmediate(sctrigger);
                    }
                }
            }
            if (ScreenMgr.defaultScreen != null)
            {
                ScreenMgr.defaultScreen.gameObject.SetActive(true);
            }
        }
        /// <summary>
        /// Duplicates specified screen and shows it
        /// Good use for messageboxes, notification boxes or anything that can or will be shown multiple times
        /// ( Use a custom class inheriting BaseScreen with OnShow(...), see Popup for example )
        /// </summary>
        /// <param name="screenName"></param>
        /// <param name="data"></param>
        public T ShowPopup <T>(string screenName) where T : BaseScreen
        {
            if (!screensList.ContainsKey(screenName))
            {
                throw new KeyNotFoundException("ScreenManager: Show failed. Screen with name '" + screenName + "' does not exist.");
            }

            GameObject newDupeScreen = GameObject.Instantiate(screensList[screenName].gameObject);

            newDupeScreen.transform.SetParent(transform, false);
            BaseScreen baseScreen = newDupeScreen.GetComponent <BaseScreen>();

            baseScreen.Initialize(this, true);

            newDupeScreen.name = screenName + " (" + (baseScreen.ID) + ")";


            return(ShowScreen(baseScreen, true) as T);
        }
        private static void InsertionSort(IList <BaseScreen> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            int count = list.Count;

            for (int j = 1; j < count; j++)
            {
                BaseScreen key = list[j];

                int i = j - 1;
                for (; i >= 0 && CompareBaseScreens(list[i], key) > 0; i--)
                {
                    list[i + 1] = list[i];
                }
                list[i + 1] = key;
            }
        }
Exemple #7
0
        /// <summary>
        /// My black vodoo script, do not use outside
        /// </summary>
        /// <param name="ScreenMgr"></param>
        /// <param name="children"></param>
        private void UpdateNavigation(ScreenManager ScreenMgr, BaseScreen[] children)
        {
            for (int i = 0; i < children.Length; i++)
            {
                children[i].gameObject.SetActive(false);
            }

            List <Selectable> selectables = new List <Selectable>();

            for (int i = 0; i < children.Length; i++)
            {
                // Transform parent = children[i];
                Transform parent = children[i].transform;
                // BaseScreen screen = parent.GetComponent<BaseScreen>();
                BaseScreen screen = children[i];
                if (!screen.generateNavigation)
                {
                    continue;
                }

                parent.gameObject.SetActive(true);

                selectables.Clear();
                parent.GetComponentsInChildren <Selectable>(selectables);

                Selectable[] directions = new Selectable[4];
                foreach (Selectable selectableUI in selectables)
                {
                    //Debug.Log("<b>" + parent.name + "</b>." + selectableUI.name, selectableUI);

                    Transform t = selectableUI.transform;

                    directions[0] = FindSelectable(parent, t, selectables, t.rotation * Vector3.up);
                    directions[1] = FindSelectable(parent, t, selectables, t.rotation * Vector3.right);
                    directions[2] = FindSelectable(parent, t, selectables, t.rotation * Vector3.down);
                    directions[3] = FindSelectable(parent, t, selectables, t.rotation * Vector3.left);

                    if (selectableUI is Slider)
                    {
                        Slider.Direction dir = (selectableUI as Slider).direction;
                        if (dir == Slider.Direction.TopToBottom || dir == Slider.Direction.BottomToTop)
                        {
                            directions[0] = directions[2] = null;
                        }
                        else
                        {
                            directions[1] = directions[3] = null;
                        }
                    }

                    selectableUI.navigation = new Navigation()
                    {
                        mode          = Navigation.Mode.Explicit,
                        selectOnUp    = directions[0],
                        selectOnRight = directions[1],
                        selectOnDown  = directions[2],
                        selectOnLeft  = directions[3],
                    };


                    CancelTrigger sctrigger = selectableUI.gameObject.GetComponent <CancelTrigger>();

                    if (sctrigger == null)
                    {
                        var cancelHandlerAvailable = selectableUI.GetComponent <ICancelHandler>();
                        if (cancelHandlerAvailable == null)
                        {
                            sctrigger = selectableUI.gameObject.AddComponent <CancelTrigger>();
                            sctrigger.disableCancelHandler = cancelHandlerAvailable != null;
                        }
                    }
                }
                parent.gameObject.SetActive(false);
            }
            if (ScreenMgr.defaultScreen != null)
            {
                ScreenMgr.defaultScreen.gameObject.SetActive(true);
            }
        }
 /// <summary>
 /// Shows specified screen (with no return)
 /// </summary>
 /// <param name="screen"></param>
 public void Show(BaseScreen screen)
 {
     ShowScreen(screen);
 }
 /// <summary>
 /// Hides all screens ( clearing the stack ) and shows specified screen
 /// </summary>
 public void HideAllAndShow(BaseScreen screen)
 {
     HideAll();
     screenToShowInTheEnd = screen;
 }
        IEnumerator CoroutineUpdate()
        {
            var waitTime = new WaitForSeconds(0.1f);

            while (true)
            {
                if (screenQueueDirty)
                {
                    if (screenToKill != null && screenToKill == Current)
                    {
                        Debug("KILL: " + screenToKill);
                        if (onScreenHide != null)
                        {
                            onScreenHide.Invoke(Current);
                        }

                        if (screenQueue.Contains(screenToKill))
                        {
                            screenQueue.Remove(screenToKill);
                        }

                        EventSystem.current.SetSelectedGameObject(null);
                        screenToKill.selectedObject = null;
                        screenToKill.OnDeactivated(true, true);
                        if (screenToKill.keepOnTopWhenHiding)
                        {
                            screenToKeepOnTop = screenToKill;
                        }
                        screenToKill = null;
                        Current      = null;

                        if (screenToShowInTheEnd != null)
                        {
                            screenQueue.Add(screenToShowInTheEnd);
                            screenToShowInTheEnd = null;
                        }
                    }

                    BaseScreen maxPriorityScreen = screenQueue.LastOrDefault();

                    // Is highest-score screen different from current shown one? Then show highest-score screen and hide current
                    if (Current != maxPriorityScreen)
                    {
                        Debug("Different? " + Current + " != " + maxPriorityScreen);

                        BaseScreen previousScreen = Current;

                        if (previousScreen != null)
                        {
                            previousScreen.selectedObject = EventSystem.current.currentSelectedGameObject;
                            EventSystem.current.SetSelectedGameObject(null);
                        }

                        if (maxPriorityScreen.Transition)   // Wait for transition
                        {
                            Debug("Transition is busy?");
                            Current          = null;
                            screenQueueDirty = true;
                            yield return(waitTime);

                            continue;
                        }
                        else
                        {
                            Current = maxPriorityScreen;

                            if (Current == null && defaultScreen != null)
                            {
                                Current = defaultScreen;
                            }

                            if (Current != null)
                            {
                                if (onScreenShow != null)
                                {
                                    onScreenShow.Invoke(Current);
                                }
                                Current.OnActivated();
                            }

                            if (previousScreen != null)
                            {
                                previousScreen.OnDeactivated(Current.hideCurrent);
                            }
                            else if (Current.hideCurrent && screenQueue.Count > 1)
                            {
                                for (int i = screenQueue.Count - 2; i >= 0; i--)
                                {
                                    screenQueue[i].OnDeactivated(true);
                                }
                            }

                            if (screenToKeepOnTop != null && screenToKeepOnTop.isActiveAndEnabled)
                            {
                                screenToKeepOnTop.transform.SetAsLastSibling();
                                screenToKeepOnTop = null;
                            }
                        }
                    }

                    screenQueueDirty = false;
                }

                // Make sure we're always selecting something when always-on is enabled
                if (alwaysOnSelection)
                {
                    if (Current != null && !Current.Transition)
                    {
                        GameObject selectedGameObject = EventSystem.current.currentSelectedGameObject;
                        bool       isCurrentActive    = (selectedGameObject != null && selectedGameObject.activeInHierarchy);

                        if (!isCurrentActive)
                        {
                            if (lastSelection != null && lastSelection.activeInHierarchy && lastSelection.transform.IsChildOf(Current.transform))
                            {
                                EventSystem.current.SetSelectedGameObject(lastSelection);
                            }
                            else if (Current.defaultSelection != null && Current.defaultSelection.gameObject.activeInHierarchy)
                            {
                                EventSystem.current.SetSelectedGameObject(Current.defaultSelection.gameObject);
                                lastSelection = Current.defaultSelection.gameObject;
                            }
                        }
                        else
                        {
                            //Save last selection when everything is fine
                            lastSelection = selectedGameObject;
                        }
                    }
                }

                yield return(waitTime);
            }
        }
 /// <summary>
 /// Shows specified screen (with no return)
 /// </summary>
 /// <param name="screen"></param>
 public BaseScreen Show(BaseScreen screen, object data = null)
 {
     return(ShowScreen(screen, data));
 }
 public ScreenNavigationData(BaseScreen screen, object data = null)
 {
     this.screen = screen;
     this.data   = data;
 }