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. See main.cpp for an example of using this.
  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. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  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. int fb_width = (int)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
  45. int fb_height = (int)(io.DisplaySize.y * io.DisplayFramebufferScale.y);
  46. if (fb_width == 0 || fb_height == 0)
  47. return;
  48. draw_data->ScaleClipRects(io.DisplayFramebufferScale);
  49. // Setup viewport, orthographic projection matrix
  50. glViewport(0, 0, (GLsizei)fb_width, (GLsizei)fb_height);
  51. glMatrixMode(GL_PROJECTION);
  52. glPushMatrix();
  53. glLoadIdentity();
  54. glOrtho(0.0f, io.DisplaySize.x, io.DisplaySize.y, 0.0f, -1.0f, +1.0f);
  55. glMatrixMode(GL_MODELVIEW);
  56. glPushMatrix();
  57. glLoadIdentity();
  58. // Render command lists
  59. #define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
  60. for (int n = 0; n < draw_data->CmdListsCount; n++)
  61. {
  62. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  63. const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
  64. const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
  65. glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos)));
  66. glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv)));
  67. glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col)));
  68. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  69. {
  70. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  71. if (pcmd->UserCallback)
  72. {
  73. pcmd->UserCallback(cmd_list, pcmd);
  74. }
  75. else
  76. {
  77. glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->TextureId);
  78. 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));
  79. glDrawElements(GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer);
  80. }
  81. idx_buffer += pcmd->ElemCount;
  82. }
  83. }
  84. #undef OFFSETOF
  85. // Restore modified state
  86. glDisableClientState(GL_COLOR_ARRAY);
  87. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  88. glDisableClientState(GL_VERTEX_ARRAY);
  89. glBindTexture(GL_TEXTURE_2D, last_texture);
  90. glMatrixMode(GL_MODELVIEW);
  91. glPopMatrix();
  92. glMatrixMode(GL_PROJECTION);
  93. glPopMatrix();
  94. glPopAttrib();
  95. glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]);
  96. }
  97. static const char* ImGui_ImplGlfw_GetClipboardText()
  98. {
  99. return glfwGetClipboardString(g_Window);
  100. }
  101. static void ImGui_ImplGlfw_SetClipboardText(const char* text)
  102. {
  103. glfwSetClipboardString(g_Window, text);
  104. }
  105. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  106. {
  107. if (action == GLFW_PRESS && button >= 0 && button < 3)
  108. g_MousePressed[button] = true;
  109. }
  110. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double /*xoffset*/, double yoffset)
  111. {
  112. g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
  113. }
  114. void ImGui_ImplGlFw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  115. {
  116. ImGuiIO& io = ImGui::GetIO();
  117. if (action == GLFW_PRESS)
  118. io.KeysDown[key] = true;
  119. if (action == GLFW_RELEASE)
  120. io.KeysDown[key] = false;
  121. (void)mods; // Modifiers are not reliable across systems
  122. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  123. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  124. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  125. }
  126. void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
  127. {
  128. ImGuiIO& io = ImGui::GetIO();
  129. if (c > 0 && c < 0x10000)
  130. io.AddInputCharacter((unsigned short)c);
  131. }
  132. bool ImGui_ImplGlfw_CreateDeviceObjects()
  133. {
  134. // Build texture atlas
  135. ImGuiIO& io = ImGui::GetIO();
  136. unsigned char* pixels;
  137. int width, height;
  138. io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
  139. // Upload texture to graphics system
  140. GLint last_texture;
  141. glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture);
  142. glGenTextures(1, &g_FontTexture);
  143. glBindTexture(GL_TEXTURE_2D, g_FontTexture);
  144. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  145. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  146. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);
  147. // Store our identifier
  148. io.Fonts->TexID = (void *)(intptr_t)g_FontTexture;
  149. // Restore state
  150. glBindTexture(GL_TEXTURE_2D, last_texture);
  151. return true;
  152. }
  153. void ImGui_ImplGlfw_InvalidateDeviceObjects()
  154. {
  155. if (g_FontTexture)
  156. {
  157. glDeleteTextures(1, &g_FontTexture);
  158. ImGui::GetIO().Fonts->TexID = 0;
  159. g_FontTexture = 0;
  160. }
  161. }
  162. bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks)
  163. {
  164. g_Window = window;
  165. ImGuiIO& io = ImGui::GetIO();
  166. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  167. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  168. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  169. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  170. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  171. io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
  172. io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
  173. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  174. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  175. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  176. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  177. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  178. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  179. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  180. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  181. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  182. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  183. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  184. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  185. 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.
  186. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  187. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  188. #ifdef _WIN32
  189. io.ImeWindowHandle = glfwGetWin32Window(g_Window);
  190. #endif
  191. if (install_callbacks)
  192. {
  193. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  194. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  195. glfwSetKeyCallback(window, ImGui_ImplGlFw_KeyCallback);
  196. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  197. }
  198. return true;
  199. }
  200. void ImGui_ImplGlfw_Shutdown()
  201. {
  202. ImGui_ImplGlfw_InvalidateDeviceObjects();
  203. ImGui::Shutdown();
  204. }
  205. void ImGui_ImplGlfw_NewFrame()
  206. {
  207. if (!g_FontTexture)
  208. ImGui_ImplGlfw_CreateDeviceObjects();
  209. ImGuiIO& io = ImGui::GetIO();
  210. // Setup display size (every frame to accommodate for window resizing)
  211. int w, h;
  212. int display_w, display_h;
  213. glfwGetWindowSize(g_Window, &w, &h);
  214. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  215. io.DisplaySize = ImVec2((float)w, (float)h);
  216. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  217. // Setup time step
  218. double current_time = glfwGetTime();
  219. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  220. g_Time = current_time;
  221. // Setup inputs
  222. // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
  223. if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED))
  224. {
  225. double mouse_x, mouse_y;
  226. glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
  227. 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.)
  228. }
  229. else
  230. {
  231. io.MousePos = ImVec2(-1,-1);
  232. }
  233. for (int i = 0; i < 3; i++)
  234. {
  235. 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.
  236. g_MousePressed[i] = false;
  237. }
  238. io.MouseWheel = g_MouseWheel;
  239. g_MouseWheel = 0.0f;
  240. // Hide OS mouse cursor if ImGui is drawing it
  241. glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
  242. // Start the frame
  243. ImGui::NewFrame();
  244. }