Skip to content

Commit

Permalink
Add sensor tracking and scroll wheel API
Browse files Browse the repository at this point in the history
  • Loading branch information
Apostolique committed Jan 24, 2021
1 parent 2f58cd5 commit 68af16f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Source/MouseCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class MouseCondition : ICondition {
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 &&
Expand Down
19 changes: 14 additions & 5 deletions Source/Track/GamePadCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class GamePadCondition : ICondition {
}
/// <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>
Expand Down Expand Up @@ -73,15 +73,24 @@ public class GamePadCondition : ICondition {
}
/// <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>();
}
}
29 changes: 25 additions & 4 deletions Source/Track/MouseCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,37 @@ public class MouseCondition : ICondition {
}
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>();
}
}

0 comments on commit 68af16f

Please sign in to comment.