imgui_impl_glfw.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // ImGui GLFW binding with OpenGL
  2. // You can copy and use unmodified imgui_impl_* files in your project.
  3. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  4. // See main.cpp for an example of using this.
  5. // https://github.com/ocornut/imgui
  6. #include <imgui.h>
  7. #include "imgui_impl_glfw.h"
  8. // GLFW
  9. #include <GLFW/glfw3.h>
  10. #ifdef _WIN32
  11. #undef APIENTRY
  12. #define GLFW_EXPOSE_NATIVE_WIN32
  13. #define GLFW_EXPOSE_NATIVE_WGL
  14. #include <GLFW/glfw3native.h>
  15. #endif
  16. // Data
  17. static GLFWwindow* g_Window = NULL;
  18. static double g_Time = 0.0f;
  19. static bool g_MousePressed[3] = { false, false, false };
  20. static float g_MouseWheel = 0.0f;
  21. static GLuint g_FontTexture = 0;
  22. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  23. // If text or lines are blurry when integrating ImGui in your engine:
  24. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  25. void ImGui_ImplGlfw_RenderDrawLists(ImDrawData* draw_data)
  26. {
  27. // We are using the OpenGL fixed pipeline to make the example code simpler to read!
  28. // Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
  29. GLint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  30. GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport);
  31. glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT);
  32. glEnable(GL_BLEND);
  33. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  34. glDisable(GL_CULL_FACE);
  35. glDisable(GL_DEPTH_TEST);
  36. glEnable(GL_SCISSOR_TEST);
  37. glEnableClientState(GL_VERTEX_ARRAY);
  38. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  39. glEnableClientState(GL_COLOR_ARRAY);
  40. glEnable(GL_TEXTURE_2D);
  41. //glUseProgram(0); // You may want this if using this code in an OpenGL 3+ context
  42. // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
  43. ImGuiIO& io = ImGui::GetIO();
  44. float fb_height = io.DisplaySize.y * io.DisplayFramebufferScale.y;
  45. draw_data->ScaleClipRects(io.DisplayFramebufferScale);
  46. // Setup viewport, orthographic projection matrix
  47. glViewport(0, 0, (GLsizei)io.DisplaySize.x, (GLsizei)io.DisplaySize.y);
  48. glMatrixMode(GL_PROJECTION);
  49. glPushMatrix();
  50. glLoadIdentity();
  51. glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
  52. glMatrixMode(GL_MODELVIEW);
  53. glPushMatrix();
  54. glLoadIdentity();
  55. // Render command lists
  56. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  57. for (int n = 0; n < draw_data->CmdListsCount; n++)
  58. {
  59. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  60. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
  61. const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
  62. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
  63. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
  64. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
  65. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  66. {
  67. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  68. if (pcmd->UserCallback)
  69. {
  70. pcmd->UserCallback(cmd_list, pcmd);
  71. }
  72. else
  73. {
  74. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
  75. glScissor((int)pcmd->ClipRect.x, (int)(fb_height - pcmd->ClipRect.w), (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y));
  76. glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer);
  77. }
  78. idx_buffer += pcmd->ElemCount;
  79. }
  80. }
  81. #undef OFFSETOF
  82. // Restore modified state
  83. glDisableClientState(GL_COLOR_ARRAY);
  84. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  85. glDisableClientState(GL_VERTEX_ARRAY);
  86. glBindTexture(GL_TEXTURE_2D, last_texture);
  87. glMatrixMode(GL_MODELVIEW);
  88. glPopMatrix();
  89. glMatrixMode(GL_PROJECTION);
  90. glPopMatrix();
  91. glPopAttrib();
  92. glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
  93. }
  94. static const char* ImGui_ImplGlfw_GetClipboardText()
  95. {
  96. return glfwGetClipboardString(g_Window);
  97. }
  98. static void ImGui_ImplGlfw_SetClipboardText(const char* text)
  99. {
  100. glfwSetClipboardString(g_Window, text);
  101. }
  102. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  103. {
  104. if (action == GLFW_PRESS && button >= 0 && button < 3)
  105. g_MousePressed[button] = true;
  106. }
  107. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double /*xoffset*/, double yoffset)
  108. {
  109. g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
  110. }
  111. void ImGui_ImplGlFw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  112. {
  113. ImGuiIO& io = ImGui::GetIO();
  114. if (action == GLFW_PRESS)
  115. io.KeysDown[key] = true;
  116. if (action == GLFW_RELEASE)
  117. io.KeysDown[key] = false;
  118. (void)mods; // Modifiers are not reliable across systems
  119. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  120. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  121. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  122. }
  123. void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
  124. {
  125. ImGuiIO& io = ImGui::GetIO();
  126. if (c > 0 && c < 0x10000)
  127. io.AddInputCharacter((unsigned short)c);
  128. }
  129. bool ImGui_ImplGlfw_CreateDeviceObjects()
  130. {
  131. ImGuiIO& io = ImGui::GetIO();
  132. // Build texture atlas
  133. unsigned char* pixels;
  134. int width, height;
  135. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  136. // Create OpenGL texture
  137. GLint last_texture;
  138. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  139. glGenTextures(1, &g_FontTexture);
  140. glBindTexture(GL_TEXTURE_2D, g_FontTexture);
  141. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  142. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  143. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
  144. // Store our identifier
  145. io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
  146. // Cleanup (don't clear the input data if you want to append new fonts later)
  147. io.Fonts->ClearInputData();
  148. io.Fonts->ClearTexData();
  149. glBindTexture(GL_TEXTURE_2D, last_texture);
  150. return true;
  151. }
  152. void ImGui_ImplGlfw_InvalidateDeviceObjects()
  153. {
  154. if (g_FontTexture)
  155. {
  156. glDeleteTextures(1, &g_FontTexture);
  157. ImGui::GetIO().Fonts->TexID = 0;
  158. g_FontTexture = 0;
  159. }
  160. }
  161. bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
  162. {
  163. g_Window = window;
  164. ImGuiIO& io = ImGui::GetIO();
  165. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  166. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  167. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  168. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  169. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  170. io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
  171. io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
  172. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  173. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  174. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  175. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  176. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  177. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  178. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  179. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  180. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  181. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  182. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  183. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  184. io.RenderDrawListsFn = ImGui_ImplGlfw_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
  185. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  186. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  187. #ifdef _WIN32
  188. io.ImeWindowHandle = glfwGetWin32Window(g_Window);
  189. #endif
  190. if (install_callbacks)
  191. {
  192. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  193. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  194. glfwSetKeyCallback(window, ImGui_ImplGlFw_KeyCallback);
  195. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  196. }
  197. return true;
  198. }
  199. void ImGui_ImplGlfw_Shutdown()
  200. {
  201. ImGui_ImplGlfw_InvalidateDeviceObjects();
  202. ImGui::Shutdown();
  203. }
  204. void ImGui_ImplGlfw_NewFrame()
  205. {
  206. if (!g_FontTexture)
  207. ImGui_ImplGlfw_CreateDeviceObjects();
  208. ImGuiIO& io = ImGui::GetIO();
  209. // Setup display size (every frame to accommodate for window resizing)
  210. int w, h;
  211. int display_w, display_h;
  212. glfwGetWindowSize(g_Window, &w, &h);
  213. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  214. io.DisplaySize = ImVec2((float)w, (float)h);
  215. io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
  216. // Setup time step
  217. double current_time = glfwGetTime();
  218. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  219. g_Time = current_time;
  220. // Setup inputs
  221. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  222. if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED))
  223. {
  224. double mouse_x, mouse_y;
  225. glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
  226. io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.)
  227. }
  228. else
  229. {
  230. io.MousePos = ImVec2(-1,-1);
  231. }
  232. for (int i = 0; i < 3; i++)
  233. {
  234. io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // 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.
  235. g_MousePressed[i] = false;
  236. }
  237. io.MouseWheel = g_MouseWheel;
  238. g_MouseWheel = 0.0f;
  239. // Hide OS mouse cursor if ImGui is drawing it
  240. glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
  241. // Start the frame
  242. ImGui::NewFrame();
  243. }