imgui_impl_win32.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "imgui.h"
  2. #include "imgui_impl_win32.h"
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <windows.h>
  5. // Data
  6. static HWND g_hWnd = 0;
  7. static INT64 g_Time = 0;
  8. static INT64 g_TicksPerSecond = 0;
  9. // Functions
  10. bool ImGui_ImplWin32_Init(void* hwnd)
  11. {
  12. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  13. return false;
  14. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  15. return false;
  16. g_hWnd = (HWND)hwnd;
  17. ImGuiIO& io = ImGui::GetIO();
  18. 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.
  19. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  20. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  21. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  22. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  23. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  24. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  25. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  26. io.KeyMap[ImGuiKey_End] = VK_END;
  27. io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
  28. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  29. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  30. io.KeyMap[ImGuiKey_Space] = VK_SPACE;
  31. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  32. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  33. io.KeyMap[ImGuiKey_A] = 'A';
  34. io.KeyMap[ImGuiKey_C] = 'C';
  35. io.KeyMap[ImGuiKey_V] = 'V';
  36. io.KeyMap[ImGuiKey_X] = 'X';
  37. io.KeyMap[ImGuiKey_Y] = 'Y';
  38. io.KeyMap[ImGuiKey_Z] = 'Z';
  39. io.ImeWindowHandle = g_hWnd; return true;
  40. }
  41. void ImGui_ImplWin32_Shutdown()
  42. {
  43. g_hWnd = (HWND)0;
  44. }
  45. void ImGui_ImplWin32_NewFrame()
  46. {
  47. ImGuiIO& io = ImGui::GetIO();
  48. // Setup display size (every frame to accommodate for window resizing)
  49. RECT rect;
  50. GetClientRect(g_hWnd, &rect);
  51. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  52. // Setup time step
  53. INT64 current_time;
  54. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  55. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  56. g_Time = current_time;
  57. // Read keyboard modifiers inputs
  58. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  59. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  60. io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
  61. io.KeySuper = false;
  62. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  63. // io.MousePos : filled by WM_MOUSEMOVE events
  64. // io.MouseDown : filled by WM_*BUTTON* events
  65. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  66. // Set OS mouse position if requested last frame by io.WantMoveMouse flag (used when io.NavMovesTrue is enabled by user and using directional navigation)
  67. if (io.WantMoveMouse)
  68. {
  69. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  70. ClientToScreen(g_hWnd, &pos);
  71. SetCursorPos(pos.x, pos.y);
  72. }
  73. // Hide OS mouse cursor if ImGui is drawing it
  74. if (io.MouseDrawCursor)
  75. SetCursor(NULL);
  76. // 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.
  77. ImGui::NewFrame();
  78. }
  79. // Process Win32 mouse/keyboard inputs.
  80. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  81. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  82. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  83. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  84. // 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.
  85. // 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.
  86. IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  87. {
  88. if (ImGui::GetCurrentContext() == NULL)
  89. return 0;
  90. ImGuiIO& io = ImGui::GetIO();
  91. switch (msg)
  92. {
  93. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  94. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  95. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  96. {
  97. int button = 0;
  98. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) button = 0;
  99. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) button = 1;
  100. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) button = 2;
  101. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
  102. ::SetCapture(hwnd);
  103. io.MouseDown[button] = true;
  104. return 0;
  105. }
  106. case WM_LBUTTONUP:
  107. case WM_RBUTTONUP:
  108. case WM_MBUTTONUP:
  109. {
  110. int button = 0;
  111. if (msg == WM_LBUTTONUP) button = 0;
  112. if (msg == WM_RBUTTONUP) button = 1;
  113. if (msg == WM_MBUTTONUP) button = 2;
  114. io.MouseDown[button] = false;
  115. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
  116. ::ReleaseCapture();
  117. return 0;
  118. }
  119. case WM_MOUSEWHEEL:
  120. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  121. return 0;
  122. case WM_MOUSEHWHEEL:
  123. io.MouseWheelH += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  124. return 0;
  125. case WM_MOUSEMOVE:
  126. io.MousePos.x = (signed short)(lParam);
  127. io.MousePos.y = (signed short)(lParam >> 16);
  128. return 0;
  129. case WM_KEYDOWN:
  130. case WM_SYSKEYDOWN:
  131. if (wParam < 256)
  132. io.KeysDown[wParam] = 1;
  133. return 0;
  134. case WM_KEYUP:
  135. case WM_SYSKEYUP:
  136. if (wParam < 256)
  137. io.KeysDown[wParam] = 0;
  138. return 0;
  139. case WM_CHAR:
  140. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  141. if (wParam > 0 && wParam < 0x10000)
  142. io.AddInputCharacter((unsigned short)wParam);
  143. return 0;
  144. }
  145. return 0;
  146. }