imgui_impl_win32.cpp 9.7 KB

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