imgui_impl_sdl.cpp 25 KB

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