Skip to content

Commit 17872c4

Browse files
committed
Using the new input system and misc changes
1 parent 57948fe commit 17872c4

File tree

3 files changed

+165
-133
lines changed

3 files changed

+165
-133
lines changed

src/ImGui.NET.SampleProgram.XNA/ImGuiRenderer.cs

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public class ImGuiRenderer
3636

3737
// Input
3838
private int _scrollWheelValue;
39-
40-
private List<int> _keys = new List<int>();
41-
39+
private int _horizontalScrollWheelValue;
40+
private readonly float WHEEL_DELTA = 120;
41+
private Array _allKeys = Enum.GetValues(typeof(Keys));
4242
public ImGuiRenderer(Game game)
4343
{
4444
var context = ImGui.CreateContext();
@@ -139,40 +139,19 @@ public virtual void AfterLayout()
139139
#region Setup & Update
140140

141141
/// <summary>
142-
/// Maps ImGui keys to XNA keys. We use this later on to tell ImGui what keys were pressed
142+
/// Setup key input event handler.
143143
/// </summary>
144144
protected virtual void SetupInput()
145145
{
146146
var io = ImGui.GetIO();
147147

148-
_keys.Add(io.KeyMap[(int)ImGuiKey.Tab] = (int)Keys.Tab);
149-
_keys.Add(io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)Keys.Left);
150-
_keys.Add(io.KeyMap[(int)ImGuiKey.RightArrow] = (int)Keys.Right);
151-
_keys.Add(io.KeyMap[(int)ImGuiKey.UpArrow] = (int)Keys.Up);
152-
_keys.Add(io.KeyMap[(int)ImGuiKey.DownArrow] = (int)Keys.Down);
153-
_keys.Add(io.KeyMap[(int)ImGuiKey.PageUp] = (int)Keys.PageUp);
154-
_keys.Add(io.KeyMap[(int)ImGuiKey.PageDown] = (int)Keys.PageDown);
155-
_keys.Add(io.KeyMap[(int)ImGuiKey.Home] = (int)Keys.Home);
156-
_keys.Add(io.KeyMap[(int)ImGuiKey.End] = (int)Keys.End);
157-
_keys.Add(io.KeyMap[(int)ImGuiKey.Delete] = (int)Keys.Delete);
158-
_keys.Add(io.KeyMap[(int)ImGuiKey.Backspace] = (int)Keys.Back);
159-
_keys.Add(io.KeyMap[(int)ImGuiKey.Enter] = (int)Keys.Enter);
160-
_keys.Add(io.KeyMap[(int)ImGuiKey.Escape] = (int)Keys.Escape);
161-
_keys.Add(io.KeyMap[(int)ImGuiKey.Space] = (int)Keys.Space);
162-
_keys.Add(io.KeyMap[(int)ImGuiKey.A] = (int)Keys.A);
163-
_keys.Add(io.KeyMap[(int)ImGuiKey.C] = (int)Keys.C);
164-
_keys.Add(io.KeyMap[(int)ImGuiKey.V] = (int)Keys.V);
165-
_keys.Add(io.KeyMap[(int)ImGuiKey.X] = (int)Keys.X);
166-
_keys.Add(io.KeyMap[(int)ImGuiKey.Y] = (int)Keys.Y);
167-
_keys.Add(io.KeyMap[(int)ImGuiKey.Z] = (int)Keys.Z);
168-
169148
// MonoGame-specific //////////////////////
170149
_game.Window.TextInput += (s, a) =>
171150
{
172151
if (a.Character == '\t') return;
173-
174152
io.AddInputCharacter(a.Character);
175153
};
154+
176155
///////////////////////////////////////////
177156

178157
// FNA-specific ///////////////////////////
@@ -183,8 +162,6 @@ protected virtual void SetupInput()
183162
// ImGui.GetIO().AddInputCharacter(c);
184163
//};
185164
///////////////////////////////////////////
186-
187-
ImGui.GetIO().Fonts.AddFontDefault();
188165
}
189166

