imgui_impl_glut.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 PREHISTORIC SOFTWARE. Using GLUT is not recommended unless you really miss the 90's. !!!
  4. // !!! If someone or something is teaching you GLUT today, you are being abused. Please show some resistance. !!!
  5. // !!! Nowadays, prefer using GLFW or SDL instead!
  6. // Implemented features:
  7. // [X] Platform: Partial keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLUT values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set]
  8. // Issues:
  9. // [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
  10. // [ ] Platform: Missing horizontal mouse wheel support.
  11. // [ ] Platform: Missing mouse cursor shape/visibility support.
  12. // [ ] Platform: Missing clipboard support (not supported by Glut).
  13. // [ ] Platform: Missing gamepad support.
  14. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  15. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  16. // Learn about Dear ImGui:
  17. // - FAQ https://dearimgui.com/faq
  18. // - Getting Started https://dearimgui.com/getting-started
  19. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  20. // - Introduction, links and more at the top of imgui.cpp
  21. // CHANGELOG
  22. // (minor and older changes stripped away, please see git history for details)
  23. // 2023-04-17: BREAKING: Removed call to ImGui::NewFrame() from ImGui_ImplGLUT_NewFrame(). Needs to be called from the main application loop, like with every other backends.
  24. // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
  25. // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
  26. // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
  27. // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
  28. // 2019-04-03: Misc: Renamed imgui_impl_freeglut.cpp/.h to imgui_impl_glut.cpp/.h.
  29. // 2019-03-25: Misc: Made io.DeltaTime always above zero.
  30. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  31. // 2018-03-22: Added GLUT Platform binding.
  32. #include "imgui.h"
  33. #ifndef IMGUI_DISABLE
  34. #include "imgui_impl_glut.h"
  35. #define GL_SILENCE_DEPRECATION
  36. #ifdef __APPLE__
  37. #include <GLUT/glut.h>
  38. #else
  39. #include <GL/freeglut.h>
  40. #endif
  41. #ifdef _MSC_VER
  42. #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
  43. #endif
  44. static int g_Time = 0; // Current time, in milliseconds
  45. // Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
  46. static ImGuiKey ImGui_ImplGLUT_KeyToImGuiKey(int key)
  47. {
  48. switch (key)
  49. {
  50. case '\t': return ImGuiKey_Tab;
  51. case 256 + GLUT_KEY_LEFT: return ImGuiKey_LeftArrow;
  52. case 256 + GLUT_KEY_RIGHT: return ImGuiKey_RightArrow;
  53. case 256 + GLUT_KEY_UP: return ImGuiKey_UpArrow;
  54. case 256 + GLUT_KEY_DOWN: return ImGuiKey_DownArrow;
  55. case 256 + GLUT_KEY_PAGE_UP: return ImGuiKey_PageUp;
  56. case 256 + GLUT_KEY_PAGE_DOWN: return ImGuiKey_PageDown;
  57. case 256 + GLUT_KEY_HOME: return ImGuiKey_Home;
  58. case 256 + GLUT_KEY_END: return ImGuiKey_End;
  59. case 256 + GLUT_KEY_INSERT: return ImGuiKey_Insert;
  60. case 127: return ImGuiKey_Delete;
  61. case 8: return ImGuiKey_Backspace;
  62. case ' ': return ImGuiKey_Space;
  63. case 13: return ImGuiKey_Enter;
  64. case 27: return ImGuiKey_Escape;
  65. case 39: return ImGuiKey_Apostrophe;
  66. case 44: return ImGuiKey_Comma;
  67. case 45: return ImGuiKey_Minus;
  68. case 46: return ImGuiKey_Period;
  69. case 47: return ImGuiKey_Slash;
  70. case 59: return ImGuiKey_Semicolon;
  71. case 61: return ImGuiKey_Equal;
  72. case 91: return ImGuiKey_LeftBracket;
  73. case 92: return ImGuiKey_Backslash;
  74. case 93: return ImGuiKey_RightBracket;
  75. case 96: return ImGuiKey_GraveAccent;
  76. //case 0: return ImGuiKey_CapsLock;
  77. //case 0: return ImGuiKey_ScrollLock;
  78. case 256 + 0x006D: return ImGuiKey_NumLock;
  79. //case 0: return ImGuiKey_PrintScreen;
  80. //case 0: return ImGuiKey_Pause;
  81. //case '0': return ImGuiKey_Keypad0;
  82. //case '1': return ImGuiKey_Keypad1;
  83. //case '2': return ImGuiKey_Keypad2;
  84. //case '3': return ImGuiKey_Keypad3;
  85. //case '4': return ImGuiKey_Keypad4;
  86. //case '5': return ImGuiKey_Keypad5;
  87. //case '6': return ImGuiKey_Keypad6;
  88. //case '7': return ImGuiKey_Keypad7;
  89. //case '8': return ImGuiKey_Keypad8;
  90. //case '9': return ImGuiKey_Keypad9;
  91. //case 46: return ImGuiKey_KeypadDecimal;
  92. //case 47: return ImGuiKey_KeypadDivide;
  93. case 42: return ImGuiKey_KeypadMultiply;
  94. //case 45: return ImGuiKey_KeypadSubtract;
  95. case 43: return ImGuiKey_KeypadAdd;
  96. //case 13: return ImGuiKey_KeypadEnter;
  97. //case 0: return ImGuiKey_KeypadEqual;
  98. case 256 + 0x0072: return ImGuiKey_LeftCtrl;
  99. case 256 + 0x0070: return ImGuiKey_LeftShift;
  100. case 256 + 0x0074: return ImGuiKey_LeftAlt;
  101. //case 0: return ImGuiKey_LeftSuper;
  102. case 256 + 0x0073: return ImGuiKey_RightCtrl;
  103. case 256 + 0x0071: return ImGuiKey_RightShift;
  104. case 256 + 0x0075: return ImGuiKey_RightAlt;
  105. //case 0: return ImGuiKey_RightSuper;
  106. //case 0: return ImGuiKey_Menu;
  107. case '0': return ImGuiKey_0;
  108. case '1': return ImGuiKey_1;
  109. case '2': return ImGuiKey_2;
  110. case '3': return ImGuiKey_3;
  111. case '4': return ImGuiKey_4;
  112. case '5': return ImGuiKey_5;
  113. case '6': return ImGuiKey_6;
  114. case '7': return ImGuiKey_7;
  115. case '8': return ImGuiKey_8;
  116. case '9': return ImGuiKey_9;
  117. case 'A': case 'a': return ImGuiKey_A;
  118. case 'B': case 'b': return ImGuiKey_B;
  119. case 'C': case 'c': return ImGuiKey_C;
  120. case 'D': case 'd': return ImGuiKey_D;
  121. case 'E': case 'e': return ImGuiKey_E;
  122. case 'F': case 'f': return ImGuiKey_F;
  123. case 'G': case 'g': return ImGuiKey_G;
  124. case 'H': case 'h': return ImGuiKey_H;
  125. case 'I': case 'i': return ImGuiKey_I;
  126. case 'J': case 'j': return ImGuiKey_J;
  127. case 'K': case 'k': return ImGuiKey_K;
  128. case 'L': case 'l': return ImGuiKey_L;
  129. case 'M': case 'm': return ImGuiKey_M;
  130. case 'N': case 'n': return ImGuiKey_N;
  131. case 'O': case 'o': return ImGuiKey_O;
  132. case 'P': case 'p': return ImGuiKey_P;
  133. case 'Q': case 'q': return ImGuiKey_Q;
  134. case 'R': case 'r': return ImGuiKey_R;
  135. case 'S': case 's': return ImGuiKey_S;
  136. case 'T': case 't': return ImGuiKey_T;
  137. case 'U': case 'u': return ImGuiKey_U;
  138. case 'V': case 'v': return ImGuiKey_V;
  139. case 'W': case 'w': return ImGuiKey_W;
  140. case 'X': case 'x': return ImGuiKey_X;
  141. case 'Y': case 'y': return ImGuiKey_Y;
  142. case 'Z': case 'z': return ImGuiKey_Z;
  143. case 256 + GLUT_KEY_F1: return ImGuiKey_F1;
  144. case 256 + GLUT_KEY_F2: return ImGuiKey_F2;
  145. case 256 + GLUT_KEY_F3: return ImGuiKey_F3;
  146. case 256 + GLUT_KEY_F4: return ImGuiKey_F4;
  147. case 256 + GLUT_KEY_F5: return ImGuiKey_F5;
  148. case 256 + GLUT_KEY_F6: return ImGuiKey_F6;
  149. case 256 + GLUT_KEY_F7: return ImGuiKey_F7;
  150. case 256 + GLUT_KEY_F8: return ImGuiKey_F8;
  151. case 256 + GLUT_KEY_F9: return ImGuiKey_F9;
  152. case 256 + GLUT_KEY_F10: return ImGuiKey_F10;
  153. case 256 + GLUT_KEY_F11: return ImGuiKey_F11;
  154. case 256 + GLUT_KEY_F12: return ImGuiKey_F12;
  155. default: return ImGuiKey_None;
  156. }
  157. }
  158. bool ImGui_ImplGLUT_Init()
  159. {
  160. ImGuiIO& io = ImGui::GetIO();
  161. IMGUI_CHECKVERSION();
  162. #ifdef FREEGLUT
  163. io.BackendPlatformName = "imgui_impl_glut (freeglut)";
  164. #else
  165. io.BackendPlatformName = "imgui_impl_glut";
  166. #endif
  167. g_Time = 0;
  168. return true;
  169. }
  170. void ImGui_ImplGLUT_InstallFuncs()
  171. {
  172. glutReshapeFunc(ImGui_ImplGLUT_ReshapeFunc);
  173. glutMotionFunc(ImGui_ImplGLUT_MotionFunc);
  174. glutPassiveMotionFunc(ImGui_ImplGLUT_MotionFunc);
  175. glutMouseFunc(ImGui_ImplGLUT_MouseFunc);
  176. #ifdef __FREEGLUT_EXT_H__
  177. glutMouseWheelFunc(ImGui_ImplGLUT_MouseWheelFunc);
  178. #endif
  179. glutKeyboardFunc(ImGui_ImplGLUT_KeyboardFunc);
  180. glutKeyboardUpFunc(ImGui_ImplGLUT_KeyboardUpFunc);
  181. glutSpecialFunc(ImGui_ImplGLUT_SpecialFunc);
  182. glutSpecialUpFunc(ImGui_ImplGLUT_SpecialUpFunc);
  183. }
  184. void ImGui_ImplGLUT_Shutdown()
  185. {
  186. ImGuiIO& io = ImGui::GetIO();
  187. io.BackendPlatformName = nullptr;
  188. }
  189. void ImGui_ImplGLUT_NewFrame()
  190. {
  191. // Setup time step
  192. ImGuiIO& io = ImGui::GetIO();
  193. int current_time = glutGet(GLUT_ELAPSED_TIME);
  194. int delta_time_ms = (current_time - g_Time);
  195. if (delta_time_ms <= 0)
  196. delta_time_ms = 1;
  197. io.DeltaTime = delta_time_ms / 1000.0f;
  198. g_Time = current_time;
  199. }
  200. static void ImGui_ImplGLUT_UpdateKeyModifiers()
  201. {
  202. ImGuiIO& io = ImGui::GetIO();
  203. int glut_key_mods = glutGetModifiers();
  204. io.AddKeyEvent(ImGuiMod_Ctrl, (glut_key_mods & GLUT_ACTIVE_CTRL) != 0);
  205. io.AddKeyEvent(ImGuiMod_Shift, (glut_key_mods & GLUT_ACTIVE_SHIFT) != 0);
  206. io.AddKeyEvent(ImGuiMod_Alt, (glut_key_mods & GLUT_ACTIVE_ALT) != 0);
  207. }
  208. static void ImGui_ImplGLUT_AddKeyEvent(ImGuiKey key, bool down, int native_keycode)
  209. {
  210. ImGuiIO& io = ImGui::GetIO();
  211. io.AddKeyEvent(key, down);
  212. io.SetKeyEventNativeData(key, native_keycode, -1); // To support legacy indexing (<1.87 user code)
  213. }
  214. void ImGui_ImplGLUT_KeyboardFunc(unsigned char c, int x, int y)
  215. {
  216. // Send character to imgui
  217. //printf("char_down_func %d '%c'\n", c, c);
  218. ImGuiIO& io = ImGui::GetIO();
  219. if (c >= 32)
  220. io.AddInputCharacter((unsigned int)c);
  221. ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey(c);
  222. ImGui_ImplGLUT_AddKeyEvent(key, true, c);
  223. ImGui_ImplGLUT_UpdateKeyModifiers();
  224. (void)x; (void)y; // Unused
  225. }
  226. void ImGui_ImplGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
  227. {
  228. //printf("char_up_func %d '%c'\n", c, c);
  229. ImGuiKey key = ImGui_ImplGLUT_KeyToImGuiKey(c);
  230. ImGui_ImplGLUT_AddKeyEvent(key, false, c);
  231. ImGui_ImplGLUT_UpdateKeyModifiers();
  232. (void)x; (void)y; // Unused
  233. }
  234. void ImGui_ImplGLUT_SpecialFunc(int key, int x, int y)
  235. {
  236. //printf("key_down_func %d\n", key);
  237. ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey(key + 256);
  238. ImGui_ImplGLUT_AddKeyEvent(imgui_key, true, key + 256);
  239. ImGui_ImplGLUT_UpdateKeyModifiers();
  240. (void)x; (void)y; // Unused
  241. }
  242. void ImGui_ImplGLUT_SpecialUpFunc(int key, int x, int y)
  243. {
  244. //printf("key_up_func %d\n", key);
  245. ImGuiKey imgui_key = ImGui_ImplGLUT_KeyToImGuiKey(key + 256);
  246. ImGui_ImplGLUT_AddKeyEvent(imgui_key, false, key + 256);
  247. ImGui_ImplGLUT_UpdateKeyModifiers();
  248. (void)x; (void)y; // Unused
  249. }
  250. void ImGui_ImplGLUT_MouseFunc(int glut_button, int state, int x, int y)
  251. {
  252. ImGuiIO& io = ImGui::GetIO();
  253. io.AddMousePosEvent((float)x, (float)y);
  254. int button = -1;
  255. if (glut_button == GLUT_LEFT_BUTTON) button = 0;
  256. if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
  257. if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
  258. if (button != -1 && (state == GLUT_DOWN || state == GLUT_UP))
  259. io.AddMouseButtonEvent(button, state == GLUT_DOWN);
  260. }
  261. #ifdef __FREEGLUT_EXT_H__
  262. void ImGui_ImplGLUT_MouseWheelFunc(int button, int dir, int x, int y)
  263. {
  264. ImGuiIO& io = ImGui::GetIO();
  265. io.AddMousePosEvent((float)x, (float)y);
  266. if (dir != 0)
  267. io.AddMouseWheelEvent(0.0f, dir > 0 ? 1.0f : -1.0f);
  268. (void)button; // Unused
  269. }
  270. #endif
  271. void ImGui_ImplGLUT_ReshapeFunc(int w, int h)
  272. {
  273. ImGuiIO& io = ImGui::GetIO();
  274. io.DisplaySize = ImVec2((float)w, (float)h);
  275. }
  276. void ImGui_ImplGLUT_MotionFunc(int x, int y)
  277. {
  278. ImGuiIO& io = ImGui::GetIO();
  279. io.AddMousePosEvent((float)x, (float)y);
  280. }
  281. //-----------------------------------------------------------------------------
  282. #endif // #ifndef IMGUI_DISABLE