imgui_impl_glut.cpp 7.2 KB

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