Bläddra i källkod

Simplify GamePadButtons

Jean-David Moisan 6 år sedan
förälder
incheckning
acc472d928
4 ändrade filer med 86 tillägg och 20 borttagningar
  1. 3 2
      Source/ConditionComposite.cs
  2. 23 14
      Source/ConditionGamePad.cs
  3. 6 4
      Source/ConditionSet.cs
  4. 54 0
      Source/InputHelper.cs

+ 3 - 2
Source/ConditionComposite.cs

@@ -51,9 +51,10 @@ namespace Apos.Input {
         /// This implicitly creates a ConditionSet.
         /// This implicitly creates a ConditionSet.
         /// </summary>
         /// </summary>
         /// <param name="button">A gamepad button that will have it's own set.</param>
         /// <param name="button">A gamepad button that will have it's own set.</param>
+        /// <param name="gamePadIndex">The index of the gamepad to operate on.</param>
         /// <returns>Returns the set for easy function chaining.</returns>
         /// <returns>Returns the set for easy function chaining.</returns>
-        public ConditionSet AddSet(Func<GamePadState[], ButtonState> button) {
-            ConditionSet newSet = new ConditionSet().AddNeed(button);
+        public ConditionSet AddSet(InputHelper.GamePadButton button, int gamePadIndex) {
+            ConditionSet newSet = new ConditionSet().AddNeed(button, gamePadIndex);
             AddSet(newSet);
             AddSet(newSet);
             return newSet;
             return newSet;
         }
         }

+ 23 - 14
Source/ConditionGamePad.cs

@@ -11,46 +11,51 @@ namespace Apos.Input {
         // Group: Constructors
         // Group: Constructors
 
 
         /// <param name="needButton">The button to operate on.</param>
         /// <param name="needButton">The button to operate on.</param>
-        public ConditionGamePad(Func<GamePadState[], ButtonState> needButton) {
+        /// <param name="gamePadIndex">The index of the gamepad to operate on.</param>
+        public ConditionGamePad(InputHelper.GamePadButton needButton, int gamePadIndex) {
             _needButton = needButton;
             _needButton = needButton;
+            _gamePadIndex = gamePadIndex;
         }
         }
 
 
         // Group: Public Functions
         // Group: Public Functions
 
 
         /// <returns>Returns true when a button was not pressed and is now pressed.</returns>
         /// <returns>Returns true when a button was not pressed and is now pressed.</returns>
         public bool Pressed() {
         public bool Pressed() {
-            return Pressed(_needButton) && InputHelper.IsActive;
+            return Pressed(_needButton, _gamePadIndex) && InputHelper.IsActive;
         }
         }
         /// <returns>Returns true when a button is now pressed.</returns>
         /// <returns>Returns true when a button is now pressed.</returns>
         public bool Held() {
         public bool Held() {
-            return Held(_needButton) && InputHelper.IsActive;
+            return Held(_needButton, _gamePadIndex) && InputHelper.IsActive;
         }
         }
         /// <returns>Returns true when a button was pressed and is now pressed.</returns>
         /// <returns>Returns true when a button was pressed and is now pressed.</returns>
         public bool HeldOnly() {
         public bool HeldOnly() {
-            return HeldOnly(_needButton) && InputHelper.IsActive;
+            return HeldOnly(_needButton, _gamePadIndex) && InputHelper.IsActive;
         }
         }
         /// <returns>Returns true when a button was pressed and is now not pressed.</returns>
         /// <returns>Returns true when a button was pressed and is now not pressed.</returns>
         public bool Released() {
         public bool Released() {
-            return Released(_needButton) && InputHelper.IsActive;
+            return Released(_needButton, _gamePadIndex) && InputHelper.IsActive;
         }
         }
 
 
         // Group: Static Functions
         // Group: Static Functions
 
 
         /// <returns>Returns true when a button was not pressed and is now pressed.</returns>
         /// <returns>Returns true when a button was not pressed and is now pressed.</returns>
-        public static bool Pressed(Func<GamePadState[], ButtonState> button) {
-            return button(InputHelper.NewGamePad) == ButtonState.Pressed && button(InputHelper.OldGamePad) == ButtonState.Released;
+        public static bool Pressed(InputHelper.GamePadButton button, int gamePadIndex) {
+            return InputHelper.GamePadButtons[button](InputHelper.NewGamePad, gamePadIndex) == ButtonState.Pressed &&
+                   InputHelper.GamePadButtons[button](InputHelper.OldGamePad, gamePadIndex) == ButtonState.Released;
         }
         }
         /// <returns>Returns true when a button is now pressed.</returns>
         /// <returns>Returns true when a button is now pressed.</returns>
-        public static bool Held(Func<GamePadState[], ButtonState> button) {
-            return button(InputHelper.NewGamePad) == ButtonState.Pressed;
+        public static bool Held(InputHelper.GamePadButton button, int gamePadIndex) {
+            return InputHelper.GamePadButtons[button](InputHelper.NewGamePad, gamePadIndex) == ButtonState.Pressed;
         }
         }
         /// <returns>Returns true when a button was pressed and is now pressed.</returns>
         /// <returns>Returns true when a button was pressed and is now pressed.</returns>
-        public static bool HeldOnly(Func<GamePadState[], ButtonState> button) {
-            return button(InputHelper.NewGamePad) == ButtonState.Pressed && button(InputHelper.OldGamePad) == ButtonState.Pressed;
+        public static bool HeldOnly(InputHelper.GamePadButton button, int gamePadIndex) {
+            return InputHelper.GamePadButtons[button](InputHelper.NewGamePad, gamePadIndex) == ButtonState.Pressed &&
+                   InputHelper.GamePadButtons[button](InputHelper.OldGamePad, gamePadIndex) == ButtonState.Pressed;
         }
         }
         /// <returns>Returns true when a button was pressed and is now not pressed.</returns>
         /// <returns>Returns true when a button was pressed and is now not pressed.</returns>
-        public static bool Released(Func<GamePadState[], ButtonState> button) {
-            return button(InputHelper.NewGamePad) == ButtonState.Released && button(InputHelper.OldGamePad) == ButtonState.Pressed;
+        public static bool Released(InputHelper.GamePadButton button, int gamePadIndex) {
+            return InputHelper.GamePadButtons[button](InputHelper.NewGamePad, gamePadIndex) == ButtonState.Released &&
+                   InputHelper.GamePadButtons[button](InputHelper.OldGamePad, gamePadIndex) == ButtonState.Pressed;
         }
         }
 
 
         // Group: Private Variables
         // Group: Private Variables
@@ -58,6 +63,10 @@ namespace Apos.Input {
         /// <summary>
         /// <summary>
         /// The button that will be checked.
         /// The button that will be checked.
         /// </summary>
         /// </summary>
-        private Func<GamePadState[], ButtonState> _needButton;
+        private InputHelper.GamePadButton _needButton;
+        /// <summary>
+        /// The index for the gamepad that will be checked.
+        /// </summary>
+        private int _gamePadIndex;
     }
     }
 }
 }

