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