InputSystem optimizations and changes.
This commit is contained in:
@@ -43,17 +43,12 @@ public class TestGame : Game
|
|||||||
|
|
||||||
protected override void Ready()
|
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);
|
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected override void Update(double deltaTime)
|
protected override void Update(double deltaTime)
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void Render(double deltaTime)
|
|
||||||
{
|
{
|
||||||
if (Input.IsActionPressed("reload"))
|
if (Input.IsActionPressed("reload"))
|
||||||
{
|
{
|
||||||
@@ -70,7 +65,10 @@ public class TestGame : Game
|
|||||||
{
|
{
|
||||||
_particleSystem.SetEmitterPosition(_emitterId, Input.GetMousePosition());
|
_particleSystem.SetEmitterPosition(_emitterId, Input.GetMousePosition());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Render(double deltaTime)
|
||||||
|
{
|
||||||
Renderer.ClearBackground(Color.Black);
|
Renderer.ClearBackground(Color.Black);
|
||||||
foreach (var emitter in _particleSystem!.Emitters)
|
foreach (var emitter in _particleSystem!.Emitters)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
namespace Voile.Input
|
namespace Voile.Input
|
||||||
{
|
{
|
||||||
public abstract class InputAction
|
|
||||||
|
public interface IInputAction
|
||||||
{
|
{
|
||||||
public abstract bool IsDown(InputSystem inputHandler);
|
bool IsDown(InputSystem inputSystem);
|
||||||
public abstract bool IsPressed(InputSystem inputHandler);
|
bool IsPressed(InputSystem inputSystem);
|
||||||
public abstract bool IsReleased(InputSystem inputHandler);
|
bool IsReleased(InputSystem inputSystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class KeyInputAction : InputAction
|
public struct KeyInputAction : IInputAction
|
||||||
{
|
{
|
||||||
public KeyboardKey Key => _keyboardKey;
|
public KeyboardKey Key => _keyboardKey;
|
||||||
|
|
||||||
@@ -15,19 +16,19 @@ namespace Voile.Input
|
|||||||
{
|
{
|
||||||
_keyboardKey = keyboardKey;
|
_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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ namespace Voile.Input
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The list of all available input mappings, custom and built-in.
|
/// The list of all available input mappings, custom and built-in.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static IReadOnlyDictionary<string, List<InputAction>> InputMappings => inputMappings;
|
public static IReadOnlyDictionary<string, List<IInputAction>> InputMappings => inputMappings;
|
||||||
|
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
inputMappings = new Dictionary<string, List<InputAction>>();
|
inputMappings = new Dictionary<string, List<IInputAction>>();
|
||||||
CreateDefaultMappings();
|
CreateDefaultMappings();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,17 +25,82 @@ namespace Voile.Input
|
|||||||
public void Dispose() => GC.SuppressFinalize(this);
|
public void Dispose() => GC.SuppressFinalize(this);
|
||||||
|
|
||||||
public bool Handled { get => _handled; set => _handled = value; }
|
public bool Handled { get => _handled; set => _handled = value; }
|
||||||
|
|
||||||
|
public bool IsActionDown(string action)
|
||||||
|
{
|
||||||
|
List<IInputAction>? 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<IInputAction>? 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<IInputAction>? 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 IsKeyboardKeyDown(KeyboardKey key);
|
||||||
public abstract bool KeyboardKeyJustPressed(KeyboardKey key);
|
public abstract bool KeyboardKeyJustPressed(KeyboardKey key);
|
||||||
public abstract bool KeyboardKeyJustReleased(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 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 bool IsMouseButtonDown(MouseButton button);
|
||||||
public abstract float GetMouseWheelMovement();
|
public abstract float GetMouseWheelMovement();
|
||||||
public abstract void SetMousePosition(Vector2 position);
|
public abstract void SetMousePosition(Vector2 position);
|
||||||
@@ -46,48 +111,47 @@ namespace Voile.Input
|
|||||||
|
|
||||||
public void SetAsHandled() => _handled = true;
|
public void SetAsHandled() => _handled = true;
|
||||||
|
|
||||||
public void AddInputMapping(string actionName, IEnumerable<InputAction> inputActions)
|
public void AddInputMapping(string actionName, IEnumerable<IInputAction> inputActions)
|
||||||
{
|
{
|
||||||
inputMappings.Add(actionName, inputActions.ToList());
|
inputMappings.Add(actionName, inputActions.ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateDefaultMappings()
|
private void CreateDefaultMappings()
|
||||||
{
|
{
|
||||||
AddInputMapping("up", new InputAction[] {
|
AddInputMapping("up", [
|
||||||
new KeyInputAction(KeyboardKey.W),
|
new KeyInputAction(KeyboardKey.W),
|
||||||
new KeyInputAction(KeyboardKey.Up),
|
new KeyInputAction(KeyboardKey.Up),
|
||||||
});
|
]);
|
||||||
AddInputMapping("down", new InputAction[] {
|
AddInputMapping("down", [
|
||||||
new KeyInputAction(KeyboardKey.S),
|
new KeyInputAction(KeyboardKey.S),
|
||||||
new KeyInputAction(KeyboardKey.Down),
|
new KeyInputAction(KeyboardKey.Down),
|
||||||
});
|
]);
|
||||||
AddInputMapping("left", new InputAction[] {
|
AddInputMapping("left", [
|
||||||
new KeyInputAction(KeyboardKey.A),
|
new KeyInputAction(KeyboardKey.A),
|
||||||
new KeyInputAction(KeyboardKey.Left),
|
new KeyInputAction(KeyboardKey.Left),
|
||||||
});
|
]);
|
||||||
AddInputMapping("right", new InputAction[] {
|
AddInputMapping("right", [
|
||||||
new KeyInputAction(KeyboardKey.D),
|
new KeyInputAction(KeyboardKey.D),
|
||||||
new KeyInputAction(KeyboardKey.Right),
|
new KeyInputAction(KeyboardKey.Right),
|
||||||
});
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected bool TryGetInputMappings(string forAction, [NotNullWhen(true)] out IEnumerable<InputAction>? inputActions)
|
protected bool TryGetInputMappings(string forAction, [NotNullWhen(true)] out List<IInputAction>? inputActions)
|
||||||
{
|
{
|
||||||
var contains = inputMappings.ContainsKey(forAction);
|
|
||||||
inputActions = null;
|
inputActions = null;
|
||||||
|
|
||||||
if (!contains)
|
if (inputMappings.TryGetValue(forAction, out var actions))
|
||||||
{
|
{
|
||||||
_logger.Error($"The action \"{forAction}\" is not present in the input mappings!");
|
inputActions = actions;
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
inputActions = inputMappings[forAction];
|
_logger.Error($"The action \"{forAction}\" is not present in the input mappings!");
|
||||||
|
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Dictionary<string, List<InputAction>> inputMappings = new();
|
protected static Dictionary<string, List<IInputAction>> inputMappings = new();
|
||||||
private bool _handled;
|
private bool _handled;
|
||||||
private Logger _logger = new(nameof(InputSystem));
|
private Logger _logger = new(nameof(InputSystem));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,39 +13,6 @@ namespace Voile.Input
|
|||||||
return Raylib.GetCharPressed();
|
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()
|
public override Vector2 GetMousePosition()
|
||||||
{
|
{
|
||||||
return Raylib.GetMousePosition();
|
return Raylib.GetMousePosition();
|
||||||
@@ -61,45 +28,6 @@ namespace Voile.Input
|
|||||||
Raylib.HideCursor();
|
Raylib.HideCursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool IsActionDown(string action)
|
|
||||||
{
|
|
||||||
IEnumerable<InputAction>? 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<InputAction>? 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<InputAction>? 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)
|
public override bool IsKeyboardKeyDown(KeyboardKey key)
|
||||||
{
|
{
|
||||||
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;
|
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;
|
||||||
|
|||||||
Reference in New Issue
Block a user