imgui_impl_sdl3.cpp 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. // dear imgui: Platform Backend for SDL3 (*EXPERIMENTAL*)
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. // (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
  4. // (**IMPORTANT: SDL 3.0.0 is NOT YET RELEASED AND CURRENTLY HAS A FAST CHANGING API. THIS CODE BREAKS OFTEN AS SDL3 CHANGES.**)
  5. // Implemented features:
  6. // [X] Platform: Clipboard support.
  7. // [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
  8. // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set]
  9. // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  10. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  11. // [x] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable' -> the OS animation effect when window gets created/destroyed is problematic. SDL2 backend doesn't have issue.
  12. // Issues:
  13. // [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows).
  14. // [ ] Platform: Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
  15. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  16. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  17. // Learn about Dear ImGui:
  18. // - FAQ https://dearimgui.com/faq
  19. // - Getting Started https://dearimgui.com/getting-started
  20. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  21. // - Introduction, links and more at the top of imgui.cpp
  22. // CHANGELOG
  23. // (minor and older changes stripped away, please see git history for details)
  24. // 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  25. // 2024-09-03: Update for SDL3 api changes: SDL_GetGamepads() memory ownership revert. (#7918, #7898, #7807)
  26. // 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
  27. // - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
  28. // - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
  29. // - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
  30. // 2024-08-19: Storing SDL_WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
  31. // 2024-08-19: ImGui_ImplSDL3_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
  32. // 2024-07-22: Update for SDL3 api changes: SDL_GetGamepads() memory ownership change. (#7807)
  33. // 2024-07-18: Update for SDL3 api changes: SDL_GetClipboardText() memory ownership change. (#7801)
  34. // 2024-07-15: Update for SDL3 api changes: SDL_GetProperty() change to SDL_GetPointerProperty(). (#7794)
  35. // 2024-07-02: Update for SDL3 api changes: SDLK_x renames and SDLK_KP_x removals (#7761, #7762).
  36. // 2024-07-01: Update for SDL3 api changes: SDL_SetTextInputRect() changed to SDL_SetTextInputArea().
  37. // 2024-06-26: Update for SDL3 api changes: SDL_StartTextInput()/SDL_StopTextInput()/SDL_SetTextInputRect() functions signatures.
  38. // 2024-06-24: Update for SDL3 api changes: SDL_EVENT_KEY_DOWN/SDL_EVENT_KEY_UP contents.
  39. // 2024-06-03; Update for SDL3 api changes: SDL_SYSTEM_CURSOR_ renames.
  40. // 2024-05-15: Update for SDL3 api changes: SDLK_ renames.
  41. // 2024-04-15: Inputs: Re-enable calling SDL_StartTextInput()/SDL_StopTextInput() as SDL3 no longer enables it by default and should play nicer with IME.
  42. // 2024-02-13: Inputs: Fixed gamepad support. Handle gamepad disconnection. Added ImGui_ImplSDL3_SetGamepadMode().
  43. // 2023-11-13: Updated for recent SDL3 API changes.
  44. // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
  45. // 2023-05-04: Fixed build on Emscripten/iOS/Android. (#6391)
  46. // 2023-04-06: Inputs: Avoid calling SDL_StartTextInput()/SDL_StopTextInput() as they don't only pertain to IME. It's unclear exactly what their relation is to IME. (#6306)
  47. // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
  48. // 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
  49. // 2023-02-07: Forked "imgui_impl_sdl2" into "imgui_impl_sdl3". Removed version checks for old feature. Refer to imgui_impl_sdl2.cpp for older changelog.
  50. #include "imgui.h"
  51. #ifndef IMGUI_DISABLE
  52. #include "imgui_impl_sdl3.h"
  53. // Clang warnings with -Weverything
  54. #if defined(__clang__)
  55. #pragma clang diagnostic push
  56. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
  57. #endif
  58. // SDL
  59. #include <SDL3/SDL.h>
  60. #if defined(__APPLE__)
  61. #include <TargetConditionals.h>
  62. #endif
  63. #ifdef _WIN32
  64. #ifndef WIN32_LEAN_AND_MEAN
  65. #define WIN32_LEAN_AND_MEAN
  66. #endif
  67. #include <windows.h>
  68. #endif
  69. #if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
  70. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
  71. #else
  72. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
  73. #endif
  74. // FIXME-LEGACY: remove when SDL 3.1.3 preview is released.
  75. #ifndef SDLK_APOSTROPHE
  76. #define SDLK_APOSTROPHE SDLK_QUOTE
  77. #endif
  78. #ifndef SDLK_GRAVE
  79. #define SDLK_GRAVE SDLK_BACKQUOTE
  80. #endif
  81. // SDL Data
  82. struct ImGui_ImplSDL3_Data
  83. {
  84. SDL_Window* Window;
  85. SDL_WindowID WindowID;
  86. SDL_Renderer* Renderer;
  87. Uint64 Time;
  88. char* ClipboardTextData;
  89. bool UseVulkan;
  90. bool WantUpdateMonitors;
  91. // IME handling
  92. SDL_Window* ImeWindow;
  93. // Mouse handling
  94. Uint32 MouseWindowID;
  95. int MouseButtonsDown;
  96. SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
  97. SDL_Cursor* MouseLastCursor;
  98. int MousePendingLeaveFrame;
  99. bool MouseCanUseGlobalState;
  100. bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state.
  101. // Gamepad handling
  102. ImVector<SDL_Gamepad*> Gamepads;
  103. ImGui_ImplSDL3_GamepadMode GamepadMode;
  104. bool WantUpdateGamepadsList;
  105. ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); }
  106. };
  107. // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
  108. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  109. // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
  110. // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
  111. static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData()
  112. {
  113. return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
  114. }
  115. // Forward Declarations
  116. static void ImGui_ImplSDL3_UpdateMonitors();
  117. static void ImGui_ImplSDL3_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context);
  118. static void ImGui_ImplSDL3_ShutdownPlatformInterface();
  119. // Functions
  120. static const char* ImGui_ImplSDL3_GetClipboardText(ImGuiContext*)
  121. {
  122. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  123. if (bd->ClipboardTextData)
  124. SDL_free(bd->ClipboardTextData);
  125. const char* sdl_clipboard_text = SDL_GetClipboardText();
  126. bd->ClipboardTextData = sdl_clipboard_text ? SDL_strdup(sdl_clipboard_text) : NULL;
  127. return bd->ClipboardTextData;
  128. }
  129. static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text)
  130. {
  131. SDL_SetClipboardText(text);
  132. }
  133. static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
  134. {
  135. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  136. SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
  137. SDL_Window* window = SDL_GetWindowFromID(window_id);
  138. if ((data->WantVisible == false || bd->ImeWindow != window) && bd->ImeWindow != NULL)
  139. {
  140. SDL_StopTextInput(bd->ImeWindow);
  141. bd->ImeWindow = nullptr;
  142. }
  143. if (data->WantVisible)
  144. {
  145. SDL_Rect r;
  146. r.x = (int)(data->InputPos.x - viewport->Pos.x);
  147. r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight);
  148. r.w = 1;
  149. r.h = (int)data->InputLineHeight;
  150. SDL_SetTextInputArea(window, &r, 0);
  151. SDL_StartTextInput(window);
  152. bd->ImeWindow = window;
  153. }
  154. }
  155. static ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
  156. {
  157. // Keypad doesn't have individual key values in SDL3
  158. switch (scancode)
  159. {
  160. case SDL_SCANCODE_KP_0: return ImGuiKey_Keypad0;
  161. case SDL_SCANCODE_KP_1: return ImGuiKey_Keypad1;
  162. case SDL_SCANCODE_KP_2: return ImGuiKey_Keypad2;
  163. case SDL_SCANCODE_KP_3: return ImGuiKey_Keypad3;
  164. case SDL_SCANCODE_KP_4: return ImGuiKey_Keypad4;
  165. case SDL_SCANCODE_KP_5: return ImGuiKey_Keypad5;
  166. case SDL_SCANCODE_KP_6: return ImGuiKey_Keypad6;
  167. case SDL_SCANCODE_KP_7: return ImGuiKey_Keypad7;
  168. case SDL_SCANCODE_KP_8: return ImGuiKey_Keypad8;
  169. case SDL_SCANCODE_KP_9: return ImGuiKey_Keypad9;
  170. case SDL_SCANCODE_KP_PERIOD: return ImGuiKey_KeypadDecimal;
  171. case SDL_SCANCODE_KP_DIVIDE: return ImGuiKey_KeypadDivide;
  172. case SDL_SCANCODE_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
  173. case SDL_SCANCODE_KP_MINUS: return ImGuiKey_KeypadSubtract;
  174. case SDL_SCANCODE_KP_PLUS: return ImGuiKey_KeypadAdd;
  175. case SDL_SCANCODE_KP_ENTER: return ImGuiKey_KeypadEnter;
  176. case SDL_SCANCODE_KP_EQUALS: return ImGuiKey_KeypadEqual;
  177. default: break;
  178. }
  179. switch (keycode)
  180. {
  181. case SDLK_TAB: return ImGuiKey_Tab;
  182. case SDLK_LEFT: return ImGuiKey_LeftArrow;
  183. case SDLK_RIGHT: return ImGuiKey_RightArrow;
  184. case SDLK_UP: return ImGuiKey_UpArrow;
  185. case SDLK_DOWN: return ImGuiKey_DownArrow;
  186. case SDLK_PAGEUP: return ImGuiKey_PageUp;
  187. case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
  188. case SDLK_HOME: return ImGuiKey_Home;
  189. case SDLK_END: return ImGuiKey_End;
  190. case SDLK_INSERT: return ImGuiKey_Insert;
  191. case SDLK_DELETE: return ImGuiKey_Delete;
  192. case SDLK_BACKSPACE: return ImGuiKey_Backspace;
  193. case SDLK_SPACE: return ImGuiKey_Space;
  194. case SDLK_RETURN: return ImGuiKey_Enter;
  195. case SDLK_ESCAPE: return ImGuiKey_Escape;
  196. case SDLK_APOSTROPHE: return ImGuiKey_Apostrophe;
  197. case SDLK_COMMA: return ImGuiKey_Comma;
  198. case SDLK_MINUS: return ImGuiKey_Minus;
  199. case SDLK_PERIOD: return ImGuiKey_Period;
  200. case SDLK_SLASH: return ImGuiKey_Slash;
  201. case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
  202. case SDLK_EQUALS: return ImGuiKey_Equal;
  203. case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
  204. case SDLK_BACKSLASH: return ImGuiKey_Backslash;
  205. case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
  206. case SDLK_GRAVE: return ImGuiKey_GraveAccent;
  207. case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
  208. case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
  209. case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
  210. case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
  211. case SDLK_PAUSE: return ImGuiKey_Pause;
  212. case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
  213. case SDLK_LSHIFT: return ImGuiKey_LeftShift;
  214. case SDLK_LALT: return ImGuiKey_LeftAlt;
  215. case SDLK_LGUI: return ImGuiKey_LeftSuper;
  216. case SDLK_RCTRL: return ImGuiKey_RightCtrl;
  217. case SDLK_RSHIFT: return ImGuiKey_RightShift;
  218. case SDLK_RALT: return ImGuiKey_RightAlt;
  219. case SDLK_RGUI: return ImGuiKey_RightSuper;
  220. case SDLK_APPLICATION: return ImGuiKey_Menu;
  221. case SDLK_0: return ImGuiKey_0;
  222. case SDLK_1: return ImGuiKey_1;
  223. case SDLK_2: return ImGuiKey_2;
  224. case SDLK_3: return ImGuiKey_3;
  225. case SDLK_4: return ImGuiKey_4;
  226. case SDLK_5: return ImGuiKey_5;
  227. case SDLK_6: return ImGuiKey_6;
  228. case SDLK_7: return ImGuiKey_7;
  229. case SDLK_8: return ImGuiKey_8;
  230. case SDLK_9: return ImGuiKey_9;
  231. case SDLK_A: return ImGuiKey_A;
  232. case SDLK_B: return ImGuiKey_B;
  233. case SDLK_C: return ImGuiKey_C;
  234. case SDLK_D: return ImGuiKey_D;
  235. case SDLK_E: return ImGuiKey_E;
  236. case SDLK_F: return ImGuiKey_F;
  237. case SDLK_G: return ImGuiKey_G;
  238. case SDLK_H: return ImGuiKey_H;
  239. case SDLK_I: return ImGuiKey_I;
  240. case SDLK_J: return ImGuiKey_J;
  241. case SDLK_K: return ImGuiKey_K;
  242. case SDLK_L: return ImGuiKey_L;
  243. case SDLK_M: return ImGuiKey_M;
  244. case SDLK_N: return ImGuiKey_N;
  245. case SDLK_O: return ImGuiKey_O;
  246. case SDLK_P: return ImGuiKey_P;
  247. case SDLK_Q: return ImGuiKey_Q;
  248. case SDLK_R: return ImGuiKey_R;
  249. case SDLK_S: return ImGuiKey_S;
  250. case SDLK_T: return ImGuiKey_T;
  251. case SDLK_U: return ImGuiKey_U;
  252. case SDLK_V: return ImGuiKey_V;
  253. case SDLK_W: return ImGuiKey_W;
  254. case SDLK_X: return ImGuiKey_X;
  255. case SDLK_Y: return ImGuiKey_Y;
  256. case SDLK_Z: return ImGuiKey_Z;
  257. case SDLK_F1: return ImGuiKey_F1;
  258. case SDLK_F2: return ImGuiKey_F2;
  259. case SDLK_F3: return ImGuiKey_F3;
  260. case SDLK_F4: return ImGuiKey_F4;
  261. case SDLK_F5: return ImGuiKey_F5;
  262. case SDLK_F6: return ImGuiKey_F6;
  263. case SDLK_F7: return ImGuiKey_F7;
  264. case SDLK_F8: return ImGuiKey_F8;
  265. case SDLK_F9: return ImGuiKey_F9;
  266. case SDLK_F10: return ImGuiKey_F10;
  267. case SDLK_F11: return ImGuiKey_F11;
  268. case SDLK_F12: return ImGuiKey_F12;
  269. case SDLK_F13: return ImGuiKey_F13;
  270. case SDLK_F14: return ImGuiKey_F14;
  271. case SDLK_F15: return ImGuiKey_F15;
  272. case SDLK_F16: return ImGuiKey_F16;
  273. case SDLK_F17: return ImGuiKey_F17;
  274. case SDLK_F18: return ImGuiKey_F18;
  275. case SDLK_F19: return ImGuiKey_F19;
  276. case SDLK_F20: return ImGuiKey_F20;
  277. case SDLK_F21: return ImGuiKey_F21;
  278. case SDLK_F22: return ImGuiKey_F22;
  279. case SDLK_F23: return ImGuiKey_F23;
  280. case SDLK_F24: return ImGuiKey_F24;
  281. case SDLK_AC_BACK: return ImGuiKey_AppBack;
  282. case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
  283. default: break;
  284. }
  285. return ImGuiKey_None;
  286. }
  287. static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
  288. {
  289. ImGuiIO& io = ImGui::GetIO();
  290. io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0);
  291. io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0);
  292. io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0);
  293. io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0);
  294. }
  295. static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id)
  296. {
  297. return ImGui::FindViewportByPlatformHandle((void*)(intptr_t)window_id);
  298. }
  299. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  300. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  301. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  302. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  303. bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
  304. {
  305. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  306. IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
  307. ImGuiIO& io = ImGui::GetIO();
  308. switch (event->type)
  309. {
  310. case SDL_EVENT_MOUSE_MOTION:
  311. {
  312. if (ImGui_ImplSDL3_GetViewportForWindowID(event->motion.windowID) == NULL)
  313. return false;
  314. ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
  315. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  316. {
  317. int window_x, window_y;
  318. SDL_GetWindowPosition(SDL_GetWindowFromID(event->motion.windowID), &window_x, &window_y);
  319. mouse_pos.x += window_x;
  320. mouse_pos.y += window_y;
  321. }
  322. io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  323. io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
  324. return true;
  325. }
  326. case SDL_EVENT_MOUSE_WHEEL:
  327. {
  328. if (ImGui_ImplSDL3_GetViewportForWindowID(event->wheel.windowID) == NULL)
  329. return false;
  330. //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
  331. float wheel_x = -event->wheel.x;
  332. float wheel_y = event->wheel.y;
  333. #ifdef __EMSCRIPTEN__
  334. wheel_x /= 100.0f;
  335. #endif
  336. io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  337. io.AddMouseWheelEvent(wheel_x, wheel_y);
  338. return true;
  339. }
  340. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  341. case SDL_EVENT_MOUSE_BUTTON_UP:
  342. {
  343. if (ImGui_ImplSDL3_GetViewportForWindowID(event->button.windowID) == NULL)
  344. return false;
  345. int mouse_button = -1;
  346. if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
  347. if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
  348. if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
  349. if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
  350. if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
  351. if (mouse_button == -1)
  352. break;
  353. io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  354. io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN));
  355. bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
  356. return true;
  357. }
  358. case SDL_EVENT_TEXT_INPUT:
  359. {
  360. if (ImGui_ImplSDL3_GetViewportForWindowID(event->text.windowID) == NULL)
  361. return false;
  362. io.AddInputCharactersUTF8(event->text.text);
  363. return true;
  364. }
  365. case SDL_EVENT_KEY_DOWN:
  366. case SDL_EVENT_KEY_UP:
  367. {
  368. if (ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID) == NULL)
  369. return false;
  370. //IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%d: key=%d, scancode=%d, mod=%X\n", (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP", event->key.key, event->key.scancode, event->key.mod);
  371. ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod);
  372. ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode);
  373. io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
  374. io.SetKeyEventNativeData(key, event->key.key, event->key.scancode, event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
  375. return true;
  376. }
  377. case SDL_EVENT_DISPLAY_ORIENTATION:
  378. case SDL_EVENT_DISPLAY_ADDED:
  379. case SDL_EVENT_DISPLAY_REMOVED:
  380. case SDL_EVENT_DISPLAY_MOVED:
  381. case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
  382. {
  383. bd->WantUpdateMonitors = true;
  384. return true;
  385. }
  386. case SDL_EVENT_WINDOW_MOUSE_ENTER:
  387. {
  388. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == NULL)
  389. return false;
  390. bd->MouseWindowID = event->window.windowID;
  391. bd->MousePendingLeaveFrame = 0;
  392. return true;
  393. }
  394. // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
  395. // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
  396. // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
  397. // FIXME: Unconfirmed whether this is still needed with SDL3.
  398. case SDL_EVENT_WINDOW_MOUSE_LEAVE:
  399. {
  400. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == NULL)
  401. return false;
  402. bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1;
  403. return true;
  404. }
  405. case SDL_EVENT_WINDOW_FOCUS_GAINED:
  406. case SDL_EVENT_WINDOW_FOCUS_LOST:
  407. {
  408. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == NULL)
  409. return false;
  410. io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
  411. return true;
  412. }
  413. case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
  414. case SDL_EVENT_WINDOW_MOVED:
  415. case SDL_EVENT_WINDOW_RESIZED:
  416. {
  417. ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID);
  418. if (viewport == NULL)
  419. return false;
  420. if (event->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED)
  421. viewport->PlatformRequestClose = true;
  422. if (event->type == SDL_EVENT_WINDOW_MOVED)
  423. viewport->PlatformRequestMove = true;
  424. if (event->type == SDL_EVENT_WINDOW_RESIZED)
  425. viewport->PlatformRequestResize = true;
  426. return true;
  427. }
  428. case SDL_EVENT_GAMEPAD_ADDED:
  429. case SDL_EVENT_GAMEPAD_REMOVED:
  430. {
  431. bd->WantUpdateGamepadsList = true;
  432. return true;
  433. }
  434. }
  435. return false;
  436. }
  437. static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window)
  438. {
  439. viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window);
  440. viewport->PlatformHandleRaw = nullptr;
  441. #if defined(_WIN32) && !defined(__WINRT__)
  442. viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
  443. #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
  444. viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
  445. #endif
  446. }
  447. static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
  448. {
  449. ImGuiIO& io = ImGui::GetIO();
  450. IMGUI_CHECKVERSION();
  451. IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
  452. IM_UNUSED(sdl_gl_context); // Unused in this branch
  453. // Check and store if we are on a SDL backend that supports global mouse position
  454. // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
  455. bool mouse_can_use_global_state = false;
  456. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  457. const char* sdl_backend = SDL_GetCurrentVideoDriver();
  458. const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
  459. for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
  460. if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
  461. mouse_can_use_global_state = true;
  462. #endif
  463. // Setup backend capabilities flags
  464. ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)();
  465. io.BackendPlatformUserData = (void*)bd;
  466. io.BackendPlatformName = "imgui_impl_sdl3";
  467. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  468. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  469. if (mouse_can_use_global_state)
  470. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  471. bd->Window = window;
  472. bd->WindowID = SDL_GetWindowID(window);
  473. bd->Renderer = renderer;
  474. // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960)
  475. // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame.
  476. bd->MouseCanUseGlobalState = mouse_can_use_global_state;
  477. #ifndef __APPLE__
  478. bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState;
  479. #else
  480. bd->MouseCanReportHoveredViewport = false;
  481. #endif
  482. bd->WantUpdateMonitors = true;
  483. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  484. platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
  485. platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
  486. platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData;
  487. // Gamepad handling
  488. bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst;
  489. bd->WantUpdateGamepadsList = true;
  490. // Load mouse cursors
  491. bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
  492. bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
  493. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE);
  494. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE);
  495. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE);
  496. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE);
  497. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE);
  498. bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER);
  499. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED);
  500. // Set platform dependent data in viewport
  501. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  502. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  503. ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window);
  504. // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
  505. // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
  506. // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
  507. // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
  508. // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
  509. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  510. // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
  511. SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
  512. // SDL 3.x : see https://github.com/libsdl-org/SDL/issues/6659
  513. SDL_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0");
  514. // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
  515. // We left the call to ImGui_ImplSDL3_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
  516. if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports))
  517. ImGui_ImplSDL3_InitPlatformInterface(window, sdl_gl_context);
  518. return true;
  519. }
  520. // Should technically be a SDL_GLContext but due to typedef it is sane to keep it void* in public interface.
  521. bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
  522. {
  523. return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context);
  524. }
  525. bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window)
  526. {
  527. if (!ImGui_ImplSDL3_Init(window, nullptr, nullptr))
  528. return false;
  529. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  530. bd->UseVulkan = true;
  531. return true;
  532. }
  533. bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window)
  534. {
  535. #if !defined(_WIN32)
  536. IM_ASSERT(0 && "Unsupported");
  537. #endif
  538. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  539. }
  540. bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window)
  541. {
  542. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  543. }
  544. bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
  545. {
  546. return ImGui_ImplSDL3_Init(window, renderer, nullptr);
  547. }
  548. bool ImGui_ImplSDL3_InitForOther(SDL_Window* window)
  549. {
  550. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  551. }
  552. static void ImGui_ImplSDL3_CloseGamepads();
  553. void ImGui_ImplSDL3_Shutdown()
  554. {
  555. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  556. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  557. ImGuiIO& io = ImGui::GetIO();
  558. ImGui_ImplSDL3_ShutdownPlatformInterface();
  559. if (bd->ClipboardTextData)
  560. SDL_free(bd->ClipboardTextData);
  561. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  562. SDL_DestroyCursor(bd->MouseCursors[cursor_n]);
  563. ImGui_ImplSDL3_CloseGamepads();
  564. io.BackendPlatformName = nullptr;
  565. io.BackendPlatformUserData = nullptr;
  566. io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
  567. IM_DELETE(bd);
  568. }
  569. // This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4.
  570. static void ImGui_ImplSDL3_UpdateMouseData()
  571. {
  572. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  573. ImGuiIO& io = ImGui::GetIO();
  574. // We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below)
  575. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  576. // SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't e.g. trigger other operations outside
  577. SDL_CaptureMouse((bd->MouseButtonsDown != 0) ? SDL_TRUE : SDL_FALSE);
  578. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  579. const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL));
  580. #else
  581. SDL_Window* focused_window = bd->Window;
  582. const bool is_app_focused = (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; // SDL 2.0.3 and non-windowed systems: single-viewport only
  583. #endif
  584. if (is_app_focused)
  585. {
  586. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  587. if (io.WantSetMousePos)
  588. {
  589. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  590. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  591. SDL_WarpMouseGlobal(io.MousePos.x, io.MousePos.y);
  592. else
  593. #endif
  594. SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y);
  595. }
  596. // (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
  597. if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0)
  598. {
  599. // Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
  600. // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
  601. float mouse_x, mouse_y;
  602. int window_x, window_y;
  603. SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
  604. if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
  605. {
  606. SDL_GetWindowPosition(focused_window, &window_x, &window_y);
  607. mouse_x -= window_x;
  608. mouse_y -= window_y;
  609. }
  610. io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
  611. }
  612. }
  613. // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
  614. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
  615. // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag.
  616. // Some backend are not able to handle that correctly. If a backend report an hovered viewport that has the _NoInputs flag (e.g. when dragging a window
  617. // for docking, the viewport has the _NoInputs flag in order to allow us to find the viewport under), then Dear ImGui is forced to ignore the value reported
  618. // by the backend, and use its flawed heuristic to guess the viewport behind.
  619. // - [X] SDL backend correctly reports this regardless of another viewport behind focused and dragged from (we need this to find a useful drag and drop target).
  620. if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
  621. {
  622. ImGuiID mouse_viewport_id = 0;
  623. if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL3_GetViewportForWindowID(bd->MouseWindowID))
  624. mouse_viewport_id = mouse_viewport->ID;
  625. io.AddMouseViewportEvent(mouse_viewport_id);
  626. }
  627. }
  628. static void ImGui_ImplSDL3_UpdateMouseCursor()
  629. {
  630. ImGuiIO& io = ImGui::GetIO();
  631. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  632. return;
  633. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  634. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  635. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  636. {
  637. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  638. SDL_HideCursor();
  639. }
  640. else
  641. {
  642. // Show OS mouse cursor
  643. SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
  644. if (bd->MouseLastCursor != expected_cursor)
  645. {
  646. SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
  647. bd->MouseLastCursor = expected_cursor;
  648. }
  649. SDL_ShowCursor();
  650. }
  651. }
  652. static void ImGui_ImplSDL3_CloseGamepads()
  653. {
  654. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  655. if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
  656. for (SDL_Gamepad* gamepad : bd->Gamepads)
  657. SDL_CloseGamepad(gamepad);
  658. bd->Gamepads.resize(0);
  659. }
  660. void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count)
  661. {
  662. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  663. ImGui_ImplSDL3_CloseGamepads();
  664. if (mode == ImGui_ImplSDL3_GamepadMode_Manual)
  665. {
  666. IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0);
  667. for (int n = 0; n < manual_gamepads_count; n++)
  668. bd->Gamepads.push_back(manual_gamepads_array[n]);
  669. }
  670. else
  671. {
  672. IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
  673. bd->WantUpdateGamepadsList = true;
  674. }
  675. bd->GamepadMode = mode;
  676. }
  677. static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadButton button_no)
  678. {
  679. bool merged_value = false;
  680. for (SDL_Gamepad* gamepad : bd->Gamepads)
  681. merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0;
  682. io.AddKeyEvent(key, merged_value);
  683. }
  684. static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
  685. static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no, float v0, float v1)
  686. {
  687. float merged_value = 0.0f;
  688. for (SDL_Gamepad* gamepad : bd->Gamepads)
  689. {
  690. float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
  691. if (merged_value < vn)
  692. merged_value = vn;
  693. }
  694. io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
  695. }
  696. static void ImGui_ImplSDL3_UpdateGamepads()
  697. {
  698. ImGuiIO& io = ImGui::GetIO();
  699. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  700. // Update list of gamepads to use
  701. if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
  702. {
  703. ImGui_ImplSDL3_CloseGamepads();
  704. int sdl_gamepads_count = 0;
  705. SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count);
  706. for (int n = 0; n < sdl_gamepads_count; n++)
  707. if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n]))
  708. {
  709. bd->Gamepads.push_back(gamepad);
  710. if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst)
  711. break;
  712. }
  713. bd->WantUpdateGamepadsList = false;
  714. SDL_free(sdl_gamepads);
  715. }
  716. // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  717. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
  718. return;
  719. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  720. if (bd->Gamepads.Size == 0)
  721. return;
  722. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  723. // Update gamepad inputs
  724. const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value.
  725. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START);
  726. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK);
  727. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square
  728. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle
  729. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle
  730. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross
  731. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
  732. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
  733. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP);
  734. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
  735. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER);
  736. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER);
  737. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767);
  738. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767);
  739. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK);
  740. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK);
  741. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768);
  742. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767);
  743. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768);
  744. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767);
  745. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768);
  746. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767);
  747. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768);
  748. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767);
  749. }
  750. static void ImGui_ImplSDL3_UpdateMonitors()
  751. {
  752. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  753. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  754. platform_io.Monitors.resize(0);
  755. bd->WantUpdateMonitors = false;
  756. int display_count;
  757. SDL_DisplayID* displays = SDL_GetDisplays(&display_count);
  758. for (int n = 0; n < display_count; n++)
  759. {
  760. // 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.
  761. SDL_DisplayID display_id = displays[n];
  762. ImGuiPlatformMonitor monitor;
  763. SDL_Rect r;
  764. SDL_GetDisplayBounds(display_id, &r);
  765. monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
  766. monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
  767. SDL_GetDisplayUsableBounds(display_id, &r);
  768. monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
  769. monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
  770. // FIXME-VIEWPORT: On MacOS SDL reports actual monitor DPI scale, ignoring OS configuration. We may want to set
  771. // DpiScale to cocoa_window.backingScaleFactor here.
  772. monitor.DpiScale = SDL_GetDisplayContentScale(display_id);
  773. monitor.PlatformHandle = (void*)(intptr_t)n;
  774. if (monitor.DpiScale <= 0.0f)
  775. continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
  776. platform_io.Monitors.push_back(monitor);
  777. }
  778. SDL_free(displays);
  779. }
  780. void ImGui_ImplSDL3_NewFrame()
  781. {
  782. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  783. IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
  784. ImGuiIO& io = ImGui::GetIO();
  785. // Setup display size (every frame to accommodate for window resizing)
  786. int w, h;
  787. int display_w, display_h;
  788. SDL_GetWindowSize(bd->Window, &w, &h);
  789. if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED)
  790. w = h = 0;
  791. SDL_GetWindowSizeInPixels(bd->Window, &display_w, &display_h);
  792. io.DisplaySize = ImVec2((float)w, (float)h);
  793. if (w > 0 && h > 0)
  794. io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
  795. // Update monitors
  796. if (bd->WantUpdateMonitors)
  797. ImGui_ImplSDL3_UpdateMonitors();
  798. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  799. // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
  800. static Uint64 frequency = SDL_GetPerformanceFrequency();
  801. Uint64 current_time = SDL_GetPerformanceCounter();
  802. if (current_time <= bd->Time)
  803. current_time = bd->Time + 1;
  804. io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
  805. bd->Time = current_time;
  806. if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
  807. {
  808. bd->MouseWindowID = 0;
  809. bd->MousePendingLeaveFrame = 0;
  810. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  811. }
  812. // Our io.AddMouseViewportEvent() calls will only be valid when not capturing.
  813. // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but testing for payload reduces noise and potential side-effects.
  814. if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr)
  815. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
  816. else
  817. io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport;
  818. ImGui_ImplSDL3_UpdateMouseData();
  819. ImGui_ImplSDL3_UpdateMouseCursor();
  820. // Update game controllers (if enabled and available)
  821. ImGui_ImplSDL3_UpdateGamepads();
  822. }
  823. //--------------------------------------------------------------------------------------------------------
  824. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  825. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  826. // 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..
  827. //--------------------------------------------------------------------------------------------------------
  828. // Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data.
  829. struct ImGui_ImplSDL3_ViewportData
  830. {
  831. SDL_Window* Window;
  832. Uint32 WindowID;
  833. bool WindowOwned;
  834. SDL_GLContext GLContext;
  835. ImGui_ImplSDL3_ViewportData() { Window = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; }
  836. ~ImGui_ImplSDL3_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); }
  837. };
  838. static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport)
  839. {
  840. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  841. ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
  842. viewport->PlatformUserData = vd;
  843. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  844. ImGui_ImplSDL3_ViewportData* main_viewport_data = (ImGui_ImplSDL3_ViewportData*)main_viewport->PlatformUserData;
  845. // Share GL resources with main context
  846. bool use_opengl = (main_viewport_data->GLContext != nullptr);
  847. SDL_GLContext backup_context = nullptr;
  848. if (use_opengl)
  849. {
  850. backup_context = SDL_GL_GetCurrentContext();
  851. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  852. SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
  853. }
  854. Uint32 sdl_flags = 0;
  855. sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0);
  856. sdl_flags |= SDL_GetWindowFlags(bd->Window);
  857. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
  858. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
  859. #if !defined(_WIN32)
  860. // See SDL hack in ImGui_ImplSDL3_ShowWindow().
  861. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_UTILITY : 0;
  862. #endif
  863. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
  864. vd->Window = SDL_CreateWindow("No Title Yet", (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags);
  865. SDL_SetWindowPosition(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y);
  866. vd->WindowOwned = true;
  867. if (use_opengl)
  868. {
  869. vd->GLContext = SDL_GL_CreateContext(vd->Window);
  870. SDL_GL_SetSwapInterval(0);
  871. }
  872. if (use_opengl && backup_context)
  873. SDL_GL_MakeCurrent(vd->Window, backup_context);
  874. ImGui_ImplSDL3_SetupPlatformHandles(viewport, vd->Window);
  875. }
  876. static void ImGui_ImplSDL3_DestroyWindow(ImGuiViewport* viewport)
  877. {
  878. if (ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData)
  879. {
  880. if (vd->GLContext && vd->WindowOwned)
  881. SDL_GL_DestroyContext(vd->GLContext);
  882. if (vd->Window && vd->WindowOwned)
  883. SDL_DestroyWindow(vd->Window);
  884. vd->GLContext = nullptr;
  885. vd->Window = nullptr;
  886. IM_DELETE(vd);
  887. }
  888. viewport->PlatformUserData = viewport->PlatformHandle = nullptr;
  889. }
  890. static void ImGui_ImplSDL3_ShowWindow(ImGuiViewport* viewport)
  891. {
  892. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  893. #if defined(_WIN32)
  894. HWND hwnd = (HWND)viewport->PlatformHandleRaw;
  895. // SDL hack: Hide icon from task bar
  896. // Note: SDL 3.0.0+ has a SDL_WINDOW_UTILITY flag which is supported under Windows but the way it create the window breaks our seamless transition.
  897. if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
  898. {
  899. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  900. ex_style &= ~WS_EX_APPWINDOW;
  901. ex_style |= WS_EX_TOOLWINDOW;
  902. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  903. }
  904. #endif
  905. SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "0" : "1");
  906. SDL_ShowWindow(vd->Window);
  907. }
  908. static ImVec2 ImGui_ImplSDL3_GetWindowPos(ImGuiViewport* viewport)
  909. {
  910. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  911. int x = 0, y = 0;
  912. SDL_GetWindowPosition(vd->Window, &x, &y);
  913. return ImVec2((float)x, (float)y);
  914. }
  915. static void ImGui_ImplSDL3_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  916. {
  917. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  918. SDL_SetWindowPosition(vd->Window, (int)pos.x, (int)pos.y);
  919. }
  920. static ImVec2 ImGui_ImplSDL3_GetWindowSize(ImGuiViewport* viewport)
  921. {
  922. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  923. int w = 0, h = 0;
  924. SDL_GetWindowSize(vd->Window, &w, &h);
  925. return ImVec2((float)w, (float)h);
  926. }
  927. static void ImGui_ImplSDL3_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  928. {
  929. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  930. SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y);
  931. }
  932. static void ImGui_ImplSDL3_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  933. {
  934. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  935. SDL_SetWindowTitle(vd->Window, title);
  936. }
  937. static void ImGui_ImplSDL3_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  938. {
  939. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  940. SDL_SetWindowOpacity(vd->Window, alpha);
  941. }
  942. static void ImGui_ImplSDL3_SetWindowFocus(ImGuiViewport* viewport)
  943. {
  944. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  945. SDL_RaiseWindow(vd->Window);
  946. }
  947. static bool ImGui_ImplSDL3_GetWindowFocus(ImGuiViewport* viewport)
  948. {
  949. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  950. return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
  951. }
  952. static bool ImGui_ImplSDL3_GetWindowMinimized(ImGuiViewport* viewport)
  953. {
  954. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  955. return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0;
  956. }
  957. static void ImGui_ImplSDL3_RenderWindow(ImGuiViewport* viewport, void*)
  958. {
  959. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  960. if (vd->GLContext)
  961. SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
  962. }
  963. static void ImGui_ImplSDL3_SwapBuffers(ImGuiViewport* viewport, void*)
  964. {
  965. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  966. if (vd->GLContext)
  967. {
  968. SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
  969. SDL_GL_SwapWindow(vd->Window);
  970. }
  971. }
  972. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  973. // SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this.
  974. #include <SDL3/SDL_vulkan.h>
  975. static int ImGui_ImplSDL3_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  976. {
  977. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  978. (void)vk_allocator;
  979. SDL_bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
  980. return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
  981. }
  982. static void ImGui_ImplSDL3_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context)
  983. {
  984. // Register platform interface (will be coupled with a renderer interface)
  985. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  986. platform_io.Platform_CreateWindow = ImGui_ImplSDL3_CreateWindow;
  987. platform_io.Platform_DestroyWindow = ImGui_ImplSDL3_DestroyWindow;
  988. platform_io.Platform_ShowWindow = ImGui_ImplSDL3_ShowWindow;
  989. platform_io.Platform_SetWindowPos = ImGui_ImplSDL3_SetWindowPos;
  990. platform_io.Platform_GetWindowPos = ImGui_ImplSDL3_GetWindowPos;
  991. platform_io.Platform_SetWindowSize = ImGui_ImplSDL3_SetWindowSize;
  992. platform_io.Platform_GetWindowSize = ImGui_ImplSDL3_GetWindowSize;
  993. platform_io.Platform_SetWindowFocus = ImGui_ImplSDL3_SetWindowFocus;
  994. platform_io.Platform_GetWindowFocus = ImGui_ImplSDL3_GetWindowFocus;
  995. platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL3_GetWindowMinimized;
  996. platform_io.Platform_SetWindowTitle = ImGui_ImplSDL3_SetWindowTitle;
  997. platform_io.Platform_RenderWindow = ImGui_ImplSDL3_RenderWindow;
  998. platform_io.Platform_SwapBuffers = ImGui_ImplSDL3_SwapBuffers;
  999. platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL3_SetWindowAlpha;
  1000. platform_io.Platform_CreateVkSurface = ImGui_ImplSDL3_CreateVkSurface;
  1001. // Register main window handle (which is owned by the main application, not by us)
  1002. // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports.
  1003. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  1004. ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
  1005. vd->Window = window;
  1006. vd->WindowID = SDL_GetWindowID(window);
  1007. vd->WindowOwned = false;
  1008. vd->GLContext = (SDL_GLContext)sdl_gl_context;
  1009. main_viewport->PlatformUserData = vd;
  1010. main_viewport->PlatformHandle = (void*)(intptr_t)vd->WindowID;
  1011. }
  1012. static void ImGui_ImplSDL3_ShutdownPlatformInterface()
  1013. {
  1014. ImGui::DestroyPlatformWindows();
  1015. }
  1016. //-----------------------------------------------------------------------------
  1017. #if defined(__clang__)
  1018. #pragma clang diagnostic pop
  1019. #endif
  1020. #endif // #ifndef IMGUI_DISABLE