imgui_impl_glfw.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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] Gamepad navigation mapping. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  6. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  7. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  8. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  9. // https://github.com/ocornut/imgui
  10. // CHANGELOG
  11. // (minor and older changes stripped away, please see git history for details)
  12. // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformInterface
  13. // 2018-XX-XX: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
  14. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  15. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  16. // 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set.
  17. // 2018-01-25: Inputs: Honoring the io.WantMoveMouse by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  18. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  19. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  20. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  21. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  22. #include "imgui.h"
  23. #include "imgui_impl_glfw.h"
  24. #include "imgui_internal.h" // FIXME-PLATFORM
  25. // GLFW
  26. #include <GLFW/glfw3.h>
  27. #ifdef _WIN32
  28. #undef APIENTRY
  29. #define GLFW_EXPOSE_NATIVE_WIN32
  30. #include <GLFW/glfw3native.h> // for glfwGetWin32Window
  31. #endif
  32. // Data
  33. static GLFWwindow* g_Window = NULL;
  34. static double g_Time = 0.0f;
  35. static bool g_MouseJustPressed[5] = { false, false, false, false, false };
  36. static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_Count_] = { 0 };
  37. // Forward Declarations
  38. static void ImGui_ImplGlfw_InitPlatformInterface();
  39. static void ImGui_ImplGlfw_ShutdownPlatformInterface();
  40. static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data)
  41. {
  42. return glfwGetClipboardString((GLFWwindow*)user_data);
  43. }
  44. static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text)
  45. {
  46. glfwSetClipboardString((GLFWwindow*)user_data, text);
  47. }
  48. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  49. {
  50. if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed))
  51. g_MouseJustPressed[button] = true;
  52. }
  53. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
  54. {
  55. ImGuiIO& io = ImGui::GetIO();
  56. io.MouseWheelH += (float)xoffset;
  57. io.MouseWheel += (float)yoffset;
  58. }
  59. void ImGui_ImplGlfw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  60. {
  61. ImGuiIO& io = ImGui::GetIO();
  62. if (action == GLFW_PRESS)
  63. io.KeysDown[key] = true;
  64. if (action == GLFW_RELEASE)
  65. io.KeysDown[key] = false;
  66. (void)mods; // Modifiers are not reliable across systems
  67. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  68. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  69. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  70. io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
  71. }
  72. void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
  73. {
  74. ImGuiIO& io = ImGui::GetIO();
  75. if (c > 0 && c < 0x10000)
  76. io.AddInputCharacter((unsigned short)c);
  77. }
  78. void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
  79. {
  80. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  81. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  82. glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
  83. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  84. }
  85. bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
  86. {
  87. g_Window = window;
  88. ImGuiIO& io = ImGui::GetIO();
  89. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  90. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  91. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  92. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  93. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  94. io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
  95. io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
  96. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  97. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  98. io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT;
  99. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  100. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  101. io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE;
  102. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  103. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  104. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  105. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  106. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  107. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  108. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  109. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  110. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  111. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  112. io.ClipboardUserData = g_Window;
  113. #ifdef _WIN32
  114. io.ImeWindowHandle = glfwGetWin32Window(g_Window);
  115. #endif
  116. g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  117. g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
  118. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  119. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);
  120. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
  121. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  122. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  123. if (install_callbacks)
  124. ImGui_ImplGlfw_InstallCallbacks(window);
  125. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  126. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  127. main_viewport->PlatformHandle = (void*)g_Window;
  128. if (io.ConfigFlags & ImGuiConfigFlags_MultiViewports)
  129. ImGui_ImplGlfw_InitPlatformInterface();
  130. return true;
  131. }
  132. void ImGui_ImplGlfw_Shutdown()
  133. {
  134. ImGui_ImplGlfw_ShutdownPlatformInterface();
  135. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_Count_; cursor_n++)
  136. {
  137. glfwDestroyCursor(g_MouseCursors[cursor_n]);
  138. g_MouseCursors[cursor_n] = NULL;
  139. }
  140. }
  141. static void ImGui_ImplGlfw_UpdateMouse()
  142. {
  143. #if 0
  144. if (io.WantMoveMouse)
  145. glfwSetCursorPos(g_Window, (double)io.MousePos.x, (double)io.MousePos.y); // Set mouse position if requested by io.WantMoveMouse flag (used when io.NavMovesTrue is enabled by user and using directional navigation)
  146. #endif
  147. ImGuiIO& io = ImGui::GetIO();
  148. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  149. io.MousePosViewport = 0;
  150. io.MouseHoveredViewport = 0;
  151. // Update buttons
  152. ImGuiIO& io = ImGui::GetIO();
  153. for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
  154. {
  155. // 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.
  156. io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0;
  157. g_MouseJustPressed[i] = false;
  158. }
  159. const ImVector<ImGuiViewport*>& viewports = ImGui::GetViewports();
  160. for (int n = 0; n < viewports.Size; n++)
  161. {
  162. ImGuiViewport* viewport = viewports[n];
  163. GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle;
  164. IM_ASSERT(window != NULL);
  165. if (glfwGetWindowAttrib(window, GLFW_FOCUSED))
  166. {
  167. double mouse_x, mouse_y;
  168. glfwGetCursorPos(window, &mouse_x, &mouse_y);
  169. io.MousePos = ImVec2((float)mouse_x + viewport->Pos.x, (float)mouse_y + viewport->Pos.y);
  170. io.MousePosViewport = viewport->ID;
  171. for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
  172. io.MouseDown[i] |= glfwGetMouseButton(window, i) != 0;
  173. }
  174. #if GLFW_HAS_GLFW_HOVERED
  175. io.ConfigFlags |= ImGuiConfigFlags_PlatformHasMouseHoveredViewport;
  176. if (glfwGetWindowAttrib(data->Window, GLFW_HOVERED) && !(viewport->Flags & ImGuiViewportFlags_NoInputs))
  177. io.MouseHoveredViewport = viewport->ID;
  178. #endif
  179. }
  180. // Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
  181. ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
  182. if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
  183. {
  184. glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  185. }
  186. else
  187. {
  188. glfwSetCursor(g_Window, g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
  189. glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  190. }
  191. }
  192. void ImGui_ImplGlfw_NewFrame()
  193. {
  194. ImGuiIO& io = ImGui::GetIO();
  195. IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
  196. // Setup display size
  197. int w, h;
  198. int display_w, display_h;
  199. glfwGetWindowSize(g_Window, &w, &h);
  200. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  201. io.DisplaySize = ImVec2((float)w, (float)h);
  202. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  203. // Setup time step
  204. double current_time = glfwGetTime();
  205. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  206. g_Time = current_time;
  207. ImGui_ImplGlfw_UpdateMouse();
  208. // Gamepad navigation mapping [BETA]
  209. memset(io.NavInputs, 0, sizeof(io.NavInputs));
  210. if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad)
  211. {
  212. // Update gamepad inputs
  213. #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; }
  214. #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; }
  215. int axes_count = 0, buttons_count = 0;
  216. const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
  217. const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
  218. MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A
  219. MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B
  220. MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X
  221. MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y
  222. MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left
  223. MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right
  224. MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up
  225. MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down
  226. MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB
  227. MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB
  228. MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB
  229. MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB
  230. MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f);
  231. MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f);
  232. MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f);
  233. MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f);
  234. #undef MAP_BUTTON
  235. #undef MAP_ANALOG
  236. }
  237. // 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.
  238. ImGui::NewFrame();
  239. }
  240. // --------------------------------------------------------------------------------------------------------
  241. // Platform Windows
  242. // --------------------------------------------------------------------------------------------------------
  243. struct ImGuiPlatformDataGlfw
  244. {
  245. GLFWwindow* Window;
  246. bool WindowOwned;
  247. ImGuiPlatformDataGlfw() { Window = NULL; WindowOwned = false; }
  248. ~ImGuiPlatformDataGlfw() { IM_ASSERT(Window == NULL); }
  249. };
  250. static void ImGui_ImplGlfw_CreateViewport(ImGuiViewport* viewport)
  251. {
  252. ImGuiPlatformDataGlfw* data = IM_NEW(ImGuiPlatformDataGlfw)();
  253. viewport->PlatformUserData = data;
  254. // GLFW 3.2 unfortunately always set focus on glfwCreateWindow() if GLFW_VISIBLE is set, regardless of GLFW_FOCUSED
  255. glfwWindowHint(GLFW_VISIBLE, false);
  256. glfwWindowHint(GLFW_FOCUSED, false);
  257. glfwWindowHint(GLFW_DECORATED, (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? false : true);
  258. data->Window = glfwCreateWindow((int)viewport->Size.x, (int)viewport->Size.y, "No Title Yet", NULL, g_Window);
  259. data->WindowOwned = true;
  260. viewport->PlatformHandle = (void*)data->Window;
  261. viewport->Name = NULL;
  262. ImGui_ImplGlfw_InstallCallbacks(data->Window);
  263. }
  264. static void ImGui_ImplGlfw_DestroyViewport(ImGuiViewport* viewport)
  265. {
  266. if (ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData)
  267. {
  268. #if GLFW_HAS_GLFW_HOVERED
  269. HWND hwnd = glfwGetWin32Window(data->Window);
  270. ::RemovePropA(hwnd, "IMGUI_VIEWPORT");
  271. #endif
  272. if (data->Window && data->WindowOwned)
  273. glfwDestroyWindow(data->Window);
  274. data->Window = NULL;
  275. IM_DELETE(data);
  276. }
  277. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  278. }
  279. #if defined(_WIN32) && GLFW_HAS_GLFW_HOVERED
  280. static WNDPROC g_GlfwWndProc = NULL;
  281. static LRESULT CALLBACK WndProcNoInputs(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  282. {
  283. if (msg == WM_NCHITTEST)
  284. {
  285. ImGuiViewport* viewport = (ImGuiViewport*)::GetPropA(hWnd, "IMGUI_VIEWPORT");
  286. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  287. return HTTRANSPARENT;
  288. }
  289. return ::CallWindowProc(g_GlfwWndProc, hWnd, msg, wParam, lParam);
  290. }
  291. #endif
  292. static void ImGui_ImplGlfw_ShowWindow(ImGuiViewport* viewport)
  293. {
  294. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  295. #if defined(_WIN32)
  296. // GLFW hack: Hide icon from task bar
  297. HWND hwnd = glfwGetWin32Window(data->Window);
  298. if (viewport->Flags & ImGuiViewportFlags_NoDecoration)
  299. {
  300. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  301. ex_style &= ~WS_EX_APPWINDOW;
  302. ex_style |= WS_EX_TOOLWINDOW;
  303. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  304. }
  305. // GLFW hack: install hook for WM_NCHITTEST message handler
  306. #if GLFW_HAS_GLFW_HOVERED
  307. ::SetPropA(hwnd, "IMGUI_VIEWPORT", viewport);
  308. if (g_GlfwWndProc == NULL)
  309. g_GlfwWndProc = (WNDPROC)::GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  310. ::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)WndProcNoInputs);
  311. #endif
  312. // GLFW hack: GLFW 3.2 has a bug where glfwShowWindow() also activates/focus the window.
  313. // The fix was pushed to GLFW repository on 2018/01/09 and should be included in GLFW 3.3. See https://github.com/glfw/glfw/issues/1179
  314. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  315. {
  316. ::ShowWindow(hwnd, SW_SHOWNA);
  317. return;
  318. }
  319. #endif
  320. glfwShowWindow(data->Window);
  321. }
  322. static ImVec2 ImGui_ImplGlfw_GetWindowPos(ImGuiViewport* viewport)
  323. {
  324. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  325. int x = 0, y = 0;
  326. glfwGetWindowPos(data->Window, &x, &y);
  327. return ImVec2((float)x, (float)y);
  328. }
  329. static void ImGui_ImplGlfw_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  330. {
  331. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  332. glfwSetWindowPos(data->Window, (int)pos.x, (int)pos.y);
  333. }
  334. static ImVec2 ImGui_ImplGlfw_GetWindowSize(ImGuiViewport* viewport)
  335. {
  336. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  337. int w = 0, h = 0;
  338. glfwGetWindowSize(data->Window, &w, &h);
  339. return ImVec2((float)w, (float)h);
  340. }
  341. static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  342. {
  343. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  344. glfwSetWindowSize(data->Window, (int)size.x, (int)size.y);
  345. }
  346. static void ImGui_ImplGlfw_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  347. {
  348. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  349. glfwSetWindowTitle(data->Window, title);
  350. }
  351. static void ImGui_ImplGlfw_RenderViewport(ImGuiViewport* viewport)
  352. {
  353. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  354. glfwMakeContextCurrent(data->Window);
  355. if (glfwWindowShouldClose(data->Window))
  356. viewport->PlatformRequestClose = true;
  357. }
  358. static void ImGui_ImplGlfw_SwapBuffers(ImGuiViewport* viewport)
  359. {
  360. ImGuiPlatformDataGlfw* data = (ImGuiPlatformDataGlfw*)viewport->PlatformUserData;
  361. glfwSwapBuffers(data->Window);
  362. }
  363. static void ImGui_ImplGlfw_InitPlatformInterface()
  364. {
  365. // Register platform interface (will be coupled with a renderer interface)
  366. ImGuiIO& io = ImGui::GetIO();
  367. io.PlatformInterface.CreateViewport = ImGui_ImplGlfw_CreateViewport;
  368. io.PlatformInterface.DestroyViewport = ImGui_ImplGlfw_DestroyViewport;
  369. io.PlatformInterface.ShowWindow = ImGui_ImplGlfw_ShowWindow;
  370. io.PlatformInterface.SetWindowPos = ImGui_ImplGlfw_SetWindowPos;
  371. io.PlatformInterface.GetWindowPos = ImGui_ImplGlfw_GetWindowPos;
  372. io.PlatformInterface.SetWindowSize = ImGui_ImplGlfw_SetWindowSize;
  373. io.PlatformInterface.GetWindowSize = ImGui_ImplGlfw_GetWindowSize;
  374. io.PlatformInterface.SetWindowTitle = ImGui_ImplGlfw_SetWindowTitle;
  375. io.PlatformInterface.RenderViewport = ImGui_ImplGlfw_RenderViewport;
  376. io.PlatformInterface.SwapBuffers = ImGui_ImplGlfw_SwapBuffers;
  377. // Register main window handle
  378. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  379. ImGuiPlatformDataGlfw* data = IM_NEW(ImGuiPlatformDataGlfw)();
  380. data->Window = g_Window;
  381. data->WindowOwned = false;
  382. main_viewport->PlatformUserData = data;
  383. }
  384. static void ImGui_ImplGlfw_ShutdownPlatformInterface()
  385. {
  386. ImGuiIO& io = ImGui::GetIO();
  387. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  388. main_viewport->PlatformHandle = NULL;
  389. memset(&io.PlatformInterface, 0, sizeof(io.PlatformInterface));
  390. }