89#include "imgui_impl_sdl2.h"
93#pragma clang diagnostic push
94#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
100#include <SDL_syswm.h>
102#include <TargetConditionals.h>
105#include <emscripten/em_js.h>
108#if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
109#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
111#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
113#define SDL_HAS_WINDOW_ALPHA SDL_VERSION_ATLEAST(2,0,5)
114#define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5)
115#define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5)
116#define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4)
117#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
118#define SDL_HAS_DISPLAY_EVENT SDL_VERSION_ATLEAST(2,0,9)
120static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
156 return ImGui::GetCurrentContext() ? (
ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData :
nullptr;
160static void ImGui_ImplSDL2_UpdateMonitors();
161static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window,
void* sdl_gl_context);
162static void ImGui_ImplSDL2_ShutdownPlatformInterface();
165static const char* ImGui_ImplSDL2_GetClipboardText(
void*)
174static void ImGui_ImplSDL2_SetClipboardText(
void*,
const char* text)
176 SDL_SetClipboardText(text);
180static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
182 if (data->WantVisible)
185 r.x = (int)(data->InputPos.x - viewport->Pos.x);
186 r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight);
188 r.h = (int)data->InputLineHeight;
189 SDL_SetTextInputRect(&r);
193static ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
198 case SDLK_TAB:
return ImGuiKey_Tab;
199 case SDLK_LEFT:
return ImGuiKey_LeftArrow;
200 case SDLK_RIGHT:
return ImGuiKey_RightArrow;
201 case SDLK_UP:
return ImGuiKey_UpArrow;
202 case SDLK_DOWN:
return ImGuiKey_DownArrow;
203 case SDLK_PAGEUP:
return ImGuiKey_PageUp;
204 case SDLK_PAGEDOWN:
return ImGuiKey_PageDown;
205 case SDLK_HOME:
return ImGuiKey_Home;
206 case SDLK_END:
return ImGuiKey_End;
207 case SDLK_INSERT:
return ImGuiKey_Insert;
208 case SDLK_DELETE:
return ImGuiKey_Delete;
209 case SDLK_BACKSPACE:
return ImGuiKey_Backspace;
210 case SDLK_SPACE:
return ImGuiKey_Space;
211 case SDLK_RETURN:
return ImGuiKey_Enter;
212 case SDLK_ESCAPE:
return ImGuiKey_Escape;
213 case SDLK_QUOTE:
return ImGuiKey_Apostrophe;
214 case SDLK_COMMA:
return ImGuiKey_Comma;
215 case SDLK_MINUS:
return ImGuiKey_Minus;
216 case SDLK_PERIOD:
return ImGuiKey_Period;
217 case SDLK_SLASH:
return ImGuiKey_Slash;
218 case SDLK_SEMICOLON:
return ImGuiKey_Semicolon;
219 case SDLK_EQUALS:
return ImGuiKey_Equal;
220 case SDLK_LEFTBRACKET:
return ImGuiKey_LeftBracket;
221 case SDLK_BACKSLASH:
return ImGuiKey_Backslash;
222 case SDLK_RIGHTBRACKET:
return ImGuiKey_RightBracket;
223 case SDLK_BACKQUOTE:
return ImGuiKey_GraveAccent;
224 case SDLK_CAPSLOCK:
return ImGuiKey_CapsLock;
225 case SDLK_SCROLLLOCK:
return ImGuiKey_ScrollLock;
226 case SDLK_NUMLOCKCLEAR:
return ImGuiKey_NumLock;
227 case SDLK_PRINTSCREEN:
return ImGuiKey_PrintScreen;
228 case SDLK_PAUSE:
return ImGuiKey_Pause;
229 case SDLK_KP_0:
return ImGuiKey_Keypad0;
230 case SDLK_KP_1:
return ImGuiKey_Keypad1;
231 case SDLK_KP_2:
return ImGuiKey_Keypad2;
232 case SDLK_KP_3:
return ImGuiKey_Keypad3;
233 case SDLK_KP_4:
return ImGuiKey_Keypad4;
234 case SDLK_KP_5:
return ImGuiKey_Keypad5;
235 case SDLK_KP_6:
return ImGuiKey_Keypad6;
236 case SDLK_KP_7:
return ImGuiKey_Keypad7;
237 case SDLK_KP_8:
return ImGuiKey_Keypad8;
238 case SDLK_KP_9:
return ImGuiKey_Keypad9;
239 case SDLK_KP_PERIOD:
return ImGuiKey_KeypadDecimal;
240 case SDLK_KP_DIVIDE:
return ImGuiKey_KeypadDivide;
241 case SDLK_KP_MULTIPLY:
return ImGuiKey_KeypadMultiply;
242 case SDLK_KP_MINUS:
return ImGuiKey_KeypadSubtract;
243 case SDLK_KP_PLUS:
return ImGuiKey_KeypadAdd;
244 case SDLK_KP_ENTER:
return ImGuiKey_KeypadEnter;
245 case SDLK_KP_EQUALS:
return ImGuiKey_KeypadEqual;
246 case SDLK_LCTRL:
return ImGuiKey_LeftCtrl;
247 case SDLK_LSHIFT:
return ImGuiKey_LeftShift;
248 case SDLK_LALT:
return ImGuiKey_LeftAlt;
249 case SDLK_LGUI:
return ImGuiKey_LeftSuper;
250 case SDLK_RCTRL:
return ImGuiKey_RightCtrl;
251 case SDLK_RSHIFT:
return ImGuiKey_RightShift;
252 case SDLK_RALT:
return ImGuiKey_RightAlt;
253 case SDLK_RGUI:
return ImGuiKey_RightSuper;
254 case SDLK_APPLICATION:
return ImGuiKey_Menu;
255 case SDLK_0:
return ImGuiKey_0;
256 case SDLK_1:
return ImGuiKey_1;
257 case SDLK_2:
return ImGuiKey_2;
258 case SDLK_3:
return ImGuiKey_3;
259 case SDLK_4:
return ImGuiKey_4;
260 case SDLK_5:
return ImGuiKey_5;
261 case SDLK_6:
return ImGuiKey_6;
262 case SDLK_7:
return ImGuiKey_7;
263 case SDLK_8:
return ImGuiKey_8;
264 case SDLK_9:
return ImGuiKey_9;
265 case SDLK_a:
return ImGuiKey_A;
266 case SDLK_b:
return ImGuiKey_B;
267 case SDLK_c:
return ImGuiKey_C;
268 case SDLK_d:
return ImGuiKey_D;
269 case SDLK_e:
return ImGuiKey_E;
270 case SDLK_f:
return ImGuiKey_F;
271 case SDLK_g:
return ImGuiKey_G;
272 case SDLK_h:
return ImGuiKey_H;
273 case SDLK_i:
return ImGuiKey_I;
274 case SDLK_j:
return ImGuiKey_J;
275 case SDLK_k:
return ImGuiKey_K;
276 case SDLK_l:
return ImGuiKey_L;
277 case SDLK_m:
return ImGuiKey_M;
278 case SDLK_n:
return ImGuiKey_N;
279 case SDLK_o:
return ImGuiKey_O;
280 case SDLK_p:
return ImGuiKey_P;
281 case SDLK_q:
return ImGuiKey_Q;
282 case SDLK_r:
return ImGuiKey_R;
283 case SDLK_s:
return ImGuiKey_S;
284 case SDLK_t:
return ImGuiKey_T;
285 case SDLK_u:
return ImGuiKey_U;
286 case SDLK_v:
return ImGuiKey_V;
287 case SDLK_w:
return ImGuiKey_W;
288 case SDLK_x:
return ImGuiKey_X;
289 case SDLK_y:
return ImGuiKey_Y;
290 case SDLK_z:
return ImGuiKey_Z;
291 case SDLK_F1:
return ImGuiKey_F1;
292 case SDLK_F2:
return ImGuiKey_F2;
293 case SDLK_F3:
return ImGuiKey_F3;
294 case SDLK_F4:
return ImGuiKey_F4;
295 case SDLK_F5:
return ImGuiKey_F5;
296 case SDLK_F6:
return ImGuiKey_F6;
297 case SDLK_F7:
return ImGuiKey_F7;
298 case SDLK_F8:
return ImGuiKey_F8;
299 case SDLK_F9:
return ImGuiKey_F9;
300 case SDLK_F10:
return ImGuiKey_F10;
301 case SDLK_F11:
return ImGuiKey_F11;
302 case SDLK_F12:
return ImGuiKey_F12;
303 case SDLK_F13:
return ImGuiKey_F13;
304 case SDLK_F14:
return ImGuiKey_F14;
305 case SDLK_F15:
return ImGuiKey_F15;
306 case SDLK_F16:
return ImGuiKey_F16;
307 case SDLK_F17:
return ImGuiKey_F17;
308 case SDLK_F18:
return ImGuiKey_F18;
309 case SDLK_F19:
return ImGuiKey_F19;
310 case SDLK_F20:
return ImGuiKey_F20;
311 case SDLK_F21:
return ImGuiKey_F21;
312 case SDLK_F22:
return ImGuiKey_F22;
313 case SDLK_F23:
return ImGuiKey_F23;
314 case SDLK_F24:
return ImGuiKey_F24;
315 case SDLK_AC_BACK:
return ImGuiKey_AppBack;
316 case SDLK_AC_FORWARD:
return ImGuiKey_AppForward;
319 return ImGuiKey_None;
322static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
324 ImGuiIO& io = ImGui::GetIO();
325 io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & KMOD_CTRL) != 0);
326 io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & KMOD_SHIFT) != 0);
327 io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & KMOD_ALT) != 0);
328 io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & KMOD_GUI) != 0);
339 IM_ASSERT(bd !=
nullptr &&
"Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
340 ImGuiIO& io = ImGui::GetIO();
344 case SDL_MOUSEMOTION:
346 ImVec2 mouse_pos((
float)event->motion.x, (
float)event->motion.y);
347 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
349 int window_x, window_y;
350 SDL_GetWindowPosition(SDL_GetWindowFromID(event->motion.windowID), &window_x, &window_y);
351 mouse_pos.x += window_x;
352 mouse_pos.y += window_y;
354 io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
355 io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
361#if SDL_VERSION_ATLEAST(2,0,18)
362 float wheel_x = -
event->wheel.preciseX;
363 float wheel_y =
event->wheel.preciseY;
365 float wheel_x = -(float)event->wheel.x;
366 float wheel_y = (float)event->wheel.y;
371 io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
372 io.AddMouseWheelEvent(wheel_x, wheel_y);
375 case SDL_MOUSEBUTTONDOWN:
376 case SDL_MOUSEBUTTONUP:
378 int mouse_button = -1;
379 if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
380 if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
381 if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
382 if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
383 if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
384 if (mouse_button == -1)
386 io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
387 io.AddMouseButtonEvent(mouse_button, (event->type == SDL_MOUSEBUTTONDOWN));
393 io.AddInputCharactersUTF8(event->text.text);
399 ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
400 ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(event->key.keysym.sym, event->key.keysym.scancode);
401 io.AddKeyEvent(key, (event->type == SDL_KEYDOWN));
402 io.SetKeyEventNativeData(key, event->key.keysym.sym, event->key.keysym.scancode, event->key.keysym.scancode);
405#if SDL_HAS_DISPLAY_EVENT
406 case SDL_DISPLAYEVENT:
414 case SDL_WINDOWEVENT:
421 Uint8 window_event =
event->window.event;
422 if (window_event == SDL_WINDOWEVENT_ENTER)
427 if (window_event == SDL_WINDOWEVENT_LEAVE)
429 if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
430 io.AddFocusEvent(
true);
431 else if (window_event == SDL_WINDOWEVENT_FOCUS_LOST)
432 io.AddFocusEvent(
false);
433 if (window_event == SDL_WINDOWEVENT_CLOSE || window_event == SDL_WINDOWEVENT_MOVED || window_event == SDL_WINDOWEVENT_RESIZED)
434 if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((
void*)SDL_GetWindowFromID(event->window.windowID)))
436 if (window_event == SDL_WINDOWEVENT_CLOSE)
437 viewport->PlatformRequestClose =
true;
438 if (window_event == SDL_WINDOWEVENT_MOVED)
439 viewport->PlatformRequestMove =
true;
440 if (window_event == SDL_WINDOWEVENT_RESIZED)
441 viewport->PlatformRequestResize =
true;
446 case SDL_CONTROLLERDEVICEADDED:
447 case SDL_CONTROLLERDEVICEREMOVED:
457EM_JS(
void, ImGui_ImplSDL2_EmscriptenOpenURL, (
char const* url), { url = url ? UTF8ToString(url) : null;
if (url) window.open(url,
'_blank'); });
460static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer,
void* sdl_gl_context)
462 ImGuiIO& io = ImGui::GetIO();
463 IMGUI_CHECKVERSION();
464 IM_ASSERT(io.BackendPlatformUserData ==
nullptr &&
"Already initialized a platform backend!");
468 bool mouse_can_use_global_state =
false;
469#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
470 const char* sdl_backend = SDL_GetCurrentVideoDriver();
471 const char* global_mouse_whitelist[] = {
"windows",
"cocoa",
"x11",
"DIVE",
"VMAN" };
472 for (
int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
473 if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
474 mouse_can_use_global_state =
true;
479 io.BackendPlatformUserData = (
void*)bd;
480 io.BackendPlatformName =
"imgui_impl_sdl2";
481 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
482 io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;
483 if (mouse_can_use_global_state)
484 io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports;
499 io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
500 io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
501 io.ClipboardUserData =
nullptr;
502 io.PlatformSetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData;
504 io.PlatformOpenInShellFn = [](ImGuiContext*,
const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url);
return true; };
508 bd->
GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst;
512 bd->
MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
513 bd->
MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
514 bd->
MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
515 bd->
MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
516 bd->
MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
517 bd->
MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
518 bd->
MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
519 bd->
MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
520 bd->
MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
524 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
525 main_viewport->PlatformHandle = (
void*)window;
526 main_viewport->PlatformHandleRaw =
nullptr;
528 SDL_VERSION(&info.version);
529 if (SDL_GetWindowWMInfo(window, &info))
531#if defined(SDL_VIDEO_DRIVER_WINDOWS)
532 main_viewport->PlatformHandleRaw = (
void*)info.info.win.window;
533#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
534 main_viewport->PlatformHandleRaw = (
void*)info.info.cocoa.window;
543#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
544 SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH,
"1");
550#ifdef SDL_HINT_IME_SHOW_UI
551 SDL_SetHint(SDL_HINT_IME_SHOW_UI,
"1");
555#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
556 SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE,
"0");
561 if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) && (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports))
562 ImGui_ImplSDL2_InitPlatformInterface(window, sdl_gl_context);
569 return ImGui_ImplSDL2_Init(window,
nullptr, sdl_gl_context);
575 IM_ASSERT(0 &&
"Unsupported");
577 if (!ImGui_ImplSDL2_Init(window,
nullptr,
nullptr))
587 IM_ASSERT(0 &&
"Unsupported");
589 return ImGui_ImplSDL2_Init(window,
nullptr,
nullptr);
594 return ImGui_ImplSDL2_Init(window,
nullptr,
nullptr);
599 return ImGui_ImplSDL2_Init(window, renderer,
nullptr);
604 return ImGui_ImplSDL2_Init(window,
nullptr,
nullptr);
607static void ImGui_ImplSDL2_CloseGamepads();
612 IM_ASSERT(bd !=
nullptr &&
"No platform backend to shutdown, or already shutdown?");
613 ImGuiIO& io = ImGui::GetIO();
615 ImGui_ImplSDL2_ShutdownPlatformInterface();
619 for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
621 ImGui_ImplSDL2_CloseGamepads();
623 io.BackendPlatformName =
nullptr;
624 io.BackendPlatformUserData =
nullptr;
625 io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
630static void ImGui_ImplSDL2_UpdateMouseData()
633 ImGuiIO& io = ImGui::GetIO();
636#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
639 SDL_Window* focused_window = SDL_GetKeyboardFocus();
640 const bool is_app_focused = (focused_window && (bd->
Window == focused_window || ImGui::FindViewportByPlatformHandle((
void*)focused_window)));
642 SDL_Window* focused_window = bd->
Window;
643 const bool is_app_focused = (SDL_GetWindowFlags(bd->
Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
649 if (io.WantSetMousePos)
651#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
652 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
653 SDL_WarpMouseGlobal((
int)io.MousePos.x, (
int)io.MousePos.y);
656 SDL_WarpMouseInWindow(bd->
Window, (
int)io.MousePos.x, (
int)io.MousePos.y);
664 int mouse_x, mouse_y, window_x, window_y;
665 SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
666 if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
668 SDL_GetWindowPosition(focused_window, &window_x, &window_y);
672 io.AddMousePosEvent((
float)mouse_x, (
float)mouse_y);
683 if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
685 ImGuiID mouse_viewport_id = 0;
686 if (SDL_Window* sdl_mouse_window = SDL_GetWindowFromID(bd->
MouseWindowID))
687 if (ImGuiViewport* mouse_viewport = ImGui::FindViewportByPlatformHandle((
void*)sdl_mouse_window))
688 mouse_viewport_id = mouse_viewport->ID;
689 io.AddMouseViewportEvent(mouse_viewport_id);
693static void ImGui_ImplSDL2_UpdateMouseCursor()
695 ImGuiIO& io = ImGui::GetIO();
696 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
700 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
701 if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
704 SDL_ShowCursor(SDL_FALSE);
712 SDL_SetCursor(expected_cursor);
715 SDL_ShowCursor(SDL_TRUE);
719static void ImGui_ImplSDL2_CloseGamepads()
722 if (bd->
GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
723 for (SDL_GameController* gamepad : bd->Gamepads)
724 SDL_GameControllerClose(gamepad);
731 ImGui_ImplSDL2_CloseGamepads();
732 if (mode == ImGui_ImplSDL2_GamepadMode_Manual)
734 IM_ASSERT(manual_gamepads_array !=
nullptr && manual_gamepads_count > 0);
735 for (
int n = 0; n < manual_gamepads_count; n++)
736 bd->
Gamepads.push_back(manual_gamepads_array[n]);
740 IM_ASSERT(manual_gamepads_array ==
nullptr && manual_gamepads_count <= 0);
746static void ImGui_ImplSDL2_UpdateGamepadButton(
ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no)
748 bool merged_value =
false;
749 for (SDL_GameController* gamepad : bd->Gamepads)
750 merged_value |= SDL_GameControllerGetButton(gamepad, button_no) != 0;
751 io.AddKeyEvent(key, merged_value);
754static inline float Saturate(
float v) {
return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
755static void ImGui_ImplSDL2_UpdateGamepadAnalog(
ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no,
float v0,
float v1)
757 float merged_value = 0.0f;
758 for (SDL_GameController* gamepad : bd->Gamepads)
760 float vn = Saturate((
float)(SDL_GameControllerGetAxis(gamepad, axis_no) - v0) / (
float)(v1 - v0));
761 if (merged_value < vn)
764 io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
767static void ImGui_ImplSDL2_UpdateGamepads()
770 ImGuiIO& io = ImGui::GetIO();
775 ImGui_ImplSDL2_CloseGamepads();
776 int joystick_count = SDL_NumJoysticks();
777 for (
int n = 0; n < joystick_count; n++)
778 if (SDL_IsGameController(n))
779 if (SDL_GameController* gamepad = SDL_GameControllerOpen(n))
782 if (bd->
GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst)
789 if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
791 io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
794 io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
797 const int thumb_dead_zone = 8000;
798 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START);
799 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK);
800 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X);
801 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B);
802 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y);
803 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A);
804 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
805 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
806 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP);
807 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
808 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
809 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
810 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767);
811 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767);
812 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK);
813 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK);
814 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
815 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
816 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768);
817 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
818 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768);
819 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767);
820 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768);
821 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767);
825static void ImGui_ImplSDL2_UpdateMonitors()
828 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
829 platform_io.Monitors.resize(0);
831 int display_count = SDL_GetNumVideoDisplays();
832 for (
int n = 0; n < display_count; n++)
835 ImGuiPlatformMonitor monitor;
837 SDL_GetDisplayBounds(n, &r);
838 monitor.MainPos = monitor.WorkPos = ImVec2((
float)r.x, (
float)r.y);
839 monitor.MainSize = monitor.WorkSize = ImVec2((
float)r.w, (
float)r.h);
840#if SDL_HAS_USABLE_DISPLAY_BOUNDS
841 SDL_GetDisplayUsableBounds(n, &r);
842 monitor.WorkPos = ImVec2((
float)r.x, (
float)r.y);
843 monitor.WorkSize = ImVec2((
float)r.w, (
float)r.h);
845#if SDL_HAS_PER_MONITOR_DPI
849 if (!SDL_GetDisplayDPI(n, &dpi,
nullptr,
nullptr))
850 monitor.DpiScale = dpi / 96.0f;
852 monitor.PlatformHandle = (
void*)(intptr_t)n;
853 platform_io.Monitors.push_back(monitor);
860 IM_ASSERT(bd !=
nullptr &&
"Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
861 ImGuiIO& io = ImGui::GetIO();
865 int display_w, display_h;
866 SDL_GetWindowSize(bd->
Window, &w, &h);
867 if (SDL_GetWindowFlags(bd->
Window) & SDL_WINDOW_MINIMIZED)
870 SDL_GetRendererOutputSize(bd->
Renderer, &display_w, &display_h);
872 SDL_GL_GetDrawableSize(bd->
Window, &display_w, &display_h);
873 io.DisplaySize = ImVec2((
float)w, (
float)h);
875 io.DisplayFramebufferScale = ImVec2((
float)display_w / w, (
float)display_h / h);
879 ImGui_ImplSDL2_UpdateMonitors();
883 static Uint64 frequency = SDL_GetPerformanceFrequency();
884 Uint64 current_time = SDL_GetPerformanceCounter();
885 if (current_time <= bd->Time)
886 current_time = bd->
Time + 1;
887 io.DeltaTime = bd->
Time > 0 ? (float)((
double)(current_time - bd->
Time) / frequency) : (float)(1.0f / 60.0f);
888 bd->
Time = current_time;
894 io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
900 io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
902 io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport;
904 ImGui_ImplSDL2_UpdateMouseData();
905 ImGui_ImplSDL2_UpdateMouseCursor();
908 ImGui_ImplSDL2_UpdateGamepads();
929static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport)
933 viewport->PlatformUserData = vd;
935 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
939 bool use_opengl = (main_viewport_data->GLContext !=
nullptr);
940 SDL_GLContext backup_context =
nullptr;
943 backup_context = SDL_GL_GetCurrentContext();
944 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
945 SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
948 Uint32 sdl_flags = 0;
949 sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->
UseVulkan ? SDL_WINDOW_VULKAN : 0);
950 sdl_flags |= SDL_GetWindowFlags(bd->
Window) & SDL_WINDOW_ALLOW_HIGHDPI;
951 sdl_flags |= SDL_WINDOW_HIDDEN;
952 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
953 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
956 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_SKIP_TASKBAR : 0;
958#if SDL_HAS_ALWAYS_ON_TOP
959 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
961 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);
962 vd->WindowOwned =
true;
965 vd->GLContext = SDL_GL_CreateContext(vd->Window);
966 SDL_GL_SetSwapInterval(0);
968 if (use_opengl && backup_context)
969 SDL_GL_MakeCurrent(vd->Window, backup_context);
971 viewport->PlatformHandle = (
void*)vd->Window;
972 viewport->PlatformHandleRaw =
nullptr;
974 SDL_VERSION(&info.version);
975 if (SDL_GetWindowWMInfo(vd->Window, &info))
977#if defined(SDL_VIDEO_DRIVER_WINDOWS)
978 viewport->PlatformHandleRaw = info.info.win.window;
979#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
980 viewport->PlatformHandleRaw = (
void*)info.info.cocoa.window;
985static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport)
989 if (vd->GLContext && vd->WindowOwned)
990 SDL_GL_DeleteContext(vd->GLContext);
991 if (vd->Window && vd->WindowOwned)
992 SDL_DestroyWindow(vd->Window);
993 vd->GLContext =
nullptr;
994 vd->Window =
nullptr;
997 viewport->PlatformUserData = viewport->PlatformHandle =
nullptr;
1000static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport)
1004 HWND hwnd = (HWND)viewport->PlatformHandleRaw;
1008 if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
1010 LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
1011 ex_style &= ~WS_EX_APPWINDOW;
1012 ex_style |= WS_EX_TOOLWINDOW;
1013 ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
1017 if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
1019 ::ShowWindow(hwnd, SW_SHOWNA);
1024 SDL_ShowWindow(vd->Window);
1027static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport)
1031 SDL_GetWindowPosition(vd->Window, &x, &y);
1032 return ImVec2((
float)x, (
float)y);
1035static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
1038 SDL_SetWindowPosition(vd->Window, (
int)pos.x, (
int)pos.y);
1041static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport)
1045 SDL_GetWindowSize(vd->Window, &w, &h);
1046 return ImVec2((
float)w, (
float)h);
1049static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
1052 SDL_SetWindowSize(vd->Window, (
int)
size.x, (
int)
size.y);
1055static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport,
const char* title)
1058 SDL_SetWindowTitle(vd->Window, title);
1061#if SDL_HAS_WINDOW_ALPHA
1062static void ImGui_ImplSDL2_SetWindowAlpha(ImGuiViewport* viewport,
float alpha)
1065 SDL_SetWindowOpacity(vd->Window, alpha);
1069static void ImGui_ImplSDL2_SetWindowFocus(ImGuiViewport* viewport)
1072 SDL_RaiseWindow(vd->Window);
1075static bool ImGui_ImplSDL2_GetWindowFocus(ImGuiViewport* viewport)
1078 return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
1081static bool ImGui_ImplSDL2_GetWindowMinimized(ImGuiViewport* viewport)
1084 return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0;
1087static void ImGui_ImplSDL2_RenderWindow(ImGuiViewport* viewport,
void*)
1091 SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
1094static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport,
void*)
1099 SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
1100 SDL_GL_SwapWindow(vd->Window);
1107#include <SDL_vulkan.h>
1108static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance,
const void* vk_allocator, ImU64* out_vk_surface)
1112 SDL_bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (VkSurfaceKHR*)out_vk_surface);
1117static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window,
void* sdl_gl_context)
1120 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
1121 platform_io.Platform_CreateWindow = ImGui_ImplSDL2_CreateWindow;
1122 platform_io.Platform_DestroyWindow = ImGui_ImplSDL2_DestroyWindow;
1123 platform_io.Platform_ShowWindow = ImGui_ImplSDL2_ShowWindow;
1124 platform_io.Platform_SetWindowPos = ImGui_ImplSDL2_SetWindowPos;
1125 platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos;
1126 platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize;
1127 platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize;
1128 platform_io.Platform_SetWindowFocus = ImGui_ImplSDL2_SetWindowFocus;
1129 platform_io.Platform_GetWindowFocus = ImGui_ImplSDL2_GetWindowFocus;
1130 platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL2_GetWindowMinimized;
1131 platform_io.Platform_SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle;
1132 platform_io.Platform_RenderWindow = ImGui_ImplSDL2_RenderWindow;
1133 platform_io.Platform_SwapBuffers = ImGui_ImplSDL2_SwapBuffers;
1134#if SDL_HAS_WINDOW_ALPHA
1135 platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL2_SetWindowAlpha;
1138 platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface;
1143 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
1146 vd->
WindowID = SDL_GetWindowID(window);
1149 main_viewport->PlatformUserData = vd;
1150 main_viewport->PlatformHandle = vd->
Window;
1153static void ImGui_ImplSDL2_ShutdownPlatformInterface()
1155 ImGui::DestroyPlatformWindows();
1160#if defined(__clang__)
1161#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)
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()