-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from technologists-team/update-input
Update input
- Loading branch information
Showing
13 changed files
with
213 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
public static class ClientConstants | ||
{ | ||
public static bool MultiThreadingWindow = false; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Hypercube.Input; | ||
using Hypercube.Shared.EventBus.Events; | ||
|
||
namespace Hypercube.Client.Input.Events; | ||
|
||
public sealed class KeyHandledEvent : KeyStateChangedArgs, IEventArgs | ||
{ | ||
public KeyHandledEvent(Key key, KeyState state, KeyModifiers modifiers, int scanCode) : base(key, state, modifiers, scanCode) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Hypercube.Input; | ||
using Hypercube.Shared.EventBus.Events; | ||
|
||
namespace Hypercube.Client.Input.Events; | ||
|
||
public class MouseButtonHandledEvent : MouseButtonChangedArgs, IEventArgs | ||
{ | ||
public MouseButtonHandledEvent(MouseButton button, KeyState state, KeyModifiers modifiers) : base(button, state, modifiers) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,197 @@ | ||
using Hypercube.Client.Input.Events.Windowing; | ||
using System.Collections.Frozen; | ||
using Hypercube.Client.Input.Events; | ||
using Hypercube.Client.Input.Events.Windowing; | ||
using Hypercube.Input; | ||
using Hypercube.Shared.Dependency; | ||
using Hypercube.Shared.EventBus; | ||
using Hypercube.Shared.Logging; | ||
using Hypercube.Shared.Runtimes.Loop.Event; | ||
|
||
namespace Hypercube.Client.Input.Handler; | ||
|
||
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 FrozenDictionary<KeyState, HashSet<Key>> _keys = new Dictionary<KeyState, HashSet<Key>> | ||
{ | ||
{ KeyState.Held, [] }, | ||
{ KeyState.Release, [] }, | ||
{ KeyState.Pressed, [] }, | ||
}.ToFrozenDictionary(); | ||
|
||
private readonly FrozenDictionary<KeyState, HashSet<MouseButton>> _mouseButtons = new Dictionary<KeyState, HashSet<MouseButton>> | ||
{ | ||
{ KeyState.Held, [] }, | ||
{ KeyState.Release, [] }, | ||
{ KeyState.Pressed, [] }, | ||
}.ToFrozenDictionary(); | ||
|
||
|
||
private readonly Logger _logger = LoggingManager.GetLogger("input_handler"); | ||
|
||
public void PostInject() | ||
{ | ||
_eventBus.Subscribe<InputFrameEvent>(this, OnInputFrameUpdate); | ||
|
||
_eventBus.Subscribe<WindowingCharHandledEvent>(this, OnCharHandled); | ||
_eventBus.Subscribe<WindowingKeyHandledEvent>(this, OnKeyHandled); | ||
_eventBus.Subscribe<WindowingMouseButtonHandledEvent>(this, OnMouseButtonHandled); | ||
_eventBus.Subscribe<WindowingScrollHandledEvent>(this, OnScrollHandled); | ||
} | ||
|
||
private void OnInputFrameUpdate(ref InputFrameEvent args) | ||
{ | ||
_keys[KeyState.Pressed].Clear(); | ||
_keys[KeyState.Release].Clear(); | ||
|
||
foreach (var key in _keys[KeyState.Held]) | ||
{ | ||
// Held event don't support modifiers yet, also scanCode | ||
_eventBus.Raise(new KeyHandledEvent(key, KeyState.Held, KeyModifiers.None, 0)); | ||
} | ||
|
||
_mouseButtons[KeyState.Pressed].Clear(); | ||
_mouseButtons[KeyState.Release].Clear(); | ||
|
||
foreach (var mouseButton in _mouseButtons[KeyState.Held]) | ||
{ | ||
// Held event don't support modifiers yet | ||
_eventBus.Raise(new MouseButtonHandledEvent(mouseButton, KeyState.Held, KeyModifiers.None)); | ||
} | ||
} | ||
|
||
private void OnCharHandled(ref WindowingCharHandledEvent args) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private void OnKeyHandled(ref WindowingKeyHandledEvent args) | ||
{ | ||
#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), args.Key)) | ||
{ | ||
_logger.Warning($"Unknown key {args.State.Key} handled"); | ||
_logger.Warning($"Unknown {args.Key} handled"); | ||
return; | ||
} | ||
#endif | ||
|
||
if (args.State == KeyState.Press) | ||
switch (args.State) | ||
{ | ||
_keysDown.Add(args.State.Key); | ||
return; | ||
case KeyState.Held: | ||
return; | ||
|
||
// Legacy shit, maybe will eat many ram and cpu | ||
// We made many shit because fucking Key rollover: https://en.wikipedia.org/wiki/Key_rollover | ||
case KeyState.Pressed: | ||
_keys[KeyState.Held].Add(args.Key); | ||
_keys[KeyState.Pressed].Add(args.Key); | ||
break; | ||
|
||
case KeyState.Release: | ||
_keys[KeyState.Held].Remove(args.Key); | ||
_keys[KeyState.Pressed].Add(args.Key); | ||
break; | ||
|
||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
_keysDown.Remove(args.State.Key); | ||
|
||
_eventBus.Raise(new KeyHandledEvent(args.Key, args.State, args.Modifiers, args.ScanCode)); | ||
} | ||
|
||
private void OnMouseButtonHandled(ref WindowingMouseButtonHandledEvent args) | ||
{ | ||
#if DEBUG | ||
// Use only in Debug build, | ||
// as this check can take quite a lot of performance during input processing | ||
if (!Enum.IsDefined(typeof(MouseButton), args.Button)) | ||
{ | ||
_logger.Warning($"Unknown {args.Button} handled"); | ||
return; | ||
} | ||
#endif | ||
|
||
switch (args.State) | ||
{ | ||
case KeyState.Held: | ||
return; | ||
|
||
case KeyState.Pressed: | ||
_mouseButtons[KeyState.Held].Add(args.Button); | ||
_mouseButtons[KeyState.Pressed].Add(args.Button); | ||
break; | ||
|
||
case KeyState.Release: | ||
_mouseButtons[KeyState.Held].Remove(args.Button); | ||
_mouseButtons[KeyState.Pressed].Add(args.Button); | ||
break; | ||
|
||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
_eventBus.Raise(new MouseButtonHandledEvent(args.Button, args.State, args.Modifiers)); | ||
} | ||
|
||
private void OnScrollHandled(ref WindowingScrollHandledEvent args) | ||
{ | ||
} | ||
|
||
public bool IsKeyDown(Key key) | ||
public bool IsKeyState(Key key, KeyState state) | ||
{ | ||
return _keysDown.Contains(key); | ||
return _keys[state].Contains(key); | ||
} | ||
|
||
public bool IsKeyHeld(Key key) | ||
{ | ||
return IsKeyState(key, KeyState.Held); | ||
} | ||
|
||
public bool IsKeyPressed(Key key) | ||
{ | ||
return IsKeyState(key, KeyState.Pressed); | ||
} | ||
|
||
public bool IsKeyReleased(Key key) | ||
{ | ||
return IsKeyState(key, KeyState.Pressed); | ||
} | ||
|
||
public void KeyClear() | ||
{ | ||
foreach (var (_, key) in _keys) | ||
{ | ||
key.Clear(); | ||
} | ||
} | ||
|
||
public bool IsMouseButtonState(MouseButton button, KeyState state) | ||
{ | ||
return _mouseButtons[state].Contains(button); | ||
} | ||
|
||
public bool IsMouseButtonHeld(MouseButton button) | ||
{ | ||
return IsMouseButtonState(button, KeyState.Held); | ||
} | ||
|
||
public bool IsMouseButtonPressed(MouseButton button) | ||
{ | ||
return IsMouseButtonState(button, KeyState.Pressed); | ||
} | ||
|
||
public bool IsMouseButtonReleased(MouseButton button) | ||
{ | ||
return IsMouseButtonState(button, KeyState.Release); | ||
} | ||
|
||
public void MouseButtonClear() | ||
{ | ||
foreach (var (_, mouseButtons) in _mouseButtons) | ||
{ | ||
mouseButtons.Clear(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
public enum KeyState | ||
{ | ||
Release, | ||
Press, | ||
Repeat | ||
Pressed, | ||
Held | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.