98#include "imgui_impl_sdl2.h"
101#if defined(__clang__)
102#pragma clang diagnostic push
103#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
109#include <SDL_syswm.h>
111#include <TargetConditionals.h>
114#include <emscripten/em_js.h>
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
120#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
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)
130#include <SDL_vulkan.h>
132static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
169 return ImGui::GetCurrentContext() ? (
ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData :
nullptr;
173static void ImGui_ImplSDL2_UpdateMonitors();
174static void ImGui_ImplSDL2_InitMultiViewportSupport(SDL_Window* window,
void* sdl_gl_context);
175static void ImGui_ImplSDL2_ShutdownMultiViewportSupport();
178static const char* ImGui_ImplSDL2_GetClipboardText(ImGuiContext*)
187static void ImGui_ImplSDL2_SetClipboardText(ImGuiContext*,
const char* text)
189 SDL_SetClipboardText(text);
193static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
195 if (data->WantVisible)
198 r.x = (int)(data->InputPos.x - viewport->Pos.x);
199 r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight);
201 r.h = (int)data->InputLineHeight;
202 SDL_SetTextInputRect(&r);
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;
334 return ImGuiKey_None;
337static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
339 ImGuiIO& io = ImGui::GetIO();
340 io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & KMOD_CTRL) != 0);
341 io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & KMOD_SHIFT) != 0);
342 io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & KMOD_ALT) != 0);
343 io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & KMOD_GUI) != 0);
346static ImGuiViewport* ImGui_ImplSDL2_GetViewportForWindowID(Uint32 window_id)
348 return ImGui::FindViewportByPlatformHandle((
void*)(intptr_t)window_id);
358 IM_ASSERT(bd !=
nullptr &&
"Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
359 ImGuiIO& io = ImGui::GetIO();
363 case SDL_MOUSEMOTION:
365 if (ImGui_ImplSDL2_GetViewportForWindowID(event->motion.windowID) ==
nullptr)
367 ImVec2 mouse_pos((
float)event->motion.x, (
float)event->motion.y);
368 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
370 int window_x, window_y;
371 SDL_GetWindowPosition(SDL_GetWindowFromID(event->motion.windowID), &window_x, &window_y);
372 mouse_pos.x += window_x;
373 mouse_pos.y += window_y;
375 io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
376 io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
381 if (ImGui_ImplSDL2_GetViewportForWindowID(event->wheel.windowID) ==
nullptr)
384#if SDL_VERSION_ATLEAST(2,0,18)
385 float wheel_x = -
event->wheel.preciseX;
386 float wheel_y =
event->wheel.preciseY;
388 float wheel_x = -(float)event->wheel.x;
389 float wheel_y = (float)event->wheel.y;
391#if defined(__EMSCRIPTEN__) && !SDL_VERSION_ATLEAST(2,31,0)
394 io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
395 io.AddMouseWheelEvent(wheel_x, wheel_y);
398 case SDL_MOUSEBUTTONDOWN:
399 case SDL_MOUSEBUTTONUP:
401 if (ImGui_ImplSDL2_GetViewportForWindowID(event->button.windowID) ==
nullptr)
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)
411 io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
412 io.AddMouseButtonEvent(mouse_button, (event->type == SDL_MOUSEBUTTONDOWN));
418 if (ImGui_ImplSDL2_GetViewportForWindowID(event->text.windowID) ==
nullptr)
420 io.AddInputCharactersUTF8(event->text.text);
426 if (ImGui_ImplSDL2_GetViewportForWindowID(event->key.windowID) ==
nullptr)
428 ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
432 io.AddKeyEvent(key, (event->type == SDL_KEYDOWN));
433 io.SetKeyEventNativeData(key, event->key.keysym.sym, event->key.keysym.scancode, event->key.keysym.scancode);
436#if SDL_HAS_DISPLAY_EVENT
437 case SDL_DISPLAYEVENT:
445 case SDL_WINDOWEVENT:
447 ImGuiViewport* viewport = ImGui_ImplSDL2_GetViewportForWindowID(event->window.windowID);
448 if (viewport == NULL)
456 Uint8 window_event =
event->window.event;
457 if (window_event == SDL_WINDOWEVENT_ENTER)
462 if (window_event == SDL_WINDOWEVENT_LEAVE)
464 if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
465 io.AddFocusEvent(
true);
466 else if (window_event == SDL_WINDOWEVENT_FOCUS_LOST)
467 io.AddFocusEvent(
false);
468 else if (window_event == SDL_WINDOWEVENT_CLOSE)
469 viewport->PlatformRequestClose =
true;
470 else if (window_event == SDL_WINDOWEVENT_MOVED)
471 viewport->PlatformRequestMove =
true;
472 else if (window_event == SDL_WINDOWEVENT_RESIZED)
473 viewport->PlatformRequestResize =
true;
476 case SDL_CONTROLLERDEVICEADDED:
477 case SDL_CONTROLLERDEVICEREMOVED:
487EM_JS(
void, ImGui_ImplSDL2_EmscriptenOpenURL, (
char const* url), { url = url ? UTF8ToString(url) : null;
if (url) window.open(url,
'_blank'); });
490static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer,
void* sdl_gl_context)
492 ImGuiIO& io = ImGui::GetIO();
493 IMGUI_CHECKVERSION();
494 IM_ASSERT(io.BackendPlatformUserData ==
nullptr &&
"Already initialized a platform backend!");
498 bool mouse_can_use_global_state =
false;
499#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
500 const char* sdl_backend = SDL_GetCurrentVideoDriver();
501 const char* global_mouse_whitelist[] = {
"windows",
"cocoa",
"x11",
"DIVE",
"VMAN" };
502 for (
int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
503 if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
504 mouse_can_use_global_state =
true;
509 io.BackendPlatformUserData = (
void*)bd;
510 io.BackendPlatformName =
"imgui_impl_sdl2";
511 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
512 io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;
513 if (mouse_can_use_global_state)
514 io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports;
517 bd->
WindowID = SDL_GetWindowID(window);
529 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
530 platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
531 platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
532 platform_io.Platform_ClipboardUserData =
nullptr;
533 platform_io.Platform_SetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData;
535 platform_io.Platform_OpenInShellFn = [](ImGuiContext*,
const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url);
return true; };
539 ImGui_ImplSDL2_UpdateMonitors();
542 bd->
GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst;
546 bd->
MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
547 bd->
MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
548 bd->
MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
549 bd->
MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
550 bd->
MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
551 bd->
MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
552 bd->
MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
553 bd->
MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
554 bd->
MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
558 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
559 main_viewport->PlatformHandle = (
void*)(intptr_t)bd->
WindowID;
560 main_viewport->PlatformHandleRaw =
nullptr;
562 SDL_VERSION(&info.version);
563 if (SDL_GetWindowWMInfo(window, &info))
565#if defined(SDL_VIDEO_DRIVER_WINDOWS)
566 main_viewport->PlatformHandleRaw = (
void*)info.info.win.window;
567#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
568 main_viewport->PlatformHandleRaw = (
void*)info.info.cocoa.window;
577#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
578 SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH,
"1");
584#ifdef SDL_HINT_IME_SHOW_UI
585 SDL_SetHint(SDL_HINT_IME_SHOW_UI,
"1");
589#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
590 SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE,
"0");
595 if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports)
596 ImGui_ImplSDL2_InitMultiViewportSupport(window, sdl_gl_context);
603 return ImGui_ImplSDL2_Init(window,
nullptr, sdl_gl_context);
609 IM_ASSERT(0 &&
"Unsupported");
611 if (!ImGui_ImplSDL2_Init(window,
nullptr,
nullptr))
621 IM_ASSERT(0 &&
"Unsupported");
623 return ImGui_ImplSDL2_Init(window,
nullptr,
nullptr);
628 return ImGui_ImplSDL2_Init(window,
nullptr,
nullptr);
633 return ImGui_ImplSDL2_Init(window, renderer,
nullptr);
638 return ImGui_ImplSDL2_Init(window,
nullptr,
nullptr);
641static void ImGui_ImplSDL2_CloseGamepads();
646 IM_ASSERT(bd !=
nullptr &&
"No platform backend to shutdown, or already shutdown?");
647 ImGuiIO& io = ImGui::GetIO();
649 ImGui_ImplSDL2_ShutdownMultiViewportSupport();
653 for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
655 ImGui_ImplSDL2_CloseGamepads();
657 io.BackendPlatformName =
nullptr;
658 io.BackendPlatformUserData =
nullptr;
659 io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
664static void ImGui_ImplSDL2_UpdateMouseData()
667 ImGuiIO& io = ImGui::GetIO();
670#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
673 SDL_Window* focused_window = SDL_GetKeyboardFocus();
674 const bool is_app_focused = (focused_window && (bd->
Window == focused_window || ImGui_ImplSDL2_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL));
676 SDL_Window* focused_window = bd->
Window;
677 const bool is_app_focused = (SDL_GetWindowFlags(bd->
Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
683 if (io.WantSetMousePos)
685#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
686 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
687 SDL_WarpMouseGlobal((
int)io.MousePos.x, (
int)io.MousePos.y);
690 SDL_WarpMouseInWindow(bd->
Window, (
int)io.MousePos.x, (
int)io.MousePos.y);
698 int mouse_x, mouse_y, window_x, window_y;
699 SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
700 if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
702 SDL_GetWindowPosition(focused_window, &window_x, &window_y);
706 io.AddMousePosEvent((
float)mouse_x, (
float)mouse_y);
717 if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
719 ImGuiID mouse_viewport_id = 0;
720 if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL2_GetViewportForWindowID(bd->
MouseWindowID))
721 mouse_viewport_id = mouse_viewport->ID;
722 io.AddMouseViewportEvent(mouse_viewport_id);
726static void ImGui_ImplSDL2_UpdateMouseCursor()
728 ImGuiIO& io = ImGui::GetIO();
729 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
733 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
734 if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
737 SDL_ShowCursor(SDL_FALSE);
745 SDL_SetCursor(expected_cursor);
748 SDL_ShowCursor(SDL_TRUE);
752static void ImGui_ImplSDL2_CloseGamepads()
755 if (bd->
GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
756 for (SDL_GameController* gamepad : bd->Gamepads)
757 SDL_GameControllerClose(gamepad);
764 ImGui_ImplSDL2_CloseGamepads();
765 if (mode == ImGui_ImplSDL2_GamepadMode_Manual)
767 IM_ASSERT(manual_gamepads_array !=
nullptr && manual_gamepads_count > 0);
768 for (
int n = 0; n < manual_gamepads_count; n++)
769 bd->
Gamepads.push_back(manual_gamepads_array[n]);
773 IM_ASSERT(manual_gamepads_array ==
nullptr && manual_gamepads_count <= 0);
779static void ImGui_ImplSDL2_UpdateGamepadButton(
ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no)
781 bool merged_value =
false;
782 for (SDL_GameController* gamepad : bd->Gamepads)
783 merged_value |= SDL_GameControllerGetButton(gamepad, button_no) != 0;
784 io.AddKeyEvent(key, merged_value);
787static inline float Saturate(
float v) {
return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
788static void ImGui_ImplSDL2_UpdateGamepadAnalog(
ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no,
float v0,
float v1)
790 float merged_value = 0.0f;
791 for (SDL_GameController* gamepad : bd->Gamepads)
793 float vn = Saturate((
float)(SDL_GameControllerGetAxis(gamepad, axis_no) - v0) / (
float)(v1 - v0));
794 if (merged_value < vn)
797 io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
800static void ImGui_ImplSDL2_UpdateGamepads()
803 ImGuiIO& io = ImGui::GetIO();
808 ImGui_ImplSDL2_CloseGamepads();
809 int joystick_count = SDL_NumJoysticks();
810 for (
int n = 0; n < joystick_count; n++)
811 if (SDL_IsGameController(n))
812 if (SDL_GameController* gamepad = SDL_GameControllerOpen(n))
815 if (bd->
GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst)
822 if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
824 io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
827 io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
830 const int thumb_dead_zone = 8000;
831 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START);
832 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK);
833 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X);
834 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B);
835 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y);
836 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A);
837 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
838 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
839 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP);
840 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
841 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
842 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
843 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767);
844 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767);
845 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK);
846 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK);
847 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
848 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
849 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768);
850 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
851 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768);
852 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767);
853 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768);
854 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767);
858static void ImGui_ImplSDL2_UpdateMonitors()
861 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
862 platform_io.Monitors.resize(0);
864 int display_count = SDL_GetNumVideoDisplays();
865 for (
int n = 0; n < display_count; n++)
868 ImGuiPlatformMonitor monitor;
870 SDL_GetDisplayBounds(n, &r);
871 monitor.MainPos = monitor.WorkPos = ImVec2((
float)r.x, (
float)r.y);
872 monitor.MainSize = monitor.WorkSize = ImVec2((
float)r.w, (
float)r.h);
873#if SDL_HAS_USABLE_DISPLAY_BOUNDS
874 SDL_GetDisplayUsableBounds(n, &r);
875 monitor.WorkPos = ImVec2((
float)r.x, (
float)r.y);
876 monitor.WorkSize = ImVec2((
float)r.w, (
float)r.h);
878#if SDL_HAS_PER_MONITOR_DPI
882 if (!SDL_GetDisplayDPI(n, &dpi,
nullptr,
nullptr))
886 monitor.DpiScale = dpi / 96.0f;
889 monitor.PlatformHandle = (
void*)(intptr_t)n;
890 platform_io.Monitors.push_back(monitor);
897 IM_ASSERT(bd !=
nullptr &&
"Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
898 ImGuiIO& io = ImGui::GetIO();
902 int display_w, display_h;
903 SDL_GetWindowSize(bd->
Window, &w, &h);
904 if (SDL_GetWindowFlags(bd->
Window) & SDL_WINDOW_MINIMIZED)
907 SDL_GetRendererOutputSize(bd->
Renderer, &display_w, &display_h);
909 else if (SDL_GetWindowFlags(bd->
Window) & SDL_WINDOW_VULKAN)
910 SDL_Vulkan_GetDrawableSize(bd->
Window, &display_w, &display_h);
913 SDL_GL_GetDrawableSize(bd->
Window, &display_w, &display_h);
914 io.DisplaySize = ImVec2((
float)w, (
float)h);
916 io.DisplayFramebufferScale = ImVec2((
float)display_w / w, (
float)display_h / h);
920 ImGui_ImplSDL2_UpdateMonitors();
924 static Uint64 frequency = SDL_GetPerformanceFrequency();
925 Uint64 current_time = SDL_GetPerformanceCounter();
926 if (current_time <= bd->Time)
927 current_time = bd->
Time + 1;
928 io.DeltaTime = bd->
Time > 0 ? (float)((
double)(current_time - bd->
Time) / frequency) : (float)(1.0f / 60.0f);
929 bd->
Time = current_time;
935 io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
941 io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
943 io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport;
945 ImGui_ImplSDL2_UpdateMouseData();
946 ImGui_ImplSDL2_UpdateMouseCursor();
949 ImGui_ImplSDL2_UpdateGamepads();
970static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport)
974 viewport->PlatformUserData = vd;
976 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
980 bool use_opengl = (main_viewport_data->GLContext !=
nullptr);
981 SDL_GLContext backup_context =
nullptr;
984 backup_context = SDL_GL_GetCurrentContext();
985 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
986 SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
989 Uint32 sdl_flags = 0;
990 sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->
UseVulkan ? SDL_WINDOW_VULKAN : 0);
991 sdl_flags |= SDL_GetWindowFlags(bd->
Window) & SDL_WINDOW_ALLOW_HIGHDPI;
992 sdl_flags |= SDL_WINDOW_HIDDEN;
993 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
994 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
997 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_SKIP_TASKBAR : 0;
999#if SDL_HAS_ALWAYS_ON_TOP
1000 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
1002 vd->Window = SDL_CreateWindow(
"No Title Yet", (
int)viewport->Pos.x, (
int)viewport->Pos.y, (
int)viewport->Size.x, (
int)viewport->Size.y, sdl_flags);
1003 vd->WindowOwned =
true;
1006 vd->GLContext = SDL_GL_CreateContext(vd->Window);
1007 SDL_GL_SetSwapInterval(0);
1009 if (use_opengl && backup_context)
1010 SDL_GL_MakeCurrent(vd->Window, backup_context);
1012 viewport->PlatformHandle = (
void*)(intptr_t)SDL_GetWindowID(vd->Window);
1013 viewport->PlatformHandleRaw =
nullptr;
1015 SDL_VERSION(&info.version);
1016 if (SDL_GetWindowWMInfo(vd->Window, &info))
1018#if defined(SDL_VIDEO_DRIVER_WINDOWS)
1019 viewport->PlatformHandleRaw = info.info.win.window;
1020#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
1021 viewport->PlatformHandleRaw = (
void*)info.info.cocoa.window;
1026static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport)
1030 if (vd->GLContext && vd->WindowOwned)
1031 SDL_GL_DeleteContext(vd->GLContext);
1032 if (vd->Window && vd->WindowOwned)
1033 SDL_DestroyWindow(vd->Window);
1034 vd->GLContext =
nullptr;
1035 vd->Window =
nullptr;
1038 viewport->PlatformUserData = viewport->PlatformHandle =
nullptr;
1041static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport)
1044#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP || WINAPI_FAMILY == WINAPI_FAMILY_GAMES))
1045 HWND hwnd = (HWND)viewport->PlatformHandleRaw;
1049 if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
1051 LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
1052 ex_style &= ~WS_EX_APPWINDOW;
1053 ex_style |= WS_EX_TOOLWINDOW;
1054 ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
1058#if SDL_HAS_SHOW_WINDOW_ACTIVATION_HINT
1059 SDL_SetHint(SDL_HINT_WINDOW_NO_ACTIVATION_WHEN_SHOWN, (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ?
"1" :
"0");
1060#elif defined(_WIN32)
1062 if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
1064 ::ShowWindow(hwnd, SW_SHOWNA);
1068 SDL_ShowWindow(vd->Window);
1071static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport)
1075 SDL_GetWindowPosition(vd->Window, &x, &y);
1076 return ImVec2((
float)x, (
float)y);
1079static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
1082 SDL_SetWindowPosition(vd->Window, (
int)pos.x, (
int)pos.y);
1085static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport)
1089 SDL_GetWindowSize(vd->Window, &w, &h);
1090 return ImVec2((
float)w, (
float)h);
1093static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
1096 SDL_SetWindowSize(vd->Window, (
int)
size.x, (
int)
size.y);
1099static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport,
const char* title)
1102 SDL_SetWindowTitle(vd->Window, title);
1105#if SDL_HAS_WINDOW_ALPHA
1106static void ImGui_ImplSDL2_SetWindowAlpha(ImGuiViewport* viewport,
float alpha)
1109 SDL_SetWindowOpacity(vd->Window, alpha);
1113static void ImGui_ImplSDL2_SetWindowFocus(ImGuiViewport* viewport)
1116 SDL_RaiseWindow(vd->Window);
1119static bool ImGui_ImplSDL2_GetWindowFocus(ImGuiViewport* viewport)
1122 return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
1125static bool ImGui_ImplSDL2_GetWindowMinimized(ImGuiViewport* viewport)
1128 return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0;
1131static void ImGui_ImplSDL2_RenderWindow(ImGuiViewport* viewport,
void*)
1135 SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
1138static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport,
void*)
1143 SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
1144 SDL_GL_SwapWindow(vd->Window);
1151#include <SDL_vulkan.h>
1152static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance,
const void* vk_allocator, ImU64* out_vk_surface)
1156 SDL_bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (VkSurfaceKHR*)out_vk_surface);
1161static void ImGui_ImplSDL2_InitMultiViewportSupport(SDL_Window* window,
void* sdl_gl_context)
1164 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
1165 platform_io.Platform_CreateWindow = ImGui_ImplSDL2_CreateWindow;
1166 platform_io.Platform_DestroyWindow = ImGui_ImplSDL2_DestroyWindow;
1167 platform_io.Platform_ShowWindow = ImGui_ImplSDL2_ShowWindow;
1168 platform_io.Platform_SetWindowPos = ImGui_ImplSDL2_SetWindowPos;
1169 platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos;
1170 platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize;
1171 platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize;
1172 platform_io.Platform_SetWindowFocus = ImGui_ImplSDL2_SetWindowFocus;
1173 platform_io.Platform_GetWindowFocus = ImGui_ImplSDL2_GetWindowFocus;
1174 platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL2_GetWindowMinimized;
1175 platform_io.Platform_SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle;
1176 platform_io.Platform_RenderWindow = ImGui_ImplSDL2_RenderWindow;
1177 platform_io.Platform_SwapBuffers = ImGui_ImplSDL2_SwapBuffers;
1178#if SDL_HAS_WINDOW_ALPHA
1179 platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL2_SetWindowAlpha;
1182 platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface;
1187 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
1190 vd->
WindowID = SDL_GetWindowID(window);
1193 main_viewport->PlatformUserData = vd;
1194 main_viewport->PlatformHandle = (
void*)(intptr_t)vd->
WindowID;
1197static void ImGui_ImplSDL2_ShutdownMultiViewportSupport()
1199 ImGui::DestroyPlatformWindows();
1204#if defined(__clang__)
1205#pragma clang diagnostic pop
void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController **manual_gamepads_array, int manual_gamepads_count)
bool ImGui_ImplSDL2_InitForVulkan(SDL_Window *window)
bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window *window, SDL_Renderer *renderer)
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window *window, void *sdl_gl_context)
bool ImGui_ImplSDL2_InitForOther(SDL_Window *window)
bool ImGui_ImplSDL2_InitForMetal(SDL_Window *window)
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
void ImGui_ImplSDL2_NewFrame()
bool ImGui_ImplSDL2_InitForD3D(SDL_Window *window)
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event *event)
void ImGui_ImplSDL2_Shutdown()
size_t size(std::string_view utf8)
ImGui_ImplSDL2_GamepadMode GamepadMode
SDL_Cursor * MouseLastCursor
SDL_Cursor * MouseCursors[ImGuiMouseCursor_COUNT]
bool MouseCanUseGlobalState
ImVector< SDL_GameController * > Gamepads
bool MouseCanReportHoveredViewport
bool WantUpdateGamepadsList
ImGui_ImplSDL2_ViewportData()
~ImGui_ImplSDL2_ViewportData()