Browse Source

Add sensor tracking and scroll wheel API

Jean-David Moisan 4 năm trước cách đây
mục cha
commit
68af16f844
3 tập tin đã thay đổi với 44 bổ sung9 xóa
  1. 5 0
      Source/MouseCondition.cs
  2. 14 5
      Source/Track/GamePadCondition.cs
  3. 25 4
      Source/Track/MouseCondition.cs

+ 5 - 0
Source/MouseCondition.cs

@@ -51,6 +51,11 @@ namespace Apos.Input {
             return InputHelper.MouseButtons[button](InputHelper.NewMouse) == ButtonState.Released &&
                    InputHelper.MouseButtons[button](InputHelper.OldMouse) == ButtonState.Pressed;
         }
+        /// <returns>Returns true when the scroll wheel is scrolled.</returns>
+        public static bool Scrolled() => ScrollDelta != 0;
+        /// <returns>Returns the difference between last frame and this frame's scroll wheel value.</returns>
+        public static int ScrollDelta => InputHelper.NewMouse.ScrollWheelValue - InputHelper.OldMouse.ScrollWheelValue;
+
         /// <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 &&

+ 14 - 5
Source/Track/GamePadCondition.cs

@@ -32,7 +32,7 @@ namespace Apos.Input.Track {
         }
         /// <summary>Mark the condition as used.</summary>
         public void Consume() {
-            Tracker[(_button, _gamePadIndex)] = InputHelper.CurrentFrame;
+            ButtonTracker[(_button, _gamePadIndex)] = InputHelper.CurrentFrame;
         }
 
         /// <returns>Returns true when the mouse button was released and is now pressed.</returns>
@@ -73,15 +73,24 @@ namespace Apos.Input.Track {
         }
         /// <summary>Mark the gamepad button as used for this frame.</summary>
         public static void Consume(GamePadButton button, int gamePadIndex) {
-            Tracker[(button, gamePadIndex)] = InputHelper.CurrentFrame;
+            ButtonTracker[(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;
+        public static bool IsUnique(GamePadButton button, int gamePadIndex) => !ButtonTracker.ContainsKey((button, gamePadIndex)) || ButtonTracker[(button, gamePadIndex)] != InputHelper.CurrentFrame;
+
+        /// <summary>Mark the gamepad sensor as used for this frame.</summary>
+        public static void Consume(GamePadSensor sensor, int gamePadIndex) {
+            SensorTracker[(sensor, gamePadIndex)] = InputHelper.CurrentFrame;
+        }
+        /// <summary>Checks if the given gamepad sensor is unique for this frame.</summary>
+        public static bool IsUnique(GamePadSensor sensor, int gamePadIndex) => !SensorTracker.ContainsKey((sensor, gamePadIndex)) || SensorTracker[(sensor, gamePadIndex)] != InputHelper.CurrentFrame;
 
         private GamePadButton _button;
         private int _gamePadIndex;
 
-        /// <summary>Tracks buttons being used each frames.</summary>
-        protected static Dictionary<(GamePadButton, int), uint> Tracker = new Dictionary<(GamePadButton, int), uint>();
+        /// <summary>Tracks gamepad buttons being used each frames.</summary>
+        protected static Dictionary<(GamePadButton, int), uint> ButtonTracker = new Dictionary<(GamePadButton, int), uint>();
+        /// <summary>Tracks gamepad sensors being used each frames.</summary>
+        protected static Dictionary<(GamePadSensor, int), uint> SensorTracker = new Dictionary<(GamePadSensor, int), uint>();
     }
 }

+ 25 - 4
Source/Track/MouseCondition.cs

@@ -70,16 +70,37 @@ namespace Apos.Input.Track {
             }
             return false;
         }
+        /// <returns>Returns true when the scroll wheel is scrolled.</returns>
+        public static bool Scrolled(bool canConsume = true) {
+            if (IsUnique(MouseSensor.ScrollWheel) && Apos.Input.MouseCondition.Scrolled()) {
+                if (canConsume)
+                    Consume(MouseSensor.ScrollWheel);
+                return true;
+            }
+            return false;
+        }
+        /// <returns>Returns the difference between last frame and this frame's scroll wheel value.</returns>
+        public static int ScrollDelta => Apos.Input.MouseCondition.ScrollDelta;
+
         /// <summary>Mark the mouse button as used for this frame.</summary>
         public static void Consume(MouseButton button) {
-            Tracker[button] = InputHelper.CurrentFrame;
+            ButtonTracker[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;
+        public static bool IsUnique(MouseButton button) => !ButtonTracker.ContainsKey(button) || ButtonTracker[button] != InputHelper.CurrentFrame;
+
+        /// <summary>Mark the mouse sensor as used for this frame.</summary>
+        public static void Consume(MouseSensor sensor) {
+            SensorTracker[sensor] = InputHelper.CurrentFrame;
+        }
+        /// <summary>Checks if the given mouse sensor is unique for this frame.</summary>
+        public static bool IsUnique(MouseSensor sensor) => !SensorTracker.ContainsKey(sensor) || SensorTracker[sensor] != InputHelper.CurrentFrame;
 
         private MouseButton _button;
 
-        /// <summary>Tracks buttons being used each frames.</summary>
-        protected static Dictionary<MouseButton, uint> Tracker = new Dictionary<MouseButton, uint>();
+        /// <summary>Tracks mouse buttons being used each frames.</summary>
+        protected static Dictionary<MouseButton, uint> ButtonTracker = new Dictionary<MouseButton, uint>();
+        /// <summary>Tracks mouse sensors being used each frames.</summary>
+        protected static Dictionary<MouseSensor, uint> SensorTracker = new Dictionary<MouseSensor, uint>();
     }
 }