imgui_impl_glut.cpp 13 KB

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