Esempio n. 1
0
        /// <summary>
        /// Creates a new GamePadThumbstickEventArgs object.
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="logicalIndex"></param>
        /// <param name="thumbstick"></param>
        /// <param name="position"></param>
        /// <param name="current"></param>
        public GamePadThumbstickEventArgs(TimeSpan gameTime, PlayerIndex logicalIndex, Thumbsticks thumbstick, Vector2 position, GamePadState current)
            : base(gameTime, logicalIndex, current)
        {
            Thumbstick = thumbstick;
            Position   = position;
            PolarCoordinate polar = PolarCoordinate.FromCartesian(position);

            Angle  = polar.Angle;
            Amount = polar.Distance;
        }
Esempio n. 2
0
 private bool InDownSection(PolarCoordinate value)
 {
     return(value.Distance > ThumbstickThreshold && InDownDirection(value.Angle));
 }
Esempio n. 3
0
        /// <summary>
        /// Updates the GamePadEvents, performing polling and raising any events that have occurred.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            GamePadState current = GamePad.GetState(PhysicalIndex);

            // Button Down
            if (current.Buttons.A == ButtonState.Pressed && previous.Buttons.A == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.A, current));
            }
            if (current.Buttons.B == ButtonState.Pressed && previous.Buttons.B == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.B, current));
            }
            if (current.Buttons.X == ButtonState.Pressed && previous.Buttons.X == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.X, current));
            }
            if (current.Buttons.Y == ButtonState.Pressed && previous.Buttons.Y == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.Y, current));
            }
            if (current.Buttons.Back == ButtonState.Pressed && previous.Buttons.Back == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.Back, current));
            }
            if (current.Buttons.Start == ButtonState.Pressed && previous.Buttons.Start == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.Start, current));
            }
            if (current.Buttons.LeftShoulder == ButtonState.Pressed && previous.Buttons.LeftShoulder == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftShoulder, current));
            }
            if (current.Buttons.RightShoulder == ButtonState.Pressed && previous.Buttons.RightShoulder == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightShoulder, current));
            }

            if (current.DPad.Left == ButtonState.Pressed && previous.DPad.Left == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadLeft, current));
            }
            if (current.DPad.Right == ButtonState.Pressed && previous.DPad.Right == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadRight, current));
            }
            if (current.DPad.Up == ButtonState.Pressed && previous.DPad.Up == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadUp, current));
            }
            if (current.DPad.Down == ButtonState.Pressed && previous.DPad.Down == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadDown, current));
            }

            if (current.Triggers.Left > TriggerThreshold && previous.Triggers.Left <= TriggerThreshold)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftTrigger, current));
            }
            if (current.Triggers.Right > TriggerThreshold && previous.Triggers.Right <= TriggerThreshold)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightTrigger, current));
            }

            if (current.Buttons.LeftStick == ButtonState.Pressed && previous.Buttons.LeftStick == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstick, current));
            }
            if (current.Buttons.RightStick == ButtonState.Pressed && previous.Buttons.RightStick == ButtonState.Released)
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstick, current));
            }

            PolarCoordinate oldLeft  = PolarCoordinate.FromCartesian(previous.ThumbSticks.Left);
            PolarCoordinate newLeft  = PolarCoordinate.FromCartesian(current.ThumbSticks.Left);
            PolarCoordinate newRight = PolarCoordinate.FromCartesian(current.ThumbSticks.Right);
            PolarCoordinate oldRight = PolarCoordinate.FromCartesian(previous.ThumbSticks.Right);

            if (InLeftSection(newLeft) && !InLeftSection(oldLeft))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickLeft, current));
            }
            if (InRightSection(newLeft) && !InRightSection(oldLeft))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickRight, current));
            }
            if (InUpSection(newLeft) && !InUpSection(oldLeft))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickUp, current));
            }
            if (InDownSection(newLeft) && !InDownSection(oldLeft))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickDown, current));
            }

            if (InLeftSection(newRight) && !InLeftSection(oldRight))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickLeft, current));
            }
            if (InRightSection(newRight) && !InRightSection(oldRight))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickRight, current));
            }
            if (InUpSection(newRight) && !InUpSection(oldRight))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickUp, current));
            }
            if (InDownSection(newRight) && !InDownSection(oldRight))
            {
                OnButtonDown(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickDown, current));
            }

            // Button up
            if (current.Buttons.A == ButtonState.Released && previous.Buttons.A == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.A, current));
            }
            if (current.Buttons.B == ButtonState.Released && previous.Buttons.B == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.B, current));
            }
            if (current.Buttons.X == ButtonState.Released && previous.Buttons.X == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.X, current));
            }
            if (current.Buttons.Y == ButtonState.Released && previous.Buttons.Y == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.Y, current));
            }
            if (current.Buttons.Back == ButtonState.Released && previous.Buttons.Back == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.Back, current));
            }
            if (current.Buttons.Start == ButtonState.Released && previous.Buttons.Start == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.Start, current));
            }
            if (current.Buttons.LeftShoulder == ButtonState.Released && previous.Buttons.LeftShoulder == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftShoulder, current));
            }
            if (current.Buttons.RightShoulder == ButtonState.Released && previous.Buttons.RightShoulder == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightShoulder, current));
            }

            if (current.DPad.Left == ButtonState.Released && previous.DPad.Left == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadLeft, current));
            }
            if (current.DPad.Right == ButtonState.Released && previous.DPad.Right == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadRight, current));
            }
            if (current.DPad.Up == ButtonState.Released && previous.DPad.Up == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadUp, current));
            }
            if (current.DPad.Down == ButtonState.Released && previous.DPad.Down == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.DPadDown, current));
            }

            if (current.Triggers.Left <= TriggerThreshold && previous.Triggers.Left > TriggerThreshold)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftTrigger, current));
            }
            if (current.Triggers.Right <= TriggerThreshold && previous.Triggers.Right > TriggerThreshold)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightTrigger, current));
            }

            if (current.Buttons.LeftStick == ButtonState.Released && previous.Buttons.LeftStick == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstick, current));
            }
            if (current.Buttons.RightStick == ButtonState.Released && previous.Buttons.RightStick == ButtonState.Pressed)
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstick, current));
            }

            if (!InLeftSection(newLeft) && InLeftSection(oldLeft))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickLeft, current));
            }
            if (!InRightSection(newLeft) && InRightSection(oldLeft))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickRight, current));
            }
            if (!InUpSection(newLeft) && InUpSection(oldLeft))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickUp, current));
            }
            if (!InDownSection(newLeft) && InDownSection(oldLeft))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.LeftThumbstickDown, current));
            }

            if (!InLeftSection(newRight) && InLeftSection(oldRight))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickLeft, current));
            }
            if (!InRightSection(newRight) && InRightSection(oldRight))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickRight, current));
            }
            if (!InUpSection(newRight) && InUpSection(oldRight))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickUp, current));
            }
            if (!InDownSection(newRight) && InDownSection(oldRight))
            {
                OnButtonUp(this, new GamePadButtonEventArgs(gameTime.TotalGameTime, LogicalIndex, Buttons.RightThumbstickDown, current));
            }

            // Triggers moved.
            if (current.Triggers.Left != previous.Triggers.Left)
            {
                OnTriggerMoved(this, new GamePadTriggerEventArgs(gameTime.TotalGameTime, LogicalIndex, Triggers.Left, current.Triggers.Left, current));
                OnLeftTriggerMoved(this, new GamePadTriggerEventArgs(gameTime.TotalGameTime, LogicalIndex, Triggers.Left, current.Triggers.Left, current));
            }

            if (current.Triggers.Right != previous.Triggers.Right)
            {
                OnTriggerMoved(this, new GamePadTriggerEventArgs(gameTime.TotalGameTime, LogicalIndex, Triggers.Right, current.Triggers.Right, current));
                OnRightTriggerMoved(this, new GamePadTriggerEventArgs(gameTime.TotalGameTime, LogicalIndex, Triggers.Right, current.Triggers.Right, current));
            }

            // Thumbsticks moved.
            if (current.ThumbSticks.Left.X != previous.ThumbSticks.Left.X || current.ThumbSticks.Left.Y != previous.ThumbSticks.Left.Y)
            {
                OnThumbstickMoved(this, new GamePadThumbstickEventArgs(gameTime.TotalGameTime, LogicalIndex, Thumbsticks.Left, new Vector2(current.ThumbSticks.Left.X, current.ThumbSticks.Left.Y), current));
                OnLeftThumbstickMoved(this, new GamePadThumbstickEventArgs(gameTime.TotalGameTime, LogicalIndex, Thumbsticks.Left, new Vector2(current.ThumbSticks.Left.X, current.ThumbSticks.Left.Y), current));
            }

            if (current.ThumbSticks.Right.X != previous.ThumbSticks.Right.X || current.ThumbSticks.Right.Y != previous.ThumbSticks.Right.Y)
            {
                OnThumbstickMoved(this, new GamePadThumbstickEventArgs(gameTime.TotalGameTime, LogicalIndex, Thumbsticks.Right, new Vector2(current.ThumbSticks.Right.X, current.ThumbSticks.Right.Y), current));
                OnRightThumbstickMoved(this, new GamePadThumbstickEventArgs(gameTime.TotalGameTime, LogicalIndex, Thumbsticks.Right, new Vector2(current.ThumbSticks.Right.X, current.ThumbSticks.Right.Y), current));
            }

            // Connected and Disconnected events.
            if (current.IsConnected && !previous.IsConnected)
            {
                OnConnected(this, new GamePadEventArgs(gameTime.TotalGameTime, LogicalIndex, current));
            }
            if (!current.IsConnected && previous.IsConnected)
            {
                OnDisconnected(this, new GamePadEventArgs(gameTime.TotalGameTime, LogicalIndex, current));
            }

            previous = current;
        }
Esempio n. 4
0
 private bool InUpSection(PolarCoordinate value)
 {
     return value.Distance > ThumbstickThreshold && InUpDirection(value.Angle);
 }