imgui_impl_glfw.cpp 11 KB

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