190167
/// <summary>
@@ -217,29 +194,90 @@ protected virtual void UpdateInput()
217194

218195
var mouse = Mouse.GetState();
219196
var keyboard = Keyboard.GetState();
197+
io.AddMousePosEvent(mouse.X, mouse.Y);
198+
io.AddMouseButtonEvent(0, mouse.LeftButton == ButtonState.Pressed);
199+
io.AddMouseButtonEvent(1, mouse.RightButton == ButtonState.Pressed);
200+
io.AddMouseButtonEvent(2, mouse.MiddleButton == ButtonState.Pressed);
201+
io.AddMouseButtonEvent(3, mouse.XButton1 == ButtonState.Pressed);
202+
io.AddMouseButtonEvent(4, mouse.XButton2 == ButtonState.Pressed);
203+
204+
io.AddMouseWheelEvent(
205+
(mouse.HorizontalScrollWheelValue - _horizontalScrollWheelValue) / WHEEL_DELTA,
206+
(mouse.ScrollWheelValue - _scrollWheelValue) / WHEEL_DELTA);
207+
_scrollWheelValue = mouse.ScrollWheelValue;
208+
_horizontalScrollWheelValue = mouse.HorizontalScrollWheelValue;
220209

221-
for (int i = 0; i < _keys.Count; i++)
210+
foreach (var key in _allKeys)
222211
{
223-
io.KeysDown[_keys[i]] = keyboard.IsKeyDown((Keys)_keys[i]);
212+
if (TryMapKeys((Keys)key, out ImGuiKey imguikey))
213+
{
214+
io.AddKeyEvent(imguikey, keyboard.IsKeyDown((Keys)key));
215+
}
224216
}
225217

226-
io.KeyShift = keyboard.IsKeyDown(Keys.LeftShift) || keyboard.IsKeyDown(Keys.RightShift);
227-
io.KeyCtrl = keyboard.IsKeyDown(Keys.LeftControl) || keyboard.IsKeyDown(Keys.RightControl);
228-
io.KeyAlt = keyboard.IsKeyDown(Keys.LeftAlt) || keyboard.IsKeyDown(Keys.RightAlt);
229-
io.KeySuper = keyboard.IsKeyDown(Keys.LeftWindows) || keyboard.IsKeyDown(Keys.RightWindows);
230-
231218
io.DisplaySize = new System.Numerics.Vector2(_graphicsDevice.PresentationParameters.BackBufferWidth, _graphicsDevice.PresentationParameters.BackBufferHeight);
232219
io.DisplayFramebufferScale = new System.Numerics.Vector2(1f, 1f);
220+
}
233221

