diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs
index 05058f5..803edf1 100644
--- a/TestGame/TestGame.cs
+++ b/TestGame/TestGame.cs
@@ -43,17 +43,12 @@ public class TestGame : Game
protected override void Ready()
{
- Input.AddInputMapping("reload", new InputAction[] { new KeyInputAction(KeyboardKey.R) });
+ Input.AddInputMapping("reload", new IInputAction[] { new KeyInputAction(KeyboardKey.R) });
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
}
protected override void Update(double deltaTime)
- {
-
- }
-
- protected override void Render(double deltaTime)
{
if (Input.IsActionPressed("reload"))
{
@@ -70,7 +65,10 @@ public class TestGame : Game
{
_particleSystem.SetEmitterPosition(_emitterId, Input.GetMousePosition());
}
+ }
+ protected override void Render(double deltaTime)
+ {
Renderer.ClearBackground(Color.Black);
foreach (var emitter in _particleSystem!.Emitters)
{
diff --git a/Voile/Source/Input/InputAction.cs b/Voile/Source/Input/InputAction.cs
index 39681de..a01d0a0 100644
--- a/Voile/Source/Input/InputAction.cs
+++ b/Voile/Source/Input/InputAction.cs
@@ -1,13 +1,14 @@
namespace Voile.Input
{
- public abstract class InputAction
+
+ public interface IInputAction
{
- public abstract bool IsDown(InputSystem inputHandler);
- public abstract bool IsPressed(InputSystem inputHandler);
- public abstract bool IsReleased(InputSystem inputHandler);
+ bool IsDown(InputSystem inputSystem);
+ bool IsPressed(InputSystem inputSystem);
+ bool IsReleased(InputSystem inputSystem);
}
- public class KeyInputAction : InputAction
+ public struct KeyInputAction : IInputAction
{
public KeyboardKey Key => _keyboardKey;
@@ -15,19 +16,19 @@ namespace Voile.Input
{
_keyboardKey = keyboardKey;
}
- public override bool IsDown(InputSystem inputHandler)
+ public bool IsDown(InputSystem inputSystem)
{
- return inputHandler.IsKeyboardKeyDown(_keyboardKey);
+ return inputSystem.IsKeyboardKeyDown(_keyboardKey);
}
- public override bool IsPressed(InputSystem inputHandler)
+ public bool IsPressed(InputSystem inputSystem)
{
- return inputHandler.KeyboardKeyJustPressed(_keyboardKey);
+ return inputSystem.KeyboardKeyJustPressed(_keyboardKey);
}
- public override bool IsReleased(InputSystem inputHandler)
+ public bool IsReleased(InputSystem inputSystem)
{
- return inputHandler.KeyboardKeyJustReleased(_keyboardKey);
+ return inputSystem.KeyboardKeyJustReleased(_keyboardKey);
}
diff --git a/Voile/Source/Input/InputSystem.cs b/Voile/Source/Input/InputSystem.cs
index b2c8354..516f48f 100644
--- a/Voile/Source/Input/InputSystem.cs
+++ b/Voile/Source/Input/InputSystem.cs
@@ -12,11 +12,11 @@ namespace Voile.Input
///
/// The list of all available input mappings, custom and built-in.
///
- public static IReadOnlyDictionary> InputMappings => inputMappings;
+ public static IReadOnlyDictionary> InputMappings => inputMappings;
public void Start()
{
- inputMappings = new Dictionary>();
+ inputMappings = new Dictionary>();
CreateDefaultMappings();
}
@@ -25,17 +25,82 @@ namespace Voile.Input
public void Dispose() => GC.SuppressFinalize(this);
public bool Handled { get => _handled; set => _handled = value; }
+
+ public bool IsActionDown(string action)
+ {
+ List? mappings;
+
+ if (TryGetInputMappings(action, out mappings))
+ {
+ foreach (IInputAction inputAction in mappings)
+ if (inputAction.IsDown(this)) return true;
+ }
+
+ return false;
+ }
+
+ public bool IsActionPressed(string action)
+ {
+ List? mappings;
+
+ if (TryGetInputMappings(action, out mappings))
+ {
+ foreach (IInputAction inputAction in mappings)
+ if (inputAction.IsPressed(this)) return true;
+ }
+
+ return false;
+ }
+
+ public bool IsActionReleased(string action)
+ {
+ List? mappings;
+
+ if (TryGetInputMappings(action, out mappings))
+ {
+ foreach (IInputAction inputAction in mappings)
+ if (inputAction.IsReleased(this)) return true;
+ }
+
+ return false;
+ }
+
+ public Vector2 GetInputDirection(KeyboardKey leftKey, KeyboardKey rightKey, KeyboardKey upKey, KeyboardKey downKey)
+ {
+ Vector2 dir = Vector2.Zero;
+
+ if (IsKeyboardKeyDown(leftKey))
+ dir.X -= 1;
+ if (IsKeyboardKeyDown(rightKey))
+ dir.X += 1;
+ if (IsKeyboardKeyDown(upKey))
+ dir.Y -= 1;
+ if (IsKeyboardKeyDown(downKey))
+ dir.Y += 1;
+
+ return dir == Vector2.Zero ? Vector2.Zero : Vector2.Normalize(dir);
+ }
+ public Vector2 GetInputDirection(string leftAction, string rightAction, string upAction, string downAction)
+ {
+ Vector2 dir = Vector2.Zero;
+
+ if (IsActionDown(leftAction))
+ dir.X -= 1;
+ if (IsActionDown(rightAction))
+ dir.X += 1;
+ if (IsActionDown(upAction))
+ dir.Y -= 1;
+ if (IsActionDown(downAction))
+ dir.Y += 1;
+
+ return dir == Vector2.Zero ? Vector2.Zero : Vector2.Normalize(dir);
+ }
+
public abstract bool IsKeyboardKeyDown(KeyboardKey key);
public abstract bool KeyboardKeyJustPressed(KeyboardKey key);
public abstract bool KeyboardKeyJustReleased(KeyboardKey key);
- public abstract Vector2 GetInputDirection(KeyboardKey leftKey, KeyboardKey rightKey, KeyboardKey upKey, KeyboardKey downKey);
- public abstract Vector2 GetInputDirection(string leftAction, string rightAction, string upAction, string downAction);
+
public abstract int GetCharPressed();
-
- public abstract bool IsActionDown(string action);
- public abstract bool IsActionPressed(string action);
- public abstract bool IsActionReleased(string action);
-
public abstract bool IsMouseButtonDown(MouseButton button);
public abstract float GetMouseWheelMovement();
public abstract void SetMousePosition(Vector2 position);
@@ -46,48 +111,47 @@ namespace Voile.Input
public void SetAsHandled() => _handled = true;
- public void AddInputMapping(string actionName, IEnumerable inputActions)
+ public void AddInputMapping(string actionName, IEnumerable inputActions)
{
inputMappings.Add(actionName, inputActions.ToList());
}
private void CreateDefaultMappings()
{
- AddInputMapping("up", new InputAction[] {
+ AddInputMapping("up", [
new KeyInputAction(KeyboardKey.W),
new KeyInputAction(KeyboardKey.Up),
- });
- AddInputMapping("down", new InputAction[] {
+ ]);
+ AddInputMapping("down", [
new KeyInputAction(KeyboardKey.S),
new KeyInputAction(KeyboardKey.Down),
- });
- AddInputMapping("left", new InputAction[] {
+ ]);
+ AddInputMapping("left", [
new KeyInputAction(KeyboardKey.A),
new KeyInputAction(KeyboardKey.Left),
- });
- AddInputMapping("right", new InputAction[] {
+ ]);
+ AddInputMapping("right", [
new KeyInputAction(KeyboardKey.D),
new KeyInputAction(KeyboardKey.Right),
- });
+ ]);
}
- protected bool TryGetInputMappings(string forAction, [NotNullWhen(true)] out IEnumerable? inputActions)
+ protected bool TryGetInputMappings(string forAction, [NotNullWhen(true)] out List? inputActions)
{
- var contains = inputMappings.ContainsKey(forAction);
inputActions = null;
- if (!contains)
+ if (inputMappings.TryGetValue(forAction, out var actions))
{
- _logger.Error($"The action \"{forAction}\" is not present in the input mappings!");
- return false;
+ inputActions = actions;
+ return true;
}
- inputActions = inputMappings[forAction];
+ _logger.Error($"The action \"{forAction}\" is not present in the input mappings!");
- return true;
+ return false;
}
- protected static Dictionary> inputMappings = new();
+ protected static Dictionary> inputMappings = new();
private bool _handled;
private Logger _logger = new(nameof(InputSystem));
}
diff --git a/Voile/Source/Input/RaylibInputSystem.cs b/Voile/Source/Input/RaylibInputSystem.cs
index 2e16533..af82a74 100644
--- a/Voile/Source/Input/RaylibInputSystem.cs
+++ b/Voile/Source/Input/RaylibInputSystem.cs
@@ -13,39 +13,6 @@ namespace Voile.Input
return Raylib.GetCharPressed();
}
- public override Vector2 GetInputDirection(KeyboardKey leftKey, KeyboardKey rightKey, KeyboardKey upKey, KeyboardKey downKey)
- {
- Vector2 dir = Vector2.Zero;
-
- if (IsKeyboardKeyDown(leftKey))
- dir += new Vector2(-1, 0);
- if (IsKeyboardKeyDown(rightKey))
- dir += new Vector2(1, 0);
- if (IsKeyboardKeyDown(upKey))
- dir += new Vector2(0, -1);
- if (IsKeyboardKeyDown(downKey))
- dir += new Vector2(0, 1);
-
- return dir == Vector2.Zero ? Vector2.Zero : Vector2.Normalize(dir);
- }
-
- public override Vector2 GetInputDirection(string leftAction, string rightAction, string upAction, string downAction)
- {
- Vector2 dir = Vector2.Zero;
-
- if (IsActionDown(leftAction))
- dir += new Vector2(-1, 0);
- if (IsActionDown(rightAction))
- dir += new Vector2(1, 0);
- if (IsActionDown(upAction))
- dir += new Vector2(0, -1);
- if (IsActionDown(downAction))
- dir += new Vector2(0, 1);
-
-
- return dir == Vector2.Zero ? Vector2.Zero : Vector2.Normalize(dir);
- }
-
public override Vector2 GetMousePosition()
{
return Raylib.GetMousePosition();
@@ -61,45 +28,6 @@ namespace Voile.Input
Raylib.HideCursor();
}
- public override bool IsActionDown(string action)
- {
- IEnumerable? mappings;
-
- if (TryGetInputMappings(action, out mappings))
- {
- foreach (InputAction inputAction in mappings)
- if (inputAction.IsDown(this)) return true;
- }
-
- return false;
- }
-
- public override bool IsActionPressed(string action)
- {
- IEnumerable? mappings;
-
- if (TryGetInputMappings(action, out mappings))
- {
- foreach (InputAction inputAction in mappings)
- if (inputAction.IsPressed(this)) return true;
- }
-
- return false;
- }
-
- public override bool IsActionReleased(string action)
- {
- IEnumerable? mappings;
-
- if (TryGetInputMappings(action, out mappings))
- {
- foreach (InputAction inputAction in mappings)
- if (inputAction.IsReleased(this)) return true;
- }
-
- return false;
- }
-
public override bool IsKeyboardKeyDown(KeyboardKey key)
{
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;