+ 6 - 4
Source/ConditionSet.cs

@@ -50,9 +50,10 @@ namespace Apos.Input {
         /// This implicitly creates a ConditionGamePad.
         /// This implicitly creates a ConditionGamePad.
         /// </summary>
         /// </summary>
         /// <param name="button">Adds a new needed gamepad button.</param>
         /// <param name="button">Adds a new needed gamepad button.</param>
+        /// <param name="gamePadIndex">The index of the gamepad to operate on.</param>
         /// <returns>Returns itself for easy function chaining.</returns>
         /// <returns>Returns itself for easy function chaining.</returns>
-        public ConditionSet AddNeed(Func<GamePadState[], ButtonState> button) {
-            return AddNeed(new ConditionGamePad(button));
+        public ConditionSet AddNeed(InputHelper.GamePadButton button, int gamePadIndex) {
+            return AddNeed(new ConditionGamePad(button, gamePadIndex));
         }
         }
         /// <param name="condition">Adds a condition that is needed.</param>
         /// <param name="condition">Adds a condition that is needed.</param>
         /// <returns>Returns itself for easy function chaining.</returns>
         /// <returns>Returns itself for easy function chaining.</returns>
@@ -80,9 +81,10 @@ namespace Apos.Input {
         /// This implicitly creates a ConditionGamePad.
         /// This implicitly creates a ConditionGamePad.
         /// </summary>
         /// </summary>
         /// <param name="button">Adds a gamepad button that must not be pressed.</param>
         /// <param name="button">Adds a gamepad button that must not be pressed.</param>
+        /// <param name="gamePadIndex">The index of the gamepad to operate on.</param>
         /// <returns>Returns itself for easy function chaining.</returns>
         /// <returns>Returns itself for easy function chaining.</returns>
-        public ConditionSet AddNot(Func<GamePadState[], ButtonState> button) {
-            return AddNot(new ConditionGamePad(button));
+        public ConditionSet AddNot(InputHelper.GamePadButton button, int gamePadIndex) {
+            return AddNot(new ConditionGamePad(button, gamePadIndex));
         }
         }
         /// <param name="condition">Adds a condition that must not be pressed.</param>
         /// <param name="condition">Adds a condition that must not be pressed.</param>
         /// <returns>Returns itself for easy function chaining.</returns>
         /// <returns>Returns itself for easy function chaining.</returns>

+ 54 - 0
Source/InputHelper.cs

@@ -39,6 +39,39 @@ namespace Apos.Input {
             /// </summary>
             /// </summary>
             XButton2
             XButton2
         }
         }
+        /// <summary>Available gamepad buttons.</summary>
+        public enum GamePadButton {
+            /// <summary>A button.</summary>
+            A,
+            /// <summary>B button.</summary>
+            B,
+            /// <summary>Back button.</summary>
+            Back,
+            /// <summary>X button.</summary>
+            X,
+            /// <summary>Y button.</summary>
+            Y,
+            /// <summary>Start button.</summary>
+            Start,
+            /// <summary>LeftShould button.</summary>
+            LeftShoulder,
+            /// <summary>LeftStick button.</summary>
+            LeftStick,
+            /// <summary>RightShould button.</summary>
+            RightShoulder,
+            /// <summary>RightStick button.</summary>
+            RightStick,
+            /// <summary>BigButton button.</summary>
+            BigButton,
+            /// <summary>DPad down button.</summary>
+            Down,
+            /// <summary>DPad left button.</summary>
+            Left,
+            /// <summary>DPad right button.</summary>
+            Right,
+            /// <summary>DPad up button.</summary>
+            Up
+        }
         /// <value>Pass your game class here.</value>
         /// <value>Pass your game class here.</value>
         public static Game Game {
         public static Game Game {
             get;
             get;
@@ -104,6 +137,10 @@ namespace Apos.Input {
         /// Maps a MouseButton to a function that can extract a specific ButtonState from a MouseState.
         /// Maps a MouseButton to a function that can extract a specific ButtonState from a MouseState.
         /// </summary>
         /// </summary>
         public static Dictionary<MouseButton, Func<MouseState, ButtonState>> MouseButtons => _mouseButtons;
         public static Dictionary<MouseButton, Func<MouseState, ButtonState>> MouseButtons => _mouseButtons;
+        /// <summary>
+        /// Maps a GamePadButton to a function that can extract a specific ButtonState from a GamePadState.
+        /// </summary>
+        public static Dictionary<GamePadButton, Func<GamePadState[], int, ButtonState>> GamePadButtons => _gamePadButtons;
 
 
         // Group: Public Functions
         // Group: Public Functions
 
 
@@ -238,5 +275,22 @@ namespace Apos.Input {
             {MouseButton.XButton1, s => s.XButton1},
             {MouseButton.XButton1, s => s.XButton1},
             {MouseButton.XButton2, s => s.XButton2},
             {MouseButton.XButton2, s => s.XButton2},
         };
         };
+        private static Dictionary<GamePadButton, Func<GamePadState[], int, ButtonState>> _gamePadButtons = new Dictionary<GamePadButton, Func<GamePadState[], int, ButtonState>> {
+            {GamePadButton.A, (s, i) => s[i].Buttons.A},
+            {GamePadButton.B, (s, i) => s[i].Buttons.B},
+            {GamePadButton.Back, (s, i) => s[i].Buttons.Back},
+            {GamePadButton.X, (s, i) => s[i].Buttons.X},
+            {GamePadButton.Y, (s, i) => s[i].Buttons.Y},
+            {GamePadButton.Start, (s, i) => s[i].Buttons.Start},
+            {GamePadButton.LeftShoulder, (s, i) => s[i].Buttons.LeftShoulder},
+            {GamePadButton.LeftStick, (s, i) => s[i].Buttons.LeftStick},
+            {GamePadButton.RightShoulder, (s, i) => s[i].Buttons.RightShoulder},
+            {GamePadButton.RightStick, (s, i) => s[i].Buttons.RightStick},
+            {GamePadButton.BigButton, (s, i) => s[i].Buttons.BigButton},
+            {GamePadButton.Down, (s, i) => s[i].DPad.Down},
+            {GamePadButton.Left, (s, i) => s[i].DPad.Left},
+            {GamePadButton.Right, (s, i) => s[i].DPad.Right},
+            {GamePadButton.Up, (s, i) => s[i].DPad.Up},
+        };
     }
     }
 }
 }