imgui_impl_sdl2.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // ImGui SDL2 binding with OpenGL3
  2. // (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
  3. // (GL3W is a helper library to access OpenGL functions since there is no standard header to access modern OpenGL functions easily. Alternatives are GLEW, Glad, etc.)
  4. // Implemented features:
  5. // [X] User texture binding. Cast 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  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 use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  8. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  9. // https://github.com/ocornut/imgui
  10. // CHANGELOG
  11. // (minor and older changes stripped away, please see git history for details)
  12. // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
  13. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplSdlGL3_RenderDrawData() in the .h file so you can call it yourself.
  14. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  15. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  16. // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
  17. // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
  18. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  19. // 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS.
  20. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  21. // 2018-01-07: OpenGL: Changed GLSL shader version from 330 to 150.
  22. // 2017-09-01: OpenGL: Save and restore current bound sampler. Save and restore current polygon mode.
  23. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  24. // 2017-05-01: OpenGL: Fixed save and restore of current blend func state.
  25. // 2017-05-01: OpenGL: Fixed save and restore of current GL_ACTIVE_TEXTURE.
  26. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  27. // 2016-09-05: OpenGL: Fixed save and restore of current scissor rectangle.
  28. // 2016-07-29: OpenGL: Explicitly setting GL_UNPACK_ROW_LENGTH to reduce issues because SDL changes it. (#752)
  29. #include "imgui.h"
  30. #include "imgui_impl_sdl2.h"
  31. // SDL,GL3W
  32. #include <SDL.h>
  33. #include <SDL_syswm.h>
  34. // Data
  35. static Uint64 g_Time = 0;
  36. static bool g_MousePressed[3] = { false, false, false };
  37. static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_Count_] = { 0 };
  38. static const char* ImGui_ImplSDL2_GetClipboardText(void*)
  39. {
  40. return SDL_GetClipboardText();
  41. }
  42. static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
  43. {
  44. SDL_SetClipboardText(text);
  45. }
  46. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  47. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  48. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  49. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  50. bool ImGui_ImplSDL2_ProcessEvent(SDL_Event* event)
  51. {
  52. ImGuiIO& io = ImGui::GetIO();
  53. switch (event->type)
  54. {
  55. case SDL_MOUSEWHEEL:
  56. {
  57. if (event->wheel.x > 0) io.MouseWheelH += 1;
  58. if (event->wheel.x < 0) io.MouseWheelH -= 1;
  59. if (event->wheel.y > 0) io.MouseWheel += 1;
  60. if (event->wheel.y < 0) io.MouseWheel -= 1;
  61. return true;
  62. }
  63. case SDL_MOUSEBUTTONDOWN:
  64. {
  65. if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
  66. if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
  67. if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
  68. return true;
  69. }
  70. case SDL_TEXTINPUT:
  71. {
  72. io.AddInputCharactersUTF8(event->text.text);
  73. return true;
  74. }
  75. case SDL_KEYDOWN:
  76. case SDL_KEYUP:
  77. {
  78. int key = event->key.keysym.scancode;
  79. IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
  80. io.KeysDown[key] = (event->type == SDL_KEYDOWN);
  81. io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
  82. io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
  83. io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
  84. io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. bool ImGui_ImplSDL2_Init(SDL_Window* window)
  91. {
  92. // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
  93. ImGuiIO& io = ImGui::GetIO();
  94. io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
  95. io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
  96. io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
  97. io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
  98. io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
  99. io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
  100. io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
  101. io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
  102. io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
  103. io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
  104. io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
  105. io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
  106. io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
  107. io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
  108. io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
  109. io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
  110. io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
  111. io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
  112. io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
  113. io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
  114. io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
  115. io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
  116. io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
  117. io.ClipboardUserData = NULL;
  118. g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  119. g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
  120. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
  121. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
  122. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
  123. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
  124. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
  125. #ifdef _WIN32
  126. SDL_SysWMinfo wmInfo;
  127. SDL_VERSION(&wmInfo.version);
  128. SDL_GetWindowWMInfo(window, &wmInfo);
  129. io.ImeWindowHandle = wmInfo.info.win.window;
  130. #else
  131. (void)window;
  132. #endif
  133. return true;
  134. }
  135. void ImGui_ImplSDL2_Shutdown()
  136. {
  137. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_Count_; cursor_n++)
  138. SDL_FreeCursor(g_MouseCursors[cursor_n]);
  139. }
  140. void ImGui_ImplSDL2_NewFrame(SDL_Window* window)
  141. {
  142. ImGuiIO& io = ImGui::GetIO();
  143. IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
  144. // Setup display size (every frame to accommodate for window resizing)
  145. int w, h;
  146. int display_w, display_h;
  147. SDL_GetWindowSize(window, &w, &h);
  148. SDL_GL_GetDrawableSize(window, &display_w, &display_h);
  149. io.DisplaySize = ImVec2((float)w, (float)h);
  150. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  151. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  152. static Uint64 frequency = SDL_GetPerformanceFrequency();
  153. Uint64 current_time = SDL_GetPerformanceCounter();
  154. io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
  155. g_Time = current_time;
  156. // Setup mouse inputs (we already got mouse wheel, keyboard keys & characters from our event handler)
  157. int mx, my;
  158. Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
  159. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  160. io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  161. io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
  162. io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
  163. g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
  164. // We need to use SDL_CaptureMouse() to easily retrieve mouse coordinates outside of the client area. This is only supported from SDL 2.0.4 (released Jan 2016)
  165. #if (SDL_MAJOR_VERSION >= 2) && (SDL_MINOR_VERSION >= 0) && (SDL_PATCHLEVEL >= 4)
  166. if ((SDL_GetWindowFlags(window) & (SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_MOUSE_CAPTURE)) != 0)
  167. io.MousePos = ImVec2((float)mx, (float)my);
  168. bool any_mouse_button_down = false;
  169. for (int n = 0; n < IM_ARRAYSIZE(io.MouseDown); n++)
  170. any_mouse_button_down |= io.MouseDown[n];
  171. if (any_mouse_button_down && (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_CAPTURE) == 0)
  172. SDL_CaptureMouse(SDL_TRUE);
  173. if (!any_mouse_button_down && (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_CAPTURE) != 0)
  174. SDL_CaptureMouse(SDL_FALSE);
  175. #else
  176. if ((SDL_GetWindowFlags(window) & SDL_WINDOW_INPUT_FOCUS) != 0)
  177. io.MousePos = ImVec2((float)mx, (float)my);
  178. #endif
  179. // Hide OS mouse cursor if ImGui is drawing it
  180. ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
  181. if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
  182. {
  183. SDL_ShowCursor(0);
  184. }
  185. else
  186. {
  187. SDL_SetCursor(g_MouseCursors[cursor]);
  188. SDL_ShowCursor(1);
  189. }
  190. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  191. ImGui::NewFrame();
  192. }