imgui_impl_glut.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // dear imgui: Platform Backend for GLUT/FreeGLUT
  2. // This needs to be used along with a Renderer (e.g. OpenGL2)
  3. // !!! GLUT/FreeGLUT IS OBSOLETE SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
  4. // !!! If someone or something is teaching you GLUT in 2020, you are being abused. Please show some resistance. !!!
  5. // !!! Nowadays, prefer using GLFW or SDL instead!
  6. // Issues:
  7. // [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
  8. // [ ] Platform: Missing mouse cursor shape/visibility support.
  9. // [ ] Platform: Missing clipboard support (not supported by Glut).
  10. // [ ] Platform: Missing gamepad support.
  11. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  12. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  13. // https://github.com/ocornut/imgui
  14. // CHANGELOG
  15. // (minor and older changes stripped away, please see git history for details)
  16. // 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h.
  17. // 2019-03-25: Misc: Made io.DeltaTime always above zero.
  18. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  19. // 2018-03-22: Added GLUT Platform binding.
  20. #include "imgui.h"
  21. #include "imgui_impl_glut.h"
  22. #ifdef __APPLE__
  23. #include <GLUT/glut.h>
  24. #else
  25. #include <GL/freeglut.h>
  26. #endif
  27. #ifdef _MSC_VER
  28. #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
  29. #endif
  30. static int g_Time = 0; // Current time, in milliseconds
  31. bool ImGui_ImplGLUT_Init()
  32. {
  33. ImGuiIO& io = ImGui::GetIO();
  34. #ifdef FREEGLUT
  35. io.BackendPlatformName = "imgui_impl_glut (freeglut)";
  36. #else
  37. io.BackendPlatformName = "imgui_impl_glut";
  38. #endif
  39. g_Time = 0;
  40. // Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
  41. io.KeyMap[ImGuiKey_Tab] = '\t'; // == 9 == CTRL+I
  42. io.KeyMap[ImGuiKey_LeftArrow] = 256 + GLUT_KEY_LEFT;
  43. io.KeyMap[ImGuiKey_RightArrow] = 256 + GLUT_KEY_RIGHT;
  44. io.KeyMap[ImGuiKey_UpArrow] = 256 + GLUT_KEY_UP;
  45. io.KeyMap[ImGuiKey_DownArrow] = 256 + GLUT_KEY_DOWN;
  46. io.KeyMap[ImGuiKey_PageUp] = 256 + GLUT_KEY_PAGE_UP;
  47. io.KeyMap[ImGuiKey_PageDown] = 256 + GLUT_KEY_PAGE_DOWN;
  48. io.KeyMap[ImGuiKey_Home] = 256 + GLUT_KEY_HOME;
  49. io.KeyMap[ImGuiKey_End] = 256 + GLUT_KEY_END;
  50. io.KeyMap[ImGuiKey_Insert] = 256 + GLUT_KEY_INSERT;
  51. io.KeyMap[ImGuiKey_Delete] = 127;
  52. io.KeyMap[ImGuiKey_Backspace] = 8; // == CTRL+H
  53. io.KeyMap[ImGuiKey_Space] = ' ';
  54. io.KeyMap[ImGuiKey_Enter] = 13; // == CTRL+M
  55. io.KeyMap[ImGuiKey_Escape] = 27;
  56. io.KeyMap[ImGuiKey_KeyPadEnter] = 13; // == CTRL+M
  57. io.KeyMap[ImGuiKey_A] = 'A';
  58. io.KeyMap[ImGuiKey_C] = 'C';
  59. io.KeyMap[ImGuiKey_V] = 'V';
  60. io.KeyMap[ImGuiKey_X] = 'X';
  61. io.KeyMap[ImGuiKey_Y] = 'Y';
  62. io.KeyMap[ImGuiKey_Z] = 'Z';
  63. return true;
  64. }
  65. void ImGui_ImplGLUT_InstallFuncs()
  66. {
  67. glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
  68. glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
  69. glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
  70. glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
  71. #ifdef __FREEGLUT_EXT_H__
  72. glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
  73. #endif
  74. glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
  75. glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
  76. glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
  77. glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
  78. }
  79. void ImGui_ImplGLUT_Shutdown()
  80. {
  81. }
  82. void ImGui_ImplGLUT_NewFrame()
  83. {
  84. // Setup time step
  85. ImGuiIO& io = ImGui::GetIO();
  86. int current_time = glutGet(GLUT_ELAPSED_TIME);
  87. int delta_time_ms = (current_time - g_Time);
  88. if (delta_time_ms <= 0)
  89. delta_time_ms = 1;
  90. io.DeltaTime = delta_time_ms / 1000.0f;
  91. g_Time = current_time;
  92. // Start the frame
  93. ImGui::NewFrame();
  94. }
  95. static void ImGui_ImplGLUT_UpdateKeyboardMods()
  96. {
  97. ImGuiIO& io = ImGui::GetIO();
  98. int mods = glutGetModifiers();
  99. io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
  100. io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
  101. io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
  102. }
  103. void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
  104. {
  105. // Send character to imgui
  106. //printf("char_down_func %d '%c'\n", c, c);
  107. ImGuiIO& io = ImGui::GetIO();
  108. if (c >= 32)
  109. io.AddInputCharacter((unsigned int)c);
  110. // Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
  111. // This is a hacky mess but GLUT is unable to distinguish e.g. a TAB key from CTRL+I so this is probably the best we can do here.
  112. if (c >= 1 && c <= 26)
  113. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
  114. else if (c >= 'a' && c <= 'z')
  115. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
  116. else if (c >= 'A' && c <= 'Z')
  117. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
  118. else
  119. io.KeysDown[c] = true;
  120. ImGui_ImplGLUT_UpdateKeyboardMods();
  121. (void)x; (void)y; // Unused
  122. }
  123. void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
  124. {
  125. //printf("char_up_func %d '%c'\n", c, c);
  126. ImGuiIO& io = ImGui::GetIO();
  127. if (c >= 1 && c <= 26)
  128. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
  129. else if (c >= 'a' && c <= 'z')
  130. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
  131. else if (c >= 'A' && c <= 'Z')
  132. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
  133. else
  134. io.KeysDown[c] = false;
  135. ImGui_ImplGLUT_UpdateKeyboardMods();
  136. (void)x; (void)y; // Unused
  137. }
  138. void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
  139. {
  140. //printf("key_down_func %d\n", key);
  141. ImGuiIO& io = ImGui::GetIO();
  142. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  143. io.KeysDown[key + 256] = true;
  144. ImGui_ImplGLUT_UpdateKeyboardMods();
  145. (void)x; (void)y; // Unused
  146. }
  147. void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
  148. {
  149. //printf("key_up_func %d\n", key);
  150. ImGuiIO& io = ImGui::GetIO();
  151. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  152. io.KeysDown[key + 256] = false;
  153. ImGui_ImplGLUT_UpdateKeyboardMods();
  154. (void)x; (void)y; // Unused
  155. }
  156. void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
  157. {
  158. ImGuiIO& io = ImGui::GetIO();
  159. io.MousePos = ImVec2((float)x, (float)y);
  160. int button = -1;
  161. if (glut_button == GLUT_LEFT_BUTTON) button = 0;
  162. if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
  163. if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
  164. if (button != -1 && state == GLUT_DOWN)
  165. io.MouseDown[button] = true;
  166. if (button != -1 && state == GLUT_UP)
  167. io.MouseDown[button] = false;
  168. }
  169. #ifdef __FREEGLUT_EXT_H__
  170. void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
  171. {
  172. ImGuiIO& io = ImGui::GetIO();
  173. io.MousePos = ImVec2((float)x, (float)y);
  174. if (dir > 0)
  175. io.MouseWheel += 1.0;
  176. else if (dir < 0)
  177. io.MouseWheel -= 1.0;
  178. (void)button; // Unused
  179. }
  180. #endif
  181. void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
  182. {
  183. ImGuiIO& io = ImGui::GetIO();
  184. io.DisplaySize = ImVec2((float)w, (float)h);
  185. }
  186. void ImGui_ImplGLUT_MotionFunc(int x, int y)
  187. {
  188. ImGuiIO& io = ImGui::GetIO();
  189. io.MousePos = ImVec2((float)x, (float)y);
  190. }