Skip to content

Commit

Permalink
Update imgui input
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 21, 2024
1 parent b959db9 commit 6e7b23e
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
15 changes: 13 additions & 2 deletions Hypercube.Client/Graphics/ImGui/ImGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public void PostInject()
_eventBus.Subscribe<RenderAfterDrawingEvent>(this, OnRenderUI);

_eventBus.Subscribe<MouseButtonHandledEvent>(this, OnMouseButton);
_eventBus.Subscribe<KeyHandledEvent>(this, OnKey);
_eventBus.Subscribe<MousePositionHandledEvent>(this, OnMousePosition);
_eventBus.Subscribe<ScrollHandledEvent>(this, OnInputScroll);
_eventBus.Subscribe<CharHandledEvent>(this, OnChar);
}


private void OnGraphicsInitialized(ref GraphicsLibraryInitializedEvent args)
{
Expand Down Expand Up @@ -62,9 +63,14 @@ private void OnRenderUI(ref RenderAfterDrawingEvent args)
_controller.Render();
}

private void OnKey(ref KeyHandledEvent args)
{
_controller.UpdateKey(args.Key, args.State, args.Modifiers);
}

private void OnMouseButton(ref MouseButtonHandledEvent args)
{
_controller.UpdateMouseButtons(args.Button, args.State is KeyState.Pressed or KeyState.Held);
_controller.UpdateMouseButtons(args.Button, args.State, args.Modifiers);
}

private void OnMousePosition(ref MousePositionHandledEvent args)
Expand All @@ -76,4 +82,9 @@ private void OnInputScroll(ref ScrollHandledEvent args)
{
_controller.UpdateMouseScroll(args.Offset);
}

private void OnChar(ref CharHandledEvent args)
{
_controller.UpdateInputCharacter(args.Char);
}
}
4 changes: 3 additions & 1 deletion Hypercube.ImGui/IImGuiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface IImGuiController : IImGui

void InputFrame();
void UpdateMousePosition(Vector2Int position);
void UpdateMouseButtons(MouseButton button, bool state);
void UpdateKey(Key key, KeyState state, KeyModifiers modifiers);
void UpdateMouseButtons(MouseButton button, KeyState state, KeyModifiers modifiers);
void UpdateMouseScroll(Vector2 offset);
void UpdateInputCharacter(char character);
}
120 changes: 118 additions & 2 deletions Hypercube.ImGui/Implementations/OpenGLImGuiController.Input.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Hypercube.Input;
using Hypercube.Mathematics.Vectors;
using ImGuiNET;
using Key = Hypercube.Input.Key;
using MouseButton = Hypercube.Input.MouseButton;

namespace Hypercube.ImGui.Implementations;

Expand All @@ -15,19 +18,132 @@ public void UpdateMousePosition(Vector2Int position)
{
_io.MousePos = position;
}

public void UpdateKey(Key key, KeyState state, KeyModifiers modifiers)
{
_io.AddKeyEvent(TranslateKey(key), state is KeyState.Pressed or KeyState.Held);

if (state is not KeyState.Pressed and not KeyState.Held)
return;

switch (key)
{
case Key.LeftControl or Key.RightControl:
_io.KeyCtrl = true;
break;

case Key.LeftAlt or Key.RightAlt:
_io.KeyAlt = true;
break;

case Key.LeftShift or Key.RightShift:
_io.KeyShift = true;
break;

case Key.LeftSuper or Key.RightSuper:
_io.KeySuper = true;
break;
}
}

public void UpdateMouseButtons(MouseButton button, bool state)
public void UpdateMouseButtons(MouseButton button, KeyState state, KeyModifiers modifiers)
{
var index = (int) button;
if (index >= MouseButtons)
return;

_io.MouseDown[index] = state;
_io.MouseDown[index] = state is KeyState.Pressed or KeyState.Held;
}

public void UpdateMouseScroll(Vector2 offset)
{
_io.MouseWheelH = offset.X;
_io.MouseWheel = offset.Y;
}

