ImGuiPlugin.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2022 Alec Jacobson <[email protected]>
  4. // Copyright (C) 2018 Jérémie Dumas <[email protected]>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. ////////////////////////////////////////////////////////////////////////////////
  10. #include "ImGuiPlugin.h"
  11. #include "ImGuiHelpers.h"
  12. #include "../../../project.h"
  13. #include <backends/imgui_impl_glfw.h>
  14. #include <backends/imgui_impl_opengl3.h>
  15. #include <imgui.h>
  16. #include <imgui_fonts_droid_sans.h>
  17. #include <GLFW/glfw3.h>
  18. #include <iostream>
  19. namespace igl
  20. {
  21. namespace opengl
  22. {
  23. namespace glfw
  24. {
  25. namespace imgui
  26. {
  27. IGL_INLINE void ImGuiPlugin::init(igl::opengl::glfw::Viewer *_viewer)
  28. {
  29. ViewerPlugin::init(_viewer);
  30. // Setup ImGui binding
  31. if (_viewer)
  32. {
  33. IMGUI_CHECKVERSION();
  34. if (!context_)
  35. {
  36. // Single global context by default, but can be overridden by the user
  37. static ImGuiContext * __global_context = ImGui::CreateContext();
  38. context_ = __global_context;
  39. }
  40. const char* glsl_version = "#version 150";
  41. ImGui_ImplGlfw_InitForOpenGL(viewer->window, false);
  42. ImGui_ImplOpenGL3_Init(glsl_version);
  43. ImGui::GetIO().IniFilename = nullptr;
  44. ImGui::StyleColorsDark();
  45. ImGuiStyle& style = ImGui::GetStyle();
  46. style.FrameRounding = 5.0f;
  47. reload_font();
  48. }
  49. init_widgets();
  50. }
  51. IGL_INLINE void ImGuiPlugin::init_widgets()
  52. {
  53. // Init all widgets
  54. for(auto & widget : widgets) { widget->init(viewer, this); }
  55. }
  56. IGL_INLINE void ImGuiPlugin::reload_font(int font_size)
  57. {
  58. hidpi_scaling_ = hidpi_scaling();
  59. pixel_ratio_ = pixel_ratio();
  60. ImGuiIO& io = ImGui::GetIO();
  61. io.Fonts->Clear();
  62. io.Fonts->AddFontFromMemoryCompressedTTF(droid_sans_compressed_data,
  63. droid_sans_compressed_size, font_size * hidpi_scaling_);
  64. io.FontGlobalScale = 1.0 / pixel_ratio_;
  65. }
  66. IGL_INLINE void ImGuiPlugin::shutdown()
  67. {
  68. // Cleanup
  69. ImGui_ImplOpenGL3_Shutdown();
  70. ImGui_ImplGlfw_Shutdown();
  71. // User is responsible for destroying context if a custom context is given
  72. // ImGui::DestroyContext(*context_);
  73. }
  74. IGL_INLINE bool ImGuiPlugin::pre_draw()
  75. {
  76. glfwPollEvents();
  77. // Check whether window dpi has changed
  78. float scaling = hidpi_scaling();
  79. if (std::abs(scaling - hidpi_scaling_) > 1e-5)
  80. {
  81. reload_font();
  82. ImGui_ImplOpenGL3_DestroyDeviceObjects();
  83. }
  84. ImGui_ImplOpenGL3_NewFrame();
  85. ImGui_ImplGlfw_NewFrame();
  86. ImGui::NewFrame();
  87. return false;
  88. }
  89. IGL_INLINE bool ImGuiPlugin::post_draw()
  90. {
  91. for(auto & widget : widgets){ widget->draw(); }
  92. ImGui::Render();
  93. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  94. return false;
  95. }
  96. IGL_INLINE void ImGuiPlugin::post_resize(int width, int height)
  97. {
  98. if (context_)
  99. {
  100. ImGui::GetIO().DisplaySize.x = float(width);
  101. ImGui::GetIO().DisplaySize.y = float(height);
  102. }
  103. }
  104. // Mouse IO
  105. IGL_INLINE bool ImGuiPlugin::mouse_down(int button, int modifier)
  106. {
  107. ImGui_ImplGlfw_MouseButtonCallback(viewer->window, button, GLFW_PRESS, modifier);
  108. if(ImGui::GetIO().WantCaptureMouse){ return true; }
  109. for( auto & widget : widgets)
  110. {
  111. if(widget->mouse_down(button, modifier)) { return true; }
  112. }
  113. return false;
  114. }
  115. IGL_INLINE bool ImGuiPlugin::mouse_up(int button, int modifier)
  116. {
  117. //return ImGui::GetIO().WantCaptureMouse;
  118. // !! Should not steal mouse up
  119. for( auto & widget : widgets)
  120. {
  121. widget->mouse_up(button, modifier);
  122. }
  123. return false;
  124. }
  125. IGL_INLINE bool ImGuiPlugin::mouse_move(int mouse_x, int mouse_y)
  126. {
  127. if(ImGui::GetIO().WantCaptureMouse){ return true; }
  128. for( auto & widget : widgets)
  129. {
  130. if(widget->mouse_move(mouse_x, mouse_y)) { return true; }
  131. }
  132. return false;
  133. }
  134. IGL_INLINE bool ImGuiPlugin::mouse_scroll(float delta_y)
  135. {
  136. ImGui_ImplGlfw_ScrollCallback(viewer->window, 0.f, delta_y);
  137. return ImGui::GetIO().WantCaptureMouse;
  138. }
  139. // Keyboard IO
  140. IGL_INLINE bool ImGuiPlugin::key_pressed(unsigned int key, int modifiers)
  141. {
  142. ImGui_ImplGlfw_CharCallback(nullptr, key);
  143. if(ImGui::GetIO().WantCaptureKeyboard) { return true; }
  144. for(auto & widget : widgets)
  145. {
  146. if(widget->key_pressed(key,modifiers)) {return true; }
  147. }
  148. return false;
  149. }
  150. IGL_INLINE bool ImGuiPlugin::key_down(int key, int modifiers)
  151. {
  152. ImGui_ImplGlfw_KeyCallback(viewer->window, key, 0, GLFW_PRESS, modifiers);
  153. if(ImGui::GetIO().WantCaptureKeyboard) { return true; }
  154. for(auto & widget : widgets)
  155. {
  156. if(widget->key_down(key,modifiers)) {return true; }
  157. }
  158. return false;
  159. }
  160. IGL_INLINE bool ImGuiPlugin::key_up(int key, int modifiers)
  161. {
  162. ImGui_ImplGlfw_KeyCallback(viewer->window, key, 0, GLFW_RELEASE, modifiers);
  163. if(ImGui::GetIO().WantCaptureKeyboard) { return true; }
  164. for(auto & widget : widgets)
  165. {
  166. if(widget->key_up(key,modifiers)) { return true; }
  167. }
  168. return false;
  169. }
  170. IGL_INLINE float ImGuiPlugin::pixel_ratio()
  171. {
  172. // Computes pixel ratio for hidpi devices
  173. int buf_size[2];
  174. int win_size[2];
  175. GLFWwindow* window = glfwGetCurrentContext();
  176. glfwGetFramebufferSize(window, &buf_size[0], &buf_size[1]);
  177. glfwGetWindowSize(window, &win_size[0], &win_size[1]);
  178. return (float) buf_size[0] / (float) win_size[0];
  179. }
  180. IGL_INLINE float ImGuiPlugin::hidpi_scaling()
  181. {
  182. // Computes scaling factor for hidpi devices
  183. float xscale, yscale;
  184. GLFWwindow* window = glfwGetCurrentContext();
  185. glfwGetWindowContentScale(window, &xscale, &yscale);
  186. return 0.5 * (xscale + yscale);
  187. }
  188. } // end namespace
  189. } // end namespace
  190. } // end namespace
  191. } // end namespace