Parcourir la source

Add static methods for the tracking system, refactor tracking

Jean-David Moisan il y a 4 ans
Parent
commit
567d97dcd3

+ 5 - 10
Source/MouseCondition.cs

@@ -15,7 +15,7 @@ namespace Apos.Input {
 
         /// <returns>Returns true when the button was not pressed and is now pressed.</returns>
         public bool Pressed(bool canConsume = true) {
-            return Pressed(_button) && IsMouseValid(InputHelper.IsActive);
+            return Pressed(_button) && IsMouseValid;
         }
         /// <returns>Returns true when the button is now pressed.</returns>
         public bool Held(bool canConsume = true) {
@@ -51,15 +51,10 @@ namespace Apos.Input {
             return InputHelper.MouseButtons[button](InputHelper.NewMouse) == ButtonState.Released &&
                    InputHelper.MouseButtons[button](InputHelper.OldMouse) == ButtonState.Pressed;
         }
-        /// <returns>Returns true when the mouse is within the game window.</returns>
-        public static bool IsMouseValid(bool IsActive) {
-            if (IsActive &&
-                InputHelper.NewMouse.X >= 0 && InputHelper.NewMouse.X <= InputHelper.WindowWidth &&
-                InputHelper.NewMouse.Y >= 0 && InputHelper.NewMouse.Y <= InputHelper.WindowHeight) {
-                return true;
-            }
-            return false;
-        }
+        /// <returns>Returns true when the mouse is within the game window and active.</returns>
+        public static bool IsMouseValid => InputHelper.IsActive &&
+                0 <= InputHelper.NewMouse.X && InputHelper.NewMouse.X <= InputHelper.WindowWidth &&
+                0 <= InputHelper.NewMouse.Y && InputHelper.NewMouse.Y <= InputHelper.WindowHeight;
 
         /// <summary>
         /// The button that will be checked.

+ 48 - 49
Source/Track/GamePadCondition.cs

@@ -1,6 +1,4 @@
-using System;
-using System.Collections.Generic;
-using Microsoft.Xna.Framework.Input;
+using System.Collections.Generic;
 
 namespace Apos.Input.Track {
     /// <summary>
@@ -14,75 +12,76 @@ namespace Apos.Input.Track {
         public GamePadCondition(GamePadButton button, int gamePadIndex) {
             _button = button;
             _gamePadIndex = gamePadIndex;
-            _condition = new Input.GamePadCondition(_button, _gamePadIndex);
-
-            if (!Tracker.ContainsKey((_button, _gamePadIndex))) {
-                Tracker.Add((_button, _gamePadIndex), 0);
-            }
         }
 
         /// <returns>Returns true when the button was not pressed and is now pressed.</returns>
         public bool Pressed(bool canConsume = true) {
-            if (isUnique) {
-                bool pressed = _condition.Pressed();
-
-                if (canConsume && pressed) {
-                    Consume();
-                }
-                return pressed;
-            }
-            return false;
+            return Pressed(_button, _gamePadIndex, canConsume) && InputHelper.IsActive;
         }
         /// <returns>Returns true when the button is now pressed.</returns>
         public bool Held(bool canConsume = true) {
-            if (isUnique) {
-                bool held = _condition.Held();
-
-                if (canConsume && held) {
-                    Consume();
-                }
-                return held;
-            }
-            return false;
+            return Held(_button, _gamePadIndex, canConsume) && InputHelper.IsActive;
         }
         /// <returns>Returns true when the button was pressed and is now pressed.</returns>
         public bool HeldOnly(bool canConsume = true) {
-            if (isUnique) {
-                bool held = _condition.HeldOnly();
-
-                if (canConsume && held) {
-                    Consume();
-                }
-                return held;
-            }
-            return false;
+            return HeldOnly(_button, _gamePadIndex, canConsume) && InputHelper.IsActive;
         }
         /// <returns>Returns true when the button was pressed and is now not pressed.</returns>
         public bool Released(bool canConsume = true) {
-            if (isUnique) {
-                bool released = _condition.Released();
-
-                if (canConsume && released) {
-                    Consume();
-                }
-                return released;
-            }
-            return false;
+            return Released(_button, _gamePadIndex, canConsume) && InputHelper.IsActive;
         }
         /// <summary>Mark the condition as used.</summary>
         public void Consume() {
             Tracker[(_button, _gamePadIndex)] = InputHelper.CurrentFrame;
         }
 
-        private bool isUnique => Tracker[(_button, _gamePadIndex)] != InputHelper.CurrentFrame;
+        /// <returns>Returns true when the mouse button was released and is now pressed.</returns>
+        public static bool Pressed(GamePadButton button, int gamePadIndex, bool canConsume = true) {
+            if (IsUnique(button, gamePadIndex) && Input.GamePadCondition.Pressed(button, gamePadIndex)) {
+                if (canConsume)
+                    Consume(button, gamePadIndex);
+                return true;
+            }
+            return false;
+        }
+        /// <returns>Returns true when the gamepad button is now pressed.</returns>
+        public static bool Held(GamePadButton button, int gamePadIndex, bool canConsume = true) {
+            if (IsUnique(button, gamePadIndex) && Input.GamePadCondition.Held(button, gamePadIndex)) {
+                if (canConsume)
+                    Consume(button, gamePadIndex);
+                return true;
+            }
+            return false;
+        }
+        /// <returns>Returns true when the gamepad button was pressed and is now pressed.</returns>
+        public static bool HeldOnly(GamePadButton button, int gamePadIndex, bool canConsume = true) {
+            if (IsUnique(button, gamePadIndex) && Input.GamePadCondition.HeldOnly(button, gamePadIndex)) {
+                if (canConsume)
+                    Consume(button, gamePadIndex);
+                return true;
+            }
+            return false;
+        }
+        /// <returns>Returns true when the gamepad button was pressed and is now released.</returns>
+        public static bool Released(GamePadButton button, int gamePadIndex, bool canConsume = true) {
+            if (IsUnique(button, gamePadIndex) && Input.GamePadCondition.Released(button, gamePadIndex)) {
+                if (canConsume)
+                    Consume(button, gamePadIndex);
+                return true;
+            }
+            return false;
+        }
+        /// <summary>Mark the gamepad button as used for this frame.</summary>
+        public static void Consume(GamePadButton button, int gamePadIndex) {
+            Tracker[(button, gamePadIndex)] = InputHelper.CurrentFrame;
+        }
+        /// <summary>Checks if the given gamepad button is unique for this frame.</summary>
+        public static bool IsUnique(GamePadButton button, int gamePadIndex) => !Tracker.ContainsKey((button, gamePadIndex)) || Tracker[(button, gamePadIndex)] != InputHelper.CurrentFrame;
 
         private GamePadButton _button;
         private int _gamePadIndex;
-        private Input.GamePadCondition _condition;
 
-        /// <summary>
-        /// Tracks buttons being used each frames.
-        /// </summary>
+        /// <summary>Tracks buttons being used each frames.</summary>
         protected static Dictionary<(GamePadButton, int), uint> Tracker = new Dictionary<(GamePadButton, int), uint>();
     }
 }

+ 46 - 44
Source/Track/KeyboardCondition.cs

@@ -11,74 +11,76 @@ namespace Apos.Input.Track {
         /// <param name="key">The key to operate on.</param>
         public KeyboardCondition(Keys key) {
             _key = key;
-            _condition = new Input.KeyboardCondition(_key);
-
-            if (!Tracker.ContainsKey(_key)) {
-                Tracker.Add(_key, 0);
-            }
         }
 
         /// <returns>Returns true when the key was not pressed and is now pressed.</returns>
         public bool Pressed(bool canConsume = true) {
-            if (isUnique) {
-                bool pressed = _condition.Pressed();
+            return Pressed(_key, canConsume) && InputHelper.IsActive;
+        }
+        /// <returns>Returns true when the key is now pressed.</returns>
+        public bool Held(bool canConsume = true) {
+            return Held(_key, canConsume) && InputHelper.IsActive;
+        }
+        /// <returns>Returns true when the key was pressed and is now pressed.</returns>
+        public bool HeldOnly(bool canConsume = true) {
+            return HeldOnly(_key, canConsume) && InputHelper.IsActive;
+        }
+        /// <returns>Returns true when the key was pressed and is now not pressed.</returns>
+        public bool Released(bool canConsume = true) {
+            return Released(_key, canConsume) && InputHelper.IsActive;
+        }
+        /// <summary>Mark the key as used.</summary>
+        public void Consume() {
+            Consume(_key);
+        }
 
-                if (canConsume && pressed) {
-                    Consume();
-                }
-                return pressed;
+        /// <returns>Returns true when the key was released and is now pressed.</returns>
+        public static bool Pressed(Keys key, bool canConsume = true) {
+            if (IsUnique(key) && Input.KeyboardCondition.Pressed(key)) {
+                if (canConsume)
+                    Consume(key);
+                return true;
             }
             return false;
         }
         /// <returns>Returns true when the key is now pressed.</returns>
-        public bool Held(bool canConsume = true) {
-            if (isUnique) {
-                bool held = _condition.Held();
-
-                if (canConsume && held) {
-                    Consume();
-                }
-                return held;
+        public static bool Held(Keys key, bool canConsume = true) {
+            if (IsUnique(key) && Input.KeyboardCondition.Held(key)) {
+                if (canConsume)
+                    Consume(key);
+                return true;
             }
             return false;
         }
         /// <returns>Returns true when the key was pressed and is now pressed.</returns>
-        public bool HeldOnly(bool canConsume = true) {
-            if (isUnique) {
-                bool held = _condition.HeldOnly();
-
-                if (canConsume && held) {
-                    Consume();
-                }
-                return held;
+        public static bool HeldOnly(Keys key, bool canConsume = true) {
+            if (IsUnique(key) && Input.KeyboardCondition.HeldOnly(key)) {
+                if (canConsume)
+                    Consume(key);
+                return true;
             }
             return false;
         }
-        /// <returns>Returns true when the key was pressed and is now not pressed.</returns>
-        public bool Released(bool canConsume = true) {
-            if (isUnique) {
-                bool release = _condition.Released();
-
-                if (canConsume && release) {
-                    Consume();
-                }
-                return release;
+        /// <returns>Returns true when the key was pressed and is now released.</returns>
+        public static bool Released(Keys key, bool canConsume = true) {
+            if (IsUnique(key) && Input.KeyboardCondition.Released(key)) {
+                if (canConsume)
+                    Consume(key);
+                return true;
             }
             return false;
         }
-        /// <summary>Mark the condition as used.</summary>
-        public void Consume() {
-            Tracker[_key] = InputHelper.CurrentFrame;
+        /// <summary>Mark the key as used for this frame.</summary>
+        public static void Consume(Keys key) {
+            Tracker[key] = InputHelper.CurrentFrame;
         }
 
-        private bool isUnique => Tracker[_key] != InputHelper.CurrentFrame;
+        /// <summary>Checks if the given key is unique for this frame.</summary>
+        public static bool IsUnique(Keys key) => !Tracker.ContainsKey(key) || Tracker[key] != InputHelper.CurrentFrame;
 
         private Keys _key;
-        private Input.KeyboardCondition _condition;
 
-        /// <summary>
-        /// Tracks keys being used each frames.
-        /// </summary>
+        /// <summary>Tracks keys being used each frames.</summary>
         protected static Dictionary<Keys, uint> Tracker = new Dictionary<Keys, uint>();
     }
 }

+ 48 - 48
Source/Track/MouseCondition.cs

@@ -1,5 +1,4 @@
 using System.Collections.Generic;
-using Microsoft.Xna.Framework.Input;
 
 namespace Apos.Input.Track {
     /// <summary>
@@ -12,74 +11,75 @@ namespace Apos.Input.Track {
         /// <param name="button">The button to operate on.</param>
         public MouseCondition(MouseButton button) {
             _button = button;
-            _condition = new Input.MouseCondition(_button);
-
-            if (!Tracker.ContainsKey(_button)) {
-                Tracker.Add(_button, 0);
-            }
         }
 
         /// <returns>Returns true when the button was not pressed and is now pressed.</returns>
         public bool Pressed(bool canConsume = true) {
-            if (isUnique) {
-                bool pressed = _condition.Pressed();
-
-                if (canConsume && pressed) {
-                    Consume();
-                }
-                return pressed;
-            }
-            return false;
+            return Pressed(_button, canConsume) && Input.MouseCondition.IsMouseValid;
         }
         /// <returns>Returns true when the button is now pressed.</returns>
         public bool Held(bool canConsume = true) {
-            if (isUnique) {
-                bool held = _condition.Held();
-
-                if (canConsume && held) {
-                    Consume();
-                }
-                return held;
-            }
-            return false;
+            return Held(_button, canConsume) && InputHelper.IsActive;
         }
         /// <returns>Returns true when the button was pressed and is now pressed.</returns>
         public bool HeldOnly(bool canConsume = true) {
-            if (isUnique) {
-                bool held = _condition.HeldOnly();
-
-                if (canConsume && held) {
-                    Consume();
-                }
-                return held;
-            }
-            return false;
+            return HeldOnly(_button, canConsume) && InputHelper.IsActive;
         }
         /// <returns>Returns true when the button was pressed and is now not pressed.</returns>
         public bool Released(bool canConsume = true) {
-            if (isUnique) {
-                bool released = _condition.Released();
-
-                if (canConsume && released) {
-                    Consume();
-                }
-                return released;
-            }
-            return false;
+            return Released(_button, canConsume) && InputHelper.IsActive;
         }
         /// <summary>Mark the condition as used.</summary>
         public void Consume() {
-            Tracker[_button] = InputHelper.CurrentFrame;
+            Consume(_button);
         }
 
-        private bool isUnique => Tracker[_button] != InputHelper.CurrentFrame;
+        /// <returns>Returns true when the mouse button was released and is now pressed.</returns>
+        public static bool Pressed(MouseButton button, bool canConsume = true) {
+            if (IsUnique(button) && Input.MouseCondition.Pressed(button)) {
+                if (canConsume)
+                    Consume(button);
+                return true;
+            }
+            return false;
+        }
+        /// <returns>Returns true when the mouse button is now pressed.</returns>
+        public static bool Held(MouseButton button, bool canConsume = true) {
+            if (IsUnique(button) && Input.MouseCondition.Held(button)) {
+                if (canConsume)
+                    Consume(button);
+                return true;
+            }
+            return false;
+        }
+        /// <returns>Returns true when the mouse button was pressed and is now pressed.</returns>
+        public static bool HeldOnly(MouseButton button, bool canConsume = true) {
+            if (IsUnique(button) && Input.MouseCondition.HeldOnly(button)) {
+                if (canConsume)
+                    Consume(button);
+                return true;
+            }
+            return false;
+        }
+        /// <returns>Returns true when the mouse button was pressed and is now released.</returns>
+        public static bool Released(MouseButton button, bool canConsume = true) {
+            if (IsUnique(button) && Input.MouseCondition.Released(button)) {
+                if (canConsume)
+                    Consume(button);
+                return true;
+            }
+            return false;
+        }
+        /// <summary>Mark the mouse button as used for this frame.</summary>
+        public static void Consume(MouseButton button) {
+            Tracker[button] = InputHelper.CurrentFrame;
+        }
+        /// <summary>Checks if the given mouse button is unique for this frame.</summary>
+        public static bool IsUnique(MouseButton button) => !Tracker.ContainsKey(button) || Tracker[button] != InputHelper.CurrentFrame;
 
         private MouseButton _button;
-        private Input.MouseCondition _condition;
 
-        /// <summary>
-        /// Tracks buttons being used each frames.
-        /// </summary>
+        /// <summary>Tracks buttons being used each frames.</summary>
         protected static Dictionary<MouseButton, uint> Tracker = new Dictionary<MouseButton, uint>();
     }
 }