234-
io.MousePos = new System.Numerics.Vector2(mouse.X, mouse.Y);
222+
private bool TryMapKeys(Keys key, out ImGuiKey imguikey)
223+
{
224+
//Special case not handed in the switch...
225+
//If the actual key we put in is "None", return none and true.
226+
//otherwise, return none and false.
227+
if (key == Keys.None)
228+
{
229+
imguikey = ImGuiKey.None;
230+
return true;
231+
}
235232

236-
io.MouseDown[0] = mouse.LeftButton == ButtonState.Pressed;
237-
io.MouseDown[1] = mouse.RightButton == ButtonState.Pressed;
238-
io.MouseDown[2] = mouse.MiddleButton == ButtonState.Pressed;
233+
imguikey = key switch
234+
{
235+
Keys.Back => ImGuiKey.Backspace,
236+
Keys.Tab => ImGuiKey.Tab,
237+
Keys.Enter => ImGuiKey.Enter,
238+
Keys.CapsLock => ImGuiKey.CapsLock,
239+
Keys.Escape => ImGuiKey.Escape,
240+
Keys.Space => ImGuiKey.Space,
241+
Keys.PageUp => ImGuiKey.PageUp,
242+
Keys.PageDown => ImGuiKey.PageDown,
243+
Keys.End => ImGuiKey.End,
244+
Keys.Home => ImGuiKey.Home,
245+
Keys.Left => ImGuiKey.LeftArrow,
246+
Keys.Right => ImGuiKey.RightArrow,
247+
Keys.Up => ImGuiKey.UpArrow,
248+
Keys.Down => ImGuiKey.DownArrow,
249+
Keys.PrintScreen => ImGuiKey.PrintScreen,
250+
Keys.Insert => ImGuiKey.Insert,
251+
Keys.Delete => ImGuiKey.Delete,
252+
>= Keys.D0 and <= Keys.D9 => ImGuiKey._0 + (key - Keys.D0),
253+
>= Keys.A and <= Keys.Z => ImGuiKey.A + (key - Keys.A),
254+
>= Keys.NumPad0 and <= Keys.NumPad9 => ImGuiKey.Keypad0 + (key - Keys.NumPad0),
255+
Keys.Multiply => ImGuiKey.KeypadMultiply,
256+
Keys.Add => ImGuiKey.KeypadAdd,
257+
Keys.Subtract => ImGuiKey.KeypadSubtract,
258+
Keys.Decimal => ImGuiKey.KeypadDecimal,
259+
Keys.Divide => ImGuiKey.KeypadDivide,
260+
>= Keys.F1 and <= Keys.F12 => ImGuiKey.F1 + (key - Keys.F1),
261+
Keys.NumLock => ImGuiKey.NumLock,
262+
Keys.Scroll => ImGuiKey.ScrollLock,
263+
Keys.LeftShift or Keys.RightShift => ImGuiKey.ModShift,
264+
Keys.LeftControl or Keys.RightControl => ImGuiKey.ModCtrl,
265+
Keys.LeftAlt or Keys.RightAlt => ImGuiKey.ModAlt,
266+
Keys.OemSemicolon => ImGuiKey.Semicolon,
267+
Keys.OemPlus => ImGuiKey.Equal,
268+
Keys.OemComma => ImGuiKey.Comma,
269+
Keys.OemMinus => ImGuiKey.Minus,
270+
Keys.OemPeriod => ImGuiKey.Period,
271+
Keys.OemQuestion => ImGuiKey.Slash,
272+
Keys.OemTilde => ImGuiKey.GraveAccent,
273+
Keys.OemOpenBrackets => ImGuiKey.LeftBracket,
274+
Keys.OemCloseBrackets => ImGuiKey.RightBracket,
275+
Keys.OemPipe => ImGuiKey.Backslash,
276+
Keys.OemQuotes => ImGuiKey.Apostrophe,
277+
_ => ImGuiKey.None,
278+
};
239279

240-
var scrollDelta = mouse.ScrollWheelValue - _scrollWheelValue;
241-
io.MouseWheel = scrollDelta > 0 ? 1 : scrollDelta < 0 ? -1 : 0;
242-
_scrollWheelValue = mouse.ScrollWheelValue;
280+
return imguikey != ImGuiKey.None;
243281
}
244282

245283
#endregion Setup & Update

src/ImGui.NET.SampleProgram/ImGuiController.cs

Lines changed: 79 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,15 @@ public ImGuiController(GraphicsDevice gd, OutputDescription outputDescription, i
5959
_windowWidth = width;
6060
_windowHeight = height;
6161

62-
IntPtr context = ImGui.CreateContext();
63-
ImGui.SetCurrentContext(context);
64-
var fonts = ImGui.GetIO().Fonts;
65-
ImGui.GetIO().Fonts.AddFontDefault();
66-
ImGui.GetIO().BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset;
62+
ImGui.CreateContext();
63+
var io = ImGui.GetIO();
64+
io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset;
65+
io.ConfigFlags |= ImGuiConfigFlags.NavEnableKeyboard |
66+
ImGuiConfigFlags.DockingEnable;
67+
io.Fonts.Flags |= ImFontAtlasFlags.NoBakedLines;
6768

6869
CreateDeviceResources(gd, outputDescription);
69-
SetKeyMappings();
70-
7170
SetPerFrameImGuiData(1f / 60f);
72-
7371
ImGui.NewFrame();
7472
_frameBegun = true;
7573
}
@@ -328,100 +326,91 @@ private void SetPerFrameImGuiData(float deltaSeconds)
328326
io.DeltaTime = deltaSeconds; // DeltaTime is in seconds.
329327
}
330328

