コード例 #1
0
        private static void SetPreviousNavigation(Selectable lastElement, UiEventNavigation currentElement)
        {
            // Check if the last and current element is available
            if (currentElement != null)
            {
                // Grab the current navigation values
                Navigation newNavigation = currentElement.Selectable.navigation;

                // Customize the navigation for vertical
                if ((currentElement.ToPreviousUi & UiEventNavigation.Direction.Up) != 0)
                {
                    newNavigation.selectOnUp = lastElement;
                }
                if ((currentElement.ToPreviousUi & UiEventNavigation.Direction.Down) != 0)
                {
                    newNavigation.selectOnDown = lastElement;
                }

                // Check if element is not a slider
                if ((currentElement.Selectable is Slider) == false)
                {
                    if ((currentElement.ToPreviousUi & UiEventNavigation.Direction.Left) != 0)
                    {
                        newNavigation.selectOnLeft = lastElement;
                    }
                    if ((currentElement.ToPreviousUi & UiEventNavigation.Direction.Right) != 0)
                    {
                        newNavigation.selectOnRight = lastElement;
                    }
                }
                currentElement.Selectable.navigation = newNavigation;
            }
        }
コード例 #2
0
        private UiEventNavigation[] GenerateLanguageCheckboxes()
        {
            // Setup return variable
            UiEventNavigation[] returnToggles = new UiEventNavigation[Translations.SupportedLanguages.Count];

            // Setup the first button
            StringBuilder nameBuilder = new StringBuilder();

            SetupToggle(languageCheckbox, Translations.SupportedLanguages[0], nameBuilder);
            returnToggles[0] = languageCheckbox.Navigation;

            // Setup the rest of the buttons
            LanguageToggle clonedToggle;

            for (int index = 1; index < Translations.SupportedLanguages.Count; ++index)
            {
                // Duplicate the toggle
                clonedToggle = DuplicateToggle(languageCheckbox);

                // Setup the toggle
                SetupToggle(clonedToggle, Translations.SupportedLanguages[index], nameBuilder);
                returnToggles[index] = clonedToggle.Navigation;
            }

            // Setup the currently selected toggle
            if (languageToControlMap.TryGetValue(Translations.CurrentLanguage, out currentSelectedCheckbox) == true)
            {
                // Setup the last selected toggle
                currentSelectedCheckbox.Checkbox.isOn = true;
            }
            return(returnToggles);
        }
コード例 #3
0
 private void MenuNavigator_OnAfterEnabledAndActiveChanged(UiEventNavigation source, bool arg)
 {
     // Check if the menu is enabled
     if (arg == true)
     {
         UpdateNavigation();
     }
 }
コード例 #4
0
 private void MenuNavigator_OnAfterSubmit(UiEventNavigation source, BaseEventData arg)
 {
     // Check if submitting to this menu causes some UI to be enabled/disabled
     if ((source != null) && (source.DoesSubmitChangesInteractable == true) && (nextFrame == null))
     {
         // If so, update navigation UI
         UpdateNavigation();
     }
 }
コード例 #5
0
 private void MenuNavigator_OnAfterCancel(UiEventNavigation source, BaseEventData arg)
 {
     // Make sure the menu is managed and NOT the default
     if ((Menu != null) && (Menu.MenuType == IMenu.Type.ManagedMenu))
     {
         // Hide the menu
         Menu.Hide();
     }
 }
コード例 #6
0
 private void MenuNavigator_OnAfterSelect(UiEventNavigation source, BaseEventData arg)
 {
     // Check if we have the scroll view open
     if (scrollable != null)
     {
         // Scroll to this control
         ScrollingHelper.ScrollVerticallyTo(scrollable, source);
     }
     LastSelectedElement = source;
 }
コード例 #7
0
        public UiEventNavigation ScrollToLastSelectedElement(UiEventNavigation defaultElement, bool forceCenter)
        {
            // Make sure the last selected element is within the scrollable list
            if ((LastSelectedElement != null) && (UiElementsInScrollableSet.Contains(LastSelectedElement) == true))
            {
                // Change the default element to the last selected one
                defaultElement = LastSelectedElement;
            }

            // Scroll to this element
            ScrollToSelectable(defaultElement, forceCenter);
            return(defaultElement);
        }
