Exemple #1
0
 public void EnableExplicitFocusableControl(IGamepadFocusable upEle, IGamepadFocusable downEle, IGamepadFocusable leftEle, IGamepadFocusable rightEle)
 {
     ShouldUseExplicitFocusableControl = true;
     GamepadUpElement    = upEle;
     GamepadDownElement  = downEle;
     GamepadLeftElement  = leftEle;
     GamepadRightElement = rightEle;
 }
Exemple #2
0
        void UpdateGamepadState()
        {
            if (_gamepadFocusElement != null)
            {
                if (Input.GamePads[0].IsButtonPressed(GamepadActionButton) ||
                    (KeyboardEmulatesGamepad && Input.IsKeyPressed(KeyboardActionKey)))
                {
                    _gamepadFocusElement.OnActionButtonPressed();
                }
                else if (Input.GamePads[0].IsButtonReleased(GamepadActionButton) ||
                         (KeyboardEmulatesGamepad && Input.IsKeyReleased(KeyboardActionKey)))
                {
                    _gamepadFocusElement.OnActionButtonReleased();
                }
            }

            IGamepadFocusable nextElement = null;
            var direction = Direction.None;

            if (Input.GamePads[0].DpadLeftPressed || Input.GamePads[0].IsLeftStickLeftPressed() ||
                (KeyboardEmulatesGamepad && Input.IsKeyPressed(Keys.Left)))
            {
                direction = Direction.Left;
            }
            else if (Input.GamePads[0].DpadRightPressed || Input.GamePads[0].IsLeftStickRightPressed() ||
                     (KeyboardEmulatesGamepad && Input.IsKeyPressed(Keys.Right)))
            {
                direction = Direction.Right;
            }
            else if (Input.GamePads[0].DpadUpPressed || Input.GamePads[0].IsLeftStickUpPressed() ||
                     (KeyboardEmulatesGamepad && Input.IsKeyPressed(Keys.Up)))
            {
                direction = Direction.Up;
            }
            else if (Input.GamePads[0].DpadDownPressed || Input.GamePads[0].IsLeftStickDownPressed() ||
                     (KeyboardEmulatesGamepad && Input.IsKeyPressed(Keys.Down)))
            {
                direction = Direction.Down;
            }

            // make sure we have a valid direction
            if (direction != Direction.None)
            {
                nextElement = FindNextGamepadFocusable(_gamepadFocusElement, direction);
                if (nextElement == null)
                {
                    // we have no next Element so if the current Element has explicit focuasable control send along the unhandled direction
                    if (_gamepadFocusElement.ShouldUseExplicitFocusableControl)
                    {
                        _gamepadFocusElement.OnUnhandledDirectionPressed(direction);
                    }
                }
                else
                {
                    SetGamepadFocusElement(nextElement);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// sets the gamepad focus element and also turns on gamepad focus for this Stage. For gamepad focus to work you must set an initially
        /// focused element.
        /// </summary>
        /// <param name="focusable">Focusable.</param>
        public void SetGamepadFocusElement(IGamepadFocusable focusable)
        {
            _isGamepadFocusEnabled = true;

            if (focusable != null)
            {
                focusable.OnFocused();
            }

            if (_gamepadFocusElement != null)
            {
                _gamepadFocusElement.OnUnfocused();
            }
            _gamepadFocusElement = focusable;
        }
Exemple #4
0
        IGamepadFocusable FindNextGamepadFocusable(IGamepadFocusable relativeToFocusable, Direction direction)
        {
            // first, we check to see if the IGamepadFocusable has hard-wired control.
            if (relativeToFocusable.ShouldUseExplicitFocusableControl)
            {
                switch (direction)
                {
                case Direction.Up:
                    return(relativeToFocusable.GamepadUpElement);

                case Direction.Down:
                    return(relativeToFocusable.GamepadDownElement);

                case Direction.Left:
                    return(relativeToFocusable.GamepadLeftElement);

                case Direction.Right:
                    return(relativeToFocusable.GamepadRightElement);
                }
            }

            IGamepadFocusable nextFocusable = null;
            var distanceToNextButton        = float.MaxValue;

            var focusableEle  = relativeToFocusable as Element;
            var currentCoords = focusableEle.GetParent()
                                .LocalToStageCoordinates(new Vector2(focusableEle.GetX(), focusableEle.GetY()));
            var buttons = FindAllElementsOfType <IGamepadFocusable>();

            for (var i = 0; i < buttons.Count; i++)
            {
                if (buttons[i] == relativeToFocusable)
                {
                    continue;
                }

                // filter out buttons that are not in the disired direction
                var element      = buttons[i] as Element;
                var buttonCoords = element.GetParent()
                                   .LocalToStageCoordinates(new Vector2(element.GetX(), element.GetY()));
                var isDirectionMatch = false;
                switch (direction)
                {
                case Direction.Up:
                    if (buttonCoords.Y < currentCoords.Y)
                    {
                        isDirectionMatch = true;
                    }
                    break;

                case Direction.Down:
                    if (buttonCoords.Y > currentCoords.Y)
                    {
                        isDirectionMatch = true;
                    }
                    break;

                case Direction.Left:
                    if (buttonCoords.X < currentCoords.X)
                    {
                        isDirectionMatch = true;
                    }
                    break;

                case Direction.Right:
                    if (buttonCoords.X > currentCoords.X)
                    {
                        isDirectionMatch = true;
                    }
                    break;
                }

                // keep only the closest button if we have a match
                if (isDirectionMatch)
                {
                    if (nextFocusable == null)
                    {
                        nextFocusable        = buttons[i];
                        distanceToNextButton = Vector2.DistanceSquared(currentCoords, buttonCoords);
                    }
                    else
                    {
                        var distance = Vector2.DistanceSquared(currentCoords, buttonCoords);
                        if (distance < distanceToNextButton)
                        {
                            nextFocusable        = buttons[i];
                            distanceToNextButton = distance;
                        }
                    }
                }
            }

            return(nextFocusable);
        }
Exemple #5
0
		public void enableExplicitFocusableControl( IGamepadFocusable upEle, IGamepadFocusable downEle, IGamepadFocusable leftEle, IGamepadFocusable rightEle )
		{
			shouldUseExplicitFocusableControl = true;
			gamepadUpElement = upEle;
			gamepadDownElement = downEle;
			gamepadLeftElement = leftEle;
			gamepadRightElement = rightEle;
		}