imgui_impl_sdl.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. // dear 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] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  6. // [X] Platform: Clipboard support.
  7. // [X] Platform: Keyboard arrays indexed using SDL_SCANCODE_* codes, e.g. ImGui::IsKeyPressed(SDL_SCANCODE_SPACE).
  8. // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
  9. // Missing features:
  10. // [ ] Platform: SDL2 handling of IME under Windows appears to be broken and it explicitly disable the regular Windows IME. You can restore Windows IME by compiling SDL with SDL_DISABLE_WINDOWS_IME.
  11. // [ ] Platform: Gamepad support (need to use SDL_GameController API to fill the io.NavInputs[] value when ImGuiConfigFlags_NavEnableGamepad is set).
  12. // [ ] Platform: Multi-viewport + Minimized windows seems to break mouse wheel events (at least under Windows).
  13. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  14. // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
  15. // https://github.com/ocornut/imgui
  16. // CHANGELOG
  17. // (minor and older changes stripped away, please see git history for details)
  18. // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  19. // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
  20. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  21. // 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples.
  22. // 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter.
  23. // 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText).
  24. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  25. // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
  26. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  27. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  28. // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
  29. // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
  30. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  31. // 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.
  32. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  33. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  34. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  35. #include "imgui.h"
  36. #include "imgui_impl_sdl.h"
  37. // SDL
  38. // (the multi-viewports feature requires SDL features supported from SDL 2.0.5+)
  39. #include <SDL.h>
  40. #include <SDL_syswm.h>
  41. #define SDL_HAS_WARP_MOUSE_GLOBAL SDL_VERSION_ATLEAST(2,0,4)
  42. #define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4)
  43. #define SDL_HAS_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5)
  44. #define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5)
  45. #define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5)
  46. #define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4)
  47. #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
  48. #define SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH SDL_VERSION_ATLEAST(2,0,5)
  49. #if !SDL_HAS_VULKAN
  50. static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
  51. #endif
  52. // Data
  53. static SDL_Window* g_Window = NULL;
  54. static Uint64 g_Time = 0;
  55. static bool g_MousePressed[3] = { false, false, false };
  56. static SDL_Cursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { 0 };
  57. static char* g_ClipboardTextData = NULL;
  58. // Forward Declarations
  59. static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context);
  60. static void ImGui_ImplSDL2_ShutdownPlatformInterface();
  61. static const char* ImGui_ImplSDL2_GetClipboardText(void*)
  62. {
  63. if (g_ClipboardTextData)
  64. SDL_free(g_ClipboardTextData);
  65. g_ClipboardTextData = SDL_GetClipboardText();
  66. return g_ClipboardTextData;
  67. }
  68. static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
  69. {
  70. SDL_SetClipboardText(text);
  71. }
  72. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  73. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  74. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  75. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  76. bool ImGui_ImplSDL2_ProcessEvent(SDL_Event* event)
  77. {
  78. ImGuiIO& io = ImGui::GetIO();
  79. switch (event->type)
  80. {
  81. case SDL_MOUSEWHEEL:
  82. {
  83. if (event->wheel.x > 0) io.MouseWheelH += 1;
  84. if (event->wheel.x < 0) io.MouseWheelH -= 1;
  85. if (event->wheel.y > 0) io.MouseWheel += 1;
  86. if (event->wheel.y < 0) io.MouseWheel -= 1;
  87. return true;
  88. }
  89. case SDL_MOUSEBUTTONDOWN:
  90. {
  91. if (event->button.button == SDL_BUTTON_LEFT) g_MousePressed[0] = true;
  92. if (event->button.button == SDL_BUTTON_RIGHT) g_MousePressed[1] = true;
  93. if (event->button.button == SDL_BUTTON_MIDDLE) g_MousePressed[2] = true;
  94. return true;
  95. }
  96. case SDL_TEXTINPUT:
  97. {
  98. io.AddInputCharactersUTF8(event->text.text);
  99. return true;
  100. }
  101. case SDL_KEYDOWN:
  102. case SDL_KEYUP:
  103. {
  104. int key = event->key.keysym.scancode;
  105. IM_ASSERT(key >= 0 && key < IM_ARRAYSIZE(io.KeysDown));
  106. io.KeysDown[key] = (event->type == SDL_KEYDOWN);
  107. io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
  108. io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
  109. io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
  110. io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
  111. return true;
  112. }
  113. // Multi-viewport support
  114. case SDL_WINDOWEVENT:
  115. Uint8 window_event = event->window.event;
  116. if (window_event == SDL_WINDOWEVENT_CLOSE || window_event == SDL_WINDOWEVENT_MOVED || window_event == SDL_WINDOWEVENT_RESIZED)
  117. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)SDL_GetWindowFromID(event->window.windowID)))
  118. {
  119. if (window_event == SDL_WINDOWEVENT_CLOSE)
  120. viewport->PlatformRequestClose = true;
  121. if (window_event == SDL_WINDOWEVENT_MOVED)
  122. viewport->PlatformRequestMove = true;
  123. if (window_event == SDL_WINDOWEVENT_RESIZED)
  124. viewport->PlatformRequestResize = true;
  125. return true;
  126. }
  127. break;
  128. }
  129. return false;
  130. }
  131. static bool ImGui_ImplSDL2_Init(SDL_Window* window, void* sdl_gl_context)
  132. {
  133. g_Window = window;
  134. // Setup back-end capabilities flags
  135. ImGuiIO& io = ImGui::GetIO();
  136. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  137. #if SDL_HAS_WARP_MOUSE_GLOBAL
  138. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  139. #endif
  140. #if SDL_HAS_CAPTURE_MOUSE
  141. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  142. #endif
  143. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
  144. io.KeyMap[ImGuiKey_Tab] = SDL_SCANCODE_TAB;
  145. io.KeyMap[ImGuiKey_LeftArrow] = SDL_SCANCODE_LEFT;
  146. io.KeyMap[ImGuiKey_RightArrow] = SDL_SCANCODE_RIGHT;
  147. io.KeyMap[ImGuiKey_UpArrow] = SDL_SCANCODE_UP;
  148. io.KeyMap[ImGuiKey_DownArrow] = SDL_SCANCODE_DOWN;
  149. io.KeyMap[ImGuiKey_PageUp] = SDL_SCANCODE_PAGEUP;
  150. io.KeyMap[ImGuiKey_PageDown] = SDL_SCANCODE_PAGEDOWN;
  151. io.KeyMap[ImGuiKey_Home] = SDL_SCANCODE_HOME;
  152. io.KeyMap[ImGuiKey_End] = SDL_SCANCODE_END;
  153. io.KeyMap[ImGuiKey_Insert] = SDL_SCANCODE_INSERT;
  154. io.KeyMap[ImGuiKey_Delete] = SDL_SCANCODE_DELETE;
  155. io.KeyMap[ImGuiKey_Backspace] = SDL_SCANCODE_BACKSPACE;
  156. io.KeyMap[ImGuiKey_Space] = SDL_SCANCODE_SPACE;
  157. io.KeyMap[ImGuiKey_Enter] = SDL_SCANCODE_RETURN;
  158. io.KeyMap[ImGuiKey_Escape] = SDL_SCANCODE_ESCAPE;
  159. io.KeyMap[ImGuiKey_A] = SDL_SCANCODE_A;
  160. io.KeyMap[ImGuiKey_C] = SDL_SCANCODE_C;
  161. io.KeyMap[ImGuiKey_V] = SDL_SCANCODE_V;
  162. io.KeyMap[ImGuiKey_X] = SDL_SCANCODE_X;
  163. io.KeyMap[ImGuiKey_Y] = SDL_SCANCODE_Y;
  164. io.KeyMap[ImGuiKey_Z] = SDL_SCANCODE_Z;
  165. io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
  166. io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
  167. io.ClipboardUserData = NULL;
  168. g_MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  169. g_MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
  170. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
  171. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
  172. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
  173. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
  174. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
  175. g_MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
  176. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  177. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  178. main_viewport->PlatformHandle = (void*)window;
  179. // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
  180. // We left the call to ImGui_ImplSDL2_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
  181. if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports))
  182. ImGui_ImplSDL2_InitPlatformInterface(window, sdl_gl_context);
  183. return true;
  184. }
  185. bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
  186. {
  187. (void)sdl_gl_context; // Viewport branch will need this.
  188. return ImGui_ImplSDL2_Init(window, sdl_gl_context);
  189. }
  190. bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
  191. {
  192. #if !SDL_HAS_VULKAN
  193. IM_ASSERT(0 && "Unsupported");
  194. #endif
  195. return ImGui_ImplSDL2_Init(window, NULL);
  196. }
  197. void ImGui_ImplSDL2_Shutdown()
  198. {
  199. ImGui_ImplSDL2_ShutdownPlatformInterface();
  200. g_Window = NULL;
  201. // Destroy last known clipboard data
  202. if (g_ClipboardTextData)
  203. SDL_free(g_ClipboardTextData);
  204. g_ClipboardTextData = NULL;
  205. // Destroy SDL mouse cursors
  206. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  207. SDL_FreeCursor(g_MouseCursors[cursor_n]);
  208. memset(g_MouseCursors, 0, sizeof(g_MouseCursors));
  209. }
  210. static void ImGui_ImplSDL2_UpdateMousePosAndButtons()
  211. {
  212. ImGuiIO& io = ImGui::GetIO();
  213. io.MouseHoveredViewport = 0;
  214. // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  215. // (When multi-viewports are enabled, all imgui positions are same as OS positions.)
  216. #if SDL_HAS_WARP_MOUSE_GLOBAL
  217. if (!io.WantSetMousePos)
  218. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  219. else if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
  220. SDL_WarpMouseInWindow(g_Window, (int)io.MousePos.x, (int)io.MousePos.y);
  221. else
  222. SDL_WarpMouseGlobal((int)io.MousePos.x, (int)io.MousePos.y);
  223. #else
  224. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  225. #endif
  226. int mx, my;
  227. Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my);
  228. 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.
  229. io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0;
  230. io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0;
  231. g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false;
  232. #if SDL_HAS_CAPTURE_MOUSE && !defined(__EMSCRIPTEN__)
  233. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  234. if (focused_window)
  235. {
  236. // SDL_GetMouseState() gives mouse position seemingly based on the last window entered/focused(?)
  237. // The creation of new windows at runtime and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally.
  238. int wx, wy;
  239. SDL_GetWindowPosition(focused_window, &wx, &wy);
  240. SDL_GetGlobalMouseState(&mx, &my);
  241. mx -= wx;
  242. my -= wy;
  243. }
  244. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)focused_window))
  245. io.MousePos = ImVec2(viewport->Pos.x + (float)mx, viewport->Pos.y + (float)my);
  246. // 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
  247. // The function is only supported from SDL 2.0.4 (released Jan 2016)
  248. bool any_mouse_button_down = ImGui::IsAnyMouseDown();
  249. SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE);
  250. #else
  251. if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS)
  252. io.MousePos = ImVec2((float)mx, (float)my);
  253. #endif
  254. }
  255. static void ImGui_ImplSDL2_UpdateMouseCursor()
  256. {
  257. ImGuiIO& io = ImGui::GetIO();
  258. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  259. return;
  260. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  261. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  262. {
  263. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  264. SDL_ShowCursor(SDL_FALSE);
  265. }
  266. else
  267. {
  268. // Show OS mouse cursor
  269. SDL_SetCursor(g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
  270. SDL_ShowCursor(SDL_TRUE);
  271. }
  272. }
  273. void ImGui_ImplSDL2_NewFrame(SDL_Window* window)
  274. {
  275. ImGuiIO& io = ImGui::GetIO();
  276. IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
  277. // Setup display size (every frame to accommodate for window resizing)
  278. int w, h;
  279. int display_w, display_h;
  280. SDL_GetWindowSize(window, &w, &h);
  281. SDL_GL_GetDrawableSize(window, &display_w, &display_h);
  282. io.DisplaySize = ImVec2((float)w, (float)h);
  283. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  284. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  285. static Uint64 frequency = SDL_GetPerformanceFrequency();
  286. Uint64 current_time = SDL_GetPerformanceCounter();
  287. io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f);
  288. g_Time = current_time;
  289. ImGui_ImplSDL2_UpdateMousePosAndButtons();
  290. ImGui_ImplSDL2_UpdateMouseCursor();
  291. }
  292. //--------------------------------------------------------------------------------------------------------
  293. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  294. // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.
  295. // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
  296. //--------------------------------------------------------------------------------------------------------
  297. struct ImGuiViewportDataSDL2
  298. {
  299. SDL_Window* Window;
  300. Uint32 WindowID;
  301. bool WindowOwned;
  302. SDL_GLContext GLContext;
  303. ImGuiViewportDataSDL2() { Window = NULL; WindowID = 0; WindowOwned = false; GLContext = NULL; }
  304. ~ImGuiViewportDataSDL2() { IM_ASSERT(Window == NULL && GLContext == NULL); }
  305. };
  306. static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport)
  307. {
  308. ImGuiViewportDataSDL2* data = IM_NEW(ImGuiViewportDataSDL2)();
  309. viewport->PlatformUserData = data;
  310. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  311. ImGuiViewportDataSDL2* main_viewport_data = (ImGuiViewportDataSDL2*)main_viewport->PlatformUserData;
  312. // Share GL resources with main context
  313. bool use_opengl = (main_viewport_data->GLContext != NULL);
  314. SDL_GLContext backup_context = NULL;
  315. if (use_opengl)
  316. {
  317. backup_context = SDL_GL_GetCurrentContext();
  318. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  319. SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
  320. }
  321. // We don't enable SDL_WINDOW_RESIZABLE because it enforce windows decorations
  322. Uint32 sdl_flags = 0;
  323. sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : SDL_WINDOW_VULKAN;
  324. sdl_flags |= SDL_WINDOW_HIDDEN;
  325. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
  326. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
  327. #if SDL_HAS_ALWAYS_ON_TOP
  328. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
  329. #endif
  330. 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);
  331. data->WindowOwned = true;
  332. if (use_opengl)
  333. {
  334. data->GLContext = SDL_GL_CreateContext(data->Window);
  335. SDL_GL_SetSwapInterval(0);
  336. }
  337. if (use_opengl && backup_context)
  338. SDL_GL_MakeCurrent(data->Window, backup_context);
  339. viewport->PlatformHandle = (void*)data->Window;
  340. }
  341. static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport)
  342. {
  343. if (ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData)
  344. {
  345. if (data->GLContext && data->WindowOwned)
  346. SDL_GL_DeleteContext(data->GLContext);
  347. if (data->Window && data->WindowOwned)
  348. SDL_DestroyWindow(data->Window);
  349. data->GLContext = NULL;
  350. data->Window = NULL;
  351. IM_DELETE(data);
  352. }
  353. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  354. }
  355. static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport)
  356. {
  357. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  358. #if defined(_WIN32)
  359. SDL_SysWMinfo info;
  360. SDL_VERSION(&info.version);
  361. if (SDL_GetWindowWMInfo(data->Window, &info))
  362. {
  363. HWND hwnd = info.info.win.window;
  364. // SDL hack: Hide icon from task bar
  365. // 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.
  366. if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
  367. {
  368. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  369. ex_style &= ~WS_EX_APPWINDOW;
  370. ex_style |= WS_EX_TOOLWINDOW;
  371. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  372. }
  373. // SDL hack: SDL always activate/focus windows :/
  374. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  375. {
  376. ::ShowWindow(hwnd, SW_SHOWNA);
  377. return;
  378. }
  379. }
  380. #endif
  381. SDL_ShowWindow(data->Window);
  382. }
  383. static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport)
  384. {
  385. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  386. int x = 0, y = 0;
  387. SDL_GetWindowPosition(data->Window, &x, &y);
  388. return ImVec2((float)x, (float)y);
  389. }
  390. static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  391. {
  392. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  393. SDL_SetWindowPosition(data->Window, (int)pos.x, (int)pos.y);
  394. }
  395. static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport)
  396. {
  397. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  398. int w = 0, h = 0;
  399. SDL_GetWindowSize(data->Window, &w, &h);
  400. return ImVec2((float)w, (float)h);
  401. }
  402. static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  403. {
  404. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  405. SDL_SetWindowSize(data->Window, (int)size.x, (int)size.y);
  406. }
  407. static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  408. {
  409. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  410. SDL_SetWindowTitle(data->Window, title);
  411. }
  412. static void ImGui_ImplSDL2_SetWindowFocus(ImGuiViewport* viewport)
  413. {
  414. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  415. SDL_RaiseWindow(data->Window);
  416. }
  417. static bool ImGui_ImplSDL2_GetWindowFocus(ImGuiViewport* viewport)
  418. {
  419. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  420. return (SDL_GetWindowFlags(data->Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
  421. }
  422. static bool ImGui_ImplSDL2_GetWindowMinimized(ImGuiViewport* viewport)
  423. {
  424. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  425. return (SDL_GetWindowFlags(data->Window) & SDL_WINDOW_MINIMIZED) != 0;
  426. }
  427. static void ImGui_ImplSDL2_RenderWindow(ImGuiViewport* viewport, void*)
  428. {
  429. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  430. if (data->GLContext)
  431. SDL_GL_MakeCurrent(data->Window, data->GLContext);
  432. }
  433. static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport, void*)
  434. {
  435. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  436. if (data->GLContext)
  437. {
  438. SDL_GL_MakeCurrent(data->Window, data->GLContext);
  439. SDL_GL_SwapWindow(data->Window);
  440. }
  441. }
  442. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  443. // SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this.
  444. #if SDL_HAS_VULKAN
  445. #include <SDL_vulkan.h>
  446. static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  447. {
  448. ImGuiViewportDataSDL2* data = (ImGuiViewportDataSDL2*)viewport->PlatformUserData;
  449. (void)vk_allocator;
  450. SDL_bool ret = SDL_Vulkan_CreateSurface(data->Window, (VkInstance)vk_instance, (VkSurfaceKHR*)out_vk_surface);
  451. return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
  452. }
  453. #endif // SDL_HAS_VULKAN
  454. // FIXME-PLATFORM: SDL doesn't have an event to notify the application of display/monitor changes
  455. static void ImGui_ImplSDL2_UpdateMonitors()
  456. {
  457. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  458. platform_io.Monitors.resize(0);
  459. int display_count = SDL_GetNumVideoDisplays();
  460. for (int n = 0; n < display_count; n++)
  461. {
  462. // 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.
  463. ImGuiPlatformMonitor monitor;
  464. SDL_Rect r;
  465. SDL_GetDisplayBounds(n, &r);
  466. monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
  467. monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
  468. #if SDL_HAS_USABLE_DISPLAY_BOUNDS
  469. SDL_GetDisplayUsableBounds(n, &r);
  470. monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
  471. monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
  472. #endif
  473. #if SDL_HAS_PER_MONITOR_DPI
  474. float dpi = 0.0f;
  475. if (!SDL_GetDisplayDPI(n, &dpi, NULL, NULL))
  476. monitor.DpiScale = dpi / 96.0f;
  477. #endif
  478. platform_io.Monitors.push_back(monitor);
  479. }
  480. }
  481. static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context)
  482. {
  483. // Register platform interface (will be coupled with a renderer interface)
  484. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  485. platform_io.Platform_CreateWindow = ImGui_ImplSDL2_CreateWindow;
  486. platform_io.Platform_DestroyWindow = ImGui_ImplSDL2_DestroyWindow;
  487. platform_io.Platform_ShowWindow = ImGui_ImplSDL2_ShowWindow;
  488. platform_io.Platform_SetWindowPos = ImGui_ImplSDL2_SetWindowPos;
  489. platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos;
  490. platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize;
  491. platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize;
  492. platform_io.Platform_SetWindowFocus = ImGui_ImplSDL2_SetWindowFocus;
  493. platform_io.Platform_GetWindowFocus = ImGui_ImplSDL2_GetWindowFocus;
  494. platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL2_GetWindowMinimized;
  495. platform_io.Platform_SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle;
  496. platform_io.Platform_RenderWindow = ImGui_ImplSDL2_RenderWindow;
  497. platform_io.Platform_SwapBuffers = ImGui_ImplSDL2_SwapBuffers;
  498. #if SDL_HAS_VULKAN
  499. platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface;
  500. #endif
  501. // 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.
  502. #if SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH
  503. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  504. #endif
  505. ImGui_ImplSDL2_UpdateMonitors();
  506. // Register main window handle (which is owned by the main application, not by us)
  507. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  508. ImGuiViewportDataSDL2* data = IM_NEW(ImGuiViewportDataSDL2)();
  509. data->Window = window;
  510. data->WindowID = SDL_GetWindowID(window);
  511. data->WindowOwned = false;
  512. data->GLContext = sdl_gl_context;
  513. main_viewport->PlatformUserData = data;
  514. main_viewport->PlatformHandle = data->Window;
  515. }
  516. static void ImGui_ImplSDL2_ShutdownPlatformInterface()
  517. {
  518. }