public void UpdateInputCharacter(char character)
{
_io.AddInputCharacter(character);

var key = (Key) character;
switch (key)
{
case Key.LeftControl or Key.RightControl:
_io.KeyCtrl = true;
break;

case Key.LeftAlt or Key.RightAlt:
_io.KeyAlt = true;
break;

case Key.LeftShift or Key.RightShift:
_io.KeyShift = true;
break;

case Key.LeftSuper or Key.RightSuper:
_io.KeySuper = true;
break;
}
}

public static ImGuiKey TranslateKey(Key key)
{
return key switch
{
>= Key.Digit0 and <= Key.Digit9 => key - Key.Digit0 + ImGuiKey._0,
>= Key.A and <= Key.Z => key - Key.A + ImGuiKey.A,
>= Key.Numpad0 and <= Key.Numpad9 => key - Key.Numpad0 + ImGuiKey.Keypad0,
>= Key.F1 and <= Key.F24 => key - Key.F1 + ImGuiKey.F24,
_ => key switch
{
Key.Tab => ImGuiKey.Tab,
Key.Left => ImGuiKey.LeftArrow,
Key.Right => ImGuiKey.RightArrow,
Key.Up => ImGuiKey.UpArrow,
Key.Down => ImGuiKey.DownArrow,
Key.PageUp => ImGuiKey.PageUp,
Key.PageDown => ImGuiKey.PageDown,
Key.End => ImGuiKey.End,
Key.Insert => ImGuiKey.Insert,
Key.Delete => ImGuiKey.Delete,
Key.Backspace => ImGuiKey.Backspace,
Key.Space => ImGuiKey.Space,
Key.Enter => ImGuiKey.Enter,
Key.Escape => ImGuiKey.Escape,
Key.Apostrophe => ImGuiKey.Apostrophe,
Key.Comma => ImGuiKey.Comma,
Key.Minus => ImGuiKey.Minus,
Key.Period => ImGuiKey.Period,
Key.Slash => ImGuiKey.Slash,
Key.Semicolon => ImGuiKey.Semicolon,
Key.Equal => ImGuiKey.Equal,
Key.LeftBracket => ImGuiKey.LeftBracket,
Key.Backslash => ImGuiKey.Backslash,
Key.RightBracket => ImGuiKey.RightBracket,
Key.GraveAccent => ImGuiKey.GraveAccent,
Key.CapsLock => ImGuiKey.CapsLock,
Key.ScrollLock => ImGuiKey.ScrollLock,
Key.NumLock => ImGuiKey.NumLock,
Key.PrintScreen => ImGuiKey.PrintScreen,
Key.Pause => ImGuiKey.Pause,
Key.KeyPadDecimal => ImGuiKey.KeypadDecimal,
Key.KeyPadDivide => ImGuiKey.KeypadDivide,
Key.KeyPadMultiply => ImGuiKey.KeypadMultiply,
Key.KeyPadSubtract => ImGuiKey.KeypadSubtract,
Key.KeyPadAdd => ImGuiKey.KeypadAdd,
Key.KeyPadEnter => ImGuiKey.KeypadEnter,
Key.KeyPadEqual => ImGuiKey.KeypadEqual,
Key.LeftShift => ImGuiKey.LeftShift,
Key.LeftControl => ImGuiKey.LeftCtrl,
Key.LeftAlt => ImGuiKey.LeftAlt,
Key.LeftSuper => ImGuiKey.LeftSuper,
Key.RightShift => ImGuiKey.RightShift,
Key.RightControl => ImGuiKey.RightCtrl,
Key.RightAlt => ImGuiKey.RightAlt,
Key.RightSuper => ImGuiKey.RightSuper,
Key.Menu => ImGuiKey.Menu,
_ => ImGuiKey.None
}
};
}
}

0 comments on commit 6e7b23e

Please sign in to comment.