imgui_impl_freeglut.cpp 6.6 KB

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