| 1 |
// dear imgui: Platform Backend for SDL2 |
| 2 |
// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) |
| 3 |
// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) |
| 4 |
// (Prefer SDL 2.0.5+ for full feature support.) |
| 5 |
|
| 6 |
// Implemented features: |
| 7 |
// [X] Platform: Clipboard support. |
| 8 |
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen. |
| 9 |
// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5] |
| 10 |
// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. |
| 11 |
// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. |
| 12 |
// [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!. |
| 13 |
|
| 14 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 15 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 16 |
// Learn about Dear ImGui: |
| 17 |
// - FAQ https://dearimgui.com/faq |
| 18 |
// - Getting Started https://dearimgui.com/getting-started |
| 19 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 20 |
// - Introduction, links and more at the top of imgui.cpp |
| 21 |
|
| 22 |
// CHANGELOG |
| 23 |
// (minor and older changes stripped away, please see git history for details) |
| 24 |
// 2024-10-24: Emscripten: from SDL 2.30.9, SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f. |
| 25 |
// 2024-09-09: use SDL_Vulkan_GetDrawableSize() when available. (#7967, #3190) |
| 26 |
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO: |
| 27 |
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn |
| 28 |
// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn |
| 29 |
// - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn |
| 30 |
// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn |
| 31 |
// 2024-08-19: Storing SDL's Uint32 WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*. |
| 32 |
// 2024-08-19: ImGui_ImplSDL2_ProcessEvent() now ignores events intended for other SDL windows. (#7853) |
| 33 |
// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions. |
| 34 |
// 2024-07-02: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() renaming in main library. |
| 35 |
// 2024-02-14: Inputs: Handle gamepad disconnection. Added ImGui_ImplSDL2_SetGamepadMode(). |
| 36 |
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys. |
| 37 |
// 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) |
| 38 |
// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702) |
| 39 |
// 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644) |
| 40 |
// 2023-02-07: Implement IME handler (io.SetPlatformImeDataFn will call SDL_SetTextInputRect()/SDL_StartTextInput()). |
| 41 |
// 2023-02-07: *BREAKING CHANGE* Renamed this backend file from imgui_impl_sdl.cpp/.h to imgui_impl_sdl2.cpp/.h in prevision for the future release of SDL3. |
| 42 |
// 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version). |
| 43 |
// 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096) |
| 44 |
// 2023-02-02: Removed SDL_MOUSEWHEEL value clamping, as values seem correct in latest Emscripten. (#4019) |
| 45 |
// 2023-02-01: Flipping SDL_MOUSEWHEEL 'wheel.x' value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463) |
| 46 |
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. |
| 47 |
// 2022-09-26: Inputs: Disable SDL 2.0.22 new "auto capture" (SDL_HINT_MOUSE_AUTO_CAPTURE) which prevents drag and drop across windows for multi-viewport support + don't capture when drag and dropping. (#5710) |
| 48 |
// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). |
| 49 |
// 2022-03-22: Inputs: Fix mouse position issues when dragging outside of boundaries. SDL_CaptureMouse() erroneously still gives out LEAVE events when hovering OS decorations. |
| 50 |
// 2022-03-22: Inputs: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2). |
| 51 |
// 2022-02-04: Added SDL_Renderer* parameter to ImGui_ImplSDL2_InitForSDLRenderer(), so we can use SDL_GetRendererOutputSize() instead of SDL_GL_GetDrawableSize() when bound to a SDL_Renderer. |
| 52 |
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. |
| 53 |
// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[]. |
| 54 |
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). |
| 55 |
// 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates. |
| 56 |
// 2022-01-12: Update mouse inputs using SDL_MOUSEMOTION/SDL_WINDOWEVENT_LEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API. |
| 57 |
// 2022-01-12: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted. |
| 58 |
// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. |
| 59 |
// 2021-08-17: Calling io.AddFocusEvent() on SDL_WINDOWEVENT_FOCUS_GAINED/SDL_WINDOWEVENT_FOCUS_LOST. |
| 60 |
// 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using SDL_GetMouseFocus() + SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, requires SDL 2.0.5+) |
| 61 |
// 2021-06-29: *BREAKING CHANGE* Removed 'SDL_Window* window' parameter to ImGui_ImplSDL2_NewFrame() which was unnecessary. |
| 62 |
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). |
| 63 |
// 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950) |
| 64 |
// 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends. |
| 65 |
// 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2). |
| 66 |
// 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state). |
| 67 |
// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor. |
| 68 |
// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter. |
| 69 |
// 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application). |
| 70 |
// 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized. |
| 71 |
// 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls. |
| 72 |
// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. |
| 73 |
// 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'. |
| 74 |
// 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls. |
| 75 |
// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. |
| 76 |
// 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples. |
| 77 |
// 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter. |
| 78 |
// 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText). |
| 79 |
// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag. |
| 80 |
// 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value. |
| 81 |
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. |
| 82 |
// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. |
| 83 |
// 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS). |
| 84 |
// 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes. |
| 85 |
// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. |
| 86 |
// 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS. |
| 87 |
// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert. |
| 88 |
// 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1). |
| 89 |
// 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers. |
| 90 |
|
| 91 |
#include "imgui.h" |
| 92 |
#ifndef IMGUI_DISABLE |
| 93 |
#include "imgui_impl_sdl2.h" |
| 94 |
|
| 95 |
// Clang warnings with -Weverything |
| 96 |
#if defined(__clang__) |
| 97 |
#pragma clang diagnostic push |
| 98 |
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision |
| 99 |
#endif |
| 100 |
|
| 101 |
// SDL |
| 102 |
#include <SDL.h> |
| 103 |
#include <SDL_syswm.h> |
| 104 |
#ifdef __APPLE__ |
| 105 |
#include <TargetConditionals.h> |
| 106 |
#endif |
| 107 |
#ifdef __EMSCRIPTEN__ |
| 108 |
#include <emscripten/em_js.h> |
| 109 |
#endif |
| 110 |
|
| 111 |
#if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__) |
| 112 |
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1 |
| 113 |
#else |
| 114 |
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 |
| 115 |
#endif |
| 116 |
#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) |
| 117 |
#if SDL_HAS_VULKAN |
| 118 |
#include <SDL_vulkan.h> |
| 119 |
#endif |
| 120 |
|
| 121 |
// SDL Data |
| 122 |
struct ImGui_ImplSDL2_Data |
| 123 |
{ |
| 124 |
SDL_Window* Window; |
| 125 |
Uint32 WindowID; |
| 126 |
SDL_Renderer* Renderer; |
| 127 |
Uint64 Time; |
| 128 |
char* ClipboardTextData; |
| 129 |
|
| 130 |
// Mouse handling |
| 131 |
Uint32 MouseWindowID; |
| 132 |
int MouseButtonsDown; |
| 133 |
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT]; |
| 134 |
SDL_Cursor* MouseLastCursor; |
| 135 |
int MouseLastLeaveFrame; |
| 136 |
bool MouseCanUseGlobalState; |
| 137 |
|
| 138 |
// Gamepad handling |
| 139 |
ImVector<SDL_GameController*> Gamepads; |
| 140 |
ImGui_ImplSDL2_GamepadMode GamepadMode; |
| 141 |
bool WantUpdateGamepadsList; |
| 142 |
|
| 143 |
ImGui_ImplSDL2_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 144 |
}; |
| 145 |
|
| 146 |
// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts |
| 147 |
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
| 148 |
// FIXME: multi-context support is not well tested and probably dysfunctional in this backend. |
| 149 |
// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. |
| 150 |
static ImGui_ImplSDL2_Data* ImGui_ImplSDL2_GetBackendData() |
| 151 |
{ |
| 152 |
return ImGui::GetCurrentContext() ? (ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; |
| 153 |
} |
| 154 |
|
| 155 |
// Functions |
| 156 |
static const char* ImGui_ImplSDL2_GetClipboardText(ImGuiContext*) |
| 157 |
{ |
| 158 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 159 |
if (bd->ClipboardTextData) |
| 160 |
SDL_free(bd->ClipboardTextData); |
| 161 |
bd->ClipboardTextData = SDL_GetClipboardText(); |
| 162 |
return bd->ClipboardTextData; |
| 163 |
} |
| 164 |
|
| 165 |
static void ImGui_ImplSDL2_SetClipboardText(ImGuiContext*, const char* text) |
| 166 |
{ |
| 167 |
SDL_SetClipboardText(text); |
| 168 |
} |
| 169 |
|
| 170 |
// Note: native IME will only display if user calls SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1") _before_ SDL_CreateWindow(). |
| 171 |
static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data) |
| 172 |
{ |
| 173 |
if (data->WantVisible) |
| 174 |
{ |
| 175 |
SDL_Rect r; |
| 176 |
r.x = (int)data->InputPos.x; |
| 177 |
r.y = (int)data->InputPos.y; |
| 178 |
r.w = 1; |
| 179 |
r.h = (int)data->InputLineHeight; |
| 180 |
SDL_SetTextInputRect(&r); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// Not static to allow third-party code to use that if they want to (but undocumented) |
| 185 |
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode); |
| 186 |
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode) |
| 187 |
{ |
| 188 |
IM_UNUSED(scancode); |
| 189 |
switch (keycode) |
| 190 |
{ |
| 191 |
case SDLK_TAB: return ImGuiKey_Tab; |
| 192 |
case SDLK_LEFT: return ImGuiKey_LeftArrow; |
| 193 |
case SDLK_RIGHT: return ImGuiKey_RightArrow; |
| 194 |
case SDLK_UP: return ImGuiKey_UpArrow; |
| 195 |
case SDLK_DOWN: return ImGuiKey_DownArrow; |
| 196 |
case SDLK_PAGEUP: return ImGuiKey_PageUp; |
| 197 |
case SDLK_PAGEDOWN: return ImGuiKey_PageDown; |
| 198 |
case SDLK_HOME: return ImGuiKey_Home; |
| 199 |
case SDLK_END: return ImGuiKey_End; |
| 200 |
case SDLK_INSERT: return ImGuiKey_Insert; |
| 201 |
case SDLK_DELETE: return ImGuiKey_Delete; |
| 202 |
case SDLK_BACKSPACE: return ImGuiKey_Backspace; |
| 203 |
case SDLK_SPACE: return ImGuiKey_Space; |
| 204 |
case SDLK_RETURN: return ImGuiKey_Enter; |
| 205 |
case SDLK_ESCAPE: return ImGuiKey_Escape; |
| 206 |
case SDLK_QUOTE: return ImGuiKey_Apostrophe; |
| 207 |
case SDLK_COMMA: return ImGuiKey_Comma; |
| 208 |
case SDLK_MINUS: return ImGuiKey_Minus; |
| 209 |
case SDLK_PERIOD: return ImGuiKey_Period; |
| 210 |
case SDLK_SLASH: return ImGuiKey_Slash; |
| 211 |
case SDLK_SEMICOLON: return ImGuiKey_Semicolon; |
| 212 |
case SDLK_EQUALS: return ImGuiKey_Equal; |
| 213 |
case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket; |
| 214 |
case SDLK_BACKSLASH: return ImGuiKey_Backslash; |
| 215 |
case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket; |
| 216 |
case SDLK_BACKQUOTE: return ImGuiKey_GraveAccent; |
| 217 |
case SDLK_CAPSLOCK: return ImGuiKey_CapsLock; |
| 218 |
case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock; |
| 219 |
case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock; |
| 220 |
case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen; |
| 221 |
case SDLK_PAUSE: return ImGuiKey_Pause; |
| 222 |
case SDLK_KP_0: return ImGuiKey_Keypad0; |
| 223 |
case SDLK_KP_1: return ImGuiKey_Keypad1; |
| 224 |
case SDLK_KP_2: return ImGuiKey_Keypad2; |
| 225 |
case SDLK_KP_3: return ImGuiKey_Keypad3; |
| 226 |
case SDLK_KP_4: return ImGuiKey_Keypad4; |
| 227 |
case SDLK_KP_5: return ImGuiKey_Keypad5; |
| 228 |
case SDLK_KP_6: return ImGuiKey_Keypad6; |
| 229 |
case SDLK_KP_7: return ImGuiKey_Keypad7; |
| 230 |
case SDLK_KP_8: return ImGuiKey_Keypad8; |
| 231 |
case SDLK_KP_9: return ImGuiKey_Keypad9; |
| 232 |
case SDLK_KP_PERIOD: return ImGuiKey_KeypadDecimal; |
| 233 |
case SDLK_KP_DIVIDE: return ImGuiKey_KeypadDivide; |
| 234 |
case SDLK_KP_MULTIPLY: return ImGuiKey_KeypadMultiply; |
| 235 |
case SDLK_KP_MINUS: return ImGuiKey_KeypadSubtract; |
| 236 |
case SDLK_KP_PLUS: return ImGuiKey_KeypadAdd; |
| 237 |
case SDLK_KP_ENTER: return ImGuiKey_KeypadEnter; |
| 238 |
case SDLK_KP_EQUALS: return ImGuiKey_KeypadEqual; |
| 239 |
case SDLK_LCTRL: return ImGuiKey_LeftCtrl; |
| 240 |
case SDLK_LSHIFT: return ImGuiKey_LeftShift; |
| 241 |
case SDLK_LALT: return ImGuiKey_LeftAlt; |
| 242 |
case SDLK_LGUI: return ImGuiKey_LeftSuper; |
| 243 |
case SDLK_RCTRL: return ImGuiKey_RightCtrl; |
| 244 |
case SDLK_RSHIFT: return ImGuiKey_RightShift; |
| 245 |
case SDLK_RALT: return ImGuiKey_RightAlt; |
| 246 |
case SDLK_RGUI: return ImGuiKey_RightSuper; |
| 247 |
case SDLK_APPLICATION: return ImGuiKey_Menu; |
| 248 |
case SDLK_0: return ImGuiKey_0; |
| 249 |
case SDLK_1: return ImGuiKey_1; |
| 250 |
case SDLK_2: return ImGuiKey_2; |
| 251 |
case SDLK_3: return ImGuiKey_3; |
| 252 |
case SDLK_4: return ImGuiKey_4; |
| 253 |
case SDLK_5: return ImGuiKey_5; |
| 254 |
case SDLK_6: return ImGuiKey_6; |
| 255 |
case SDLK_7: return ImGuiKey_7; |
| 256 |
case SDLK_8: return ImGuiKey_8; |
| 257 |
case SDLK_9: return ImGuiKey_9; |
| 258 |
case SDLK_a: return ImGuiKey_A; |
| 259 |
case SDLK_b: return ImGuiKey_B; |
| 260 |
case SDLK_c: return ImGuiKey_C; |
| 261 |
case SDLK_d: return ImGuiKey_D; |
| 262 |
case SDLK_e: return ImGuiKey_E; |
| 263 |
case SDLK_f: return ImGuiKey_F; |
| 264 |
case SDLK_g: return ImGuiKey_G; |
| 265 |
case SDLK_h: return ImGuiKey_H; |
| 266 |
case SDLK_i: return ImGuiKey_I; |
| 267 |
case SDLK_j: return ImGuiKey_J; |
| 268 |
case SDLK_k: return ImGuiKey_K; |
| 269 |
case SDLK_l: return ImGuiKey_L; |
| 270 |
case SDLK_m: return ImGuiKey_M; |
| 271 |
case SDLK_n: return ImGuiKey_N; |
| 272 |
case SDLK_o: return ImGuiKey_O; |
| 273 |
case SDLK_p: return ImGuiKey_P; |
| 274 |
case SDLK_q: return ImGuiKey_Q; |
| 275 |
case SDLK_r: return ImGuiKey_R; |
| 276 |
case SDLK_s: return ImGuiKey_S; |
| 277 |
case SDLK_t: return ImGuiKey_T; |
| 278 |
case SDLK_u: return ImGuiKey_U; |
| 279 |
case SDLK_v: return ImGuiKey_V; |
| 280 |
case SDLK_w: return ImGuiKey_W; |
| 281 |
case SDLK_x: return ImGuiKey_X; |
| 282 |
case SDLK_y: return ImGuiKey_Y; |
| 283 |
case SDLK_z: return ImGuiKey_Z; |
| 284 |
case SDLK_F1: return ImGuiKey_F1; |
| 285 |
case SDLK_F2: return ImGuiKey_F2; |
| 286 |
case SDLK_F3: return ImGuiKey_F3; |
| 287 |
case SDLK_F4: return ImGuiKey_F4; |
| 288 |
case SDLK_F5: return ImGuiKey_F5; |
| 289 |
case SDLK_F6: return ImGuiKey_F6; |
| 290 |
case SDLK_F7: return ImGuiKey_F7; |
| 291 |
case SDLK_F8: return ImGuiKey_F8; |
| 292 |
case SDLK_F9: return ImGuiKey_F9; |
| 293 |
case SDLK_F10: return ImGuiKey_F10; |
| 294 |
case SDLK_F11: return ImGuiKey_F11; |
| 295 |
case SDLK_F12: return ImGuiKey_F12; |
| 296 |
case SDLK_F13: return ImGuiKey_F13; |
| 297 |
case SDLK_F14: return ImGuiKey_F14; |
| 298 |
case SDLK_F15: return ImGuiKey_F15; |
| 299 |
case SDLK_F16: return ImGuiKey_F16; |
| 300 |
case SDLK_F17: return ImGuiKey_F17; |
| 301 |
case SDLK_F18: return ImGuiKey_F18; |
| 302 |
case SDLK_F19: return ImGuiKey_F19; |
| 303 |
case SDLK_F20: return ImGuiKey_F20; |
| 304 |
case SDLK_F21: return ImGuiKey_F21; |
| 305 |
case SDLK_F22: return ImGuiKey_F22; |
| 306 |
case SDLK_F23: return ImGuiKey_F23; |
| 307 |
case SDLK_F24: return ImGuiKey_F24; |
| 308 |
case SDLK_AC_BACK: return ImGuiKey_AppBack; |
| 309 |
case SDLK_AC_FORWARD: return ImGuiKey_AppForward; |
| 310 |
default: break; |
| 311 |
} |
| 312 |
return ImGuiKey_None; |
| 313 |
} |
| 314 |
|
| 315 |
static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods) |
| 316 |
{ |
| 317 |
ImGuiIO& io = ImGui::GetIO(); |
| 318 |
io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & KMOD_CTRL) != 0); |
| 319 |
io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & KMOD_SHIFT) != 0); |
| 320 |
io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & KMOD_ALT) != 0); |
| 321 |
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & KMOD_GUI) != 0); |
| 322 |
} |
| 323 |
|
| 324 |
static ImGuiViewport* ImGui_ImplSDL2_GetViewportForWindowID(Uint32 window_id) |
| 325 |
{ |
| 326 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 327 |
return (window_id == bd->WindowID) ? ImGui::GetMainViewport() : nullptr; |
| 328 |
} |
| 329 |
|
| 330 |
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. |
| 331 |
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
| 332 |
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
| 333 |
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. |
| 334 |
// 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. |
| 335 |
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event) |
| 336 |
{ |
| 337 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 338 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?"); |
| 339 |
ImGuiIO& io = ImGui::GetIO(); |
| 340 |
|
| 341 |
switch (event->type) |
| 342 |
{ |
| 343 |
case SDL_MOUSEMOTION: |
| 344 |
{ |
| 345 |
if (ImGui_ImplSDL2_GetViewportForWindowID(event->motion.windowID) == nullptr) |
| 346 |
return false; |
| 347 |
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y); |
| 348 |
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
| 349 |
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y); |
| 350 |
return true; |
| 351 |
} |
| 352 |
case SDL_MOUSEWHEEL: |
| 353 |
{ |
| 354 |
if (ImGui_ImplSDL2_GetViewportForWindowID(event->wheel.windowID) == nullptr) |
| 355 |
return false; |
| 356 |
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY); |
| 357 |
#if SDL_VERSION_ATLEAST(2,0,18) // If this fails to compile on Emscripten: update to latest Emscripten! |
| 358 |
float wheel_x = -event->wheel.preciseX; |
| 359 |
float wheel_y = event->wheel.preciseY; |
| 360 |
#else |
| 361 |
float wheel_x = -(float)event->wheel.x; |
| 362 |
float wheel_y = (float)event->wheel.y; |
| 363 |
#endif |
| 364 |
#if defined(__EMSCRIPTEN__) && !SDL_VERSION_ATLEAST(2,31,0) |
| 365 |
wheel_x /= 100.0f; |
| 366 |
#endif |
| 367 |
io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
| 368 |
io.AddMouseWheelEvent(wheel_x, wheel_y); |
| 369 |
return true; |
| 370 |
} |
| 371 |
case SDL_MOUSEBUTTONDOWN: |
| 372 |
case SDL_MOUSEBUTTONUP: |
| 373 |
{ |
| 374 |
if (ImGui_ImplSDL2_GetViewportForWindowID(event->button.windowID) == nullptr) |
| 375 |
return false; |
| 376 |
int mouse_button = -1; |
| 377 |
if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; } |
| 378 |
if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; } |
| 379 |
if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; } |
| 380 |
if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; } |
| 381 |
if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; } |
| 382 |
if (mouse_button == -1) |
| 383 |
break; |
| 384 |
io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); |
| 385 |
io.AddMouseButtonEvent(mouse_button, (event->type == SDL_MOUSEBUTTONDOWN)); |
| 386 |
bd->MouseButtonsDown = (event->type == SDL_MOUSEBUTTONDOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button)); |
| 387 |
return true; |
| 388 |
} |
| 389 |
case SDL_TEXTINPUT: |
| 390 |
{ |
| 391 |
if (ImGui_ImplSDL2_GetViewportForWindowID(event->text.windowID) == nullptr) |
| 392 |
return false; |
| 393 |
io.AddInputCharactersUTF8(event->text.text); |
| 394 |
return true; |
| 395 |
} |
| 396 |
case SDL_KEYDOWN: |
| 397 |
case SDL_KEYUP: |
| 398 |
{ |
| 399 |
if (ImGui_ImplSDL2_GetViewportForWindowID(event->key.windowID) == nullptr) |
| 400 |
return false; |
| 401 |
ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod); |
| 402 |
ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(event->key.keysym.sym, event->key.keysym.scancode); |
| 403 |
io.AddKeyEvent(key, (event->type == SDL_KEYDOWN)); |
| 404 |
io.SetKeyEventNativeData(key, event->key.keysym.sym, event->key.keysym.scancode, event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions. |
| 405 |
return true; |
| 406 |
} |
| 407 |
case SDL_WINDOWEVENT: |
| 408 |
{ |
| 409 |
if (ImGui_ImplSDL2_GetViewportForWindowID(event->window.windowID) == nullptr) |
| 410 |
return false; |
| 411 |
// - When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right. |
| 412 |
// - However we won't get a correct LEAVE event for a captured window. |
| 413 |
// - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late, |
| 414 |
// causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why |
| 415 |
// we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details. |
| 416 |
Uint8 window_event = event->window.event; |
| 417 |
if (window_event == SDL_WINDOWEVENT_ENTER) |
| 418 |
{ |
| 419 |
bd->MouseWindowID = event->window.windowID; |
| 420 |
bd->MouseLastLeaveFrame = 0; |
| 421 |
} |
| 422 |
if (window_event == SDL_WINDOWEVENT_LEAVE) |
| 423 |
bd->MouseLastLeaveFrame = ImGui::GetFrameCount() + 1; |
| 424 |
if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED) |
| 425 |
io.AddFocusEvent(true); |
| 426 |
else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST) |
| 427 |
io.AddFocusEvent(false); |
| 428 |
return true; |
| 429 |
} |
| 430 |
case SDL_CONTROLLERDEVICEADDED: |
| 431 |
case SDL_CONTROLLERDEVICEREMOVED: |
| 432 |
{ |
| 433 |
bd->WantUpdateGamepadsList = true; |
| 434 |
return true; |
| 435 |
} |
| 436 |
} |
| 437 |
return false; |
| 438 |
} |
| 439 |
|
| 440 |
#ifdef __EMSCRIPTEN__ |
| 441 |
EM_JS(void, ImGui_ImplSDL2_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); }); |
| 442 |
#endif |
| 443 |
|
| 444 |
static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context) |
| 445 |
{ |
| 446 |
ImGuiIO& io = ImGui::GetIO(); |
| 447 |
IMGUI_CHECKVERSION(); |
| 448 |
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); |
| 449 |
|
| 450 |
// Check and store if we are on a SDL backend that supports global mouse position |
| 451 |
// ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list) |
| 452 |
bool mouse_can_use_global_state = false; |
| 453 |
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE |
| 454 |
const char* sdl_backend = SDL_GetCurrentVideoDriver(); |
| 455 |
const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" }; |
| 456 |
for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++) |
| 457 |
if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0) |
| 458 |
mouse_can_use_global_state = true; |
| 459 |
#endif |
| 460 |
|
| 461 |
// Setup backend capabilities flags |
| 462 |
ImGui_ImplSDL2_Data* bd = IM_NEW(ImGui_ImplSDL2_Data)(); |
| 463 |
io.BackendPlatformUserData = (void*)bd; |
| 464 |
io.BackendPlatformName = "imgui_impl_sdl2"; |
| 465 |
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) |
| 466 |
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) |
| 467 |
|
| 468 |
bd->Window = window; |
| 469 |
bd->WindowID = SDL_GetWindowID(window); |
| 470 |
bd->Renderer = renderer; |
| 471 |
bd->MouseCanUseGlobalState = mouse_can_use_global_state; |
| 472 |
|
| 473 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 474 |
platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText; |
| 475 |
platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText; |
| 476 |
platform_io.Platform_ClipboardUserData = nullptr; |
| 477 |
platform_io.Platform_SetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData; |
| 478 |
#ifdef __EMSCRIPTEN__ |
| 479 |
platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url); return true; }; |
| 480 |
#endif |
| 481 |
|
| 482 |
// Gamepad handling |
| 483 |
bd->GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst; |
| 484 |
bd->WantUpdateGamepadsList = true; |
| 485 |
|
| 486 |
// Load mouse cursors |
| 487 |
bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); |
| 488 |
bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); |
| 489 |
bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL); |
| 490 |
bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS); |
| 491 |
bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE); |
| 492 |
bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW); |
| 493 |
bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE); |
| 494 |
bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND); |
| 495 |
bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO); |
| 496 |
|
| 497 |
// Set platform dependent data in viewport |
| 498 |
// Our mouse update function expect PlatformHandle to be filled for the main viewport |
| 499 |
ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
| 500 |
main_viewport->PlatformHandle = (void*)(intptr_t)bd->WindowID; |
| 501 |
main_viewport->PlatformHandleRaw = nullptr; |
| 502 |
SDL_SysWMinfo info; |
| 503 |
SDL_VERSION(&info.version); |
| 504 |
if (SDL_GetWindowWMInfo(window, &info)) |
| 505 |
{ |
| 506 |
#if defined(SDL_VIDEO_DRIVER_WINDOWS) |
| 507 |
main_viewport->PlatformHandleRaw = (void*)info.info.win.window; |
| 508 |
#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) |
| 509 |
main_viewport->PlatformHandleRaw = (void*)info.info.cocoa.window; |
| 510 |
#endif |
| 511 |
} |
| 512 |
|
| 513 |
// From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event. |
| 514 |
// Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered. |
| 515 |
// (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application. |
| 516 |
// It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click: |
| 517 |
// you can ignore SDL_MOUSEBUTTONDOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED) |
| 518 |
#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH |
| 519 |
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); |
| 520 |
#endif |
| 521 |
|
| 522 |
// From 2.0.18: Enable native IME. |
| 523 |
// IMPORTANT: This is used at the time of SDL_CreateWindow() so this will only affects secondary windows, if any. |
| 524 |
// For the main window to be affected, your application needs to call this manually before calling SDL_CreateWindow(). |
| 525 |
#ifdef SDL_HINT_IME_SHOW_UI |
| 526 |
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); |
| 527 |
#endif |
| 528 |
|
| 529 |
// From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710) |
| 530 |
#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE |
| 531 |
SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); |
| 532 |
#endif |
| 533 |
|
| 534 |
(void)sdl_gl_context; // Unused in 'master' branch. |
| 535 |
return true; |
| 536 |
} |
| 537 |
|
| 538 |
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context) |
| 539 |
{ |
| 540 |
return ImGui_ImplSDL2_Init(window, nullptr, sdl_gl_context); |
| 541 |
} |
| 542 |
|
| 543 |
bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window) |
| 544 |
{ |
| 545 |
#if !SDL_HAS_VULKAN |
| 546 |
IM_ASSERT(0 && "Unsupported"); |
| 547 |
#endif |
| 548 |
return ImGui_ImplSDL2_Init(window, nullptr, nullptr); |
| 549 |
} |
| 550 |
|
| 551 |
bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window) |
| 552 |
{ |
| 553 |
#if !defined(_WIN32) |
| 554 |
IM_ASSERT(0 && "Unsupported"); |
| 555 |
#endif |
| 556 |
return ImGui_ImplSDL2_Init(window, nullptr, nullptr); |
| 557 |
} |
| 558 |
|
| 559 |
bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window) |
| 560 |
{ |
| 561 |
return ImGui_ImplSDL2_Init(window, nullptr, nullptr); |
| 562 |
} |
| 563 |
|
| 564 |
bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer) |
| 565 |
{ |
| 566 |
return ImGui_ImplSDL2_Init(window, renderer, nullptr); |
| 567 |
} |
| 568 |
|
| 569 |
bool ImGui_ImplSDL2_InitForOther(SDL_Window* window) |
| 570 |
{ |
| 571 |
return ImGui_ImplSDL2_Init(window, nullptr, nullptr); |
| 572 |
} |
| 573 |
|
| 574 |
static void ImGui_ImplSDL2_CloseGamepads(); |
| 575 |
|
| 576 |
void ImGui_ImplSDL2_Shutdown() |
| 577 |
{ |
| 578 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 579 |
IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); |
| 580 |
ImGuiIO& io = ImGui::GetIO(); |
| 581 |
|
| 582 |
if (bd->ClipboardTextData) |
| 583 |
SDL_free(bd->ClipboardTextData); |
| 584 |
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) |
| 585 |
SDL_FreeCursor(bd->MouseCursors[cursor_n]); |
| 586 |
ImGui_ImplSDL2_CloseGamepads(); |
| 587 |
|
| 588 |
io.BackendPlatformName = nullptr; |
| 589 |
io.BackendPlatformUserData = nullptr; |
| 590 |
io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad); |
| 591 |
IM_DELETE(bd); |
| 592 |
} |
| 593 |
|
| 594 |
static void ImGui_ImplSDL2_UpdateMouseData() |
| 595 |
{ |
| 596 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 597 |
ImGuiIO& io = ImGui::GetIO(); |
| 598 |
|
| 599 |
// We forward mouse input when hovered or captured (via SDL_MOUSEMOTION) or when focused (below) |
| 600 |
#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE |
| 601 |
// 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 |
| 602 |
SDL_CaptureMouse((bd->MouseButtonsDown != 0) ? SDL_TRUE : SDL_FALSE); |
| 603 |
SDL_Window* focused_window = SDL_GetKeyboardFocus(); |
| 604 |
const bool is_app_focused = (bd->Window == focused_window); |
| 605 |
#else |
| 606 |
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 |
| 607 |
#endif |
| 608 |
if (is_app_focused) |
| 609 |
{ |
| 610 |
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user) |
| 611 |
if (io.WantSetMousePos) |
| 612 |
SDL_WarpMouseInWindow(bd->Window, (int)io.MousePos.x, (int)io.MousePos.y); |
| 613 |
|
| 614 |
// (Optional) Fallback to provide mouse position when focused (SDL_MOUSEMOTION already provides this when hovered or captured) |
| 615 |
if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0) |
| 616 |
{ |
| 617 |
int window_x, window_y, mouse_x_global, mouse_y_global; |
| 618 |
SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global); |
| 619 |
SDL_GetWindowPosition(bd->Window, &window_x, &window_y); |
| 620 |
io.AddMousePosEvent((float)(mouse_x_global - window_x), (float)(mouse_y_global - window_y)); |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
static void ImGui_ImplSDL2_UpdateMouseCursor() |
| 626 |
{ |
| 627 |
ImGuiIO& io = ImGui::GetIO(); |
| 628 |
if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) |
| 629 |
return; |
| 630 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 631 |
|
| 632 |
ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); |
| 633 |
if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) |
| 634 |
{ |
| 635 |
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor |
| 636 |
SDL_ShowCursor(SDL_FALSE); |
| 637 |
} |
| 638 |
else |
| 639 |
{ |
| 640 |
// Show OS mouse cursor |
| 641 |
SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]; |
| 642 |
if (bd->MouseLastCursor != expected_cursor) |
| 643 |
{ |
| 644 |
SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113) |
| 645 |
bd->MouseLastCursor = expected_cursor; |
| 646 |
} |
| 647 |
SDL_ShowCursor(SDL_TRUE); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
static void ImGui_ImplSDL2_CloseGamepads() |
| 652 |
{ |
| 653 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 654 |
if (bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual) |
| 655 |
for (SDL_GameController* gamepad : bd->Gamepads) |
| 656 |
SDL_GameControllerClose(gamepad); |
| 657 |
bd->Gamepads.resize(0); |
| 658 |
} |
| 659 |
|
| 660 |
void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array, int manual_gamepads_count) |
| 661 |
{ |
| 662 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 663 |
ImGui_ImplSDL2_CloseGamepads(); |
| 664 |
if (mode == ImGui_ImplSDL2_GamepadMode_Manual) |
| 665 |
{ |
| 666 |
IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0); |
| 667 |
for (int n = 0; n < manual_gamepads_count; n++) |
| 668 |
bd->Gamepads.push_back(manual_gamepads_array[n]); |
| 669 |
} |
| 670 |
else |
| 671 |
{ |
| 672 |
IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0); |
| 673 |
bd->WantUpdateGamepadsList = true; |
| 674 |
} |
| 675 |
bd->GamepadMode = mode; |
| 676 |
} |
| 677 |
|
| 678 |
static void ImGui_ImplSDL2_UpdateGamepadButton(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no) |
| 679 |
{ |
| 680 |
bool merged_value = false; |
| 681 |
for (SDL_GameController* gamepad : bd->Gamepads) |
| 682 |
merged_value |= SDL_GameControllerGetButton(gamepad, button_no) != 0; |
| 683 |
io.AddKeyEvent(key, merged_value); |
| 684 |
} |
| 685 |
|
| 686 |
static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; } |
| 687 |
static void ImGui_ImplSDL2_UpdateGamepadAnalog(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no, float v0, float v1) |
| 688 |
{ |
| 689 |
float merged_value = 0.0f; |
| 690 |
for (SDL_GameController* gamepad : bd->Gamepads) |
| 691 |
{ |
| 692 |
float vn = Saturate((float)(SDL_GameControllerGetAxis(gamepad, axis_no) - v0) / (float)(v1 - v0)); |
| 693 |
if (merged_value < vn) |
| 694 |
merged_value = vn; |
| 695 |
} |
| 696 |
io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value); |
| 697 |
} |
| 698 |
|
| 699 |
static void ImGui_ImplSDL2_UpdateGamepads() |
| 700 |
{ |
| 701 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 702 |
ImGuiIO& io = ImGui::GetIO(); |
| 703 |
|
| 704 |
// Update list of controller(s) to use |
| 705 |
if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual) |
| 706 |
{ |
| 707 |
ImGui_ImplSDL2_CloseGamepads(); |
| 708 |
int joystick_count = SDL_NumJoysticks(); |
| 709 |
for (int n = 0; n < joystick_count; n++) |
| 710 |
if (SDL_IsGameController(n)) |
| 711 |
if (SDL_GameController* gamepad = SDL_GameControllerOpen(n)) |
| 712 |
{ |
| 713 |
bd->Gamepads.push_back(gamepad); |
| 714 |
if (bd->GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst) |
| 715 |
break; |
| 716 |
} |
| 717 |
bd->WantUpdateGamepadsList = false; |
| 718 |
} |
| 719 |
|
| 720 |
// FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs. |
| 721 |
if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) |
| 722 |
return; |
| 723 |
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; |
| 724 |
if (bd->Gamepads.Size == 0) |
| 725 |
return; |
| 726 |
io.BackendFlags |= ImGuiBackendFlags_HasGamepad; |
| 727 |
|
| 728 |
// Update gamepad inputs |
| 729 |
const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value. |
| 730 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START); |
| 731 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK); |
| 732 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square |
| 733 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle |
| 734 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle |
| 735 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross |
| 736 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT); |
| 737 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); |
| 738 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP); |
| 739 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN); |
| 740 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); |
| 741 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); |
| 742 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767); |
| 743 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767); |
| 744 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK); |
| 745 |
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK); |
| 746 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768); |
| 747 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767); |
| 748 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768); |
| 749 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767); |
| 750 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768); |
| 751 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767); |
| 752 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768); |
| 753 |
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767); |
| 754 |
} |
| 755 |
|
| 756 |
void ImGui_ImplSDL2_NewFrame() |
| 757 |
{ |
| 758 |
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData(); |
| 759 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?"); |
| 760 |
ImGuiIO& io = ImGui::GetIO(); |
| 761 |
|
| 762 |
// Setup display size (every frame to accommodate for window resizing) |
| 763 |
int w, h; |
| 764 |
int display_w, display_h; |
| 765 |
SDL_GetWindowSize(bd->Window, &w, &h); |
| 766 |
if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED) |
| 767 |
w = h = 0; |
| 768 |
if (bd->Renderer != nullptr) |
| 769 |
SDL_GetRendererOutputSize(bd->Renderer, &display_w, &display_h); |
| 770 |
#if SDL_HAS_VULKAN |
| 771 |
else if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_VULKAN) |
| 772 |
SDL_Vulkan_GetDrawableSize(bd->Window, &display_w, &display_h); |
| 773 |
#endif |
| 774 |
else |
| 775 |
SDL_GL_GetDrawableSize(bd->Window, &display_w, &display_h); |
| 776 |
io.DisplaySize = ImVec2((float)w, (float)h); |
| 777 |
if (w > 0 && h > 0) |
| 778 |
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); |
| 779 |
|
| 780 |
// Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) |
| 781 |
// (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644) |
| 782 |
static Uint64 frequency = SDL_GetPerformanceFrequency(); |
| 783 |
Uint64 current_time = SDL_GetPerformanceCounter(); |
| 784 |
if (current_time <= bd->Time) |
| 785 |
current_time = bd->Time + 1; |
| 786 |
io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f); |
| 787 |
bd->Time = current_time; |
| 788 |
|
| 789 |
if (bd->MouseLastLeaveFrame && bd->MouseLastLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0) |
| 790 |
{ |
| 791 |
bd->MouseWindowID = 0; |
| 792 |
bd->MouseLastLeaveFrame = 0; |
| 793 |
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); |
| 794 |
} |
| 795 |
|
| 796 |
ImGui_ImplSDL2_UpdateMouseData(); |
| 797 |
ImGui_ImplSDL2_UpdateMouseCursor(); |
| 798 |
|
| 799 |
// Update game controllers (if enabled and available) |
| 800 |
ImGui_ImplSDL2_UpdateGamepads(); |
| 801 |
} |
| 802 |
|
| 803 |
//----------------------------------------------------------------------------- |
| 804 |
|
| 805 |
#if defined(__clang__) |
| 806 |
#pragma clang diagnostic pop |
| 807 |
#endif |
| 808 |
|
| 809 |
#endif // #ifndef IMGUI_DISABLE |