@@ -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 )
0 commit comments