imgui_impl_sdl2.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // ImGui Platform Binding for: SDL2
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. // (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
  4. // Implemented features:
  5. // [X] Multi-viewport windows (when ImGuiConfigFlags_ViewportsEnable is enabled).
  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-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  13. // 2018-XX-XX: Misc: ImGui_ImplSDL2_Init() now takes a SDL_GLContext parameter.
  14. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  15. // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
  16. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  17. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  18. // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
  19. // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
  20. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  21. // 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.
  22. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  23. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  24. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  25. #include "imgui.h"
  26. #include "imgui_impl_sdl2.h"
  27. // SDL
  28. // (the multi-viewports feature requires SDL features supported from SDL 2.0.5+)
  29. #include <SDL.h>
  30. #include <SDL_syswm.h>
  31. #define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4)
  32. #define SDL_HAS_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5)
  33. #define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5)
  34. #define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5)
  35. #define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4)
  36. #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
  37. #define SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH SDL_VERSION_ATLEAST(2,0,5)
  38. #if !SDL_HAS_VULKAN
  39. static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
  40. #endif
  41. // Data
  42. static SDL_Window* g_Window = NULL;
  43. static Uint64 g_Time = 0;
  44. static bool g_MousePressed[3] = { false, false, false };
  45. static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_Count_] = { 0 };
  46. // Forward Declarations
  47. static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context);
  48. static void ImGui_ImplSDL2_ShutdownPlatformInterface();
  49. static const char* ImGui_ImplSDL2_GetClipboardText(void*)
  50. {
  51. return SDL_GetClipboardText();
  52. }
  53. static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
  54. {
  55. SDL_SetClipboardText(text);
  56. }
  57. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  58. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  59. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  60. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  61. bool ImGui_ImplSDL2_ProcessEvent(SDL_Event* event)
  62. {
  63. ImGuiIO& io = ImGui::GetIO();
  64. switch (event->type)
  65. {
  66. case SDL_MOUSEWHEEL:
  67. {
  68. if (event->wheel.x > 0) io.MouseWheelH += 1;
  69. if (event->wheel.x < 0) io.MouseWheelH -= 1;
  70. if (event->wheel.y > 0) io.MouseWheel += 1;
  71. if (event->wheel.y < 0) io.MouseWheel -= 1;
  72. return true;
  73. }
  74. case SDL_MOUSEBUTTONDOWN:
  75. {
  76. if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
  77. if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
  78. if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
  79. return true;
  80. }
  81. case SDL_TEXTINPUT:
  82. {
  83. io.AddInputCharactersUTF8(event->text.text);
  84. return true;
  85. }
  86. case SDL_KEYDOWN:
  87. case SDL_KEYUP:
  88. {
  89. int key = event->key.keysym.scancode;
  90. IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
  91. io.KeysDown[key] = (event->type == SDL_KEYDOWN);
  92. io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
  93. io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
  94. io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
  95. io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
  96. return true;
  97. }
  98. // Multi-viewport support
  99. case SDL_WINDOWEVENT:
  100. Uint8 window_event = event->window.event;
  101. if (window_event == SDL_WINDOWEVENT_CLOSE || window_event == SDL_WINDOWEVENT_MOVED || window_event == SDL_WINDOWEVENT_RESIZED)
  102. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)SDL_GetWindowFromID(event->window.windowID)))
  103. {
  104. if (window_event == SDL_WINDOWEVENT_CLOSE)
  105. viewport->PlatformRequestClose = true;
  106. if (window_event == SDL_WINDOWEVENT_MOVED)
  107. viewport->PlatformRequestMove = true;
  108. if (window_event == SDL_WINDOWEVENT_RESIZED)
  109. viewport->PlatformRequestResize = true;
  110. return true;
  111. }
  112. break;
  113. }
  114. return false;
  115. }
  116. bool ImGui_ImplSDL2_Init(SDL_Window* window, void* sdl_gl_context)
  117. {
  118. g_Window = window;
  119. // Setup back-end capabilities flags
  120. ImGuiIO& io = ImGui::GetIO();
  121. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  122. #if SDL_HAS_CAPTURE_MOUSE
  123. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  124. #endif
  125. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
  126. io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
  127. io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
  128. io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
  129. io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
  130. io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
  131. io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
  132. io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
  133. io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
  134. io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
  135. io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
  136. io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
  137. io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
  138. io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
  139. io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
  140. io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
  141. io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
  142. io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
  143. io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
  144. io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
  145. io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
  146. io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
  147. io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
  148. io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
  149. io.ClipboardUserData = NULL;
  150. g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  151. g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
  152. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
  153. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
  154. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
  155. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
  156. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
  157. #ifdef _WIN32
  158. SDL_SysWMinfo wmInfo;
  159. SDL_VERSION(&wmInfo.version);
  160. SDL_GetWindowWMInfo(window, &wmInfo);
  161. io.ImeWindowHandle = wmInfo.info.win.window;
  162. #endif
  163. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  164. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  165. main_viewport->PlatformHandle = (void*)window;
  166. // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
  167. // We left the call to ImGui_ImplSDL2_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
  168. if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports))
  169. ImGui_ImplSDL2_InitPlatformInterface(window, sdl_gl_context);
  170. return true;
  171. }
  172. void ImGui_ImplSDL2_Shutdown()
  173. {
  174. ImGui_ImplSDL2_ShutdownPlatformInterface();
  175. g_Window = NULL;
  176. // Destroy SDL mouse cursors
  177. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_Count_; cursor_n++)
  178. SDL_FreeCursor(g_MouseCursors[cursor_n]);
  179. memset(g_MouseCursors, 0, sizeof(g_MouseCursors));
  180. }
  181. static void ImGui_ImplSDL2_UpdateMouse()
  182. {
  183. ImGuiIO& io = ImGui::GetIO();
  184. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  185. io.MousePosViewport = 0;
  186. io.MouseHoveredViewport = 0;
  187. int mx, my;
  188. Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
  189. 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.
  190. io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
  191. io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
  192. g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
  193. #if SDL_HAS_CAPTURE_MOUSE
  194. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  195. if (focused_window)
  196. {
  197. // SDL_GetMouseState() gives me mouse position seemingly based on the last window entered/focused(?)
  198. // The creation of new window and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally.
  199. int wx, wy;
  200. SDL_GetWindowPosition(focused_window, &wx, &wy);
  201. SDL_GetGlobalMouseState(&mx, &my);
  202. mx -= wx;
  203. my -= wy;
  204. }
  205. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)focused_window))
  206. {
  207. io.MousePos = ImVec2(viewport->Pos.x + (float)mx, viewport->Pos.y + (float)my);
  208. io.MousePosViewport = viewport->ID;
  209. }
  210. // We already retrieve global mouse position, SDL_CaptureMouse() also let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't trigger the OS window resize cursor
  211. // The function is only supported from SDL 2.0.4 (released Jan 2016)
  212. bool any_mouse_button_down = ImGui::IsAnyMouseDown();
  213. SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE);
  214. #else
  215. if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS)
  216. io.MousePos = ImVec2((float)mx, (float)my);
  217. #endif
  218. // Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
  219. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) == 0)
  220. {
  221. ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
  222. if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
  223. {
  224. SDL_ShowCursor(SDL_FALSE);
  225. }
  226. else
  227. {
  228. SDL_SetCursor(g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
  229. SDL_ShowCursor(SDL_TRUE);
  230. }
  231. }
  232. }
  233. void ImGui_ImplSDL2_NewFrame(SDL_Window* window)
  234. {
  235. ImGuiIO& io = ImGui::GetIO();
  236. IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
  237. // Setup display size (every frame to accommodate for window resizing)
  238. int w, h;
  239. int display_w, display_h;
  240. SDL_GetWindowSize(window, &w, &h);
  241. SDL_GL_GetDrawableSize(window, &display_w, &display_h);
  242. io.DisplaySize = ImVec2((float)w, (float)h);
  243. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  244. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  245. static Uint64 frequency = SDL_GetPerformanceFrequency();
  246. Uint64 current_time = SDL_GetPerformanceCounter();
  247. io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
  248. g_Time = current_time;
  249. ImGui_ImplSDL2_UpdateMouse();
  250. // 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.
  251. ImGui::NewFrame();
  252. }
  253. //--------------------------------------------------------------------------------------------------------
  254. // Platform Interface (Optional, for multi-viewport support)
  255. //--------------------------------------------------------------------------------------------------------
  256. struct ImGuiViewportDataSDL2
  257. {
  258. SDL_Window* Window;
  259. Uint32 WindowID;
  260. bool WindowOwned;
  261. SDL_GLContext GLContext;
  262. ImGuiViewportDataSDL2() { Window = NULL; WindowID = 0; WindowOwned = false; GLContext = NULL; }
  263. ~ImGuiViewportDataSDL2() { IM_ASSERT(Window == NULL && GLContext == NULL); }
  264. };
  265. static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport)
  266. {
  267. ImGuiViewportDataSDL2* data = IM_NEW(ImGuiViewportDataSDL2)();
  268. viewport->PlatformUserData = data;
  269. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  270. ImGuiViewportDataSDL2* main_viewport_data = (ImGuiViewportDataSDL2*)main_viewport->PlatformUserData;
  271. // Share GL resources with main context
  272. bool use_opengl = (main_viewport_data->GLContext != NULL);
  273. SDL_GLContext backup_context = NULL;
  274. if (use_opengl)
  275. {
  276. backup_context = SDL_GL_GetCurrentContext();
  277. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  278. SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
  279. }
  280. // We don't enable SDL_WINDOW_RESIZABLE because it enforce windows decorations
  281. Uint32 sdl_flags = 0;
  282. sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : SDL_WINDOW_VULKAN;
  283. sdl_flags |= SDL_WINDOW_HIDDEN;
  284. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
  285. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
  286. #if SDL_HAS_ALWAYS_ON_TOP
  287. sdl_flags |= (viewport->Flags & imGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
  288. #endif
  289. data->Window = SDL_CreateWindow("No Title Yet", (int)viewport->Pos.x, (int)viewport->Pos.y, (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags);
  290. data->WindowOwned = true;
  291. if (use_opengl)
  292. data->GLContext = SDL_GL_CreateContext(data->Window);
  293. if (use_opengl && backup_context)
  294. SDL_GL_MakeCurrent(data->Window, backup_context);
  295. viewport->PlatformHandle = (void*)data->Window;
  296. }
  297. static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport)
  298. {
  299. if (ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData)
  300. {
  301. if (data->GLContext && data->WindowOwned)
  302. SDL_GL_DeleteContext(data->GLContext);
  303. if (data->Window && data->WindowOwned)
  304. SDL_DestroyWindow(data->Window);
  305. data->GLContext = NULL;
  306. data->Window = NULL;
  307. IM_DELETE(data);
  308. }
  309. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  310. }
  311. static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport)
  312. {
  313. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  314. #if defined(_WIN32)
  315. SDL_SysWMinfo info;
  316. SDL_VERSION(&info.version);
  317. if (SDL_GetWindowWMInfo(data->Window, &info))
  318. {
  319. HWND hwnd = info.info.win.window;
  320. // SDL hack: Hide icon from task bar
  321. // Note: SDL 2.0.6+ has a SDL_WINDOW_SKIP_TASKBAR flag which is supported under Windows but the way it create the window breaks our seamless transition.
  322. if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
  323. {
  324. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  325. ex_style &= ~WS_EX_APPWINDOW;
  326. ex_style |= WS_EX_TOOLWINDOW;
  327. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  328. }
  329. // SDL hack: SDL always activate/focus windows :/
  330. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  331. {
  332. ::ShowWindow(hwnd, SW_SHOWNA);
  333. return;
  334. }
  335. }
  336. #endif
  337. SDL_ShowWindow(data->Window);
  338. }
  339. static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport)
  340. {
  341. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  342. int x = 0, y = 0;
  343. SDL_GetWindowPosition(data->Window, &x, &y);
  344. return ImVec2((float)x, (float)y);
  345. }
  346. static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  347. {
  348. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  349. SDL_SetWindowPosition(data->Window, (int)pos.x, (int)pos.y);
  350. }
  351. static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport)
  352. {
  353. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  354. int w = 0, h = 0;
  355. SDL_GetWindowSize(data->Window, &w, &h);
  356. return ImVec2((float)w, (float)h);
  357. }
  358. static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  359. {
  360. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  361. SDL_SetWindowSize(data->Window, (int)size.x, (int)size.y);
  362. }
  363. static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  364. {
  365. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  366. SDL_SetWindowTitle(data->Window, title);
  367. }
  368. static void ImGui_ImplSDL2_RenderWindow(ImGuiViewport* viewport, void*)
  369. {
  370. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  371. if (data->GLContext)
  372. SDL_GL_MakeCurrent(data->Window, data->GLContext);
  373. }
  374. static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport, void*)
  375. {
  376. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  377. if (data->GLContext)
  378. {
  379. SDL_GL_MakeCurrent(data->Window, data->GLContext);
  380. SDL_GL_SwapWindow(data->Window);
  381. }
  382. }
  383. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  384. // SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this.
  385. #if SDL_HAS_VULKAN
  386. #include <SDL_vulkan.h>
  387. static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  388. {
  389. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  390. (void)vk_allocator;
  391. SDL_bool ret = SDL_Vulkan_CreateSurface(data->Window, (VkInstance)vk_instance, (VkSurfaceKHR*)out_vk_surface);
  392. return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
  393. }
  394. #endif // SDL_HAS_VULKAN
  395. // FIXME-PLATFORM: Update monitor list when changed?
  396. static void ImGui_ImplSDL2_UpdateMonitors()
  397. {
  398. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  399. platform_io.Monitors.resize(0);
  400. int display_count = SDL_GetNumVideoDisplays();
  401. for (int n = 0; n < display_count; n++)
  402. {
  403. // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime.
  404. ImGuiPlatformMonitor monitor;
  405. SDL_Rect r;
  406. SDL_GetDisplayBounds(n, &r);
  407. monitor.FullMin = monitor.WorkMin = ImVec2((float)(r.x), (float)(r.y));
  408. monitor.FullMax = monitor.WorkMax = ImVec2((float)(r.x + r.w), (float)(r.y + r.h));
  409. #if SDL_HAS_USABLE_DISPLAY_BOUNDS
  410. SDL_GetDisplayUsableBounds(n, &r);
  411. monitor.WorkMin = ImVec2((float)(r.x), (float)(r.y));
  412. monitor.WorkMax = ImVec2((float)(r.x + r.w), (float)(r.y + r.h));
  413. #endif
  414. #if SDL_HAS_PER_MONITOR_DPI
  415. float dpi = 0.0f;
  416. if (SDL_GetDisplayDPI(n, &dpi, NULL, NULL))
  417. monitor.DpiScale = dpi / 96.0f;
  418. #endif
  419. platform_io.Monitors.push_back(monitor);
  420. }
  421. }
  422. static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context)
  423. {
  424. // Register platform interface (will be coupled with a renderer interface)
  425. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  426. platform_io.Platform_CreateWindow = ImGui_ImplSDL2_CreateWindow;
  427. platform_io.Platform_DestroyWindow = ImGui_ImplSDL2_DestroyWindow;
  428. platform_io.Platform_ShowWindow = ImGui_ImplSDL2_ShowWindow;
  429. platform_io.Platform_SetWindowPos = ImGui_ImplSDL2_SetWindowPos;
  430. platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos;
  431. platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize;
  432. platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize;
  433. platform_io.Platform_SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle;
  434. platform_io.Platform_RenderWindow = ImGui_ImplSDL2_RenderWindow;
  435. platform_io.Platform_SwapBuffers = ImGui_ImplSDL2_SwapBuffers;
  436. #if SDL_HAS_VULKAN
  437. platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface;
  438. #endif
  439. // SDL2 by default doesn't pass mouse clicks to the application when the click focused a window. This is getting in the way of our interactions and we disable that behavior.
  440. #if SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH
  441. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  442. #endif
  443. ImGui_ImplSDL2_UpdateMonitors();
  444. // Register main window handle (which is owned by the main application, not by us)
  445. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  446. ImGuiViewportDataSDL2* data = IM_NEW(ImGuiViewportDataSDL2)();
  447. data->Window = window;
  448. data->WindowID = SDL_GetWindowID(window);
  449. data->WindowOwned = false;
  450. data->GLContext = sdl_gl_context;
  451. main_viewport->PlatformUserData = data;
  452. main_viewport->PlatformHandle = data->Window;
  453. }
  454. static void ImGui_ImplSDL2_ShutdownPlatformInterface()
  455. {
  456. }