Exemplo n.º 1
0
        internal static bool HandleNavigationButtons(IGamepadControllable Instance, Buttons?PressedButtons, Rectangle?CurrentSlotPosition)
        {
            bool IsFocused = true;

            foreach (NavigationDirection Direction in Enum.GetValues(typeof(NavigationDirection)).Cast <NavigationDirection>())
            {
                //  Handle navigating a single slot at a time
                bool HandleSingleSlotNavigation;
                if (PressedButtons.HasValue)
                {
                    HandleSingleSlotNavigation = IsMatch(PressedButtons.Value, NavigateSingleButtons[Direction]);
                }
                else
                {
                    HandleSingleSlotNavigation = InputHandler.IsNavigationButtonPressed(Direction) && DateTime.Now.Subtract(InputHandler.NavigationButtonsPressedTime[Direction]).TotalMilliseconds >= Current.NavigationRepeatInitialDelay;
                }
                if (HandleSingleSlotNavigation)
                {
                    NavigationWrappingMode HorizontalWrapping = NavigationWrappingMode.AllowWrapToSame;
                    NavigationWrappingMode VerticalWrapping   = NavigationWrappingMode.AllowWrapToSame;

                    bool HasNeighbor = Instance.TryGetMenuNeighbor(Direction, out IGamepadControllable Neighbor);
                    if (HasNeighbor)
                    {
                        if (Direction == NavigationDirection.Left || Direction == NavigationDirection.Right)
                        {
                            HorizontalWrapping = NavigationWrappingMode.NoWrap;
                        }
                        else
                        {
                            VerticalWrapping = NavigationWrappingMode.NoWrap;
                        }
                    }

                    if (!Instance.TryNavigate(Direction, HorizontalWrapping, VerticalWrapping))
                    {
                        //  If we're unable to continue moving the cursor in the desired direction,
                        //  then focus the gamepad controls on the appropriate neighboring UI element
                        if (HasNeighbor)
                        {
                            NavigationDirection StartingSide = GetOppositeDirection(Direction);
                            if (Neighbor.TryNavigateEnter(StartingSide, CurrentSlotPosition))
                            {
                                IsFocused = false;
                            }
                        }
                    }
                }

                //  Handle navigating across an entire UI element, or to the end of the current element's boundary
                if (PressedButtons.HasValue && IsMatch(PressedButtons.Value, NavigateMultipleButtons[Direction]))
                {
                    bool Handled = false;

                    //  Try to change focus to the neighboring UI element in this direction
                    bool HasNeighbor = Instance.TryGetMenuNeighbor(Direction, out IGamepadControllable Neighbor);
                    if (HasNeighbor)
                    {
                        NavigationDirection StartingSide = GetOppositeDirection(Direction);
                        if (Neighbor.TryNavigateEnter(StartingSide, CurrentSlotPosition))
                        {
                            Handled   = true;
                            IsFocused = false;
                        }
                    }

                    //  Navigate to the end of this element's boundary
                    if (!Handled)
                    {
                        while (Instance.TryNavigate(Direction, NavigationWrappingMode.NoWrap, NavigationWrappingMode.NoWrap))
                        {
                        }
                    }
                }
            }

            return(IsFocused);
        }