331-
private void UpdateImGuiInput(InputSnapshot snapshot)
329+
private bool TryMapKey(Key key, out ImGuiKey result)
332330
{
333-
ImGuiIOPtr io = ImGui.GetIO();
334-
335-
Vector2 mousePosition = snapshot.MousePosition;
336-
337-
// Determine if any of the mouse buttons were pressed during this snapshot period, even if they are no longer held.
338-
bool leftPressed = false;
339-
bool middlePressed = false;
340-
bool rightPressed = false;
341-
foreach (MouseEvent me in snapshot.MouseEvents)
331+
ImGuiKey KeyToImGuiKeyShortcut(Key keyToConvert, Key startKey1, ImGuiKey startKey2)
342332
{
343-
if (me.Down)
344-
{
345-
switch (me.MouseButton)
346-
{
347-
case MouseButton.Left:
348-
leftPressed = true;
349-
break;
350-
case MouseButton.Middle:
351-
middlePressed = true;
352-
break;
353-
case MouseButton.Right:
354-
rightPressed = true;
355-
break;
356-
}
357-
}
333+
int changeFromStart1 = (int)keyToConvert - (int)startKey1;
334+
return startKey2 + changeFromStart1;
358335
}
359336

360-
io.MouseDown[0] = leftPressed || snapshot.IsMouseDown(MouseButton.Left);
361-
io.MouseDown[1] = rightPressed || snapshot.IsMouseDown(MouseButton.Right);
362-
io.MouseDown[2] = middlePressed || snapshot.IsMouseDown(MouseButton.Middle);
363-
io.MousePos = mousePosition;
364-
io.MouseWheel = snapshot.WheelDelta;
337+
result = key switch
338+
{
339+
>= Key.F1 and <= Key.F12 => KeyToImGuiKeyShortcut(key, Key.F1, ImGuiKey.F1),
340+
>= Key.Keypad0 and <= Key.Keypad9 => KeyToImGuiKeyShortcut(key, Key.Keypad0, ImGuiKey.Keypad0),
341+
>= Key.A and <= Key.Z => KeyToImGuiKeyShortcut(key, Key.A, ImGuiKey.A),
342+
>= Key.Number0 and <= Key.Number9 => KeyToImGuiKeyShortcut(key, Key.Number0, ImGuiKey._0),
343+
Key.ShiftLeft or Key.ShiftRight => ImGuiKey.ModShift,
344+
Key.ControlLeft or Key.ControlRight => ImGuiKey.ModCtrl,
345+
Key.AltLeft or Key.AltRight => ImGuiKey.ModAlt,
346+
Key.WinLeft or Key.WinRight => ImGuiKey.ModSuper,
347+
Key.Menu => ImGuiKey.Menu,
348+
Key.Up => ImGuiKey.UpArrow,
349+
Key.Down => ImGuiKey.DownArrow,
350+
Key.Left => ImGuiKey.LeftArrow,
351+
Key.Right => ImGuiKey.RightArrow,
352+
Key.Enter => ImGuiKey.Enter,
353+
Key.Escape => ImGuiKey.Escape,
354+
Key.Space => ImGuiKey.Space,
355+
Key.Tab => ImGuiKey.Tab,
356+
Key.BackSpace => ImGuiKey.Backspace,
357+
Key.Insert => ImGuiKey.Insert,
358+
Key.Delete => ImGuiKey.Delete,
359+
Key.PageUp => ImGuiKey.PageUp,
360+
Key.PageDown => ImGuiKey.PageDown,
361+
Key.Home => ImGuiKey.Home,
362+
Key.End => ImGuiKey.End,
363+
Key.CapsLock => ImGuiKey.CapsLock,
364+
Key.ScrollLock => ImGuiKey.ScrollLock,
365+
Key.PrintScreen => ImGuiKey.PrintScreen,
366+
Key.Pause => ImGuiKey.Pause,
367+
Key.NumLock => ImGuiKey.NumLock,
368+
Key.KeypadDivide => ImGuiKey.KeypadDivide,
369+
Key.KeypadMultiply => ImGuiKey.KeypadMultiply,
370+
Key.KeypadSubtract => ImGuiKey.KeypadSubtract,
371+
Key.KeypadAdd => ImGuiKey.KeypadAdd,
372+
Key.KeypadDecimal => ImGuiKey.KeypadDecimal,
373+
Key.KeypadEnter => ImGuiKey.KeypadEnter,
374+
Key.Tilde => ImGuiKey.GraveAccent,
375+
Key.Minus => ImGuiKey.Minus,
376+
Key.Plus => ImGuiKey.Equal,
377+
Key.BracketLeft => ImGuiKey.LeftBracket,
378+
Key.BracketRight => ImGuiKey.RightBracket,
379+
Key.Semicolon => ImGuiKey.Semicolon,
380+
Key.Quote => ImGuiKey.Apostrophe,
381+
Key.Comma => ImGuiKey.Comma,
382+
Key.Period => ImGuiKey.Period,
383+
Key.Slash => ImGuiKey.Slash,
384+
Key.BackSlash or Key.NonUSBackSlash => ImGuiKey.Backslash,
385+
_ => ImGuiKey.None
386+
};
387+
388+
return result != ImGuiKey.None;
389+
}
365390

