Skip to content

Commit

Permalink
Update InputHandler.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 14, 2024
1 parent 6abd3bd commit 885a901
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions Hypercube.Client/Input/Handler/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ public sealed class InputHandler : IInputHandler, IPostInject
{
[Dependency] private readonly IEventBus _eventBus = default!;

private readonly HashSet<Key> _keysRelease = [];
private readonly HashSet<Key> _keysPressed = [];
private readonly HashSet<Key> _keysDown = [];

private readonly Dictionary<Key, KeyState> _keys = [];

private readonly Logger _logger = LoggingManager.GetLogger("input_handler");

public void PostInject()
Expand All @@ -31,23 +29,24 @@ private void OnCharHandled(ref WindowingCharHandledEvent args)

private void OnKeyHandled(ref WindowingKeyHandledEvent args)
{
var state = args.State.State;
var key = args.State.Key;

#if DEBUG
// Use only in Debug build,
// as this check can take quite a lot of performance during input processing
if (!Enum.IsDefined(typeof(Key), args.State.Key))
if (!Enum.IsDefined(typeof(Key), key))
{
_logger.Warning($"Unknown key {args.State.Key} handled");
_logger.Warning($"Unknown key {key} handled");
return;
}
#endif

// Legacy shit, maybe will eat many ram and cpu
// We made many shit because fucking Key rollover: https://en.wikipedia.org/wiki/Key_rollover

if (args.State == KeyState.Press)
{
_keysDown.Add(args.State.Key);
return;
}

_keysDown.Remove(args.State.Key);
_logger.Warning($"{key} {state}");
}

private void OnMouseButtonHandled(ref WindowingMouseButtonHandledEvent args)
Expand All @@ -58,8 +57,28 @@ private void OnScrollHandled(ref WindowingScrollHandledEvent args)
{
}

public bool IsKeyDown(Key key)
public bool IsKeyState(Key key, KeyState state)
{
return _keys.TryGetValue(key, out var keyState) && keyState == state;
}

public bool IsKeyHeld(Key key)
{
return _keys.TryGetValue(key, out var keyState) && keyState is KeyState.Held or KeyState.Pressed;
}

public bool IsKeyPressed(Key key)
{
return IsKeyState(key, KeyState.Pressed);
}

public bool IsKeyReleased(Key key)
{
return IsKeyState(key, KeyState.Pressed);
}

public void KeyClear()
{
return _keysDown.Contains(key);
_keys.Clear();
}
}

0 comments on commit 885a901

Please sign in to comment.