コード例 #8
0
        public void ScrollToSelectable(UiEventNavigation selectable, bool forceCenter)
        {
            // Check if we have the scroll view open
            if ((scrollable != null) && (selectable != null))
            {
                // Scroll to this control
                ScrollingHelper.ScrollVerticallyTo(scrollable, selectable, boundsCache, forceCenter);

                // Highlight this element
                MenuManager manager = Singleton.Get <MenuManager>();
                if (manager != null)
                {
                    manager.SelectGui(selectable.Selectable);
                }
            }
        }
コード例 #9
0
        private void SetupUiElementsInScrollable(UiEventNavigation nextElement, ref UiEventNavigation lastElement, ref UiEventNavigation topMostElement)
        {
            // Check if this is the top-most element
            if (topMostElement == null)
            {
                // If so, set the variable
                topMostElement = nextElement;
            }

            // Check if there's an element above this
            ResetNavigation(nextElement.Selectable);
            SetNextNavigation(lastElement, nextElement.Selectable, (enableNavigatingToSlider ? VerticalScrollbar : null));
            if (lastElement != null)
            {
                SetPreviousNavigation(lastElement.Selectable, nextElement);
            }
            lastElement = nextElement;
        }
コード例 #10
0
        public static void ScrollVerticallyTo(ScrollRect parentScrollRect, UiEventNavigation childControl, Dictionary <UiEventNavigation, SingleAxisBounds> cacheDict = null, bool centerTo = false)
        {
            if ((parentScrollRect != null) && (childControl != null) && (childControl.Selectable != null))
            {
                // Check the cache, and see if the bounds for this child control already exists
                SingleAxisBounds childBounds;
                if ((cacheDict == null) || (cacheDict.TryGetValue(childControl, out childBounds) == false))
                {
                    // Calculate the top and bottom position of the the control
                    float topPos, centerPos, bottomPos;
                    centerPos   = GetVerticalAnchoredPositionInContent(parentScrollRect.content, childControl, out topPos, out bottomPos);
                    childBounds = new SingleAxisBounds(topPos, centerPos, bottomPos);

                    // Cache these values
                    if (cacheDict != null)
                    {
                        cacheDict.Add(childControl, childBounds);
                    }
                }

                // Check whether we need to scroll or not, and if so, in which snapping direction
                ScrollVerticalSnap snapTo = ScrollVerticalSnap.CenterToChild;
                if (centerTo == false)
                {
                    snapTo = GetVerticalSnapping(parentScrollRect.content, parentScrollRect.viewport, ref childBounds);
                }

                // Check whether we want to scroll or not
                if (snapTo != ScrollVerticalSnap.None)
                {
                    // Grab the position to scroll to
                    float selectionPosition = GetScrollToPosition(parentScrollRect.viewport, snapTo, ref childBounds);

                    // Clamp the selection position value
                    float maxPosition = (parentScrollRect.content.rect.height - parentScrollRect.viewport.rect.height);
                    selectionPosition = Mathf.Clamp(selectionPosition, 0, maxPosition);

                    // Directly set the position of the ScrollRect's content
                    Vector3 scrollPosition = parentScrollRect.content.anchoredPosition;
                    scrollPosition.y = selectionPosition;
                    parentScrollRect.content.anchoredPosition = scrollPosition;
                }
            }
        }
コード例 #11
0
        private Scrollbar SetupHorizontalScrollBar(UiEventNavigation lastElement)
        {
            Scrollbar horizontalScrollbar = null;

            if ((lastElement != null) && (scrollable != null) && (scrollable.horizontal == true) && (scrollable.horizontalScrollbar != null) && (scrollable.viewport.rect.width < scrollable.content.rect.width))
            {
                horizontalScrollbar = scrollable.horizontalScrollbar;
                SetNextNavigation(lastElement, horizontalScrollbar);

                // Grab the current navigation values
                Navigation newNavigation = horizontalScrollbar.navigation;

                // Customize the navigation
                newNavigation.mode             = Navigation.Mode.Explicit;
                newNavigation.selectOnUp       = lastElement.Selectable;
                horizontalScrollbar.navigation = newNavigation;
            }
            return(horizontalScrollbar);
        }
