imgui_impl_freeglut.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // ImGui Platform Binding for: FreeGLUT
  2. // This needs to be used along with a Renderer (e.g. OpenGL2)
  3. // Issues:
  4. // [ ] Platform: GLUT is unable to distinguish e.g. Backspace from CTRL+H or TAB from CTRL+I
  5. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  6. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  7. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  8. // https://github.com/ocornut/imgui
  9. // CHANGELOG
  10. // (minor and older changes stripped away, please see git history for details)
  11. // 2018-03-22: Added FreeGLUT Platform binding.
  12. #include "imgui.h"
  13. #include "imgui_impl_freeglut.h"
  14. #include <GL/freeglut.h>
  15. #ifdef _MSC_VER
  16. #pragma warning (disable: 4505) // unreferenced local function has been removed (stb stuff)
  17. #endif
  18. static int g_Time = 0; // Current time, in milliseconds
  19. bool ImGui_ImplFreeGLUT_Init()
  20. {
  21. ImGuiIO& io = ImGui::GetIO();
  22. g_Time = 0;
  23. // Glut has 1 function for characters and one for "special keys". We map the characters in the 0..255 range and the keys above.
  24. io.KeyMap[ImGuiKey_Tab] = '\t'; // == 9 == CTRL+I
  25. io.KeyMap[ImGuiKey_LeftArrow] = 256 + GLUT_KEY_LEFT;
  26. io.KeyMap[ImGuiKey_RightArrow] = 256 + GLUT_KEY_RIGHT;
  27. io.KeyMap[ImGuiKey_UpArrow] = 256 + GLUT_KEY_UP;
  28. io.KeyMap[ImGuiKey_DownArrow] = 256 + GLUT_KEY_DOWN;
  29. io.KeyMap[ImGuiKey_PageUp] = 256 + GLUT_KEY_PAGE_UP;
  30. io.KeyMap[ImGuiKey_PageDown] = 256 + GLUT_KEY_PAGE_DOWN;
  31. io.KeyMap[ImGuiKey_Home] = 256 + GLUT_KEY_HOME;
  32. io.KeyMap[ImGuiKey_End] = 256 + GLUT_KEY_END;
  33. io.KeyMap[ImGuiKey_Insert] = 256 + GLUT_KEY_INSERT;
  34. io.KeyMap[ImGuiKey_Delete] = 127;
  35. io.KeyMap[ImGuiKey_Backspace] = 8; // == CTRL+H
  36. io.KeyMap[ImGuiKey_Space] = ' ';
  37. io.KeyMap[ImGuiKey_Enter] = 13; // == CTRL+M
  38. io.KeyMap[ImGuiKey_Escape] = 27;
  39. io.KeyMap[ImGuiKey_A] = 'A';
  40. io.KeyMap[ImGuiKey_C] = 'C';
  41. io.KeyMap[ImGuiKey_V] = 'V';
  42. io.KeyMap[ImGuiKey_X] = 'X';
  43. io.KeyMap[ImGuiKey_Y] = 'Y';
  44. io.KeyMap[ImGuiKey_Z] = 'Z';
  45. return true;
  46. }
  47. void ImGui_ImplFreeGLUT_InstallFuncs()
  48. {
  49. glutReshapeFunc(ImGui_ImplFreeGLUT_ReshapeFunc);
  50. glutMotionFunc(ImGui_ImplFreeGLUT_MotionFunc);
  51. glutPassiveMotionFunc(ImGui_ImplFreeGLUT_MotionFunc);
  52. glutMouseFunc(ImGui_ImplFreeGLUT_MouseFunc);
  53. glutMouseWheelFunc(ImGui_ImplFreeGLUT_MouseWheelFunc);
  54. glutKeyboardFunc(ImGui_ImplFreeGLUT_KeyboardFunc);
  55. glutKeyboardUpFunc(ImGui_ImplFreeGLUT_KeyboardUpFunc);
  56. glutSpecialFunc(ImGui_ImplFreeGLUT_SpecialFunc);
  57. glutSpecialUpFunc(ImGui_ImplFreeGLUT_SpecialUpFunc);
  58. }
  59. void ImGui_ImplFreeGLUT_Shutdown()
  60. {
  61. }
  62. void ImGui_ImplFreeGLUT_NewFrame()
  63. {
  64. // Setup time step
  65. ImGuiIO& io = ImGui::GetIO();
  66. int current_time = glutGet(GLUT_ELAPSED_TIME);
  67. io.DeltaTime = (current_time - g_Time) / 1000.0f;
  68. g_Time = current_time;
  69. // Start the frame
  70. ImGui::NewFrame();
  71. }
  72. static void ImGui_ImplFreeGLUT_UpdateKeyboardMods()
  73. {
  74. ImGuiIO& io = ImGui::GetIO();
  75. int mods = glutGetModifiers();
  76. io.KeyCtrl = (mods & GLUT_ACTIVE_CTRL) != 0;
  77. io.KeyShift = (mods & GLUT_ACTIVE_SHIFT) != 0;
  78. io.KeyAlt = (mods & GLUT_ACTIVE_ALT) != 0;
  79. }
  80. void ImGui_ImplFreeGLUT_KeyboardFunc(unsigned char c, int x, int y)
  81. {
  82. // Send character to imgui
  83. //printf("char_down_func %d '%c'\n", c, c);
  84. ImGuiIO& io = ImGui::GetIO();
  85. if (c >= 32)
  86. io.AddInputCharacter(c);
  87. // Store letters in KeysDown[] array as both uppercase and lowercase + Handle GLUT translating CTRL+A..CTRL+Z as 1..26.
  88. // This is a hacky mess but GLUT is unable to distinguish e.g. a TAB key from CTRL+I so this is probably the best we can do here.
  89. if (c >= 1 && c <= 26)
  90. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = true;
  91. else if (c >= 'a' && c <= 'z')
  92. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = true;
  93. else if (c >= 'A' && c <= 'Z')
  94. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = true;
  95. else
  96. io.KeysDown[c] = true;
  97. ImGui_ImplFreeGLUT_UpdateKeyboardMods();
  98. (void)x; (void)y; // Unused
  99. }
  100. void ImGui_ImplFreeGLUT_KeyboardUpFunc(unsigned char c, int x, int y)
  101. {
  102. //printf("char_up_func %d '%c'\n", c, c);
  103. ImGuiIO& io = ImGui::GetIO();
  104. if (c >= 1 && c <= 26)
  105. io.KeysDown[c] = io.KeysDown[c - 1 + 'a'] = io.KeysDown[c - 1 + 'A'] = false;
  106. else if (c >= 'a' && c <= 'z')
  107. io.KeysDown[c] = io.KeysDown[c - 'a' + 'A'] = false;
  108. else if (c >= 'A' && c <= 'Z')
  109. io.KeysDown[c] = io.KeysDown[c - 'A' + 'a'] = false;
  110. else
  111. io.KeysDown[c] = false;
  112. ImGui_ImplFreeGLUT_UpdateKeyboardMods();
  113. (void)x; (void)y; // Unused
  114. }
  115. void ImGui_ImplFreeGLUT_SpecialFunc(int key, int x, int y)
  116. {
  117. //printf("key_down_func %d\n", key);
  118. ImGuiIO& io = ImGui::GetIO();
  119. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  120. io.KeysDown[key + 256] = true;
  121. ImGui_ImplFreeGLUT_UpdateKeyboardMods();
  122. (void)x; (void)y; // Unused
  123. }
  124. void ImGui_ImplFreeGLUT_SpecialUpFunc(int key, int x, int y)
  125. {
  126. //printf("key_up_func %d\n", key);
  127. ImGuiIO& io = ImGui::GetIO();
  128. if (key + 256 < IM_ARRAYSIZE(io.KeysDown))
  129. io.KeysDown[key + 256] = false;
  130. ImGui_ImplFreeGLUT_UpdateKeyboardMods();
  131. (void)x; (void)y; // Unused
  132. }
  133. void ImGui_ImplFreeGLUT_MouseFunc(int glut_button, int state, int x, int y)
  134. {
  135. ImGuiIO& io = ImGui::GetIO();
  136. io.MousePos = ImVec2((float)x, (float)y);
  137. int button = -1;
  138. if (glut_button == GLUT_LEFT_BUTTON) button = 0;
  139. if (glut_button == GLUT_RIGHT_BUTTON) button = 1;
  140. if (glut_button == GLUT_MIDDLE_BUTTON) button = 2;
  141. if (button != -1 && state == GLUT_DOWN)
  142. io.MouseDown[button] = true;
  143. if (button != -1 && state == GLUT_UP)
  144. io.MouseDown[button] = false;
  145. }
  146. void ImGui_ImplFreeGLUT_MouseWheelFunc(int button, int dir, int x, int y)
  147. {
  148. ImGuiIO& io = ImGui::GetIO();
  149. io.MousePos = ImVec2((float)x, (float)y);
  150. if (dir > 0)
  151. io.MouseWheel += 1.0;
  152. else if (dir < 0)
  153. io.MouseWheel -= 1.0;
  154. (void)button; // Unused
  155. }
  156. void ImGui_ImplFreeGLUT_ReshapeFunc(int w, int h)
  157. {
  158. ImGuiIO& io = ImGui::GetIO();
  159. io.DisplaySize = ImVec2((float)w, (float)h);
  160. }
  161. void ImGui_ImplFreeGLUT_MotionFunc(int x, int y)
  162. {
  163. ImGuiIO& io = ImGui::GetIO();
  164. io.MousePos = ImVec2((float)x, (float)y);
  165. }