imgui_impl_glut.cpp 13 KB

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