コード例 #12
0
        void ScrollToDefaultUi(VisibilityState from, bool forceCenter)
        {
            if (DefaultUi != null)
            {
                // Check if we have scrolling to be concerned about
                if (Navigator != null)
                {
                    UiEventNavigation uiToNavigateTo = DefaultUi.GetComponent <UiEventNavigation>();
                    if (uiToNavigateTo != null)
                    {
                        if (from == VisibilityState.Hidden)
                        {
                            // Scroll to the default UI
                            Navigator.ScrollToSelectable(uiToNavigateTo, forceCenter);

                            // If so, update the menu manager to select the default UI
                            Manager.SelectGui(DefaultUi);
                        }
                        else
                        {
                            // Scroll to the last selected element
                            uiToNavigateTo = Navigator.ScrollToLastSelectedElement(uiToNavigateTo, forceCenter);
                            if ((uiToNavigateTo != null) && (uiToNavigateTo.Selectable != null))
                            {
                                // Select the last selected UI
                                Manager.SelectGui(uiToNavigateTo.Selectable);
                            }
                            else
                            {
                                // If so, update the menu manager to select the default UI
                                Manager.SelectGui(DefaultUi);
                            }
                        }
                    }
                }
                else
                {
                    // If so, update the menu manager to select the default UI
                    Manager.SelectGui(DefaultUi);
                }
            }
        }
コード例 #13
0
        private void UpdateUiNavigation()
        {
            // Cache the top-most and bottom-most element
            UiEventNavigation topMostElement = null;
            UiEventNavigation lastElement    = null;

            // Setup navigating to UI Elements in the scrollable area
            foreach (UiEventNavigation nextElement in UiElementsInScrollable)
            {
                // Enable navigation to elements that are active
                if ((nextElement != null) && (nextElement.isActiveAndEnabled == true) && (nextElement.Selectable.interactable == true))
                {
                    SetupUiElementsInScrollable(nextElement, ref lastElement, ref topMostElement);
                }
            }

            // Setup navigating to the horizontal scroll bar
            Scrollbar horizontalScrollbar = SetupHorizontalScrollBar(lastElement);

            // Setup navigating to UI Elements below the scrollable area
            foreach (UiEventNavigation nextElement in UiElementsBelowScrollable)
            {
                // Enable navigation to elements that are active
                if ((nextElement != null) && (nextElement.isActiveAndEnabled == true) && (nextElement.Selectable.interactable == true))
                {
                    SetupUiElementsBelowScrollable(nextElement, horizontalScrollbar, ref lastElement, ref topMostElement);
                    horizontalScrollbar = null;
                }
            }

            // Finally, allow looping controls
            if ((topMostElement != null) && (lastElement != null))
            {
                SetNextNavigation(lastElement, topMostElement.Selectable);
                SetPreviousNavigation(lastElement.Selectable, topMostElement);
            }

            // Double-check if the currently selected UI is active and interactable
            GuaranteeUiElementIsSelected(pastElements);
        }
コード例 #14
0
        private static void SetNextNavigation(UiEventNavigation lastElement, Selectable currentElement, Scrollbar verticalScrollbar = null)
        {
            // Check if the last and current element is available
            if (lastElement != null)
            {
                // Grab the last navigation values
                Navigation newNavigation = lastElement.Selectable.navigation;

                // Customize the navigation for vertical
                if ((lastElement.ToNextUi & UiEventNavigation.Direction.Up) != 0)
                {
                    newNavigation.selectOnUp = currentElement;
                }
                if ((lastElement.ToNextUi & UiEventNavigation.Direction.Down) != 0)
                {
                    newNavigation.selectOnDown = currentElement;
                }

                // Check if element is not a slider
                if ((lastElement.Selectable is Slider) == false)
                {
                    // Customize the navigation for horizontal
                    if ((lastElement.ToNextUi & UiEventNavigation.Direction.Left) != 0)
                    {
                        newNavigation.selectOnLeft = currentElement;
                    }
                    if ((lastElement.ToNextUi & UiEventNavigation.Direction.Right) != 0)
                    {
                        newNavigation.selectOnRight = currentElement;
                    }
                    else if (verticalScrollbar != null)
                    {
                        // If right-direction navigation isn't overridden, and a scrollbar is provided,
                        // navigate to the scroll on right
                        newNavigation.selectOnRight = verticalScrollbar;
                    }
                }
                lastElement.Selectable.navigation = newNavigation;
            }
        }
