Skip to content

Commit

Permalink
Add new events to input
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 15, 2024
1 parent 8fdaee2 commit 242109c
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Hypercube.Client.Input.Events.Windowing;
using Hypercube.EventBus.Events;
using Hypercube.Input;
using Hypercube.Math.Vectors;
using Hypercube.OpenGL.Utilities.Helpers;
using OpenTK.Windowing.GraphicsLibraryFramework;

Expand Down Expand Up @@ -65,12 +66,12 @@ private void OnWindowKeyHandled(Window* window, Keys glfwKey, int scanCode, Inpu

private void OnWindowCharHandled(Window* window, uint codepoint)
{
RaiseInput(new WindowingCharHandledEvent());
RaiseInput(new WindowingCharHandledEvent(codepoint));
}

private void OnWindowScrollHandled(Window* window, double offsetX, double offsetY)
{
RaiseInput(new WindowingScrollHandledEvent());
RaiseInput(new WindowingScrollHandledEvent(new Vector2((float) offsetX, (float) offsetY)));
}

private void OnMouseButtonHandled(Window* window, GlfwMouseButton button, InputAction action, GlfwKeyModifiers mods)
Expand Down Expand Up @@ -125,7 +126,7 @@ private static KeyState Convert(InputAction action)
{
return action switch
{
InputAction.Release => KeyState.Release,
InputAction.Release => KeyState.Released,
InputAction.Press => KeyState.Pressed,
InputAction.Repeat => KeyState.Held,
_ => throw new ArgumentOutOfRangeException()
Expand Down
33 changes: 33 additions & 0 deletions Hypercube.Client/Input/Events/CharHandledEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Hypercube.EventBus.Events;
using JetBrains.Annotations;

namespace Hypercube.Client.Input.Events;

[PublicAPI]
public sealed class CharHandledEvent : IEventArgs
{
public readonly uint Code;

public char Char => (char) Code;
public string String => Code.ToString();

public CharHandledEvent(uint code)
{
Code = code;
}

public static implicit operator uint(CharHandledEvent @event)
{
return @event.Code;
}

public static implicit operator char(CharHandledEvent @event)
{
return @event.Char;
}

public static implicit operator string(CharHandledEvent @event)
{
return @event.String;
}
}
2 changes: 2 additions & 0 deletions Hypercube.Client/Input/Events/MouseButtonHandledEvent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Hypercube.EventBus.Events;
using Hypercube.Input;
using JetBrains.Annotations;

namespace Hypercube.Client.Input.Events;

[PublicAPI]
public class MouseButtonHandledEvent : MouseButtonChangedArgs, IEventArgs
{
public MouseButtonHandledEvent(MouseButton button, KeyState state, KeyModifiers modifiers) : base(button, state, modifiers)
Expand Down
16 changes: 16 additions & 0 deletions Hypercube.Client/Input/Events/ScrollHandledEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Hypercube.EventBus.Events;
using Hypercube.Math.Vectors;
using JetBrains.Annotations;

namespace Hypercube.Client.Input.Events;

[PublicAPI]
public class ScrollHandledEvent : IEventArgs
{
public readonly Vector2 Offset;

public ScrollHandledEvent(Vector2 offset)
{
Offset = offset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@

namespace Hypercube.Client.Input.Events.Windowing;

public readonly record struct WindowingCharHandledEvent : IEventArgs;
public class WindowingCharHandledEvent : IEventArgs
{
public readonly uint Code;

public WindowingCharHandledEvent(uint code)
{
Code = code;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
using Hypercube.EventBus.Events;
using Hypercube.Math.Vectors;

namespace Hypercube.Client.Input.Events.Windowing;

public readonly record struct WindowingScrollHandledEvent : IEventArgs;
public class WindowingScrollHandledEvent : IEventArgs
{
public readonly Vector2 Offset;

public WindowingScrollHandledEvent(Vector2 offset)
{
Offset = offset;
}
}
37 changes: 37 additions & 0 deletions Hypercube.Client/Input/Handler/IInputHandler.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,61 @@
using Hypercube.Client.Graphics.Windows;
using Hypercube.Client.Input.Events;
using Hypercube.EventBus;
using Hypercube.Input;
using JetBrains.Annotations;

namespace Hypercube.Client.Input.Handler;

/// <summary>
/// Receives requests from user input, via <see cref="IWindowManager"/>,
/// and submits them for further work via events.
/// </summary>
[PublicAPI]
public interface IInputHandler : IEventSubscriber
{
bool IsKeyState(Key key, KeyState state);

/// <summary>
/// Checks the state <see cref="Key"/> is <see cref="KeyState.Held"/> or <see cref="KeyState.Pressed"/>.
/// </summary>
bool IsKeyHeld(Key key);

/// <summary>
/// Checks the state <see cref="Key"/> is <see cref="KeyState.Pressed"/>.
/// </summary>
bool IsKeyPressed(Key key);

/// <summary>
/// Checks the state <see cref="Key"/> is <see cref="KeyState.Released"/>.
/// </summary>
bool IsKeyReleased(Key key);

/// <summary>
/// Clears all received <see cref="KeyState"/> of <see cref="Key"/>,
/// and can abort all checks and <see cref="KeyHandledEvent"/> (<see cref="KeyState.Held"/>) events.
/// </summary>
void KeyClear();

bool IsMouseButtonState(MouseButton button, KeyState state);

/// <summary>
/// Checks the state <see cref="MouseButton"/> is <see cref="KeyState.Held"/> or <see cref="KeyState.Pressed"/>.
/// </summary>
bool IsMouseButtonHeld(MouseButton button);

/// <summary>
/// Checks the state <see cref="MouseButton"/> is <see cref="KeyState.Pressed"/>.
/// </summary>
bool IsMouseButtonPressed(MouseButton button);

/// <summary>
/// Checks the state <see cref="MouseButton"/> is <see cref="KeyState.Released"/>.
/// </summary>
bool IsMouseButtonReleased(MouseButton button);

/// <summary>
/// Clears all received <see cref="KeyState"/> of <see cref="MouseButton"/>,
/// and can abort all checks and <see cref="MouseButtonHandledEvent"/> (<see cref="KeyState.Held"/>) events.
/// </summary>
void MouseButtonClear();
}
29 changes: 20 additions & 9 deletions Hypercube.Client/Input/Handler/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@
using Hypercube.Input;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Runtimes.Loop.Event;
using JetBrains.Annotations;

namespace Hypercube.Client.Input.Handler;

[PublicAPI]
public sealed class InputHandler : IInputHandler, IPostInject
{
[Dependency] private readonly IEventBus _eventBus = default!;

/// <summary>
/// List of all <see cref="KeyState"/> of <see cref="Key"/> for legacy input and raising events.
/// </summary>
private readonly FrozenDictionary<KeyState, HashSet<Key>> _keys = new Dictionary<KeyState, HashSet<Key>>
{
{ KeyState.Held, [] },
{ KeyState.Release, [] },
{ KeyState.Released, [] },
{ KeyState.Pressed, [] },
}.ToFrozenDictionary();


/// <summary>
/// List of all <see cref="KeyState"/> of <see cref="MouseButton"/> for legacy input and raising events.
/// </summary>
private readonly FrozenDictionary<KeyState, HashSet<MouseButton>> _mouseButtons = new Dictionary<KeyState, HashSet<MouseButton>>
{
{ KeyState.Held, [] },
{ KeyState.Release, [] },
{ KeyState.Released, [] },
{ KeyState.Pressed, [] },
}.ToFrozenDictionary();


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

Expand All @@ -43,7 +50,7 @@ public void PostInject()
private void OnInputFrameUpdate(ref InputFrameEvent args)
{
_keys[KeyState.Pressed].Clear();
_keys[KeyState.Release].Clear();
_keys[KeyState.Released].Clear();

foreach (var key in _keys[KeyState.Held])
{
Expand All @@ -52,7 +59,7 @@ private void OnInputFrameUpdate(ref InputFrameEvent args)
}

_mouseButtons[KeyState.Pressed].Clear();
_mouseButtons[KeyState.Release].Clear();
_mouseButtons[KeyState.Released].Clear();

foreach (var mouseButton in _mouseButtons[KeyState.Held])
{
Expand All @@ -63,6 +70,8 @@ private void OnInputFrameUpdate(ref InputFrameEvent args)

private void OnCharHandled(ref WindowingCharHandledEvent args)
{
var ev = new CharHandledEvent(args.Code);
_eventBus.Raise(ev);
}

private void OnKeyHandled(ref WindowingKeyHandledEvent args)
Expand All @@ -89,7 +98,7 @@ private void OnKeyHandled(ref WindowingKeyHandledEvent args)
_keys[KeyState.Pressed].Add(args.Key);
break;

case KeyState.Release:
case KeyState.Released:
_keys[KeyState.Held].Remove(args.Key);
_keys[KeyState.Pressed].Add(args.Key);
break;
Expand Down Expand Up @@ -123,7 +132,7 @@ private void OnMouseButtonHandled(ref WindowingMouseButtonHandledEvent args)
_mouseButtons[KeyState.Pressed].Add(args.Button);
break;

case KeyState.Release:
case KeyState.Released:
_mouseButtons[KeyState.Held].Remove(args.Button);
_mouseButtons[KeyState.Pressed].Add(args.Button);
break;
Expand All @@ -137,6 +146,8 @@ private void OnMouseButtonHandled(ref WindowingMouseButtonHandledEvent args)

private void OnScrollHandled(ref WindowingScrollHandledEvent args)
{
var ev = new ScrollHandledEvent(args.Offset);
_eventBus.Raise(ev);
}

public bool IsKeyState(Key key, KeyState state)
Expand Down Expand Up @@ -184,7 +195,7 @@ public bool IsMouseButtonPressed(MouseButton button)

public bool IsMouseButtonReleased(MouseButton button)
{
return IsMouseButtonState(button, KeyState.Release);
return IsMouseButtonState(button, KeyState.Released);
}

public void MouseButtonClear()
Expand Down
2 changes: 1 addition & 1 deletion Hypercube.Input/KeyState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public enum KeyState
{
Release,
Released,
Pressed,
Held
}

0 comments on commit 242109c

Please sign in to comment.