imgui_impl_glut.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 2019, 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_A] = 'A';
  53. io.KeyMap[ImGuiKey_C] = 'C';
  54. io.KeyMap[ImGuiKey_V] = 'V';
  55. io.KeyMap[ImGuiKey_X] = 'X';
  56. io.KeyMap[ImGuiKey_Y] = 'Y';
  57. io.KeyMap[ImGuiKey_Z] = 'Z';
  58. return true;
  59. }
  60. void ImGui_ImplGLUT_InstallFuncs()
  61. {
  62. glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
  63. glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
  64. glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
  65. glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
  66. #ifdef __FREEGLUT_EXT_H__
  67. glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
  68. #endif
  69. glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
  70. glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
  71. glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
  72. glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
  73. }
  74. void ImGui_ImplGLUT_Shutdown()
  75. {
  76. }
  77. void ImGui_ImplGLUT_NewFrame()
  78. {
  79. // Setup time step
  80. ImGuiIO& io = ImGui::GetIO();
  81. int current_time = glutGet(GLUT_ELAPSED_TIME);
  82. int delta_time_ms = (current_time - g_Time);
  83. if (delta_time_ms <= 0)
  84. delta_time_ms = 1;
  85. io.DeltaTime = delta_time_ms / 1000.0f;
  86. g_Time = current_time;
  87. // Start the frame
  88. ImGui::NewFrame();
  89. }
  90. static void ImGui_ImplGLUT_UpdateKeyboardMods()
  91. {
  92. ImGuiIO& io = ImGui::GetIO();
  93. int mods = glutGetModifiers();
  94. io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
  95. io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
  96. io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
  97. }
  98. void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
  99. {
  100. // Send character to imgui
  101. //printf("char_down_func %d '%c'\n", c, c);
  102. ImGuiIO& io = ImGui::GetIO();
  103. if (c >= 32)
  104. io.AddInputCharacter((unsigned int)c);
  105. // Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
  106. // 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.
  107. if (c >= 1 && c <= 26)
  108. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
  109. else if (c >= 'a' && c <= 'z')
  110. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
  111. else if (c >= 'A' && c <= 'Z')
  112. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
  113. else
  114. io.KeysDown[c] = true;
  115. ImGui_ImplGLUT_UpdateKeyboardMods();
  116. (void)x; (void)y; // Unused
  117. }
  118. void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
  119. {
  120. //printf("char_up_func %d '%c'\n", c, c);
  121. ImGuiIO& io = ImGui::GetIO();
  122. if (c >= 1 && c <= 26)
  123. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
  124. else if (c >= 'a' && c <= 'z')
  125. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
  126. else if (c >= 'A' && c <= 'Z')
  127. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
  128. else
  129. io.KeysDown[c] = false;
  130. ImGui_ImplGLUT_UpdateKeyboardMods();
  131. (void)x; (void)y; // Unused
  132. }
  133. void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
  134. {
  135. //printf("key_down_func %d\n", key);
  136. ImGuiIO& io = ImGui::GetIO();
  137. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  138. io.KeysDown[key + 256] = true;
  139. ImGui_ImplGLUT_UpdateKeyboardMods();
  140. (void)x; (void)y; // Unused
  141. }
  142. void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
  143. {
  144. //printf("key_up_func %d\n", key);
  145. ImGuiIO& io = ImGui::GetIO();
  146. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  147. io.KeysDown[key + 256] = false;
  148. ImGui_ImplGLUT_UpdateKeyboardMods();
  149. (void)x; (void)y; // Unused
  150. }
  151. void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
  152. {
  153. ImGuiIO& io = ImGui::GetIO();
  154. io.MousePos = ImVec2((float)x, (float)y);
  155. int button = -1;
  156. if (glut_button == GLUT_LEFT_BUTTON) button = 0;
  157. if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
  158. if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
  159. if (button != -1 && state == GLUT_DOWN)
  160. io.MouseDown[button] = true;
  161. if (button != -1 && state == GLUT_UP)
  162. io.MouseDown[button] = false;
  163. }
  164. #ifdef __FREEGLUT_EXT_H__
  165. void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
  166. {
  167. ImGuiIO& io = ImGui::GetIO();
  168. io.MousePos = ImVec2((float)x, (float)y);
  169. if (dir > 0)
  170. io.MouseWheel += 1.0;
  171. else if (dir < 0)
  172. io.MouseWheel -= 1.0;
  173. (void)button; // Unused
  174. }
  175. #endif
  176. void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
  177. {
  178. ImGuiIO& io = ImGui::GetIO();
  179. io.DisplaySize = ImVec2((float)w, (float)h);
  180. }
  181. void ImGui_ImplGLUT_MotionFunc(int x, int y)
  182. {
  183. ImGuiIO& io = ImGui::GetIO();
  184. io.MousePos = ImVec2((float)x, (float)y);
  185. }