imgui_impl_glut.cpp 13 KB

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