imgui_impl_glut.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // dear imgui: Platform Binding 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. io.BackendPlatformName ="imgui_impl_glut";
  35. g_Time = 0;
  36. // Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
  37. io.KeyMap[ImGuiKey_Tab] = '\t'; // == 9 == CTRL+I
  38. io.KeyMap[ImGuiKey_LeftArrow] = 256 + GLUT_KEY_LEFT;
  39. io.KeyMap[ImGuiKey_RightArrow] = 256 + GLUT_KEY_RIGHT;
  40. io.KeyMap[ImGuiKey_UpArrow] = 256 + GLUT_KEY_UP;
  41. io.KeyMap[ImGuiKey_DownArrow] = 256 + GLUT_KEY_DOWN;
  42. io.KeyMap[ImGuiKey_PageUp] = 256 + GLUT_KEY_PAGE_UP;
  43. io.KeyMap[ImGuiKey_PageDown] = 256 + GLUT_KEY_PAGE_DOWN;
  44. io.KeyMap[ImGuiKey_Home] = 256 + GLUT_KEY_HOME;
  45. io.KeyMap[ImGuiKey_End] = 256 + GLUT_KEY_END;
  46. io.KeyMap[ImGuiKey_Insert] = 256 + GLUT_KEY_INSERT;
  47. io.KeyMap[ImGuiKey_Delete] = 127;
  48. io.KeyMap[ImGuiKey_Backspace] = 8; // == CTRL+H
  49. io.KeyMap[ImGuiKey_Space] = ' ';
  50. io.KeyMap[ImGuiKey_Enter] = 13; // == CTRL+M
  51. io.KeyMap[ImGuiKey_Escape] = 27;
  52. io.KeyMap[ImGuiKey_KeyPadEnter] = 13; // == CTRL+M
  53. io.KeyMap[ImGuiKey_A] = 'A';
  54. io.KeyMap[ImGuiKey_C] = 'C';
  55. io.KeyMap[ImGuiKey_V] = 'V';
  56. io.KeyMap[ImGuiKey_X] = 'X';
  57. io.KeyMap[ImGuiKey_Y] = 'Y';
  58. io.KeyMap[ImGuiKey_Z] = 'Z';
  59. return true;
  60. }
  61. void ImGui_ImplGLUT_InstallFuncs()
  62. {
  63. glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
  64. glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
  65. glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
  66. glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
  67. #ifdef __FREEGLUT_EXT_H__
  68. glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
  69. #endif
  70. glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
  71. glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
  72. glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
  73. glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
  74. }
  75. void ImGui_ImplGLUT_Shutdown()
  76. {
  77. }
  78. void ImGui_ImplGLUT_NewFrame()
  79. {
  80. // Setup time step
  81. ImGuiIO& io = ImGui::GetIO();
  82. int current_time = glutGet(GLUT_ELAPSED_TIME);
  83. int delta_time_ms = (current_time - g_Time);
  84. if (delta_time_ms <= 0)
  85. delta_time_ms = 1;
  86. io.DeltaTime = delta_time_ms / 1000.0f;
  87. g_Time = current_time;
  88. // Start the frame
  89. ImGui::NewFrame();
  90. }
  91. static void ImGui_ImplGLUT_UpdateKeyboardMods()
  92. {
  93. ImGuiIO& io = ImGui::GetIO();
  94. int mods = glutGetModifiers();
  95. io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
  96. io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
  97. io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
  98. }
  99. void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
  100. {
  101. // Send character to imgui
  102. //printf("char_down_func %d '%c'\n", c, c);
  103. ImGuiIO& io = ImGui::GetIO();
  104. if (c >= 32)
  105. io.AddInputCharacter((unsigned int)c);
  106. // Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
  107. // 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.
  108. if (c >= 1 && c <= 26)
  109. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
  110. else if (c >= 'a' && c <= 'z')
  111. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
  112. else if (c >= 'A' && c <= 'Z')
  113. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
  114. else
  115. io.KeysDown[c] = true;
  116. ImGui_ImplGLUT_UpdateKeyboardMods();
  117. (void)x; (void)y; // Unused
  118. }
  119. void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
  120. {
  121. //printf("char_up_func %d '%c'\n", c, c);
  122. ImGuiIO& io = ImGui::GetIO();
  123. if (c >= 1 && c <= 26)
  124. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
  125. else if (c >= 'a' && c <= 'z')
  126. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
  127. else if (c >= 'A' && c <= 'Z')
  128. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
  129. else
  130. io.KeysDown[c] = false;
  131. ImGui_ImplGLUT_UpdateKeyboardMods();
  132. (void)x; (void)y; // Unused
  133. }
  134. void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
  135. {
  136. //printf("key_down_func %d\n", key);
  137. ImGuiIO& io = ImGui::GetIO();
  138. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  139. io.KeysDown[key + 256] = true;
  140. ImGui_ImplGLUT_UpdateKeyboardMods();
  141. (void)x; (void)y; // Unused
  142. }
  143. void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
  144. {
  145. //printf("key_up_func %d\n", key);
  146. ImGuiIO& io = ImGui::GetIO();
  147. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  148. io.KeysDown[key + 256] = false;
  149. ImGui_ImplGLUT_UpdateKeyboardMods();
  150. (void)x; (void)y; // Unused
  151. }
  152. void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
  153. {
  154. ImGuiIO& io = ImGui::GetIO();
  155. io.MousePos = ImVec2((float)x, (float)y);
  156. int button = -1;
  157. if (glut_button == GLUT_LEFT_BUTTON) button = 0;
  158. if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
  159. if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
  160. if (button != -1 && state == GLUT_DOWN)
  161. io.MouseDown[button] = true;
  162. if (button != -1 && state == GLUT_UP)
  163. io.MouseDown[button] = false;
  164. }
  165. #ifdef __FREEGLUT_EXT_H__
  166. void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
  167. {
  168. ImGuiIO& io = ImGui::GetIO();
  169. io.MousePos = ImVec2((float)x, (float)y);
  170. if (dir > 0)
  171. io.MouseWheel += 1.0;
  172. else if (dir < 0)
  173. io.MouseWheel -= 1.0;
  174. (void)button; // Unused
  175. }
  176. #endif
  177. void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
  178. {
  179. ImGuiIO& io = ImGui::GetIO();
  180. io.DisplaySize = ImVec2((float)w, (float)h);
  181. }
  182. void ImGui_ImplGLUT_MotionFunc(int x, int y)
  183. {
  184. ImGuiIO& io = ImGui::GetIO();
  185. io.MousePos = ImVec2((float)x, (float)y);
  186. }