1 | // dear imgui: Platform Backend 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 | // (Prefer SDL 2.0.5+ for full feature support.) |
5 | |
6 | // Implemented features: |
7 | // [X] Platform: Clipboard support. |
8 | // [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen. |
9 | // [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 are obsolete since 1.87 and not supported since 1.91.5] |
10 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. |
11 | // [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. |
12 | // [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!. |
13 | // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. |
14 | // Missing features or Issues: |
15 | // [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows). |
16 | // [ ] Platform: Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor). |
17 | |
18 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
19 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
20 | // Learn about Dear ImGui: |
21 | // - FAQ https://dearimgui.com/faq |
22 | // - Getting Started https://dearimgui.com/getting-started |
23 | // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
24 | // - Introduction, links and more at the top of imgui.cpp |
25 | |
26 | // CHANGELOG |
27 | // (minor and older changes stripped away, please see git history for details) |
28 | // 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. |
29 | // 2024-10-24: Emscripten: from SDL 2.30.9, SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f. |
30 | // 2024-09-09: use SDL_Vulkan_GetDrawableSize() when available. (#7967, #3190) |
31 | // 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO: |
32 | // - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn |
33 | // - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn |
34 | // - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn |
35 | // - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn |
36 | // 2024-08-19: Storing SDL's Uint32 WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*. |
37 | // 2024-08-19: ImGui_ImplSDL2_ProcessEvent() now ignores events intended for other SDL windows. (#7853) |
38 | // 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions. |
39 | // 2024-07-02: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() renaming in main library. |
40 | // 2024-02-14: Inputs: Handle gamepad disconnection. Added ImGui_ImplSDL2_SetGamepadMode(). |
41 | // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys. |
42 | // 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) |
43 | // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702) |
44 | // 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644) |
45 | // 2023-02-07: Implement IME handler (io.SetPlatformImeDataFn will call SDL_SetTextInputRect()/SDL_StartTextInput()). |
46 | // 2023-02-07: *BREAKING CHANGE* Renamed this backend file from imgui_impl_sdl.cpp/.h to imgui_impl_sdl2.cpp/.h in prevision for the future release of SDL3. |
47 | // 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version). |
48 | // 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096) |
49 | // 2023-02-02: Removed SDL_MOUSEWHEEL value clamping, as values seem correct in latest Emscripten. (#4019) |
50 | // 2023-02-01: Flipping SDL_MOUSEWHEEL 'wheel.x' value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463) |
51 | // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. |
52 | // 2022-09-26: Inputs: Disable SDL 2.0.22 new "auto capture" (SDL_HINT_MOUSE_AUTO_CAPTURE) which prevents drag and drop across windows for multi-viewport support + don't capture when drag and dropping. (#5710) |
53 | // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). |
54 | // 2022-03-22: Inputs: Fix mouse position issues when dragging outside of boundaries. SDL_CaptureMouse() erroneously still gives out LEAVE events when hovering OS decorations. |
55 | // 2022-03-22: Inputs: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2). |
56 | // 2022-02-04: Added SDL_Renderer* parameter to ImGui_ImplSDL2_InitForSDLRenderer(), so we can use SDL_GetRendererOutputSize() instead of SDL_GL_GetDrawableSize() when bound to a SDL_Renderer. |
57 | // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. |
58 | // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[]. |
59 | // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). |
60 | // 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates. |
61 | // 2022-01-12: Update mouse inputs using SDL_MOUSEMOTION/SDL_WINDOWEVENT_LEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API. |
62 | // 2022-01-12: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted. |
63 | // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. |
64 | // 2021-08-17: Calling io.AddFocusEvent() on SDL_WINDOWEVENT_FOCUS_GAINED/SDL_WINDOWEVENT_FOCUS_LOST. |
65 | // 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using SDL_GetMouseFocus() + SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, requires SDL 2.0.5+) |
66 | // 2021-06:29: *BREAKING CHANGE* Removed 'SDL_Window* window' parameter to ImGui_ImplSDL2_NewFrame() which was unnecessary. |
67 | // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). |
68 | // 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950) |
69 | // 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends. |
70 | // 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2). |
71 | // 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state). |
72 | // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor. |
73 | // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter. |
74 | // 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application). |
75 | // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized. |
76 | // 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls. |
77 | // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. |
78 | // 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'. |
79 | // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls. |
80 | // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. |
81 | // 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples. |
82 | // 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter. |
83 | // 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText). |
84 | // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag. |
85 | // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value. |
86 | // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. |
87 | // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. |
88 | // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS). |
89 | // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes. |
90 | // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. |
91 | // 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. |
92 | // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert. |
93 | // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1). |
94 | // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers. |
95 | |
96 | #include "imgui.h" |
97 | #ifndef IMGUI_DISABLE |
98 | #include "imgui_impl_sdl2.h" |
99 | |
100 | // Clang warnings with -Weverything |
101 | #if defined(__clang__) |
102 | #pragma clang diagnostic push |
103 | #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision |
104 | #endif |
105 | |
106 | // SDL |
107 | // (the multi-viewports feature requires SDL features supported from SDL 2.0.4+. SDL 2.0.5+ is highly recommended) |
108 | #include <SDL.h> |
109 | #include <SDL_syswm.h> |
110 | #ifdef __APPLE__ |
111 | #include <TargetConditionals.h> |
112 | #endif |
113 | #ifdef __EMSCRIPTEN__ |
114 | #include <emscripten/em_js.h> |
115 | #endif |
116 | |
117 | #if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__) |
118 | #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1 |
119 | #else |
120 | #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 |
121 | #endif |
122 | #define SDL_HAS_WINDOW_ALPHA SDL_VERSION_ATLEAST(2,0,5) |
123 | #define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5) |
124 | #define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5) |
125 | #define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4) |
126 | #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) |
127 | #define SDL_HAS_DISPLAY_EVENT SDL_VERSION_ATLEAST(2,0,9) |
128 | #define SDL_HAS_SHOW_WINDOW_ACTIVATION_HINT SDL_VERSION_ATLEAST(2,0,18) |
129 | #if SDL_HAS_VULKAN |
130 | #include <SDL_vulkan.h> |
131 | #else |
132 | static const Uint32 SDL_WINDOW_VULKAN = 0x10000000; |
133 | #endif |
134 | |
135 | // SDL Data |
136 | struct ImGui_ImplSDL2_Data |
137 | { |
138 | SDL_Window* Window; |
139 | Uint32 WindowID; |
140 | SDL_Renderer* Renderer; |
141 | Uint64 Time; |
142 | char* ClipboardTextData; |
143 | bool UseVulkan; |
144 | bool WantUpdateMonitors; |
145 | |
146 | // Mouse handling |
147 | Uint32 MouseWindowID; |
148 | int MouseButtonsDown; |
149 | SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT]; |
150 | SDL_Cursor* MouseLastCursor; |
151 | int MouseLastLeaveFrame; |
152 | bool MouseCanUseGlobalState; |
153 | bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state. |
154 | |
155 | // Gamepad handling |
156 | ImVector<SDL_GameController*> Gamepads; |
157 | ImGui_ImplSDL2_GamepadMode GamepadMode; |
158 | bool WantUpdateGamepadsList; |
159 | |
160 | ImGui_ImplSDL2_Data() { memset(s: (void*)this, c: 0, n: sizeof(*this)); } |
161 | }; |
162 | |
163 | // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts |
164 | // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
165 | // FIXME: multi-context support is not well tested and probably dysfunctional in this backend. |
166 | // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. |
167 | static ImGui_ImplSDL2_Data* ImGui_ImplSDL2_GetBackendData() |
168 | { |
169 | return ImGui::GetCurrentContext() ? (ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; |
170 | } |
171 | |
172 | // Forward Declarations |
173 | static void ImGui_ImplSDL2_UpdateMonitors(); |
174 | static void ImGui_ImplSDL2_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context); |
175 | static void ImGui_ImplSDL2_ShutdownMultiViewportSupport(); |
176 | |
177 | // Functions |
178 | static const char* ImGui_ImplSDL2_GetClipboardText(ImGuiContext*) |
179 | { |
180 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
181 | if (bd->ClipboardTextData) |
182 | SDL_free(mem: bd->ClipboardTextData); |
183 | bd->ClipboardTextData = SDL_GetClipboardText(); |
184 | return bd->ClipboardTextData; |
185 | } |
186 | |
187 | static void ImGui_ImplSDL2_SetClipboardText(ImGuiContext*, const char* text) |
188 | { |
189 | SDL_SetClipboardText(text); |
190 | } |
191 | |
192 | // Note: native IME will only display if user calls SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1") _before_ SDL_CreateWindow(). |
193 | static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data) |
194 | { |
195 | if (data->WantVisible) |
196 | { |
197 | SDL_Rect r; |
198 | r.x = (int)(data->InputPos.x - viewport->Pos.x); |
199 | r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight); |
200 | r.w = 1; |
201 | r.h = (int)data->InputLineHeight; |
202 | SDL_SetTextInputRect(rect: &r); |
203 | } |
204 | } |
205 | |
206 | // Not static to allow third-party code to use that if they want to (but undocumented) |
207 | ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode); |
208 | ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode) |
209 | { |
210 | IM_UNUSED(scancode); |
211 | switch (keycode) |
212 | { |
213 | case SDLK_TAB: return ImGuiKey_Tab; |
214 | case SDLK_LEFT: return ImGuiKey_LeftArrow; |
215 | case SDLK_RIGHT: return ImGuiKey_RightArrow; |
216 | case SDLK_UP: return ImGuiKey_UpArrow; |
217 | case SDLK_DOWN: return ImGuiKey_DownArrow; |
218 | case SDLK_PAGEUP: return ImGuiKey_PageUp; |
219 | case SDLK_PAGEDOWN: return ImGuiKey_PageDown; |
220 | case SDLK_HOME: return ImGuiKey_Home; |
221 | case SDLK_END: return ImGuiKey_End; |
222 | case SDLK_INSERT: return ImGuiKey_Insert; |
223 | case SDLK_DELETE: return ImGuiKey_Delete; |
224 | case SDLK_BACKSPACE: return ImGuiKey_Backspace; |
225 | case SDLK_SPACE: return ImGuiKey_Space; |
226 | case SDLK_RETURN: return ImGuiKey_Enter; |
227 | case SDLK_ESCAPE: return ImGuiKey_Escape; |
228 | case SDLK_QUOTE: return ImGuiKey_Apostrophe; |
229 | case SDLK_COMMA: return ImGuiKey_Comma; |
230 | case SDLK_MINUS: return ImGuiKey_Minus; |
231 | case SDLK_PERIOD: return ImGuiKey_Period; |
232 | case SDLK_SLASH: return ImGuiKey_Slash; |
233 | case SDLK_SEMICOLON: return ImGuiKey_Semicolon; |
234 | case SDLK_EQUALS: return ImGuiKey_Equal; |
235 | case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket; |
236 | case SDLK_BACKSLASH: return ImGuiKey_Backslash; |
237 | case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket; |
238 | case SDLK_BACKQUOTE: return ImGuiKey_GraveAccent; |
239 | case SDLK_CAPSLOCK: return ImGuiKey_CapsLock; |
240 | case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock; |
241 | case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock; |
242 | case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen; |
243 | case SDLK_PAUSE: return ImGuiKey_Pause; |
244 | case SDLK_KP_0: return ImGuiKey_Keypad0; |
245 | case SDLK_KP_1: return ImGuiKey_Keypad1; |
246 | case SDLK_KP_2: return ImGuiKey_Keypad2; |
247 | case SDLK_KP_3: return ImGuiKey_Keypad3; |
248 | case SDLK_KP_4: return ImGuiKey_Keypad4; |
249 | case SDLK_KP_5: return ImGuiKey_Keypad5; |
250 | case SDLK_KP_6: return ImGuiKey_Keypad6; |
251 | case SDLK_KP_7: return ImGuiKey_Keypad7; |
252 | case SDLK_KP_8: return ImGuiKey_Keypad8; |
253 | case SDLK_KP_9: return ImGuiKey_Keypad9; |
254 | case SDLK_KP_PERIOD: return ImGuiKey_KeypadDecimal; |
255 | case SDLK_KP_DIVIDE: return ImGuiKey_KeypadDivide; |
256 | case SDLK_KP_MULTIPLY: return ImGuiKey_KeypadMultiply; |
257 | case SDLK_KP_MINUS: return ImGuiKey_KeypadSubtract; |
258 | case SDLK_KP_PLUS: return ImGuiKey_KeypadAdd; |
259 | case SDLK_KP_ENTER: return ImGuiKey_KeypadEnter; |
260 | case SDLK_KP_EQUALS: return ImGuiKey_KeypadEqual; |
261 | case SDLK_LCTRL: return ImGuiKey_LeftCtrl; |
262 | case SDLK_LSHIFT: return ImGuiKey_LeftShift; |
263 | case SDLK_LALT: return ImGuiKey_LeftAlt; |
264 | case SDLK_LGUI: return ImGuiKey_LeftSuper; |
265 | case SDLK_RCTRL: return ImGuiKey_RightCtrl; |
266 | case SDLK_RSHIFT: return ImGuiKey_RightShift; |
267 | case SDLK_RALT: return ImGuiKey_RightAlt; |
268 | case SDLK_RGUI: return ImGuiKey_RightSuper; |
269 | case SDLK_APPLICATION: return ImGuiKey_Menu; |
270 | case SDLK_0: return ImGuiKey_0; |
271 | case SDLK_1: return ImGuiKey_1; |
272 | case SDLK_2: return ImGuiKey_2; |
273 | case SDLK_3: return ImGuiKey_3; |
274 | case SDLK_4: return ImGuiKey_4; |
275 | case SDLK_5: return ImGuiKey_5; |
276 | case SDLK_6: return ImGuiKey_6; |
277 | case SDLK_7: return ImGuiKey_7; |
278 | case SDLK_8: return ImGuiKey_8; |
279 | case SDLK_9: return ImGuiKey_9; |
280 | case SDLK_a: return ImGuiKey_A; |
281 | case SDLK_b: return ImGuiKey_B; |
282 | case SDLK_c: return ImGuiKey_C; |
283 | case SDLK_d: return ImGuiKey_D; |
284 | case SDLK_e: return ImGuiKey_E; |
285 | case SDLK_f: return ImGuiKey_F; |
286 | case SDLK_g: return ImGuiKey_G; |
287 | case SDLK_h: return ImGuiKey_H; |
288 | case SDLK_i: return ImGuiKey_I; |
289 | case SDLK_j: return ImGuiKey_J; |
290 | case SDLK_k: return ImGuiKey_K; |
291 | case SDLK_l: return ImGuiKey_L; |
292 | case SDLK_m: return ImGuiKey_M; |
293 | case SDLK_n: return ImGuiKey_N; |
294 | case SDLK_o: return ImGuiKey_O; |
295 | case SDLK_p: return ImGuiKey_P; |
296 | case SDLK_q: return ImGuiKey_Q; |
297 | case SDLK_r: return ImGuiKey_R; |
298 | case SDLK_s: return ImGuiKey_S; |
299 | case SDLK_t: return ImGuiKey_T; |
300 | case SDLK_u: return ImGuiKey_U; |
301 | case SDLK_v: return ImGuiKey_V; |
302 | case SDLK_w: return ImGuiKey_W; |
303 | case SDLK_x: return ImGuiKey_X; |
304 | case SDLK_y: return ImGuiKey_Y; |
305 | case SDLK_z: return ImGuiKey_Z; |
306 | case SDLK_F1: return ImGuiKey_F1; |
307 | case SDLK_F2: return ImGuiKey_F2; |
308 | case SDLK_F3: return ImGuiKey_F3; |
309 | case SDLK_F4: return ImGuiKey_F4; |
310 | case SDLK_F5: return ImGuiKey_F5; |
311 | case SDLK_F6: return ImGuiKey_F6; |
312 | case SDLK_F7: return ImGuiKey_F7; |
313 | case SDLK_F8: return ImGuiKey_F8; |
314 | case SDLK_F9: return ImGuiKey_F9; |
315 | case SDLK_F10: return ImGuiKey_F10; |
316 | case SDLK_F11: return ImGuiKey_F11; |
317 | case SDLK_F12: return ImGuiKey_F12; |
318 | case SDLK_F13: return ImGuiKey_F13; |
319 | case SDLK_F14: return ImGuiKey_F14; |
320 | case SDLK_F15: return ImGuiKey_F15; |
321 | case SDLK_F16: return ImGuiKey_F16; |
322 | case SDLK_F17: return ImGuiKey_F17; |
323 | case SDLK_F18: return ImGuiKey_F18; |
324 | case SDLK_F19: return ImGuiKey_F19; |
325 | case SDLK_F20: return ImGuiKey_F20; |
326 | case SDLK_F21: return ImGuiKey_F21; |
327 | case SDLK_F22: return ImGuiKey_F22; |
328 | case SDLK_F23: return ImGuiKey_F23; |
329 | case SDLK_F24: return ImGuiKey_F24; |
330 | case SDLK_AC_BACK: return ImGuiKey_AppBack; |
331 | case SDLK_AC_FORWARD: return ImGuiKey_AppForward; |
332 | default: break; |
333 | } |
334 | return ImGuiKey_None; |
335 | } |
336 | |
337 | static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods) |
338 | { |
339 | ImGuiIO& io = ImGui::GetIO(); |
340 | io.AddKeyEvent(key: ImGuiMod_Ctrl, down: (sdl_key_mods & KMOD_CTRL) != 0); |
341 | io.AddKeyEvent(key: ImGuiMod_Shift, down: (sdl_key_mods & KMOD_SHIFT) != 0); |
342 | io.AddKeyEvent(key: ImGuiMod_Alt, down: (sdl_key_mods & KMOD_ALT) != 0); |
343 | io.AddKeyEvent(key: ImGuiMod_Super, down: (sdl_key_mods & KMOD_GUI) != 0); |
344 | } |
345 | |
346 | static ImGuiViewport* ImGui_ImplSDL2_GetViewportForWindowID(Uint32 window_id) |
347 | { |
348 | return ImGui::FindViewportByPlatformHandle(platform_handle: (void*)(intptr_t)window_id); |
349 | } |
350 | |
351 | // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
352 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
353 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
354 | // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
355 | bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event) |
356 | { |
357 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
358 | IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?"); |
359 | ImGuiIO& io = ImGui::GetIO(); |
360 | |
361 | switch (event->type) |
362 | { |
363 | case SDL_MOUSEMOTION: |
364 | { |
365 | if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->motion.windowID) == nullptr) |
366 | return false; |
367 | ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y); |
368 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
369 | { |
370 | int window_x, window_y; |
371 | SDL_GetWindowPosition(window: SDL_GetWindowFromID(id: event->motion.windowID), x: &window_x, y: &window_y); |
372 | mouse_pos.x += window_x; |
373 | mouse_pos.y += window_y; |
374 | } |
375 | io.AddMouseSourceEvent(source: event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
376 | io.AddMousePosEvent(x: mouse_pos.x, y: mouse_pos.y); |
377 | return true; |
378 | } |
379 | case SDL_MOUSEWHEEL: |
380 | { |
381 | if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->wheel.windowID) == nullptr) |
382 | return false; |
383 | //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY); |
384 | #if SDL_VERSION_ATLEAST(2,0,18) // If this fails to compile on Emscripten: update to latest Emscripten! |
385 | float wheel_x = -event->wheel.preciseX; |
386 | float wheel_y = event->wheel.preciseY; |
387 | #else |
388 | float wheel_x = -(float)event->wheel.x; |
389 | float wheel_y = (float)event->wheel.y; |
390 | #endif |
391 | #if defined(__EMSCRIPTEN__) && !SDL_VERSION_ATLEAST(2,31,0) |
392 | wheel_x /= 100.0f; |
393 | #endif |
394 | io.AddMouseSourceEvent(source: event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
395 | io.AddMouseWheelEvent(wheel_x, wheel_y); |
396 | return true; |
397 | } |
398 | case SDL_MOUSEBUTTONDOWN: |
399 | case SDL_MOUSEBUTTONUP: |
400 | { |
401 | if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->button.windowID) == nullptr) |
402 | return false; |
403 | int mouse_button = -1; |
404 | if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; } |
405 | if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; } |
406 | if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; } |
407 | if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; } |
408 | if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; } |
409 | if (mouse_button == -1) |
410 | break; |
411 | io.AddMouseSourceEvent(source: event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
412 | io.AddMouseButtonEvent(button: mouse_button, down: (event->type == SDL_MOUSEBUTTONDOWN)); |
413 | bd->MouseButtonsDown = (event->type == SDL_MOUSEBUTTONDOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button)); |
414 | return true; |
415 | } |
416 | case SDL_TEXTINPUT: |
417 | { |
418 | if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->text.windowID) == nullptr) |
419 | return false; |
420 | io.AddInputCharactersUTF8(str: event->text.text); |
421 | return true; |
422 | } |
423 | case SDL_KEYDOWN: |
424 | case SDL_KEYUP: |
425 | { |
426 | if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->key.windowID) == nullptr) |
427 | return false; |
428 | ImGui_ImplSDL2_UpdateKeyModifiers(sdl_key_mods: (SDL_Keymod)event->key.keysym.mod); |
429 | ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(keycode: event->key.keysym.sym, scancode: event->key.keysym.scancode); |
430 | io.AddKeyEvent(key, down: (event->type == SDL_KEYDOWN)); |
431 | io.SetKeyEventNativeData(key, native_keycode: event->key.keysym.sym, native_scancode: event->key.keysym.scancode, native_legacy_index: event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions. |
432 | return true; |
433 | } |
434 | #if SDL_HAS_DISPLAY_EVENT |
435 | case SDL_DISPLAYEVENT: |
436 | { |
437 | // 2.0.26 has SDL_DISPLAYEVENT_CONNECTED/SDL_DISPLAYEVENT_DISCONNECTED/SDL_DISPLAYEVENT_ORIENTATION, |
438 | // so change of DPI/Scaling are not reflected in this event. (SDL3 has it) |
439 | bd->WantUpdateMonitors = true; |
440 | return true; |
441 | } |
442 | #endif |
443 | case SDL_WINDOWEVENT: |
444 | { |
445 | ImGuiViewport* viewport = ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->window.windowID); |
446 | if (viewport == NULL) |
447 | return false; |
448 | |
449 | // - When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right. |
450 | // - However we won't get a correct LEAVE event for a captured window. |
451 | // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late, |
452 | // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why |
453 | // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details. |
454 | Uint8 window_event = event->window.event; |
455 | if (window_event == SDL_WINDOWEVENT_ENTER) |
456 | { |
457 | bd->MouseWindowID = event->window.windowID; |
458 | bd->MouseLastLeaveFrame = 0; |
459 | } |
460 | if (window_event == SDL_WINDOWEVENT_LEAVE) |
461 | bd->MouseLastLeaveFrame = ImGui::GetFrameCount() + 1; |
462 | if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED) |
463 | io.AddFocusEvent(focused: true); |
464 | else if (window_event == SDL_WINDOWEVENT_FOCUS_LOST) |
465 | io.AddFocusEvent(focused: false); |
466 | else if (window_event == SDL_WINDOWEVENT_CLOSE) |
467 | viewport->PlatformRequestClose = true; |
468 | else if (window_event == SDL_WINDOWEVENT_MOVED) |
469 | viewport->PlatformRequestMove = true; |
470 | else if (window_event == SDL_WINDOWEVENT_RESIZED) |
471 | viewport->PlatformRequestResize = true; |
472 | return true; |
473 | } |
474 | case SDL_CONTROLLERDEVICEADDED: |
475 | case SDL_CONTROLLERDEVICEREMOVED: |
476 | { |
477 | bd->WantUpdateGamepadsList = true; |
478 | return true; |
479 | } |
480 | } |
481 | return false; |
482 | } |
483 | |
484 | #ifdef __EMSCRIPTEN__ |
485 | EM_JS(void, ImGui_ImplSDL2_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); }); |
486 | #endif |
487 | |
488 | static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context) |
489 | { |
490 | ImGuiIO& io = ImGui::GetIO(); |
491 | IMGUI_CHECKVERSION(); |
492 | IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); |
493 | |
494 | // Check and store if we are on a SDL backend that supports global mouse position |
495 | // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list) |
496 | bool mouse_can_use_global_state = false; |
497 | #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE |
498 | const char* sdl_backend = SDL_GetCurrentVideoDriver(); |
499 | const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN"}; |
500 | for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++) |
501 | if (strncmp(s1: sdl_backend, s2: global_mouse_whitelist[n], n: strlen(s: global_mouse_whitelist[n])) == 0) |
502 | mouse_can_use_global_state = true; |
503 | #endif |
504 | |
505 | // Setup backend capabilities flags |
506 | ImGui_ImplSDL2_Data* bd = IM_NEW(ImGui_ImplSDL2_Data)(); |
507 | io.BackendPlatformUserData = (void*)bd; |
508 | io.BackendPlatformName = "imgui_impl_sdl2"; |
509 | io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) |
510 | io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) |
511 | if (mouse_can_use_global_state) |
512 | io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) |
513 | |
514 | bd->Window = window; |
515 | bd->WindowID = SDL_GetWindowID(window); |
516 | bd->Renderer = renderer; |
517 | |
518 | // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960) |
519 | // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame. |
520 | bd->MouseCanUseGlobalState = mouse_can_use_global_state; |
521 | #ifndef __APPLE__ |
522 | bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState; |
523 | #else |
524 | bd->MouseCanReportHoveredViewport = false; |
525 | #endif |
526 | |
527 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
528 | platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText; |
529 | platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText; |
530 | platform_io.Platform_ClipboardUserData = nullptr; |
531 | platform_io.Platform_SetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData; |
532 | #ifdef __EMSCRIPTEN__ |
533 | platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url); return true; }; |
534 | #endif |
535 | |
536 | // Update monitor a first time during init |
537 | ImGui_ImplSDL2_UpdateMonitors(); |
538 | |
539 | // Gamepad handling |
540 | bd->GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst; |
541 | bd->WantUpdateGamepadsList = true; |
542 | |
543 | // Load mouse cursors |
544 | bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_ARROW); |
545 | bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_IBEAM); |
546 | bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZEALL); |
547 | bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZENS); |
548 | bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZEWE); |
549 | bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZENESW); |
550 | bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZENWSE); |
551 | bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_HAND); |
552 | bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_NO); |
553 | |
554 | // Set platform dependent data in viewport |
555 | // Our mouse update function expect PlatformHandle to be filled for the main viewport |
556 | ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
557 | main_viewport->PlatformHandle = (void*)(intptr_t)bd->WindowID; |
558 | main_viewport->PlatformHandleRaw = nullptr; |
559 | SDL_SysWMinfo info; |
560 | SDL_VERSION(&info.version); |
561 | if (SDL_GetWindowWMInfo(window, info: &info)) |
562 | { |
563 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) |
564 | main_viewport->PlatformHandleRaw = (void*)info.info.win.window; |
565 | #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) |
566 | main_viewport->PlatformHandleRaw = (void*)info.info.cocoa.window; |
567 | #endif |
568 | } |
569 | |
570 | // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event. |
571 | // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered. |
572 | // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application. |
573 | // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click: |
574 | // you can ignore SDL_MOUSEBUTTONDOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED) |
575 | #ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH |
576 | SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, value: "1"); |
577 | #endif |
578 | |
579 | // From 2.0.18: Enable native IME. |
580 | // IMPORTANT: This is used at the time of SDL_CreateWindow() so this will only affects secondary windows, if any. |
581 | // For the main window to be affected, your application needs to call this manually before calling SDL_CreateWindow(). |
582 | #ifdef SDL_HINT_IME_SHOW_UI |
583 | SDL_SetHint(SDL_HINT_IME_SHOW_UI, value: "1"); |
584 | #endif |
585 | |
586 | // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710) |
587 | #ifdef SDL_HINT_MOUSE_AUTO_CAPTURE |
588 | SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); |
589 | #endif |
590 | |
591 | // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports. |
592 | // We left the call to ImGui_ImplSDL2_InitMultiViewportSupport() outside of #ifdef to avoid unused-function warnings. |
593 | if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports) |
594 | ImGui_ImplSDL2_InitMultiViewportSupport(window, sdl_gl_context); |
595 | |
596 | return true; |
597 | } |
598 | |
599 | bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context) |
600 | { |
601 | return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context); |
602 | } |
603 | |
604 | bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window) |
605 | { |
606 | #if !SDL_HAS_VULKAN |
607 | IM_ASSERT(0 && "Unsupported"); |
608 | #endif |
609 | if (!ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr)) |
610 | return false; |
611 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
612 | bd->UseVulkan = true; |
613 | return true; |
614 | } |
615 | |
616 | bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window) |
617 | { |
618 | #if !defined(_WIN32) |
619 | IM_ASSERT(0 && "Unsupported"); |
620 | #endif |
621 | return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr); |
622 | } |
623 | |
624 | bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window) |
625 | { |
626 | return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr); |
627 | } |
628 | |
629 | bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer) |
630 | { |
631 | return ImGui_ImplSDL2_Init(window, renderer, sdl_gl_context: nullptr); |
632 | } |
633 | |
634 | bool ImGui_ImplSDL2_InitForOther(SDL_Window* window) |
635 | { |
636 | return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr); |
637 | } |
638 | |
639 | static void ImGui_ImplSDL2_CloseGamepads(); |
640 | |
641 | void ImGui_ImplSDL2_Shutdown() |
642 | { |
643 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
644 | IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); |
645 | ImGuiIO& io = ImGui::GetIO(); |
646 | |
647 | ImGui_ImplSDL2_ShutdownMultiViewportSupport(); |
648 | |
649 | if (bd->ClipboardTextData) |
650 | SDL_free(mem: bd->ClipboardTextData); |
651 | for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) |
652 | SDL_FreeCursor(cursor: bd->MouseCursors[cursor_n]); |
653 | ImGui_ImplSDL2_CloseGamepads(); |
654 | |
655 | io.BackendPlatformName = nullptr; |
656 | io.BackendPlatformUserData = nullptr; |
657 | io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport); |
658 | IM_DELETE(p: bd); |
659 | } |
660 | |
661 | // This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4. |
662 | static void ImGui_ImplSDL2_UpdateMouseData() |
663 | { |
664 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
665 | ImGuiIO& io = ImGui::GetIO(); |
666 | |
667 | // We forward mouse input when hovered or captured (via SDL_MOUSEMOTION) or when focused (below) |
668 | #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE |
669 | // 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 |
670 | SDL_CaptureMouse(enabled: (bd->MouseButtonsDown != 0) ? SDL_TRUE : SDL_FALSE); |
671 | SDL_Window* focused_window = SDL_GetKeyboardFocus(); |
672 | const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL2_GetViewportForWindowID(window_id: SDL_GetWindowID(window: focused_window)) != NULL)); |
673 | #else |
674 | SDL_Window* focused_window = bd->Window; |
675 | 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 |
676 | #endif |
677 | |
678 | if (is_app_focused) |
679 | { |
680 | // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user) |
681 | if (io.WantSetMousePos) |
682 | { |
683 | #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE |
684 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
685 | SDL_WarpMouseGlobal(x: (int)io.MousePos.x, y: (int)io.MousePos.y); |
686 | else |
687 | #endif |
688 | SDL_WarpMouseInWindow(window: bd->Window, x: (int)io.MousePos.x, y: (int)io.MousePos.y); |
689 | } |
690 | |
691 | // (Optional) Fallback to provide mouse position when focused (SDL_MOUSEMOTION already provides this when hovered or captured) |
692 | if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0) |
693 | { |
694 | // 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) |
695 | // 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) |
696 | int mouse_x, mouse_y, window_x, window_y; |
697 | SDL_GetGlobalMouseState(x: &mouse_x, y: &mouse_y); |
698 | if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)) |
699 | { |
700 | SDL_GetWindowPosition(window: focused_window, x: &window_x, y: &window_y); |
701 | mouse_x -= window_x; |
702 | mouse_y -= window_y; |
703 | } |
704 | io.AddMousePosEvent(x: (float)mouse_x, y: (float)mouse_y); |
705 | } |
706 | } |
707 | |
708 | // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering. |
709 | // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic. |
710 | // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag. |
711 | // 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 |
712 | // 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 |
713 | // by the backend, and use its flawed heuristic to guess the viewport behind. |
714 | // - [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). |
715 | if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport) |
716 | { |
717 | ImGuiID mouse_viewport_id = 0; |
718 | if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL2_GetViewportForWindowID(window_id: bd->MouseWindowID)) |
719 | mouse_viewport_id = mouse_viewport->ID; |
720 | io.AddMouseViewportEvent(id: mouse_viewport_id); |
721 | } |
722 | } |
723 | |
724 | static void ImGui_ImplSDL2_UpdateMouseCursor() |
725 | { |
726 | ImGuiIO& io = ImGui::GetIO(); |
727 | if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) |
728 | return; |
729 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
730 | |
731 | ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); |
732 | if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) |
733 | { |
734 | // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor |
735 | SDL_ShowCursor(toggle: SDL_FALSE); |
736 | } |
737 | else |
738 | { |
739 | // Show OS mouse cursor |
740 | SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]; |
741 | if (bd->MouseLastCursor != expected_cursor) |
742 | { |
743 | SDL_SetCursor(cursor: expected_cursor); // SDL function doesn't have an early out (see #6113) |
744 | bd->MouseLastCursor = expected_cursor; |
745 | } |
746 | SDL_ShowCursor(toggle: SDL_TRUE); |
747 | } |
748 | } |
749 | |
750 | static void ImGui_ImplSDL2_CloseGamepads() |
751 | { |
752 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
753 | if (bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual) |
754 | for (SDL_GameController* gamepad : bd->Gamepads) |
755 | SDL_GameControllerClose(gamecontroller: gamepad); |
756 | bd->Gamepads.resize(new_size: 0); |
757 | } |
758 | |
759 | void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array, int manual_gamepads_count) |
760 | { |
761 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
762 | ImGui_ImplSDL2_CloseGamepads(); |
763 | if (mode == ImGui_ImplSDL2_GamepadMode_Manual) |
764 | { |
765 | IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0); |
766 | for (int n = 0; n < manual_gamepads_count; n++) |
767 | bd->Gamepads.push_back(v: manual_gamepads_array[n]); |
768 | } |
769 | else |
770 | { |
771 | IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0); |
772 | bd->WantUpdateGamepadsList = true; |
773 | } |
774 | bd->GamepadMode = mode; |
775 | } |
776 | |
777 | static void ImGui_ImplSDL2_UpdateGamepadButton(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no) |
778 | { |
779 | bool merged_value = false; |
780 | for (SDL_GameController* gamepad : bd->Gamepads) |
781 | merged_value |= SDL_GameControllerGetButton(gamecontroller: gamepad, button: button_no) != 0; |
782 | io.AddKeyEvent(key, down: merged_value); |
783 | } |
784 | |
785 | static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; } |
786 | static void ImGui_ImplSDL2_UpdateGamepadAnalog(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no, float v0, float v1) |
787 | { |
788 | float merged_value = 0.0f; |
789 | for (SDL_GameController* gamepad : bd->Gamepads) |
790 | { |
791 | float vn = Saturate(v: (float)(SDL_GameControllerGetAxis(gamecontroller: gamepad, axis: axis_no) - v0) / (float)(v1 - v0)); |
792 | if (merged_value < vn) |
793 | merged_value = vn; |
794 | } |
795 | io.AddKeyAnalogEvent(key, down: merged_value > 0.1f, v: merged_value); |
796 | } |
797 | |
798 | static void ImGui_ImplSDL2_UpdateGamepads() |
799 | { |
800 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
801 | ImGuiIO& io = ImGui::GetIO(); |
802 | |
803 | // Update list of controller(s) to use |
804 | if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual) |
805 | { |
806 | ImGui_ImplSDL2_CloseGamepads(); |
807 | int joystick_count = SDL_NumJoysticks(); |
808 | for (int n = 0; n < joystick_count; n++) |
809 | if (SDL_IsGameController(joystick_index: n)) |
810 | if (SDL_GameController* gamepad = SDL_GameControllerOpen(joystick_index: n)) |
811 | { |
812 | bd->Gamepads.push_back(v: gamepad); |
813 | if (bd->GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst) |
814 | break; |
815 | } |
816 | bd->WantUpdateGamepadsList = false; |
817 | } |
818 | |
819 | // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs. |
820 | if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) |
821 | return; |
822 | io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; |
823 | if (bd->Gamepads.Size == 0) |
824 | return; |
825 | io.BackendFlags |= ImGuiBackendFlags_HasGamepad; |
826 | |
827 | // Update gamepad inputs |
828 | const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value. |
829 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadStart, button_no: SDL_CONTROLLER_BUTTON_START); |
830 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadBack, button_no: SDL_CONTROLLER_BUTTON_BACK); |
831 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceLeft, button_no: SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square |
832 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceRight, button_no: SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle |
833 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceUp, button_no: SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle |
834 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceDown, button_no: SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross |
835 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadLeft, button_no: SDL_CONTROLLER_BUTTON_DPAD_LEFT); |
836 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadRight, button_no: SDL_CONTROLLER_BUTTON_DPAD_RIGHT); |
837 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadUp, button_no: SDL_CONTROLLER_BUTTON_DPAD_UP); |
838 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadDown, button_no: SDL_CONTROLLER_BUTTON_DPAD_DOWN); |
839 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadL1, button_no: SDL_CONTROLLER_BUTTON_LEFTSHOULDER); |
840 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadR1, button_no: SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); |
841 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadL2, axis_no: SDL_CONTROLLER_AXIS_TRIGGERLEFT, v0: 0.0f, v1: 32767); |
842 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadR2, axis_no: SDL_CONTROLLER_AXIS_TRIGGERRIGHT, v0: 0.0f, v1: 32767); |
843 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadL3, button_no: SDL_CONTROLLER_BUTTON_LEFTSTICK); |
844 | ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadR3, button_no: SDL_CONTROLLER_BUTTON_RIGHTSTICK); |
845 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickLeft, axis_no: SDL_CONTROLLER_AXIS_LEFTX, v0: -thumb_dead_zone, v1: -32768); |
846 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickRight, axis_no: SDL_CONTROLLER_AXIS_LEFTX, v0: +thumb_dead_zone, v1: +32767); |
847 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickUp, axis_no: SDL_CONTROLLER_AXIS_LEFTY, v0: -thumb_dead_zone, v1: -32768); |
848 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickDown, axis_no: SDL_CONTROLLER_AXIS_LEFTY, v0: +thumb_dead_zone, v1: +32767); |
849 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickLeft, axis_no: SDL_CONTROLLER_AXIS_RIGHTX, v0: -thumb_dead_zone, v1: -32768); |
850 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickRight, axis_no: SDL_CONTROLLER_AXIS_RIGHTX, v0: +thumb_dead_zone, v1: +32767); |
851 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickUp, axis_no: SDL_CONTROLLER_AXIS_RIGHTY, v0: -thumb_dead_zone, v1: -32768); |
852 | ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickDown, axis_no: SDL_CONTROLLER_AXIS_RIGHTY, v0: +thumb_dead_zone, v1: +32767); |
853 | } |
854 | |
855 | // FIXME: Note that doesn't update with DPI/Scaling change only as SDL2 doesn't have an event for it (SDL3 has). |
856 | static void ImGui_ImplSDL2_UpdateMonitors() |
857 | { |
858 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
859 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
860 | platform_io.Monitors.resize(new_size: 0); |
861 | bd->WantUpdateMonitors = false; |
862 | int display_count = SDL_GetNumVideoDisplays(); |
863 | for (int n = 0; n < display_count; n++) |
864 | { |
865 | // 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. |
866 | ImGuiPlatformMonitor monitor; |
867 | SDL_Rect r; |
868 | SDL_GetDisplayBounds(displayIndex: n, rect: &r); |
869 | monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y); |
870 | monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h); |
871 | #if SDL_HAS_USABLE_DISPLAY_BOUNDS |
872 | SDL_GetDisplayUsableBounds(displayIndex: n, rect: &r); |
873 | monitor.WorkPos = ImVec2((float)r.x, (float)r.y); |
874 | monitor.WorkSize = ImVec2((float)r.w, (float)r.h); |
875 | #endif |
876 | #if SDL_HAS_PER_MONITOR_DPI |
877 | // FIXME-VIEWPORT: On MacOS SDL reports actual monitor DPI scale, ignoring OS configuration. We may want to set |
878 | // DpiScale to cocoa_window.backingScaleFactor here. |
879 | float dpi = 0.0f; |
880 | if (!SDL_GetDisplayDPI(displayIndex: n, ddpi: &dpi, hdpi: nullptr, vdpi: nullptr)) |
881 | { |
882 | if (dpi <= 0.0f) |
883 | continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902. |
884 | monitor.DpiScale = dpi / 96.0f; |
885 | } |
886 | #endif |
887 | monitor.PlatformHandle = (void*)(intptr_t)n; |
888 | platform_io.Monitors.push_back(v: monitor); |
889 | } |
890 | } |
891 | |
892 | void ImGui_ImplSDL2_NewFrame() |
893 | { |
894 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
895 | IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?"); |
896 | ImGuiIO& io = ImGui::GetIO(); |
897 | |
898 | // Setup display size (every frame to accommodate for window resizing) |
899 | int w, h; |
900 | int display_w, display_h; |
901 | SDL_GetWindowSize(window: bd->Window, w: &w, h: &h); |
902 | if (SDL_GetWindowFlags(window: bd->Window) & SDL_WINDOW_MINIMIZED) |
903 | w = h = 0; |
904 | if (bd->Renderer != nullptr) |
905 | SDL_GetRendererOutputSize(renderer: bd->Renderer, w: &display_w, h: &display_h); |
906 | #if SDL_HAS_VULKAN |
907 | else if (SDL_GetWindowFlags(window: bd->Window) & SDL_WINDOW_VULKAN) |
908 | SDL_Vulkan_GetDrawableSize(window: bd->Window, w: &display_w, h: &display_h); |
909 | #endif |
910 | else |
911 | SDL_GL_GetDrawableSize(window: bd->Window, w: &display_w, h: &display_h); |
912 | io.DisplaySize = ImVec2((float)w, (float)h); |
913 | if (w > 0 && h > 0) |
914 | io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); |
915 | |
916 | // Update monitors |
917 | if (bd->WantUpdateMonitors) |
918 | ImGui_ImplSDL2_UpdateMonitors(); |
919 | |
920 | // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) |
921 | // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644) |
922 | static Uint64 frequency = SDL_GetPerformanceFrequency(); |
923 | Uint64 current_time = SDL_GetPerformanceCounter(); |
924 | if (current_time <= bd->Time) |
925 | current_time = bd->Time + 1; |
926 | io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f); |
927 | bd->Time = current_time; |
928 | |
929 | if (bd->MouseLastLeaveFrame && bd->MouseLastLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0) |
930 | { |
931 | bd->MouseWindowID = 0; |
932 | bd->MouseLastLeaveFrame = 0; |
933 | io.AddMousePosEvent(x: -FLT_MAX, y: -FLT_MAX); |
934 | } |
935 | |
936 | // Our io.AddMouseViewportEvent() calls will only be valid when not capturing. |
937 | // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but testing for payload reduces noise and potential side-effects. |
938 | if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr) |
939 | io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; |
940 | else |
941 | io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport; |
942 | |
943 | ImGui_ImplSDL2_UpdateMouseData(); |
944 | ImGui_ImplSDL2_UpdateMouseCursor(); |
945 | |
946 | // Update game controllers (if enabled and available) |
947 | ImGui_ImplSDL2_UpdateGamepads(); |
948 | } |
949 | |
950 | //-------------------------------------------------------------------------------------------------------- |
951 | // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT |
952 | // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. |
953 | // 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.. |
954 | //-------------------------------------------------------------------------------------------------------- |
955 | |
956 | // Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data. |
957 | struct ImGui_ImplSDL2_ViewportData |
958 | { |
959 | SDL_Window* Window; |
960 | Uint32 WindowID; |
961 | bool WindowOwned; |
962 | SDL_GLContext GLContext; |
963 | |
964 | ImGui_ImplSDL2_ViewportData() { Window = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; } |
965 | ~ImGui_ImplSDL2_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); } |
966 | }; |
967 | |
968 | static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport) |
969 | { |
970 | ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
971 | ImGui_ImplSDL2_ViewportData* vd = IM_NEW(ImGui_ImplSDL2_ViewportData)(); |
972 | viewport->PlatformUserData = vd; |
973 | |
974 | ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
975 | ImGui_ImplSDL2_ViewportData* main_viewport_data = (ImGui_ImplSDL2_ViewportData*)main_viewport->PlatformUserData; |
976 | |
977 | // Share GL resources with main context |
978 | bool use_opengl = (main_viewport_data->GLContext != nullptr); |
979 | SDL_GLContext backup_context = nullptr; |
980 | if (use_opengl) |
981 | { |
982 | backup_context = SDL_GL_GetCurrentContext(); |
983 | SDL_GL_SetAttribute(attr: SDL_GL_SHARE_WITH_CURRENT_CONTEXT, value: 1); |
984 | SDL_GL_MakeCurrent(window: main_viewport_data->Window, context: main_viewport_data->GLContext); |
985 | } |
986 | |
987 | Uint32 sdl_flags = 0; |
988 | sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0); |
989 | sdl_flags |= SDL_GetWindowFlags(window: bd->Window) & SDL_WINDOW_ALLOW_HIGHDPI; |
990 | sdl_flags |= SDL_WINDOW_HIDDEN; |
991 | sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0; |
992 | sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE; |
993 | #if !defined(_WIN32) |
994 | // See SDL hack in ImGui_ImplSDL2_ShowWindow(). |
995 | sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_SKIP_TASKBAR : 0; |
996 | #endif |
997 | #if SDL_HAS_ALWAYS_ON_TOP |
998 | sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0; |
999 | #endif |
1000 | vd->Window = SDL_CreateWindow(title: "No Title Yet", x: (int)viewport->Pos.x, y: (int)viewport->Pos.y, w: (int)viewport->Size.x, h: (int)viewport->Size.y, flags: sdl_flags); |
1001 | vd->WindowOwned = true; |
1002 | if (use_opengl) |
1003 | { |
1004 | vd->GLContext = SDL_GL_CreateContext(window: vd->Window); |
1005 | SDL_GL_SetSwapInterval(interval: 0); |
1006 | } |
1007 | if (use_opengl && backup_context) |
1008 | SDL_GL_MakeCurrent(window: vd->Window, context: backup_context); |
1009 | |
1010 | viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window: vd->Window); |
1011 | viewport->PlatformHandleRaw = nullptr; |
1012 | SDL_SysWMinfo info; |
1013 | SDL_VERSION(&info.version); |
1014 | if (SDL_GetWindowWMInfo(window: vd->Window, info: &info)) |
1015 | { |
1016 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) |
1017 | viewport->PlatformHandleRaw = info.info.win.window; |
1018 | #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) |
1019 | viewport->PlatformHandleRaw = (void*)info.info.cocoa.window; |
1020 | #endif |
1021 | } |
1022 | } |
1023 | |
1024 | static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport) |
1025 | { |
1026 | if (ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData) |
1027 | { |
1028 | if (vd->GLContext && vd->WindowOwned) |
1029 | SDL_GL_DeleteContext(context: vd->GLContext); |
1030 | if (vd->Window && vd->WindowOwned) |
1031 | SDL_DestroyWindow(window: vd->Window); |
1032 | vd->GLContext = nullptr; |
1033 | vd->Window = nullptr; |
1034 | IM_DELETE(p: vd); |
1035 | } |
1036 | viewport->PlatformUserData = viewport->PlatformHandle = nullptr; |
1037 | } |
1038 | |
1039 | static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport) |
1040 | { |
1041 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1042 | #if defined(_WIN32) && !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP || WINAPI_FAMILY == WINAPI_FAMILY_GAMES)) |
1043 | HWND hwnd = (HWND)viewport->PlatformHandleRaw; |
1044 | |
1045 | // SDL hack: Hide icon from task bar |
1046 | // 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. |
1047 | if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) |
1048 | { |
1049 | LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE); |
1050 | ex_style &= ~WS_EX_APPWINDOW; |
1051 | ex_style |= WS_EX_TOOLWINDOW; |
1052 | ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style); |
1053 | } |
1054 | #endif |
1055 | |
1056 | #if SDL_HAS_SHOW_WINDOW_ACTIVATION_HINT |
1057 | SDL_SetHint(SDL_HINT_WINDOW_NO_ACTIVATION_WHEN_SHOWN, value: (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "1": "0"); |
1058 | #elif defined(_WIN32) |
1059 | // SDL hack: SDL always activate/focus windows :/ |
1060 | if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) |
1061 | { |
1062 | ::ShowWindow(hwnd, SW_SHOWNA); |
1063 | return; |
1064 | } |
1065 | #endif |
1066 | SDL_ShowWindow(window: vd->Window); |
1067 | } |
1068 | |
1069 | static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport) |
1070 | { |
1071 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1072 | int x = 0, y = 0; |
1073 | SDL_GetWindowPosition(window: vd->Window, x: &x, y: &y); |
1074 | return ImVec2((float)x, (float)y); |
1075 | } |
1076 | |
1077 | static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) |
1078 | { |
1079 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1080 | SDL_SetWindowPosition(window: vd->Window, x: (int)pos.x, y: (int)pos.y); |
1081 | } |
1082 | |
1083 | static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport) |
1084 | { |
1085 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1086 | int w = 0, h = 0; |
1087 | SDL_GetWindowSize(window: vd->Window, w: &w, h: &h); |
1088 | return ImVec2((float)w, (float)h); |
1089 | } |
1090 | |
1091 | static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) |
1092 | { |
1093 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1094 | SDL_SetWindowSize(window: vd->Window, w: (int)size.x, h: (int)size.y); |
1095 | } |
1096 | |
1097 | static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport, const char* title) |
1098 | { |
1099 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1100 | SDL_SetWindowTitle(window: vd->Window, title); |
1101 | } |
1102 | |
1103 | #if SDL_HAS_WINDOW_ALPHA |
1104 | static void ImGui_ImplSDL2_SetWindowAlpha(ImGuiViewport* viewport, float alpha) |
1105 | { |
1106 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1107 | SDL_SetWindowOpacity(window: vd->Window, opacity: alpha); |
1108 | } |
1109 | #endif |
1110 | |
1111 | static void ImGui_ImplSDL2_SetWindowFocus(ImGuiViewport* viewport) |
1112 | { |
1113 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1114 | SDL_RaiseWindow(window: vd->Window); |
1115 | } |
1116 | |
1117 | static bool ImGui_ImplSDL2_GetWindowFocus(ImGuiViewport* viewport) |
1118 | { |
1119 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1120 | return (SDL_GetWindowFlags(window: vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; |
1121 | } |
1122 | |
1123 | static bool ImGui_ImplSDL2_GetWindowMinimized(ImGuiViewport* viewport) |
1124 | { |
1125 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1126 | return (SDL_GetWindowFlags(window: vd->Window) & SDL_WINDOW_MINIMIZED) != 0; |
1127 | } |
1128 | |
1129 | static void ImGui_ImplSDL2_RenderWindow(ImGuiViewport* viewport, void*) |
1130 | { |
1131 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1132 | if (vd->GLContext) |
1133 | SDL_GL_MakeCurrent(window: vd->Window, context: vd->GLContext); |
1134 | } |
1135 | |
1136 | static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport, void*) |
1137 | { |
1138 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1139 | if (vd->GLContext) |
1140 | { |
1141 | SDL_GL_MakeCurrent(window: vd->Window, context: vd->GLContext); |
1142 | SDL_GL_SwapWindow(window: vd->Window); |
1143 | } |
1144 | } |
1145 | |
1146 | // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface) |
1147 | // SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this. |
1148 | #if SDL_HAS_VULKAN |
1149 | #include <SDL_vulkan.h> |
1150 | static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface) |
1151 | { |
1152 | ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData; |
1153 | (void)vk_allocator; |
1154 | SDL_bool ret = SDL_Vulkan_CreateSurface(window: vd->Window, instance: (VkInstance)vk_instance, surface: (VkSurfaceKHR*)out_vk_surface); |
1155 | return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY |
1156 | } |
1157 | #endif // SDL_HAS_VULKAN |
1158 | |
1159 | static void ImGui_ImplSDL2_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context) |
1160 | { |
1161 | // Register platform interface (will be coupled with a renderer interface) |
1162 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
1163 | platform_io.Platform_CreateWindow = ImGui_ImplSDL2_CreateWindow; |
1164 | platform_io.Platform_DestroyWindow = ImGui_ImplSDL2_DestroyWindow; |
1165 | platform_io.Platform_ShowWindow = ImGui_ImplSDL2_ShowWindow; |
1166 | platform_io.Platform_SetWindowPos = ImGui_ImplSDL2_SetWindowPos; |
1167 | platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos; |
1168 | platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize; |
1169 | platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize; |
1170 | platform_io.Platform_SetWindowFocus = ImGui_ImplSDL2_SetWindowFocus; |
1171 | platform_io.Platform_GetWindowFocus = ImGui_ImplSDL2_GetWindowFocus; |
1172 | platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL2_GetWindowMinimized; |
1173 | platform_io.Platform_SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle; |
1174 | platform_io.Platform_RenderWindow = ImGui_ImplSDL2_RenderWindow; |
1175 | platform_io.Platform_SwapBuffers = ImGui_ImplSDL2_SwapBuffers; |
1176 | #if SDL_HAS_WINDOW_ALPHA |
1177 | platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL2_SetWindowAlpha; |
1178 | #endif |
1179 | #if SDL_HAS_VULKAN |
1180 | platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface; |
1181 | #endif |
1182 | |
1183 | // Register main window handle (which is owned by the main application, not by us) |
1184 | // 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. |
1185 | ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
1186 | ImGui_ImplSDL2_ViewportData* vd = IM_NEW(ImGui_ImplSDL2_ViewportData)(); |
1187 | vd->Window = window; |
1188 | vd->WindowID = SDL_GetWindowID(window); |
1189 | vd->WindowOwned = false; |
1190 | vd->GLContext = sdl_gl_context; |
1191 | main_viewport->PlatformUserData = vd; |
1192 | main_viewport->PlatformHandle = (void*)(intptr_t)vd->WindowID; |
1193 | } |
1194 | |
1195 | static void ImGui_ImplSDL2_ShutdownMultiViewportSupport() |
1196 | { |
1197 | ImGui::DestroyPlatformWindows(); |
1198 | } |
1199 | |
1200 | //----------------------------------------------------------------------------- |
1201 | |
1202 | #if defined(__clang__) |
1203 | #pragma clang diagnostic pop |
1204 | #endif |
1205 | |
1206 | #endif // #ifndef IMGUI_DISABLE |
1207 |
Definitions
- ImGui_ImplSDL2_Data
- ImGui_ImplSDL2_Data
- ImGui_ImplSDL2_GetBackendData
- ImGui_ImplSDL2_GetClipboardText
- ImGui_ImplSDL2_SetClipboardText
- ImGui_ImplSDL2_PlatformSetImeData
- ImGui_ImplSDL2_KeyEventToImGuiKey
- ImGui_ImplSDL2_UpdateKeyModifiers
- ImGui_ImplSDL2_GetViewportForWindowID
- ImGui_ImplSDL2_ProcessEvent
- ImGui_ImplSDL2_Init
- ImGui_ImplSDL2_InitForOpenGL
- ImGui_ImplSDL2_InitForVulkan
- ImGui_ImplSDL2_InitForD3D
- ImGui_ImplSDL2_InitForMetal
- ImGui_ImplSDL2_InitForSDLRenderer
- ImGui_ImplSDL2_InitForOther
- ImGui_ImplSDL2_Shutdown
- ImGui_ImplSDL2_UpdateMouseData
- ImGui_ImplSDL2_UpdateMouseCursor
- ImGui_ImplSDL2_CloseGamepads
- ImGui_ImplSDL2_SetGamepadMode
- ImGui_ImplSDL2_UpdateGamepadButton
- Saturate
- ImGui_ImplSDL2_UpdateGamepadAnalog
- ImGui_ImplSDL2_UpdateGamepads
- ImGui_ImplSDL2_UpdateMonitors
- ImGui_ImplSDL2_NewFrame
- ImGui_ImplSDL2_ViewportData
- ImGui_ImplSDL2_ViewportData
- ~ImGui_ImplSDL2_ViewportData
- ImGui_ImplSDL2_CreateWindow
- ImGui_ImplSDL2_DestroyWindow
- ImGui_ImplSDL2_ShowWindow
- ImGui_ImplSDL2_GetWindowPos
- ImGui_ImplSDL2_SetWindowPos
- ImGui_ImplSDL2_GetWindowSize
- ImGui_ImplSDL2_SetWindowSize
- ImGui_ImplSDL2_SetWindowTitle
- ImGui_ImplSDL2_SetWindowAlpha
- ImGui_ImplSDL2_SetWindowFocus
- ImGui_ImplSDL2_GetWindowFocus
- ImGui_ImplSDL2_GetWindowMinimized
- ImGui_ImplSDL2_RenderWindow
- ImGui_ImplSDL2_SwapBuffers
- ImGui_ImplSDL2_CreateVkSurface
- ImGui_ImplSDL2_InitMultiViewportSupport
Learn to use CMake with our Intro Training
Find out more