| 1 |
// dear imgui: Platform Backend for SDL3 (*EXPERIMENTAL*) |
| 2 |
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) |
| 3 |
// (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) |
| 4 |
|
| 5 |
// (**IMPORTANT: SDL 3.0.0 is NOT YET RELEASED AND CURRENTLY HAS A FAST CHANGING API. THIS CODE BREAKS OFTEN AS SDL3 CHANGES.**) |
| 6 |
|
| 7 |
// Implemented features: |
| 8 |
// [X] Platform: Clipboard support. |
| 9 |
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen. |
| 10 |
// [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] |
| 11 |
// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. |
| 12 |
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. |
| 13 |
// [X] Platform: IME support. |
| 14 |
|
| 15 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 16 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 17 |
// Learn about Dear ImGui: |
| 18 |
// - FAQ https://dearimgui.com/faq |
| 19 |
// - Getting Started https://dearimgui.com/getting-started |
| 20 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 21 |
// - Introduction, links and more at the top of imgui.cpp |
| 22 |
|
| 23 |
// CHANGELOG |
| 24 |
// (minor and older changes stripped away, please see git history for details) |
| 25 |
// 2024-10-24: Emscripten: SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f on Emscripten. |
| 26 |
// 2024-09-03: Update for SDL3 api changes: SDL_GetGamepads() memory ownership revert. (#7918, #7898, #7807) |
| 27 |
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO: |
| 28 |
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn |
| 29 |
// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn |
| 30 |
// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn |
| 31 |
// 2024-08-19: Storing SDL_WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*. |
| 32 |
// 2024-08-19: ImGui_ImplSDL3_ProcessEvent() now ignores events intended for other SDL windows. (#7853) |
| 33 |
// 2024-07-22: Update for SDL3 api changes: SDL_GetGamepads() memory ownership change. (#7807) |
| 34 |
// 2024-07-18: Update for SDL3 api changes: SDL_GetClipboardText() memory ownership change. (#7801) |
| 35 |
// 2024-07-15: Update for SDL3 api changes: SDL_GetProperty() change to SDL_GetPointerProperty(). (#7794) |
| 36 |
// 2024-07-02: Update for SDL3 api changes: SDLK_x renames and SDLK_KP_x removals (#7761, #7762). |
| 37 |
// 2024-07-01: Update for SDL3 api changes: SDL_SetTextInputRect() changed to SDL_SetTextInputArea(). |
| 38 |
// 2024-06-26: Update for SDL3 api changes: SDL_StartTextInput()/SDL_StopTextInput()/SDL_SetTextInputRect() functions signatures. |
| 39 |
// 2024-06-24: Update for SDL3 api changes: SDL_EVENT_KEY_DOWN/SDL_EVENT_KEY_UP contents. |
| 40 |
// 2024-06-03; Update for SDL3 api changes: SDL_SYSTEM_CURSOR_ renames. |
| 41 |
// 2024-05-15: Update for SDL3 api changes: SDLK_ renames. |
| 42 |
// 2024-04-15: Inputs: Re-enable calling SDL_StartTextInput()/SDL_StopTextInput() as SDL3 no longer enables it by default and should play nicer with IME. |
| 43 |
// 2024-02-13: Inputs: Fixed gamepad support. Handle gamepad disconnection. Added ImGui_ImplSDL3_SetGamepadMode(). |
| 44 |
// 2023-11-13: Updated for recent SDL3 API changes. |
| 45 |
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys. |
| 46 |
// 2023-05-04: Fixed build on Emscripten/iOS/Android. (#6391) |
| 47 |
// 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) |
| 48 |
// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702) |
| 49 |
// 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644) |
| 50 |
// 2023-02-07: Forked "imgui_impl_sdl2" into "imgui_impl_sdl3". Removed version checks for old feature. Refer to imgui_impl_sdl2.cpp for older changelog. |
| 51 |
|
| 52 |
#include "imgui.h" |
| 53 |
#ifndef IMGUI_DISABLE |
| 54 |
#include "imgui_impl_sdl3.h" |
| 55 |
|
| 56 |
// Clang warnings with -Weverything |
| 57 |
#if defined(__clang__) |
| 58 |
#pragma clang diagnostic push |
| 59 |
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision |
| 60 |
#endif |
| 61 |
|
| 62 |
// SDL |
| 63 |
#include <SDL3/SDL.h> |
| 64 |
#if defined(__APPLE__) |
| 65 |
#include <TargetConditionals.h> |
| 66 |
#endif |
| 67 |
#ifdef _WIN32 |
| 68 |
#ifndef WIN32_LEAN_AND_MEAN |
| 69 |
#define WIN32_LEAN_AND_MEAN |
| 70 |
#endif |
| 71 |
#include <windows.h> |
| 72 |
#endif |
| 73 |
|
| 74 |
#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__) |
| 75 |
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1 |
| 76 |
#else |
| 77 |
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 |
| 78 |
#endif |
| 79 |
|
| 80 |
// FIXME-LEGACY: remove when SDL 3.1.3 preview is released. |
| 81 |
#ifndef SDLK_APOSTROPHE |
| 82 |
#define SDLK_APOSTROPHE SDLK_QUOTE |
| 83 |
#endif |
| 84 |
#ifndef SDLK_GRAVE |
| 85 |
#define SDLK_GRAVE SDLK_BACKQUOTE |
| 86 |
#endif |
| 87 |
|
| 88 |
// SDL Data |
| 89 |
struct ImGui_ImplSDL3_Data |
| 90 |
{ |
| 91 |
SDL_Window* Window; |
| 92 |
SDL_WindowID WindowID; |
| 93 |
SDL_Renderer* Renderer; |
| 94 |
Uint64 Time; |
| 95 |
char* ClipboardTextData; |
| 96 |
|
| 97 |
// IME handling |
| 98 |
SDL_Window* ImeWindow; |
| 99 |
|
| 100 |
// Mouse handling |
| 101 |
Uint32 MouseWindowID; |
| 102 |
int MouseButtonsDown; |
| 103 |
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT]; |
| 104 |
SDL_Cursor* MouseLastCursor; |
| 105 |
int MousePendingLeaveFrame; |
| 106 |
bool MouseCanUseGlobalState; |
| 107 |
|
| 108 |
// Gamepad handling |
| 109 |
ImVector<SDL_Gamepad*> Gamepads; |
| 110 |
ImGui_ImplSDL3_GamepadMode GamepadMode; |
| 111 |
bool WantUpdateGamepadsList; |
| 112 |
|
| 113 |
ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 114 |
}; |
| 115 |
|
| 116 |
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts |
| 117 |
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
| 118 |
// FIXME: multi-context support is not well tested and probably dysfunctional in this backend. |
| 119 |
// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. |
| 120 |
static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData() |
| 121 |
{ |
| 122 |
return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
// Functions |
| 126 |
static const char* ImGui_ImplSDL3_GetClipboardText(ImGuiContext*) |
| 127 |
{ |
| 128 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 129 |
if (bd->ClipboardTextData) |
| 130 |
SDL_free(bd->ClipboardTextData); |
| 131 |
const char* sdl_clipboard_text = SDL_GetClipboardText(); |
| 132 |
bd->ClipboardTextData = sdl_clipboard_text ? SDL_strdup(sdl_clipboard_text) : nullptr; |
| 133 |
return bd->ClipboardTextData; |
| 134 |
} |
| 135 |
|
| 136 |
static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text) |
| 137 |
{ |
| 138 |
SDL_SetClipboardText(text); |
| 139 |
} |
| 140 |
|
| 141 |
static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data) |
| 142 |
{ |
| 143 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 144 |
SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle; |
| 145 |
SDL_Window* window = SDL_GetWindowFromID(window_id); |
| 146 |
if ((data->WantVisible == false || bd->ImeWindow != window) && bd->ImeWindow != nullptr) |
| 147 |
{ |
| 148 |
SDL_StopTextInput(bd->ImeWindow); |
| 149 |
bd->ImeWindow = nullptr; |
| 150 |
} |
| 151 |
if (data->WantVisible) |
| 152 |
{ |
| 153 |
SDL_Rect r; |
| 154 |
r.x = (int)data->InputPos.x; |
| 155 |
r.y = (int)data->InputPos.y; |
| 156 |
r.w = 1; |
| 157 |
r.h = (int)data->InputLineHeight; |
| 158 |
SDL_SetTextInputArea(window, &r, 0); |
| 159 |
SDL_StartTextInput(window); |
| 160 |
bd->ImeWindow = window; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// Not static to allow third-party code to use that if they want to (but undocumented) |
| 165 |
ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode); |
| 166 |
ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode) |
| 167 |
{ |
| 168 |
// Keypad doesn't have individual key values in SDL3 |
| 169 |
switch (scancode) |
| 170 |
{ |
| 171 |
case SDL_SCANCODE_KP_0: return ImGuiKey_Keypad0; |
| 172 |
case SDL_SCANCODE_KP_1: return ImGuiKey_Keypad1; |
| 173 |
case SDL_SCANCODE_KP_2: return ImGuiKey_Keypad2; |
| 174 |
case SDL_SCANCODE_KP_3: return ImGuiKey_Keypad3; |
| 175 |
case SDL_SCANCODE_KP_4: return ImGuiKey_Keypad4; |
| 176 |
case SDL_SCANCODE_KP_5: return ImGuiKey_Keypad5; |
| 177 |
case SDL_SCANCODE_KP_6: return ImGuiKey_Keypad6; |
| 178 |
case SDL_SCANCODE_KP_7: return ImGuiKey_Keypad7; |
| 179 |
case SDL_SCANCODE_KP_8: return ImGuiKey_Keypad8; |
| 180 |
case SDL_SCANCODE_KP_9: return ImGuiKey_Keypad9; |
| 181 |
case SDL_SCANCODE_KP_PERIOD: return ImGuiKey_KeypadDecimal; |
| 182 |
case SDL_SCANCODE_KP_DIVIDE: return ImGuiKey_KeypadDivide; |
| 183 |
case SDL_SCANCODE_KP_MULTIPLY: return ImGuiKey_KeypadMultiply; |
| 184 |
case SDL_SCANCODE_KP_MINUS: return ImGuiKey_KeypadSubtract; |
| 185 |
case SDL_SCANCODE_KP_PLUS: return ImGuiKey_KeypadAdd; |
| 186 |
case SDL_SCANCODE_KP_ENTER: return ImGuiKey_KeypadEnter; |
| 187 |
case SDL_SCANCODE_KP_EQUALS: return ImGuiKey_KeypadEqual; |
| 188 |
default: break; |
| 189 |
} |
| 190 |
switch (keycode) |
| 191 |
{ |
| 192 |
case SDLK_TAB: return ImGuiKey_Tab; |
| 193 |
case SDLK_LEFT: return ImGuiKey_LeftArrow; |
| 194 |
case SDLK_RIGHT: return ImGuiKey_RightArrow; |
| 195 |
case SDLK_UP: return ImGuiKey_UpArrow; |
| 196 |
case SDLK_DOWN: return ImGuiKey_DownArrow; |
| 197 |
case SDLK_PAGEUP: return ImGuiKey_PageUp; |
| 198 |
case SDLK_PAGEDOWN: return ImGuiKey_PageDown; |
| 199 |
case SDLK_HOME: return ImGuiKey_Home; |
| 200 |
case SDLK_END: return ImGuiKey_End; |
| 201 |
case SDLK_INSERT: return ImGuiKey_Insert; |
| 202 |
case SDLK_DELETE: return ImGuiKey_Delete; |
| 203 |
case SDLK_BACKSPACE: return ImGuiKey_Backspace; |
| 204 |
case SDLK_SPACE: return ImGuiKey_Space; |
| 205 |
case SDLK_RETURN: return ImGuiKey_Enter; |
| 206 |
case SDLK_ESCAPE: return ImGuiKey_Escape; |
| 207 |
case SDLK_APOSTROPHE: return ImGuiKey_Apostrophe; |
| 208 |
case SDLK_COMMA: return ImGuiKey_Comma; |
| 209 |
case SDLK_MINUS: return ImGuiKey_Minus; |
| 210 |
case SDLK_PERIOD: return ImGuiKey_Period; |
| 211 |
case SDLK_SLASH: return ImGuiKey_Slash; |
| 212 |
case SDLK_SEMICOLON: return ImGuiKey_Semicolon; |
| 213 |
case SDLK_EQUALS: return ImGuiKey_Equal; |
| 214 |
case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket; |
| 215 |
case SDLK_BACKSLASH: return ImGuiKey_Backslash; |
| 216 |
case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket; |
| 217 |
case SDLK_GRAVE: return ImGuiKey_GraveAccent; |
| 218 |
case SDLK_CAPSLOCK: return ImGuiKey_CapsLock; |
| 219 |
case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock; |
| 220 |
case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock; |
| 221 |
case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen; |
| 222 |
case SDLK_PAUSE: return ImGuiKey_Pause; |
| 223 |
case SDLK_LCTRL: return ImGuiKey_LeftCtrl; |
| 224 |
case SDLK_LSHIFT: return ImGuiKey_LeftShift; |
| 225 |
case SDLK_LALT: return ImGuiKey_LeftAlt; |
| 226 |
case SDLK_LGUI: return ImGuiKey_LeftSuper; |
| 227 |
case SDLK_RCTRL: return ImGuiKey_RightCtrl; |
| 228 |
case SDLK_RSHIFT: return ImGuiKey_RightShift; |
| 229 |
case SDLK_RALT: return ImGuiKey_RightAlt; |
| 230 |
case SDLK_RGUI: return ImGuiKey_RightSuper; |
| 231 |
case SDLK_APPLICATION: return ImGuiKey_Menu; |
| 232 |
case SDLK_0: return ImGuiKey_0; |
| 233 |
case SDLK_1: return ImGuiKey_1; |
| 234 |
case SDLK_2: return ImGuiKey_2; |
| 235 |
case SDLK_3: return ImGuiKey_3; |
| 236 |
case SDLK_4: return ImGuiKey_4; |
| 237 |
case SDLK_5: return ImGuiKey_5; |
| 238 |
case SDLK_6: return ImGuiKey_6; |
| 239 |
case SDLK_7: return ImGuiKey_7; |
| 240 |
case SDLK_8: return ImGuiKey_8; |
| 241 |
case SDLK_9: return ImGuiKey_9; |
| 242 |
case SDLK_A: return ImGuiKey_A; |
| 243 |
case SDLK_B: return ImGuiKey_B; |
| 244 |
case SDLK_C: return ImGuiKey_C; |
| 245 |
case SDLK_D: return ImGuiKey_D; |
| 246 |
case SDLK_E: return ImGuiKey_E; |
| 247 |
case SDLK_F: return ImGuiKey_F; |
| 248 |
case SDLK_G: return ImGuiKey_G; |
| 249 |
case SDLK_H: return ImGuiKey_H; |
| 250 |
case SDLK_I: return ImGuiKey_I; |
| 251 |
case SDLK_J: return ImGuiKey_J; |
| 252 |
case SDLK_K: return ImGuiKey_K; |
| 253 |
case SDLK_L: return ImGuiKey_L; |
| 254 |
case SDLK_M: return ImGuiKey_M; |
| 255 |
case SDLK_N: return ImGuiKey_N; |
| 256 |
case SDLK_O: return ImGuiKey_O; |
| 257 |
case SDLK_P: return ImGuiKey_P; |
| 258 |
case SDLK_Q: return ImGuiKey_Q; |
| 259 |
case SDLK_R: return ImGuiKey_R; |
| 260 |
case SDLK_S: return ImGuiKey_S; |
| 261 |
case SDLK_T: return ImGuiKey_T; |
| 262 |
case SDLK_U: return ImGuiKey_U; |
| 263 |
case SDLK_V: return ImGuiKey_V; |
| 264 |
case SDLK_W: return ImGuiKey_W; |
| 265 |
case SDLK_X: return ImGuiKey_X; |
| 266 |
case SDLK_Y: return ImGuiKey_Y; |
| 267 |
case SDLK_Z: return ImGuiKey_Z; |
| 268 |
case SDLK_F1: return ImGuiKey_F1; |
| 269 |
case SDLK_F2: return ImGuiKey_F2; |
| 270 |
case SDLK_F3: return ImGuiKey_F3; |
| 271 |
case SDLK_F4: return ImGuiKey_F4; |
| 272 |
case SDLK_F5: return ImGuiKey_F5; |
| 273 |
case SDLK_F6: return ImGuiKey_F6; |
| 274 |
case SDLK_F7: return ImGuiKey_F7; |
| 275 |
case SDLK_F8: return ImGuiKey_F8; |
| 276 |
case SDLK_F9: return ImGuiKey_F9; |
| 277 |
case SDLK_F10: return ImGuiKey_F10; |
| 278 |
case SDLK_F11: return ImGuiKey_F11; |
| 279 |
case SDLK_F12: return ImGuiKey_F12; |
| 280 |
case SDLK_F13: return ImGuiKey_F13; |
| 281 |
case SDLK_F14: return ImGuiKey_F14; |
| 282 |
case SDLK_F15: return ImGuiKey_F15; |
| 283 |
case SDLK_F16: return ImGuiKey_F16; |
| 284 |
case SDLK_F17: return ImGuiKey_F17; |
| 285 |
case SDLK_F18: return ImGuiKey_F18; |
| 286 |
case SDLK_F19: return ImGuiKey_F19; |
| 287 |
case SDLK_F20: return ImGuiKey_F20; |
| 288 |
case SDLK_F21: return ImGuiKey_F21; |
| 289 |
case SDLK_F22: return ImGuiKey_F22; |
| 290 |
case SDLK_F23: return ImGuiKey_F23; |
| 291 |
case SDLK_F24: return ImGuiKey_F24; |
| 292 |
case SDLK_AC_BACK: return ImGuiKey_AppBack; |
| 293 |
case SDLK_AC_FORWARD: return ImGuiKey_AppForward; |
| 294 |
default: break; |
| 295 |
} |
| 296 |
return ImGuiKey_None; |
| 297 |
} |
| 298 |
|
| 299 |
static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods) |
| 300 |
{ |
| 301 |
ImGuiIO& io = ImGui::GetIO(); |
| 302 |
io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0); |
| 303 |
io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0); |
| 304 |
io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0); |
| 305 |
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id) |
| 310 |
{ |
| 311 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 312 |
return (window_id == bd->WindowID) ? ImGui::GetMainViewport() : nullptr; |
| 313 |
} |
| 314 |
|
| 315 |
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
| 316 |
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
| 317 |
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
| 318 |
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
| 319 |
// If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field. |
| 320 |
bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event) |
| 321 |
{ |
| 322 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 323 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?"); |
| 324 |
ImGuiIO& io = ImGui::GetIO(); |
| 325 |
|
| 326 |
switch (event->type) |
| 327 |
{ |
| 328 |
case SDL_EVENT_MOUSE_MOTION: |
| 329 |
{ |
| 330 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->motion.windowID) == nullptr) |
| 331 |
return false; |
| 332 |
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y); |
| 333 |
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
| 334 |
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y); |
| 335 |
return true; |
| 336 |
} |
| 337 |
case SDL_EVENT_MOUSE_WHEEL: |
| 338 |
{ |
| 339 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->wheel.windowID) == nullptr) |
| 340 |
return false; |
| 341 |
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY); |
| 342 |
float wheel_x = -event->wheel.x; |
| 343 |
float wheel_y = event->wheel.y; |
| 344 |
io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
| 345 |
io.AddMouseWheelEvent(wheel_x, wheel_y); |
| 346 |
return true; |
| 347 |
} |
| 348 |
case SDL_EVENT_MOUSE_BUTTON_DOWN: |
| 349 |
case SDL_EVENT_MOUSE_BUTTON_UP: |
| 350 |
{ |
| 351 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->button.windowID) == nullptr) |
| 352 |
return false; |
| 353 |
int mouse_button = -1; |
| 354 |
if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; } |
| 355 |
if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; } |
| 356 |
if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; } |
| 357 |
if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; } |
| 358 |
if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; } |
| 359 |
if (mouse_button == -1) |
| 360 |
break; |
| 361 |
io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
| 362 |
io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN)); |
| 363 |
bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button)); |
| 364 |
return true; |
| 365 |
} |
| 366 |
case SDL_EVENT_TEXT_INPUT: |
| 367 |
{ |
| 368 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->text.windowID) == nullptr) |
| 369 |
return false; |
| 370 |
io.AddInputCharactersUTF8(event->text.text); |
| 371 |
return true; |
| 372 |
} |
| 373 |
case SDL_EVENT_KEY_DOWN: |
| 374 |
case SDL_EVENT_KEY_UP: |
| 375 |
{ |
| 376 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID) == nullptr) |
| 377 |
return false; |
| 378 |
//IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%d: key=%d, scancode=%d, mod=%X\n", (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP", event->key.key, event->key.scancode, event->key.mod); |
| 379 |
ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod); |
| 380 |
ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode); |
| 381 |
io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN)); |
| 382 |
io.SetKeyEventNativeData(key, event->key.key, event->key.scancode, event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions. |
| 383 |
return true; |
| 384 |
} |
| 385 |
case SDL_EVENT_WINDOW_MOUSE_ENTER: |
| 386 |
{ |
| 387 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) |
| 388 |
return false; |
| 389 |
bd->MouseWindowID = event->window.windowID; |
| 390 |
bd->MousePendingLeaveFrame = 0; |
| 391 |
return true; |
| 392 |
} |
| 393 |
// - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late, |
| 394 |
// causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why |
| 395 |
// we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details. |
| 396 |
// FIXME: Unconfirmed whether this is still needed with SDL3. |
| 397 |
case SDL_EVENT_WINDOW_MOUSE_LEAVE: |
| 398 |
{ |
| 399 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) |
| 400 |
return false; |
| 401 |
bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1; |
| 402 |
return true; |
| 403 |
} |
| 404 |
case SDL_EVENT_WINDOW_FOCUS_GAINED: |
| 405 |
case SDL_EVENT_WINDOW_FOCUS_LOST: |
| 406 |
{ |
| 407 |
if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) |
| 408 |
return false; |
| 409 |
io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED); |
| 410 |
return true; |
| 411 |
} |
| 412 |
case SDL_EVENT_GAMEPAD_ADDED: |
| 413 |
case SDL_EVENT_GAMEPAD_REMOVED: |
| 414 |
{ |
| 415 |
bd->WantUpdateGamepadsList = true; |
| 416 |
return true; |
| 417 |
} |
| 418 |
} |
| 419 |
return false; |
| 420 |
} |
| 421 |
|
| 422 |
static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window) |
| 423 |
{ |
| 424 |
viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window); |
| 425 |
viewport->PlatformHandleRaw = nullptr; |
| 426 |
#if defined(_WIN32) && !defined(__WINRT__) |
| 427 |
viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr); |
| 428 |
#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) |
| 429 |
viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr); |
| 430 |
#endif |
| 431 |
} |
| 432 |
|
| 433 |
static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context) |
| 434 |
{ |
| 435 |
ImGuiIO& io = ImGui::GetIO(); |
| 436 |
IMGUI_CHECKVERSION(); |
| 437 |
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); |
| 438 |
IM_UNUSED(sdl_gl_context); // Unused in this branch |
| 439 |
|
| 440 |
// Check and store if we are on a SDL backend that supports global mouse position |
| 441 |
// ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list) |
| 442 |
bool mouse_can_use_global_state = false; |
| 443 |
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE |
| 444 |
const char* sdl_backend = SDL_GetCurrentVideoDriver(); |
| 445 |
const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" }; |
| 446 |
for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++) |
| 447 |
if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0) |
| 448 |
mouse_can_use_global_state = true; |
| 449 |
#endif |
| 450 |
|
| 451 |
// Setup backend capabilities flags |
| 452 |
ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)(); |
| 453 |
io.BackendPlatformUserData = (void*)bd; |
| 454 |
io.BackendPlatformName = "imgui_impl_sdl3"; |
| 455 |
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) |
| 456 |
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) |
| 457 |
|
| 458 |
bd->Window = window; |
| 459 |
bd->WindowID = SDL_GetWindowID(window); |
| 460 |
bd->Renderer = renderer; |
| 461 |
bd->MouseCanUseGlobalState = mouse_can_use_global_state; |
| 462 |
|
| 463 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 464 |
platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText; |
| 465 |
platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText; |
| 466 |
platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData; |
| 467 |
|
| 468 |
// Gamepad handling |
| 469 |
bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst; |
| 470 |
bd->WantUpdateGamepadsList = true; |
| 471 |
|
| 472 |
// Load mouse cursors |
| 473 |
bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT); |
| 474 |
bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT); |
| 475 |
bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE); |
| 476 |
bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE); |
| 477 |
bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE); |
| 478 |
bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE); |
| 479 |
bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE); |
| 480 |
bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER); |
| 481 |
bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED); |
| 482 |
|
| 483 |
// Set platform dependent data in viewport |
| 484 |
// Our mouse update function expect PlatformHandle to be filled for the main viewport |
| 485 |
ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
| 486 |
ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window); |
| 487 |
|
| 488 |
// From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event. |
| 489 |
// Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered. |
| 490 |
// (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application. |
| 491 |
// It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click: |
| 492 |
// you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED) |
| 493 |
#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH |
| 494 |
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); |
| 495 |
#endif |
| 496 |
|
| 497 |
// From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710) |
| 498 |
#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE |
| 499 |
SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); |
| 500 |
#endif |
| 501 |
|
| 502 |
return true; |
| 503 |
} |
| 504 |
|
| 505 |
bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context) |
| 506 |
{ |
| 507 |
IM_UNUSED(sdl_gl_context); // Viewport branch will need this. |
| 508 |
return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context); |
| 509 |
} |
| 510 |
|
| 511 |
bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window) |
| 512 |
{ |
| 513 |
return ImGui_ImplSDL3_Init(window, nullptr, nullptr); |
| 514 |
} |
| 515 |
|
| 516 |
bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window) |
| 517 |
{ |
| 518 |
#if !defined(_WIN32) |
| 519 |
IM_ASSERT(0 && "Unsupported"); |
| 520 |
#endif |
| 521 |
return ImGui_ImplSDL3_Init(window, nullptr, nullptr); |
| 522 |
} |
| 523 |
|
| 524 |
bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window) |
| 525 |
{ |
| 526 |
return ImGui_ImplSDL3_Init(window, nullptr, nullptr); |
| 527 |
} |
| 528 |
|
| 529 |
bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer) |
| 530 |
{ |
| 531 |
return ImGui_ImplSDL3_Init(window, renderer, nullptr); |
| 532 |
} |
| 533 |
|
| 534 |
bool ImGui_ImplSDL3_InitForOther(SDL_Window* window) |
| 535 |
{ |
| 536 |
return ImGui_ImplSDL3_Init(window, nullptr, nullptr); |
| 537 |
} |
| 538 |
|
| 539 |
static void ImGui_ImplSDL3_CloseGamepads(); |
| 540 |
|
| 541 |
void ImGui_ImplSDL3_Shutdown() |
| 542 |
{ |
| 543 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 544 |
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); |
| 545 |
ImGuiIO& io = ImGui::GetIO(); |
| 546 |
|
| 547 |
if (bd->ClipboardTextData) |
| 548 |
SDL_free(bd->ClipboardTextData); |
| 549 |
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) |
| 550 |
SDL_DestroyCursor(bd->MouseCursors[cursor_n]); |
| 551 |
ImGui_ImplSDL3_CloseGamepads(); |
| 552 |
|
| 553 |
io.BackendPlatformName = nullptr; |
| 554 |
io.BackendPlatformUserData = nullptr; |
| 555 |
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad); |
| 556 |
IM_DELETE(bd); |
| 557 |
} |
| 558 |
|
| 559 |
static void ImGui_ImplSDL3_UpdateMouseData() |
| 560 |
{ |
| 561 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 562 |
ImGuiIO& io = ImGui::GetIO(); |
| 563 |
|
| 564 |
// We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below) |
| 565 |
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE |
| 566 |
// 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 |
| 567 |
SDL_CaptureMouse(bd->MouseButtonsDown != 0); |
| 568 |
SDL_Window* focused_window = SDL_GetKeyboardFocus(); |
| 569 |
const bool is_app_focused = (bd->Window == focused_window); |
| 570 |
#else |
| 571 |
SDL_Window* focused_window = bd->Window; |
| 572 |
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 |
| 573 |
#endif |
| 574 |
if (is_app_focused) |
| 575 |
{ |
| 576 |
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user) |
| 577 |
if (io.WantSetMousePos) |
| 578 |
SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y); |
| 579 |
|
| 580 |
// (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured) |
| 581 |
if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0) |
| 582 |
{ |
| 583 |
// 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) |
| 584 |
float mouse_x_global, mouse_y_global; |
| 585 |
int window_x, window_y; |
| 586 |
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global); |
| 587 |
SDL_GetWindowPosition(focused_window, &window_x, &window_y); |
| 588 |
io.AddMousePosEvent(mouse_x_global - window_x, mouse_y_global - window_y); |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
static void ImGui_ImplSDL3_UpdateMouseCursor() |
| 594 |
{ |
| 595 |
ImGuiIO& io = ImGui::GetIO(); |
| 596 |
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) |
| 597 |
return; |
| 598 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 599 |
|
| 600 |
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); |
| 601 |
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) |
| 602 |
{ |
| 603 |
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor |
| 604 |
SDL_HideCursor(); |
| 605 |
} |
| 606 |
else |
| 607 |
{ |
| 608 |
// Show OS mouse cursor |
| 609 |
SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]; |
| 610 |
if (bd->MouseLastCursor != expected_cursor) |
| 611 |
{ |
| 612 |
SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113) |
| 613 |
bd->MouseLastCursor = expected_cursor; |
| 614 |
} |
| 615 |
SDL_ShowCursor(); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
static void ImGui_ImplSDL3_CloseGamepads() |
| 620 |
{ |
| 621 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 622 |
if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual) |
| 623 |
for (SDL_Gamepad* gamepad : bd->Gamepads) |
| 624 |
SDL_CloseGamepad(gamepad); |
| 625 |
bd->Gamepads.resize(0); |
| 626 |
} |
| 627 |
|
| 628 |
void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count) |
| 629 |
{ |
| 630 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 631 |
ImGui_ImplSDL3_CloseGamepads(); |
| 632 |
if (mode == ImGui_ImplSDL3_GamepadMode_Manual) |
| 633 |
{ |
| 634 |
IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0); |
| 635 |
for (int n = 0; n < manual_gamepads_count; n++) |
| 636 |
bd->Gamepads.push_back(manual_gamepads_array[n]); |
| 637 |
} |
| 638 |
else |
| 639 |
{ |
| 640 |
IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0); |
| 641 |
bd->WantUpdateGamepadsList = true; |
| 642 |
} |
| 643 |
bd->GamepadMode = mode; |
| 644 |
} |
| 645 |
|
| 646 |
static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadButton button_no) |
| 647 |
{ |
| 648 |
bool merged_value = false; |
| 649 |
for (SDL_Gamepad* gamepad : bd->Gamepads) |
| 650 |
merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0; |
| 651 |
io.AddKeyEvent(key, merged_value); |
| 652 |
} |
| 653 |
|
| 654 |
static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; } |
| 655 |
static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no, float v0, float v1) |
| 656 |
{ |
| 657 |
float merged_value = 0.0f; |
| 658 |
for (SDL_Gamepad* gamepad : bd->Gamepads) |
| 659 |
{ |
| 660 |
float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0)); |
| 661 |
if (merged_value < vn) |
| 662 |
merged_value = vn; |
| 663 |
} |
| 664 |
io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value); |
| 665 |
} |
| 666 |
|
| 667 |
static void ImGui_ImplSDL3_UpdateGamepads() |
| 668 |
{ |
| 669 |
ImGuiIO& io = ImGui::GetIO(); |
| 670 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 671 |
|
| 672 |
// Update list of gamepads to use |
| 673 |
if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual) |
| 674 |
{ |
| 675 |
ImGui_ImplSDL3_CloseGamepads(); |
| 676 |
int sdl_gamepads_count = 0; |
| 677 |
SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count); |
| 678 |
for (int n = 0; n < sdl_gamepads_count; n++) |
| 679 |
if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n])) |
| 680 |
{ |
| 681 |
bd->Gamepads.push_back(gamepad); |
| 682 |
if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst) |
| 683 |
break; |
| 684 |
} |
| 685 |
bd->WantUpdateGamepadsList = false; |
| 686 |
SDL_free(sdl_gamepads); |
| 687 |
} |
| 688 |
|
| 689 |
// FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs. |
| 690 |
if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) |
| 691 |
return; |
| 692 |
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; |
| 693 |
if (bd->Gamepads.Size == 0) |
| 694 |
return; |
| 695 |
io.BackendFlags |= ImGuiBackendFlags_HasGamepad; |
| 696 |
|
| 697 |
// Update gamepad inputs |
| 698 |
const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value. |
| 699 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START); |
| 700 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK); |
| 701 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square |
| 702 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle |
| 703 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle |
| 704 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross |
| 705 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT); |
| 706 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT); |
| 707 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP); |
| 708 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN); |
| 709 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER); |
| 710 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER); |
| 711 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767); |
| 712 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767); |
| 713 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK); |
| 714 |
ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK); |
| 715 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768); |
| 716 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767); |
| 717 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768); |
| 718 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767); |
| 719 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768); |
| 720 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767); |
| 721 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768); |
| 722 |
ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767); |
| 723 |
} |
| 724 |
|
| 725 |
void ImGui_ImplSDL3_NewFrame() |
| 726 |
{ |
| 727 |
ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); |
| 728 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?"); |
| 729 |
ImGuiIO& io = ImGui::GetIO(); |
| 730 |
|
| 731 |
// Setup display size (every frame to accommodate for window resizing) |
| 732 |
int w, h; |
| 733 |
int display_w, display_h; |
| 734 |
SDL_GetWindowSize(bd->Window, &w, &h); |
| 735 |
if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED) |
| 736 |
w = h = 0; |
| 737 |
SDL_GetWindowSizeInPixels(bd->Window, &display_w, &display_h); |
| 738 |
io.DisplaySize = ImVec2((float)w, (float)h); |
| 739 |
if (w > 0 && h > 0) |
| 740 |
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); |
| 741 |
|
| 742 |
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) |
| 743 |
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644) |
| 744 |
static Uint64 frequency = SDL_GetPerformanceFrequency(); |
| 745 |
Uint64 current_time = SDL_GetPerformanceCounter(); |
| 746 |
if (current_time <= bd->Time) |
| 747 |
current_time = bd->Time + 1; |
| 748 |
io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f); |
| 749 |
bd->Time = current_time; |
| 750 |
|
| 751 |
if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0) |
| 752 |
{ |
| 753 |
bd->MouseWindowID = 0; |
| 754 |
bd->MousePendingLeaveFrame = 0; |
| 755 |
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); |
| 756 |
} |
| 757 |
|
| 758 |
ImGui_ImplSDL3_UpdateMouseData(); |
| 759 |
ImGui_ImplSDL3_UpdateMouseCursor(); |
| 760 |
|
| 761 |
// Update game controllers (if enabled and available) |
| 762 |
ImGui_ImplSDL3_UpdateGamepads(); |
| 763 |
} |
| 764 |
|
| 765 |
//----------------------------------------------------------------------------- |
| 766 |
|
| 767 |
#if defined(__clang__) |
| 768 |
#pragma clang diagnostic pop |
| 769 |
#endif |
| 770 |
|
| 771 |
#endif // #ifndef IMGUI_DISABLE |