コード例 #15
0
        private int UiElementsSorter(UiEventNavigation left, UiEventNavigation right)
        {
            // Take priority on right by default
            int returnNum = 1;

            if ((left != null) && (right != null))
            {
                // Check difference in vertical placement of 2 elements
                Vector2 leftPlacement  = left.RectTransform.rect.min;
                Vector2 rightPlacement = right.RectTransform.rect.min;
                if (Mathf.Approximately(leftPlacement.y, rightPlacement.y) == true)
                {
                    if (Mathf.Approximately(leftPlacement.x, rightPlacement.x) == true)
                    {
                        returnNum = 0;
                    }
                    else if (leftPlacement.x < rightPlacement.x)
                    {
                        returnNum = -1;
                    }
                }
                else if (leftPlacement.y < rightPlacement.y)
                {
                    returnNum = -1;
                }
            }
            else if (left != null)
            {
                // Take priority on left
                returnNum = -1;
            }
            else if (right == null)
            {
                // If both null, they're equal
                returnNum = 0;
            }
            return(returnNum);
        }
コード例 #16
0
        private void GuaranteeUiElementIsSelected(HashSet <Selectable> pastElements)
        {
            if (LastSelectedElement != null)
            {
                // If not, go back up until an active control is found
                pastElements.Clear();
                Selectable nextElement = LastSelectedElement.Selectable;
                Navigation navigation;
                while ((nextElement != null) && (pastElements.Contains(nextElement) == false) && (IsControlActive(nextElement) == false))
                {
                    navigation = nextElement.navigation;
                    if (navigation.mode == Navigation.Mode.Explicit)
                    {
                        // Add this element to the past elements
                        pastElements.Add(nextElement);

                        // Navigate upwards
                        nextElement = navigation.selectOnUp;
                    }
                    else
                    {
                        // Can't navigate anywhere, quit looping
                        break;
                    }
                }

                // If one is found, select this element automatically
                if (nextElement != null)
                {
                    UiEventNavigation uiNavigation = nextElement.GetComponent <UiEventNavigation>();
                    if (uiNavigation != null)
                    {
                        ScrollToSelectable(uiNavigation, false);
                    }
                }
            }
        }
コード例 #17
0
        private static void SetupUiElementsBelowScrollable(UiEventNavigation nextElement, Scrollbar horizontalScrollbar, ref UiEventNavigation lastElement, ref UiEventNavigation topMostElement)
        {
            // Check if this is the top-most element
            if (topMostElement == null)
            {
                // If so, set the variable
                topMostElement = nextElement;
            }

            // Check if there's an element above this
            ResetNavigation(nextElement.Selectable);
            SetNextNavigation(lastElement, nextElement.Selectable);
            if (horizontalScrollbar != null)
            {
                // If the horizontal scroll bar is above these elements, setup navigation to that first
                SetPreviousNavigation(horizontalScrollbar, nextElement);
            }
            else if (lastElement != null)
            {
                // If last element is available, setup that element
                SetPreviousNavigation(lastElement.Selectable, nextElement);
            }
            lastElement = nextElement;
        }
コード例 #18
0
        public static float GetVerticalAnchoredPositionInContent(RectTransform contentTransform, UiEventNavigation childControl)
        {
            float selectionPosition = 0f;

            if ((childControl != null) && (childControl.Selectable != null))
            {
                selectionPosition = GetVerticalAnchoredPositionInContent(contentTransform, ((RectTransform)childControl.Selectable.transform));
            }
            return(selectionPosition);
        }
コード例 #19
0
        public static float GetVerticalAnchoredPositionInContent(RectTransform contentTransform, UiEventNavigation childControl, out float top, out float bottom)
        {
            top    = 0f;
            bottom = 0f;
            float returnCenter = 0f;

            if ((childControl != null) && (childControl.Selectable != null))
            {
                // Calculate as normal
                GetVerticalAnchoredPositionInContent(contentTransform, ((RectTransform)childControl.Selectable.transform), out top, out bottom);
                returnCenter = (top + bottom) / 2f;

                // Grab new top value
                if (childControl.UpperBoundToScrollTo != null)
                {
                    float dummyBottom;
                    GetVerticalAnchoredPositionInContent(contentTransform, childControl.UpperBoundToScrollTo, out top, out dummyBottom);
                }

                // Grab new bottom value
                if (childControl.LowerBoundToScrollTo != null)
                {
                    float dummyTop;
                    GetVerticalAnchoredPositionInContent(contentTransform, childControl.LowerBoundToScrollTo, out dummyTop, out bottom);
                }
            }
            return(returnCenter);
        }