summaryrefslogtreecommitdiff
path: root/source/game/input
diff options
context:
space:
mode:
Diffstat (limited to 'source/game/input')
-rw-r--r--source/game/input/GamepadHelper.cs59
-rw-r--r--source/game/input/InputManager.cs33
-rw-r--r--source/game/input/KeyboardHelper.cs53
-rw-r--r--source/game/input/MouseHelper.cs37
-rw-r--r--source/game/input/conditions/AllCondition.cs15
-rw-r--r--source/game/input/conditions/AnyCondition.cs15
-rw-r--r--source/game/input/conditions/AverageCondition.cs16
-rw-r--r--source/game/input/conditions/ICondition.cs5
-rw-r--r--source/game/input/definitions/InputDefinition.cs16
-rw-r--r--source/game/input/definitions/gamepad/BinaryGamepadDefinition.cs10
-rw-r--r--source/game/input/definitions/gamepad/SensorGamepadDefinition.cs13
-rw-r--r--source/game/input/definitions/keyboard/BinaryKeyboardDefinition.cs11
-rw-r--r--source/game/input/definitions/keyboard/TrinaryKeyboardDefinition.cs18
-rw-r--r--source/game/input/definitions/mouse/BinaryMouseDefinition.cs10
14 files changed, 311 insertions, 0 deletions
diff --git a/source/game/input/GamepadHelper.cs b/source/game/input/GamepadHelper.cs
new file mode 100644
index 0000000..dfc3175
--- /dev/null
+++ b/source/game/input/GamepadHelper.cs
@@ -0,0 +1,59 @@
+using System;
+using Celesteia.Game.Input.Definitions;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Input;
+
+namespace Celesteia.Game.Input {
+ public static class GamepadHelper {
+ private static GamePadState _prev;
+ private static GamePadState _curr;
+
+ public static void Update() {
+ _prev = _curr;
+ _curr = GamePad.GetState(0);
+ }
+
+ public static bool AnyButton() {
+ foreach (Buttons b in Enum.GetValues<Buttons>())
+ if (IsDown(b)) return true;
+
+ return false;
+ }
+
+ // Was true, now true.
+ public static bool Held(Buttons buttons) => _prev.IsButtonDown(buttons) && _curr.IsButtonDown(buttons);
+ // Is true.
+ public static bool IsDown(Buttons buttons) => _curr.IsButtonDown(buttons);
+ // Was false, now true.
+ public static bool Pressed(Buttons buttons) => !_prev.IsButtonDown(buttons) && _curr.IsButtonDown(buttons);
+ // Was true, now false.
+ public static bool Released(Buttons buttons) => _prev.IsButtonDown(buttons) && !_curr.IsButtonDown(buttons);
+
+ public static bool PollButtons(Buttons buttons, InputPollType type) => type switch {
+ InputPollType.Held => Held(buttons),
+ InputPollType.IsDown => IsDown(buttons),
+ InputPollType.Pressed => Pressed(buttons),
+ InputPollType.Released => Released(buttons),
+ _ => false
+ };
+
+ public static float PollSensor(GamePadSensor sensor) => sensor switch {
+ GamePadSensor.LeftThumbStickX => _curr.ThumbSticks.Left.X,
+ GamePadSensor.LeftThumbStickY => _curr.ThumbSticks.Left.Y,
+ GamePadSensor.LeftTrigger => _curr.Triggers.Left,
+ GamePadSensor.RightThumbStickX => _curr.ThumbSticks.Right.X,
+ GamePadSensor.RightThumbStickY => _curr.ThumbSticks.Right.Y,
+ GamePadSensor.RightTrigger => _curr.Triggers.Right,
+ _ => 0f
+ };
+ }
+
+ public enum GamePadSensor {
+ LeftThumbStickX,
+ LeftThumbStickY,
+ LeftTrigger,
+ RightThumbStickX,
+ RightThumbStickY,
+ RightTrigger
+ }
+} \ No newline at end of file
diff --git a/source/game/input/InputManager.cs b/source/game/input/InputManager.cs
new file mode 100644
index 0000000..1bd3e38
--- /dev/null
+++ b/source/game/input/InputManager.cs
@@ -0,0 +1,33 @@
+using Microsoft.Xna.Framework;
+
+namespace Celesteia.Game.Input {
+ public class InputManager : GameComponent {
+ private new GameInstance Game => (GameInstance) base.Game;
+ public InputManager(GameInstance Game) : base(Game) {}
+
+ public override void Initialize()
+ {
+ base.Initialize();
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ base.Dispose(disposing);
+ }
+
+ public override void Update(GameTime gameTime)
+ {
+ if (!Enabled) return;
+
+ KeyboardHelper.Update();
+ GamepadHelper.Update();
+ MouseHelper.Update();
+
+ base.Update(gameTime);
+ }
+
+ public bool GetAny() {
+ return KeyboardHelper.AnyKey() || GamepadHelper.AnyButton();
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/input/KeyboardHelper.cs b/source/game/input/KeyboardHelper.cs
new file mode 100644
index 0000000..a8fd4f0
--- /dev/null
+++ b/source/game/input/KeyboardHelper.cs
@@ -0,0 +1,53 @@
+using System.Collections.Generic;
+using System.Linq;
+using Celesteia.Game.Input.Definitions;
+using Microsoft.Xna.Framework.Input;
+using MonoGame.Extended.Input;
+
+namespace Celesteia.Game.Input {
+ public static class KeyboardHelper {
+ private static KeyboardStateExtended _prev;
+ private static KeyboardStateExtended _curr;
+
+ public static void Update() {
+ _prev = _curr;
+ _curr = KeyboardExtended.GetState();
+ }
+
+ // Is any key (excluding the exemptions) down in the current state?
+ public static List<Keys> exemptedFromAny = new List<Keys> {
+ { Keys.F3 }, { Keys.F11 }
+ };
+
+ private static Keys[] pressed;
+ public static bool AnyKey() {
+ pressed = _curr.GetPressedKeys();
+ bool anyKey = pressed.Length > 0;
+
+ if (anyKey)
+ for (int i = 0; i < exemptedFromAny.Count; i++)
+ anyKey = !pressed.Contains(exemptedFromAny[i]);
+
+ return anyKey;
+ }
+
+ // Was true, now true.
+ public static bool Held(Keys keys) => _prev.IsKeyDown(keys) && _curr.IsKeyDown(keys);
+ // Is true.
+ public static bool IsDown(Keys keys) => _curr.IsKeyDown(keys);
+ // Was false, now true.
+ public static bool Pressed(Keys keys) => !_prev.IsKeyDown(keys) && _curr.IsKeyDown(keys);
+ // Was true, now false.
+ public static bool Released(Keys keys) => _prev.IsKeyDown(keys) && !_curr.IsKeyDown(keys);
+
+ public static bool Poll(Keys keys, InputPollType type) => type switch {
+ InputPollType.Held => Held(keys),
+ InputPollType.IsDown => IsDown(keys),
+ InputPollType.Pressed => Pressed(keys),
+ InputPollType.Released => Released(keys),
+ _ => false
+ };
+ }
+
+
+} \ No newline at end of file
diff --git a/source/game/input/MouseHelper.cs b/source/game/input/MouseHelper.cs
new file mode 100644
index 0000000..3000c6e
--- /dev/null
+++ b/source/game/input/MouseHelper.cs
@@ -0,0 +1,37 @@
+using Celesteia.Game.Input.Definitions;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Input;
+
+namespace Celesteia.Game.Input {
+ public static class MouseHelper {
+ private static MouseStateExtended _prev;
+ private static MouseStateExtended _curr;
+
+ public static int ScrollDelta => _curr.ScrollWheelValue - _prev.ScrollWheelValue;
+
+ // Get the position of the mouse pointer on the SCREEN, not the VIEWPORT.
+ public static Point Position => _curr.Position;
+
+ public static void Update() {
+ _prev = _curr;
+ _curr = MouseExtended.GetState();
+ }
+
+ // Was true, now true.
+ public static bool Held(MouseButton button) => _prev.IsButtonDown(button) && _curr.IsButtonDown(button);
+ // Is true.
+ public static bool IsDown(MouseButton button) => _curr.IsButtonDown(button);
+ // Was false, now true.
+ public static bool Pressed(MouseButton button) => !_prev.IsButtonDown(button) && _curr.IsButtonDown(button);
+ // Was true, now false.
+ public static bool Released(MouseButton button) => _prev.IsButtonDown(button) && !_curr.IsButtonDown(button);
+
+ public static bool Poll(MouseButton button, InputPollType type) => type switch {
+ InputPollType.Held => Held(button),
+ InputPollType.IsDown => IsDown(button),
+ InputPollType.Pressed => Pressed(button),
+ InputPollType.Released => Released(button),
+ _ => false
+ };
+ }
+} \ No newline at end of file
diff --git a/source/game/input/conditions/AllCondition.cs b/source/game/input/conditions/AllCondition.cs
new file mode 100644
index 0000000..183acfb
--- /dev/null
+++ b/source/game/input/conditions/AllCondition.cs
@@ -0,0 +1,15 @@
+using Celesteia.Game.Input.Definitions;
+
+namespace Celesteia.Game.Input.Conditions {
+ public class AllCondition : ICondition<bool> {
+ private IBinaryInputDefinition[] _definitions;
+
+ public AllCondition(params IBinaryInputDefinition[] definitions)
+ => _definitions = definitions;
+
+ public bool Poll() {
+ for (int i = 0; i < _definitions.Length; i++) if (!_definitions[i].Test()) return false;
+ return true;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/input/conditions/AnyCondition.cs b/source/game/input/conditions/AnyCondition.cs
new file mode 100644
index 0000000..a870539
--- /dev/null
+++ b/source/game/input/conditions/AnyCondition.cs
@@ -0,0 +1,15 @@
+using Celesteia.Game.Input.Definitions;
+
+namespace Celesteia.Game.Input.Conditions {
+ public class AnyCondition : ICondition<bool> {
+ private IBinaryInputDefinition[] _definitions;
+
+ public AnyCondition(params IBinaryInputDefinition[] definitions)
+ => _definitions = definitions;
+
+ public bool Poll() {
+ for (int i = 0; i < _definitions.Length; i++) if (_definitions[i].Test()) return true;
+ return false;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/input/conditions/AverageCondition.cs b/source/game/input/conditions/AverageCondition.cs
new file mode 100644
index 0000000..08abdb7
--- /dev/null
+++ b/source/game/input/conditions/AverageCondition.cs
@@ -0,0 +1,16 @@
+using Celesteia.Game.Input.Definitions;
+
+namespace Celesteia.Game.Input.Conditions {
+ public class AverageCondition : ICondition<float> {
+ private IFloatInputDefinition[] _definitions;
+
+ public AverageCondition(params IFloatInputDefinition[] definitions)
+ => _definitions = definitions;
+
+ public float Poll() {
+ float total = 0f;
+ for (int i = 0; i < _definitions.Length; i++) total += _definitions[i].Test();
+ return total / _definitions.Length;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/input/conditions/ICondition.cs b/source/game/input/conditions/ICondition.cs
new file mode 100644
index 0000000..32bbc74
--- /dev/null
+++ b/source/game/input/conditions/ICondition.cs
@@ -0,0 +1,5 @@
+namespace Celesteia.Game.Input.Conditions {
+ public interface ICondition<T> {
+ T Poll();
+ }
+} \ No newline at end of file
diff --git a/source/game/input/definitions/InputDefinition.cs b/source/game/input/definitions/InputDefinition.cs
new file mode 100644
index 0000000..7d56f99
--- /dev/null
+++ b/source/game/input/definitions/InputDefinition.cs
@@ -0,0 +1,16 @@
+namespace Celesteia.Game.Input.Definitions {
+ public interface IInputDefinition<T> {
+ T Test();
+ }
+
+ public interface IBinaryInputDefinition : IInputDefinition<bool> {}
+ public interface IFloatInputDefinition : IInputDefinition<float> {}
+
+ public enum InputType {
+ Keyboard, Gamepad
+ }
+
+ public enum InputPollType {
+ IsDown, Held, Pressed, Released
+ }
+} \ No newline at end of file
diff --git a/source/game/input/definitions/gamepad/BinaryGamepadDefinition.cs b/source/game/input/definitions/gamepad/BinaryGamepadDefinition.cs
new file mode 100644
index 0000000..5aec7a8
--- /dev/null
+++ b/source/game/input/definitions/gamepad/BinaryGamepadDefinition.cs
@@ -0,0 +1,10 @@
+using Microsoft.Xna.Framework.Input;
+
+namespace Celesteia.Game.Input.Definitions.Gamepad {
+ public class BinaryGamepadDefinition : IBinaryInputDefinition {
+ public Buttons Buttons;
+ public InputPollType PollType;
+
+ public bool Test() => GamepadHelper.PollButtons(Buttons, PollType);
+ }
+} \ No newline at end of file
diff --git a/source/game/input/definitions/gamepad/SensorGamepadDefinition.cs b/source/game/input/definitions/gamepad/SensorGamepadDefinition.cs
new file mode 100644
index 0000000..ebbf2be
--- /dev/null
+++ b/source/game/input/definitions/gamepad/SensorGamepadDefinition.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace Celesteia.Game.Input.Definitions.Gamepad {
+ public class SensorGamepadDefinition : IFloatInputDefinition {
+ public GamePadSensor Sensor;
+ private float _current = 0;
+
+ public float Test() {
+ _current = GamepadHelper.PollSensor(Sensor);
+ return _current;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/input/definitions/keyboard/BinaryKeyboardDefinition.cs b/source/game/input/definitions/keyboard/BinaryKeyboardDefinition.cs
new file mode 100644
index 0000000..87ebb77
--- /dev/null
+++ b/source/game/input/definitions/keyboard/BinaryKeyboardDefinition.cs
@@ -0,0 +1,11 @@
+using System;
+using Microsoft.Xna.Framework.Input;
+
+namespace Celesteia.Game.Input.Definitions.Keyboard {
+ public class BinaryKeyboardDefinition : IBinaryInputDefinition {
+ public Keys Keys;
+ public InputPollType PollType;
+
+ public bool Test() => KeyboardHelper.Poll(Keys, PollType);
+ }
+} \ No newline at end of file
diff --git a/source/game/input/definitions/keyboard/TrinaryKeyboardDefinition.cs b/source/game/input/definitions/keyboard/TrinaryKeyboardDefinition.cs
new file mode 100644
index 0000000..7439859
--- /dev/null
+++ b/source/game/input/definitions/keyboard/TrinaryKeyboardDefinition.cs
@@ -0,0 +1,18 @@
+using Microsoft.Xna.Framework.Input;
+
+namespace Celesteia.Game.Input.Definitions.Keyboard {
+ public class TrinaryKeyboardDefinition : IFloatInputDefinition {
+ public Keys Negative;
+ public Keys Positive;
+ public InputPollType PollType;
+
+ private float _current = 0;
+
+ public float Test() {
+ _current =
+ (KeyboardHelper.Poll(Negative, PollType) ? -1 : 0) +
+ (KeyboardHelper.Poll(Positive, PollType) ? 1 : 0);
+ return _current;
+ }
+ }
+} \ No newline at end of file
diff --git a/source/game/input/definitions/mouse/BinaryMouseDefinition.cs b/source/game/input/definitions/mouse/BinaryMouseDefinition.cs
new file mode 100644
index 0000000..0008ded
--- /dev/null
+++ b/source/game/input/definitions/mouse/BinaryMouseDefinition.cs
@@ -0,0 +1,10 @@
+using MonoGame.Extended.Input;
+
+namespace Celesteia.Game.Input.Definitions.Mouse {
+ public class BinaryMouseDefinition : IBinaryInputDefinition {
+ public MouseButton Button;
+ public InputPollType PollType;
+
+ public bool Test() => MouseHelper.Poll(Button, PollType);
+ }
+} \ No newline at end of file