366-
IReadOnlyList<char> keyCharPresses = snapshot.KeyCharPresses;
367-
for (int i = 0; i < keyCharPresses.Count; i++)
391+
private void UpdateImGuiInput(InputSnapshot snapshot)
392+
{
393+
ImGuiIOPtr io = ImGui.GetIO();
394+
io.AddMousePosEvent(snapshot.MousePosition.X, snapshot.MousePosition.Y);
395+
io.AddMouseButtonEvent(0, snapshot.IsMouseDown(MouseButton.Left));
396+
io.AddMouseButtonEvent(1, snapshot.IsMouseDown(MouseButton.Right));
397+
io.AddMouseButtonEvent(2, snapshot.IsMouseDown(MouseButton.Middle));
398+
io.AddMouseButtonEvent(3, snapshot.IsMouseDown(MouseButton.Button1));
399+
io.AddMouseButtonEvent(4, snapshot.IsMouseDown(MouseButton.Button2));
400+
io.AddMouseWheelEvent(0f, snapshot.WheelDelta);
401+
for (int i = 0; i < snapshot.KeyCharPresses.Count; i++)
368402
{
369-
char c = keyCharPresses[i];
370-
io.AddInputCharacter(c);
403+
io.AddInputCharacter(snapshot.KeyCharPresses[i]);
371404
}
372405

373-
IReadOnlyList<KeyEvent> keyEvents = snapshot.KeyEvents;
374-
for (int i = 0; i < keyEvents.Count; i++)
406+
for (int i = 0; i < snapshot.KeyEvents.Count; i++)
375407
{
376-
KeyEvent keyEvent = keyEvents[i];
377-
io.KeysDown[(int)keyEvent.Key] = keyEvent.Down;
378-
if (keyEvent.Key == Key.ControlLeft)
379-
{
380-
_controlDown = keyEvent.Down;
381-
}
382-
if (keyEvent.Key == Key.ShiftLeft)
383-
{
384-
_shiftDown = keyEvent.Down;
385-
}
386-
if (keyEvent.Key == Key.AltLeft)
408+
KeyEvent keyEvent = snapshot.KeyEvents[i];
409+
if (TryMapKey(keyEvent.Key, out ImGuiKey imguikey))
387410
{
388-
_altDown = keyEvent.Down;
389-
}
390-
if (keyEvent.Key == Key.WinLeft)
391-
{
392-
_winKeyDown = keyEvent.Down;
411+
io.AddKeyEvent(imguikey, keyEvent.Down);
393412
}
394413
}
395-
396-
io.KeyCtrl = _controlDown;
397-
io.KeyAlt = _altDown;
398-
io.KeyShift = _shiftDown;
399-
io.KeySuper = _winKeyDown;
400-
}
401-
402-
private static void SetKeyMappings()
403-
{
404-
ImGuiIOPtr io = ImGui.GetIO();
405-
io.KeyMap[(int)ImGuiKey.Tab] = (int)Key.Tab;
406-
io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)Key.Left;
407-
io.KeyMap[(int)ImGuiKey.RightArrow] = (int)Key.Right;
408-
io.KeyMap[(int)ImGuiKey.UpArrow] = (int)Key.Up;
409-
io.KeyMap[(int)ImGuiKey.DownArrow] = (int)Key.Down;
410-
io.KeyMap[(int)ImGuiKey.PageUp] = (int)Key.PageUp;
411-
io.KeyMap[(int)ImGuiKey.PageDown] = (int)Key.PageDown;
412-
io.KeyMap[(int)ImGuiKey.Home] = (int)Key.Home;
413-
io.KeyMap[(int)ImGuiKey.End] = (int)Key.End;
414-
io.KeyMap[(int)ImGuiKey.Delete] = (int)Key.Delete;
415-
io.KeyMap[(int)ImGuiKey.Backspace] = (int)Key.BackSpace;
416-
io.KeyMap[(int)ImGuiKey.Enter] = (int)Key.Enter;
417-
io.KeyMap[(int)ImGuiKey.Escape] = (int)Key.Escape;
418-
io.KeyMap[(int)ImGuiKey.Space] = (int)Key.Space;
419-
io.KeyMap[(int)ImGuiKey.A] = (int)Key.A;
420-
io.KeyMap[(int)ImGuiKey.C] = (int)Key.C;
421-
io.KeyMap[(int)ImGuiKey.V] = (int)Key.V;
422-
io.KeyMap[(int)ImGuiKey.X] = (int)Key.X;
423-
io.KeyMap[(int)ImGuiKey.Y] = (int)Key.Y;
424-
io.KeyMap[(int)ImGuiKey.Z] = (int)Key.Z;
425414
}
426415

427416
private void RenderImDrawData(ImDrawDataPtr draw_data, GraphicsDevice gd, CommandList cl)

src/ImGui.NET.SampleProgram/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Linq;
34
using System.Numerics;
45
using ImPlotNET;
@@ -51,12 +52,16 @@ static void Main(string[] args)
5152
Random random = new Random();
5253
_memoryEditorData = Enumerable.Range(0, 1024).Select(i => (byte)random.Next(255)).ToArray();
5354

55+
var stopwatch = Stopwatch.StartNew();
56+
float deltaTime = 0f;
5457
// Main application loop
5558
while (_window.Exists)
5659
{
60+
deltaTime = stopwatch.ElapsedTicks / (float)Stopwatch.Frequency;
61+
stopwatch.Restart();
5762
InputSnapshot snapshot = _window.PumpEvents();
5863
if (!_window.Exists) { break; }
59-
_controller.Update(1f / 60f, snapshot); // Feed the input events to our ImGui controller, which passes them through to ImGui.
64+
_controller.Update(deltaTime, snapshot); // Feed the input events to our ImGui controller, which passes them through to ImGui.
6065

6166
SubmitUI();
6267

0 commit comments

Comments
 (0)