imgui_impl_glfw.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // ImGui Platform Binding for: GLFW
  2. // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..)
  3. // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
  4. // Implemented features:
  5. // [X] Platform: Clipboard support.
  6. // [X] Platform: Gamepad navigation mapping. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  7. // [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. FIXME: 3 cursors types are missing from GLFW.
  8. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  9. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  10. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  11. // https://github.com/ocornut/imgui
  12. // CHANGELOG
  13. // (minor and older changes stripped away, please see git history for details)
  14. // 2018-06-08: Misc: Extracted imgui_impl_glfw.cpp/.h away from the old combined GLFW+OpenGL/Vulkan examples.
  15. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  16. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value, passed to glfwSetCursor()).
  17. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  18. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  19. // 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set.
  20. // 2018-01-25: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  21. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  22. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  23. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  24. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  25. #include "imgui.h"
  26. #include "imgui_impl_glfw.h"
  27. // GLFW
  28. #include <GLFW/glfw3.h>
  29. #ifdef _WIN32
  30. #undef APIENTRY
  31. #define GLFW_EXPOSE_NATIVE_WIN32
  32. #include <GLFW/glfw3native.h> // for glfwGetWin32Window
  33. #endif
  34. #define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING
  35. #define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED
  36. #define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity
  37. #define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale
  38. #define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface
  39. // Data
  40. enum GlfwClientApi
  41. {
  42. GlfwClientApi_Unknown,
  43. GlfwClientApi_OpenGL,
  44. GlfwClientApi_Vulkan
  45. };
  46. static GLFWwindow* g_Window = NULL;
  47. static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown;
  48. static double g_Time = 0.0;
  49. static bool g_MouseJustPressed[5] = { false, false, false, false, false };
  50. static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 };
  51. static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data)
  52. {
  53. return glfwGetClipboardString((GLFWwindow*)user_data);
  54. }
  55. static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text)
  56. {
  57. glfwSetClipboardString((GLFWwindow*)user_data, text);
  58. }
  59. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  60. {
  61. if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed))
  62. g_MouseJustPressed[button] = true;
  63. }
  64. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
  65. {
  66. ImGuiIO& io = ImGui::GetIO();
  67. io.MouseWheelH += (float)xoffset;
  68. io.MouseWheel += (float)yoffset;
  69. }
  70. void ImGui_ImplGlfw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  71. {
  72. ImGuiIO& io = ImGui::GetIO();
  73. if (action == GLFW_PRESS)
  74. io.KeysDown[key] = true;
  75. if (action == GLFW_RELEASE)
  76. io.KeysDown[key] = false;
  77. (void)mods; // Modifiers are not reliable across systems
  78. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  79. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  80. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  81. io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
  82. }
  83. void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
  84. {
  85. ImGuiIO& io = ImGui::GetIO();
  86. if (c > 0 && c < 0x10000)
  87. io.AddInputCharacter((unsigned short)c);
  88. }
  89. void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
  90. {
  91. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  92. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  93. glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
  94. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  95. }
  96. static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api)
  97. {
  98. g_Window = window;
  99. g_Time = 0.0;
  100. // Setup back-end capabilities flags
  101. ImGuiIO& io = ImGui::GetIO();
  102. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  103. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  104. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
  105. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
  106. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  107. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  108. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  109. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  110. io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
  111. io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
  112. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  113. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  114. io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT;
  115. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  116. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  117. io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE;
  118. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  119. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  120. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  121. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  122. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  123. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  124. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  125. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  126. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  127. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  128. io.ClipboardUserData = g_Window;
  129. #if defined(_WIN32)
  130. io.ImeWindowHandle = (void*)glfwGetWin32Window(g_Window);
  131. #endif
  132. g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  133. g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
  134. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  135. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);
  136. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
  137. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  138. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  139. if (install_callbacks)
  140. ImGui_ImplGlfw_InstallCallbacks(window);
  141. g_ClientApi = client_api;
  142. return true;
  143. }
  144. bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks)
  145. {
  146. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_OpenGL);
  147. }
  148. bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks)
  149. {
  150. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan);
  151. }
  152. void ImGui_ImplGlfw_Shutdown()
  153. {
  154. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  155. {
  156. glfwDestroyCursor(g_MouseCursors[cursor_n]);
  157. g_MouseCursors[cursor_n] = NULL;
  158. }
  159. g_ClientApi = GlfwClientApi_Unknown;
  160. }
  161. static void ImGui_ImplGlfw_UpdateMousePosAndButtons()
  162. {
  163. // Update buttons
  164. ImGuiIO& io = ImGui::GetIO();
  165. for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
  166. {
  167. // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  168. io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0;
  169. g_MouseJustPressed[i] = false;
  170. }
  171. // Update mouse position
  172. const ImVec2 mouse_pos_backup = io.MousePos;
  173. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  174. if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED))
  175. {
  176. if (io.WantSetMousePos)
  177. {
  178. glfwSetCursorPos(g_Window, (double)mouse_pos_backup.x, (double)mouse_pos_backup.y);
  179. }
  180. else
  181. {
  182. double mouse_x, mouse_y;
  183. glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
  184. io.MousePos = ImVec2((float)mouse_x, (float)mouse_y);
  185. }
  186. }
  187. }
  188. static void ImGui_ImplGlfw_UpdateMouseCursor()
  189. {
  190. ImGuiIO& io = ImGui::GetIO();
  191. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(g_Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
  192. return;
  193. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  194. if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
  195. {
  196. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  197. glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  198. }
  199. else
  200. {
  201. // Show OS mouse cursor
  202. // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
  203. glfwSetCursor(g_Window, g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
  204. glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  205. }
  206. }
  207. void ImGui_ImplGlfw_NewFrame()
  208. {
  209. ImGuiIO& io = ImGui::GetIO();
  210. IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
  211. // Setup display size
  212. int w, h;
  213. int display_w, display_h;
  214. glfwGetWindowSize(g_Window, &w, &h);
  215. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  216. io.DisplaySize = ImVec2((float)w, (float)h);
  217. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  218. // Setup time step
  219. double current_time = glfwGetTime();
  220. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  221. g_Time = current_time;
  222. ImGui_ImplGlfw_UpdateMousePosAndButtons();
  223. ImGui_ImplGlfw_UpdateMouseCursor();
  224. // Gamepad navigation mapping [BETA]
  225. memset(io.NavInputs, 0, sizeof(io.NavInputs));
  226. if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad)
  227. {
  228. // Update gamepad inputs
  229. #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; }
  230. #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; }
  231. int axes_count = 0, buttons_count = 0;
  232. const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
  233. const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
  234. MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A
  235. MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B
  236. MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X
  237. MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y
  238. MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left
  239. MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right
  240. MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up
  241. MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down
  242. MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB
  243. MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB
  244. MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB
  245. MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB
  246. MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f);
  247. MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f);
  248. MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f);
  249. MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f);
  250. #undef MAP_BUTTON
  251. #undef MAP_ANALOG
  252. if (axes_count > 0 && buttons_count > 0)
  253. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  254. else
  255. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  256. }
  257. }