imgui_impl_win32.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // ImGui Platform Binding for: Windows (standard windows API for 32 and 64 bits applications)
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. #include "imgui.h"
  4. #include "imgui_impl_win32.h"
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. // CHANGELOG
  8. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
  9. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  10. // 2018-02-06: Inputs: Honoring the io.WantMoveMouse by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  11. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  12. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  13. // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
  14. // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
  15. // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
  16. // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
  17. // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.
  18. // Win32 Data
  19. static HWND g_hWnd = 0;
  20. static INT64 g_Time = 0;
  21. static INT64 g_TicksPerSecond = 0;
  22. static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_Count_;
  23. // Functions
  24. bool ImGui_ImplWin32_Init(void* hwnd)
  25. {
  26. if (!::QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  27. return false;
  28. if (!::QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  29. return false;
  30. g_hWnd = (HWND)hwnd;
  31. ImGuiIO& io = ImGui::GetIO();
  32. io.KeyMap[ImGuiKey_Tab] = VK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
  33. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  34. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  35. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  36. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  37. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  38. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  39. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  40. io.KeyMap[ImGuiKey_End] = VK_END;
  41. io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
  42. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  43. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  44. io.KeyMap[ImGuiKey_Space] = VK_SPACE;
  45. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  46. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  47. io.KeyMap[ImGuiKey_A] = 'A';
  48. io.KeyMap[ImGuiKey_C] = 'C';
  49. io.KeyMap[ImGuiKey_V] = 'V';
  50. io.KeyMap[ImGuiKey_X] = 'X';
  51. io.KeyMap[ImGuiKey_Y] = 'Y';
  52. io.KeyMap[ImGuiKey_Z] = 'Z';
  53. io.ImeWindowHandle = g_hWnd; return true;
  54. }
  55. void ImGui_ImplWin32_Shutdown()
  56. {
  57. g_hWnd = (HWND)0;
  58. }
  59. static void ImGui_ImplWin32_UpdateMouseCursor()
  60. {
  61. ImGuiIO& io = ImGui::GetIO();
  62. ImGuiMouseCursor imgui_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  63. if (imgui_cursor == ImGuiMouseCursor_None)
  64. {
  65. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  66. ::SetCursor(NULL);
  67. }
  68. else
  69. {
  70. // Hardware cursor type
  71. LPTSTR win32_cursor = IDC_ARROW;
  72. switch (imgui_cursor)
  73. {
  74. case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
  75. case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
  76. case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
  77. case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
  78. case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
  79. case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
  80. case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
  81. }
  82. ::SetCursor(::LoadCursor(NULL, win32_cursor));
  83. }
  84. }
  85. void ImGui_ImplWin32_NewFrame()
  86. {
  87. ImGuiIO& io = ImGui::GetIO();
  88. // Setup display size (every frame to accommodate for window resizing)
  89. RECT rect;
  90. ::GetClientRect(g_hWnd, &rect);
  91. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  92. // Setup time step
  93. INT64 current_time;
  94. ::QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  95. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  96. g_Time = current_time;
  97. // Read keyboard modifiers inputs
  98. io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
  99. io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;
  100. io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;
  101. io.KeySuper = false;
  102. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  103. // io.MousePos : filled by WM_MOUSEMOVE events
  104. // io.MouseDown : filled by WM_*BUTTON* events
  105. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  106. // Set OS mouse position if requested last frame by io.WantMoveMouse flag (used when io.NavMovesTrue is enabled by user and using directional navigation)
  107. if (io.WantMoveMouse)
  108. {
  109. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  110. ::ClientToScreen(g_hWnd, &pos);
  111. ::SetCursorPos(pos.x, pos.y);
  112. }
  113. // Update OS mouse cursor with the cursor requested by imgui
  114. ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  115. if (g_LastMouseCursor != mouse_cursor)
  116. {
  117. g_LastMouseCursor = mouse_cursor;
  118. ImGui_ImplWin32_UpdateMouseCursor();
  119. }
  120. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  121. ImGui::NewFrame();
  122. }
  123. // Process Win32 mouse/keyboard inputs.
  124. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  125. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  126. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  127. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  128. // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
  129. // PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag.
  130. IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  131. {
  132. if (ImGui::GetCurrentContext() == NULL)
  133. return 0;
  134. ImGuiIO& io = ImGui::GetIO();
  135. switch (msg)
  136. {
  137. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  138. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  139. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  140. {
  141. int button = 0;
  142. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) button = 0;
  143. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) button = 1;
  144. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) button = 2;
  145. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
  146. ::SetCapture(hwnd);
  147. io.MouseDown[button] = true;
  148. return 0;
  149. }
  150. case WM_LBUTTONUP:
  151. case WM_RBUTTONUP:
  152. case WM_MBUTTONUP:
  153. {
  154. int button = 0;
  155. if (msg == WM_LBUTTONUP) button = 0;
  156. if (msg == WM_RBUTTONUP) button = 1;
  157. if (msg == WM_MBUTTONUP) button = 2;
  158. io.MouseDown[button] = false;
  159. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
  160. ::ReleaseCapture();
  161. return 0;
  162. }
  163. case WM_MOUSEWHEEL:
  164. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  165. return 0;
  166. case WM_MOUSEHWHEEL:
  167. io.MouseWheelH += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  168. return 0;
  169. case WM_MOUSEMOVE:
  170. io.MousePos.x = (signed short)(lParam);
  171. io.MousePos.y = (signed short)(lParam >> 16);
  172. return 0;
  173. case WM_KEYDOWN:
  174. case WM_SYSKEYDOWN:
  175. if (wParam < 256)
  176. io.KeysDown[wParam] = 1;
  177. return 0;
  178. case WM_KEYUP:
  179. case WM_SYSKEYUP:
  180. if (wParam < 256)
  181. io.KeysDown[wParam] = 0;
  182. return 0;
  183. case WM_CHAR:
  184. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  185. if (wParam > 0 && wParam < 0x10000)
  186. io.AddInputCharacter((unsigned short)wParam);
  187. return 0;
  188. case WM_SETCURSOR:
  189. if (LOWORD(lParam) == HTCLIENT)
  190. {
  191. ImGui_ImplWin32_UpdateMouseCursor();
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. return 0;
  197. }