Revision 932a86a1
Added by david.sorber over 1 year ago
| src/backend/imgui_impl_sdl2.cpp | ||
|---|---|---|
|
// Implemented features:
|
||
|
// [X] Platform: Clipboard support.
|
||
|
// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
|
||
|
// [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 will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set]
|
||
|
// [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]
|
||
|
// [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
|
||
|
// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
|
||
|
// [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
|
||
| ... | ... | |
|
|
||
|
// CHANGELOG
|
||
|
// (minor and older changes stripped away, please see git history for details)
|
||
|
// 2024-10-24: Emscripten: from SDL 2.30.9, SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f.
|
||
|
// 2024-09-09: use SDL_Vulkan_GetDrawableSize() when available. (#7967, #3190)
|
||
|
// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
|
||
|
// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
|
||
|
// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
|
||
|
// - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn
|
||
|
// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
|
||
|
// 2024-08-19: Storing SDL's Uint32 WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
|
||
|
// 2024-08-19: ImGui_ImplSDL2_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
|
||
|
// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions.
|
||
|
// 2024-07-02: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() renaming in main library.
|
||
|
// 2024-02-14: Inputs: Handle gamepad disconnection. Added ImGui_ImplSDL2_SetGamepadMode().
|
||
|
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
|
||
|
// 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)
|
||
|
// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
|
||
| ... | ... | |
|
// SDL
|
||
|
#include <SDL.h>
|
||
|
#include <SDL_syswm.h>
|
||
|
#if defined(__APPLE__)
|
||
|
#ifdef __APPLE__
|
||
|
#include <TargetConditionals.h>
|
||
|
#endif
|
||
|
#ifdef __EMSCRIPTEN__
|
||
|
#include <emscripten/em_js.h>
|
||
|
#endif
|
||
|
|
||
|
#if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
|
||
|
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
|
||
| ... | ... | |
|
#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
|
||
|
#endif
|
||
|
#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
|
||
|
#if SDL_HAS_VULKAN
|
||
|
#include <SDL_vulkan.h>
|
||
|
#endif
|
||
|
|
||
|
// SDL Data
|
||
|
struct ImGui_ImplSDL2_Data
|
||
|
{
|
||
|
SDL_Window* Window;
|
||
|
SDL_Renderer* Renderer;
|
||
|
Uint64 Time;
|
||
|
Uint32 MouseWindowID;
|
||
|
int MouseButtonsDown;
|
||
|
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
|
||
|
SDL_Cursor* LastMouseCursor;
|
||
|
int PendingMouseLeaveFrame;
|
||
|
char* ClipboardTextData;
|
||
|
bool MouseCanUseGlobalState;
|
||
|
SDL_Window* Window;
|
||
|
Uint32 WindowID;
|
||
|
SDL_Renderer* Renderer;
|
||
|
Uint64 Time;
|
||
|
char* ClipboardTextData;
|
||
|
|
||
|
// Mouse handling
|
||
|
Uint32 MouseWindowID;
|
||
|
int MouseButtonsDown;
|
||
|
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
|
||
|
SDL_Cursor* MouseLastCursor;
|
||
|
int MouseLastLeaveFrame;
|
||
|
bool MouseCanUseGlobalState;
|
||
|
|
||
|
// Gamepad handling
|
||
|
ImVector<SDL_GameController*> Gamepads;
|
||
|
ImGui_ImplSDL2_GamepadMode GamepadMode;
|
||
|
bool WantUpdateGamepadsList;
|
||
|
|
||
|
ImGui_ImplSDL2_Data() { memset((void*)this, 0, sizeof(*this)); }
|
||
|
};
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
// Functions
|
||
|
static const char* ImGui_ImplSDL2_GetClipboardText(void*)
|
||
|
static const char* ImGui_ImplSDL2_GetClipboardText(ImGuiContext*)
|
||
|
{
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
|
if (bd->ClipboardTextData)
|
||
| ... | ... | |
|
return bd->ClipboardTextData;
|
||
|
}
|
||
|
|
||
|
static void ImGui_ImplSDL2_SetClipboardText(void*, const char* text)
|
||
|
static void ImGui_ImplSDL2_SetClipboardText(ImGuiContext*, const char* text)
|
||
|
{
|
||
|
SDL_SetClipboardText(text);
|
||
|
}
|
||
|
|
||
|
// Note: native IME will only display if user calls SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1") _before_ SDL_CreateWindow().
|
||
|
static void ImGui_ImplSDL2_SetPlatformImeData(ImGuiViewport*, ImGuiPlatformImeData* data)
|
||
|
static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data)
|
||
|
{
|
||
|
if (data->WantVisible)
|
||
|
{
|
||
| ... | ... | |
|
}
|
||
|
}
|
||
|
|
||
|
static ImGuiKey ImGui_ImplSDL2_KeycodeToImGuiKey(int keycode)
|
||
|
// Not static to allow third-party code to use that if they want to (but undocumented)
|
||
|
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode);
|
||
|
ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
|
||
|
{
|
||
|
IM_UNUSED(scancode);
|
||
|
switch (keycode)
|
||
|
{
|
||
|
case SDLK_TAB: return ImGuiKey_Tab;
|
||
| ... | ... | |
|
case SDLK_F24: return ImGuiKey_F24;
|
||
|
case SDLK_AC_BACK: return ImGuiKey_AppBack;
|
||
|
case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
|
||
|
default: break;
|
||
|
}
|
||
|
return ImGuiKey_None;
|
||
|
}
|
||
| ... | ... | |
|
io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & KMOD_GUI) != 0);
|
||
|
}
|
||
|
|
||
|
static ImGuiViewport* ImGui_ImplSDL2_GetViewportForWindowID(Uint32 window_id)
|
||
|
{
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
|
return (window_id == bd->WindowID) ? ImGui::GetMainViewport() : NULL;
|
||
|
}
|
||
|
|
||
|
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
||
|
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
||
|
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
||
| ... | ... | |
|
// 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.
|
||
|
bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
|
||
|
{
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
|
|
||
|
switch (event->type)
|
||
|
{
|
||
|
case SDL_MOUSEMOTION:
|
||
|
{
|
||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->motion.windowID) == NULL)
|
||
|
return false;
|
||
|
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
|
||
|
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||
|
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
|
||
| ... | ... | |
|
}
|
||
|
case SDL_MOUSEWHEEL:
|
||
|
{
|
||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->wheel.windowID) == NULL)
|
||
|
return false;
|
||
|
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
|
||
|
#if SDL_VERSION_ATLEAST(2,0,18) // If this fails to compile on Emscripten: update to latest Emscripten!
|
||
|
float wheel_x = -event->wheel.preciseX;
|
||
| ... | ... | |
|
float wheel_x = -(float)event->wheel.x;
|
||
|
float wheel_y = (float)event->wheel.y;
|
||
|
#endif
|
||
|
#ifdef __EMSCRIPTEN__
|
||
|
#if defined(__EMSCRIPTEN__) && !SDL_VERSION_ATLEAST(2,31,0)
|
||
|
wheel_x /= 100.0f;
|
||
|
#endif
|
||
|
io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
|
||
| ... | ... | |
|
case SDL_MOUSEBUTTONDOWN:
|
||
|
case SDL_MOUSEBUTTONUP:
|
||
|
{
|
||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->button.windowID) == NULL)
|
||
|
return false;
|
||
|
int mouse_button = -1;
|
||
|
if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
|
||
|
if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
|
||
| ... | ... | |
|
}
|
||
|
case SDL_TEXTINPUT:
|
||
|
{
|
||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->text.windowID) == NULL)
|
||
|
return false;
|
||
|
io.AddInputCharactersUTF8(event->text.text);
|
||
|
return true;
|
||
|
}
|
||
|
case SDL_KEYDOWN:
|
||
|
case SDL_KEYUP:
|
||
|
{
|
||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->key.windowID) == NULL)
|
||
|
return false;
|
||
|
ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
|
||
|
ImGuiKey key = ImGui_ImplSDL2_KeycodeToImGuiKey(event->key.keysym.sym);
|
||
|
ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(event->key.keysym.sym, event->key.keysym.scancode);
|
||
|
io.AddKeyEvent(key, (event->type == SDL_KEYDOWN));
|
||
|
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.
|
||
|
return true;
|
||
|
}
|
||
|
case SDL_WINDOWEVENT:
|
||
|
{
|
||
|
if (ImGui_ImplSDL2_GetViewportForWindowID(event->window.windowID) == NULL)
|
||
|
return false;
|
||
|
// - 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.
|
||
|
// - However we won't get a correct LEAVE event for a captured window.
|
||
|
// - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
|
||
| ... | ... | |
|
if (window_event == SDL_WINDOWEVENT_ENTER)
|
||
|
{
|
||
|
bd->MouseWindowID = event->window.windowID;
|
||
|
bd->PendingMouseLeaveFrame = 0;
|
||
|
bd->MouseLastLeaveFrame = 0;
|
||
|
}
|
||
|
if (window_event == SDL_WINDOWEVENT_LEAVE)
|
||
|
bd->PendingMouseLeaveFrame = ImGui::GetFrameCount() + 1;
|
||
|
bd->MouseLastLeaveFrame = ImGui::GetFrameCount() + 1;
|
||
|
if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
|
||
|
io.AddFocusEvent(true);
|
||
|
else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
|
||
|
io.AddFocusEvent(false);
|
||
|
return true;
|
||
|
}
|
||
|
case SDL_CONTROLLERDEVICEADDED:
|
||
|
case SDL_CONTROLLERDEVICEREMOVED:
|
||
|
{
|
||
|
bd->WantUpdateGamepadsList = true;
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer)
|
||
|
#ifdef __EMSCRIPTEN__
|
||
|
EM_JS(void, ImGui_ImplSDL2_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); });
|
||
|
#endif
|
||
|
|
||
|
static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
|
||
|
{
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
|
IMGUI_CHECKVERSION();
|
||
|
IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
|
||
|
|
||
|
// Check and store if we are on a SDL backend that supports global mouse position
|
||
| ... | ... | |
|
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
|
||
|
|
||
|
bd->Window = window;
|
||
|
bd->WindowID = SDL_GetWindowID(window);
|
||
|
bd->Renderer = renderer;
|
||
|
bd->MouseCanUseGlobalState = mouse_can_use_global_state;
|
||
|
|
||
|
io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
|
||
|
io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
|
||
|
io.ClipboardUserData = nullptr;
|
||
|
io.SetPlatformImeDataFn = ImGui_ImplSDL2_SetPlatformImeData;
|
||
|
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
|
||
|
platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
|
||
|
platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
|
||
|
platform_io.Platform_ClipboardUserData = nullptr;
|
||
|
platform_io.Platform_SetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData;
|
||
|
#ifdef __EMSCRIPTEN__
|
||
|
platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url); return true; };
|
||
|
#endif
|
||
|
|
||
|
// Gamepad handling
|
||
|
bd->GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst;
|
||
|
bd->WantUpdateGamepadsList = true;
|
||
|
|
||
|
// Load mouse cursors
|
||
|
bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
||
| ... | ... | |
|
// Set platform dependent data in viewport
|
||
|
// Our mouse update function expect PlatformHandle to be filled for the main viewport
|
||
|
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
|
||
|
main_viewport->PlatformHandle = (void*)(intptr_t)bd->WindowID;
|
||
|
main_viewport->PlatformHandleRaw = nullptr;
|
||
|
SDL_SysWMinfo info;
|
||
|
SDL_VERSION(&info.version);
|
||
| ... | ... | |
|
SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
|
||
|
#endif
|
||
|
|
||
|
(void)sdl_gl_context; // Unused in 'master' branch.
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
|
||
|
{
|
||
|
IM_UNUSED(sdl_gl_context); // Viewport branch will need this.
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr);
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr, sdl_gl_context);
|
||
|
}
|
||
|
|
||
|
bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
|
||
| ... | ... | |
|
#if !SDL_HAS_VULKAN
|
||
|
IM_ASSERT(0 && "Unsupported");
|
||
|
#endif
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr);
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||
|
}
|
||
|
|
||
|
bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
|
||
| ... | ... | |
|
#if !defined(_WIN32)
|
||
|
IM_ASSERT(0 && "Unsupported");
|
||
|
#endif
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr);
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||
|
}
|
||
|
|
||
|
bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
|
||
|
{
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr);
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||
|
}
|
||
|
|
||
|
bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
|
||
|
{
|
||
|
return ImGui_ImplSDL2_Init(window, renderer);
|
||
|
return ImGui_ImplSDL2_Init(window, renderer, nullptr);
|
||
|
}
|
||
|
|
||
|
bool ImGui_ImplSDL2_InitForOther(SDL_Window* window)
|
||
|
{
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr);
|
||
|
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
|
||
|
}
|
||
|
|
||
|
static void ImGui_ImplSDL2_CloseGamepads();
|
||
|
|
||
|
void ImGui_ImplSDL2_Shutdown()
|
||
|
{
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
| ... | ... | |
|
SDL_free(bd->ClipboardTextData);
|
||
|
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
|
||
|
SDL_FreeCursor(bd->MouseCursors[cursor_n]);
|
||
|
bd->LastMouseCursor = nullptr;
|
||
|
ImGui_ImplSDL2_CloseGamepads();
|
||
|
|
||
|
io.BackendPlatformName = nullptr;
|
||
|
io.BackendPlatformUserData = nullptr;
|
||
| ... | ... | |
|
#endif
|
||
|
if (is_app_focused)
|
||
|
{
|
||
|
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
|
||
|
// (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
|
||
|
if (io.WantSetMousePos)
|
||
|
SDL_WarpMouseInWindow(bd->Window, (int)io.MousePos.x, (int)io.MousePos.y);
|
||
|
|
||
| ... | ... | |
|
{
|
||
|
// Show OS mouse cursor
|
||
|
SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
|
||
|
if (bd->LastMouseCursor != expected_cursor)
|
||
|
if (bd->MouseLastCursor != expected_cursor)
|
||
|
{
|
||
|
SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
|
||
|
bd->LastMouseCursor = expected_cursor;
|
||
|
bd->MouseLastCursor = expected_cursor;
|
||
|
}
|
||
|
SDL_ShowCursor(SDL_TRUE);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void ImGui_ImplSDL2_CloseGamepads()
|
||
|
{
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
|
if (bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
|
||
|
for (SDL_GameController* gamepad : bd->Gamepads)
|
||
|
SDL_GameControllerClose(gamepad);
|
||
|
bd->Gamepads.resize(0);
|
||
|
}
|
||
|
|
||
|
void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array, int manual_gamepads_count)
|
||
|
{
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
|
ImGui_ImplSDL2_CloseGamepads();
|
||
|
if (mode == ImGui_ImplSDL2_GamepadMode_Manual)
|
||
|
{
|
||
|
IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0);
|
||
|
for (int n = 0; n < manual_gamepads_count; n++)
|
||
|
bd->Gamepads.push_back(manual_gamepads_array[n]);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
|
||
|
bd->WantUpdateGamepadsList = true;
|
||
|
}
|
||
|
bd->GamepadMode = mode;
|
||
|
}
|
||
|
|
||
|
static void ImGui_ImplSDL2_UpdateGamepadButton(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no)
|
||
|
{
|
||
|
bool merged_value = false;
|
||
|
for (SDL_GameController* gamepad : bd->Gamepads)
|
||
|
merged_value |= SDL_GameControllerGetButton(gamepad, button_no) != 0;
|
||
|
io.AddKeyEvent(key, merged_value);
|
||
|
}
|
||
|
|
||
|
static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
|
||
|
static void ImGui_ImplSDL2_UpdateGamepadAnalog(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no, float v0, float v1)
|
||
|
{
|
||
|
float merged_value = 0.0f;
|
||
|
for (SDL_GameController* gamepad : bd->Gamepads)
|
||
|
{
|
||
|
float vn = Saturate((float)(SDL_GameControllerGetAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
|
||
|
if (merged_value < vn)
|
||
|
merged_value = vn;
|
||
|
}
|
||
|
io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
|
||
|
}
|
||
|
|
||
|
static void ImGui_ImplSDL2_UpdateGamepads()
|
||
|
{
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
|
if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
|
||
|
return;
|
||
|
|
||
|
// Get gamepad
|
||
|
// Update list of controller(s) to use
|
||
|
if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
|
||
|
{
|
||
|
ImGui_ImplSDL2_CloseGamepads();
|
||
|
int joystick_count = SDL_NumJoysticks();
|
||
|
for (int n = 0; n < joystick_count; n++)
|
||
|
if (SDL_IsGameController(n))
|
||
|
if (SDL_GameController* gamepad = SDL_GameControllerOpen(n))
|
||
|
{
|
||
|
bd->Gamepads.push_back(gamepad);
|
||
|
if (bd->GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst)
|
||
|
break;
|
||
|
}
|
||
|
bd->WantUpdateGamepadsList = false;
|
||
|
}
|
||
|
|
||
|
// FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
|
||
|
if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
|
||
|
return;
|
||
|
io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
|
||
|
SDL_GameController* game_controller = SDL_GameControllerOpen(0);
|
||
|
if (!game_controller)
|
||
|
if (bd->Gamepads.Size == 0)
|
||
|
return;
|
||
|
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
|
||
|
|
||
|
// Update gamepad inputs
|
||
|
#define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V)
|
||
|
#define MAP_BUTTON(KEY_NO, BUTTON_NO) { io.AddKeyEvent(KEY_NO, SDL_GameControllerGetButton(game_controller, BUTTON_NO) != 0); }
|
||
|
#define MAP_ANALOG(KEY_NO, AXIS_NO, V0, V1) { float vn = (float)(SDL_GameControllerGetAxis(game_controller, AXIS_NO) - V0) / (float)(V1 - V0); vn = IM_SATURATE(vn); io.AddKeyAnalogEvent(KEY_NO, vn > 0.1f, vn); }
|
||
|
const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
|
||
|
MAP_BUTTON(ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square
|
||
|
MAP_BUTTON(ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle
|
||
|
MAP_BUTTON(ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle
|
||
|
MAP_BUTTON(ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross
|
||
|
MAP_BUTTON(ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK);
|
||
|
MAP_BUTTON(ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768);
|
||
|
MAP_ANALOG(ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767);
|
||
|
#undef MAP_BUTTON
|
||
|
#undef MAP_ANALOG
|
||
|
const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK);
|
||
|
ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768);
|
||
|
ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767);
|
||
|
}
|
||
|
|
||
|
void ImGui_ImplSDL2_NewFrame()
|
||
|
{
|
||
|
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
|
||
|
IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplSDL2_Init()?");
|
||
|
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
|
||
|
ImGuiIO& io = ImGui::GetIO();
|
||
|
|
||
|
// Setup display size (every frame to accommodate for window resizing)
|
||
| ... | ... | |
|
w = h = 0;
|
||
|
if (bd->Renderer != nullptr)
|
||
|
SDL_GetRendererOutputSize(bd->Renderer, &display_w, &display_h);
|
||
|
#if SDL_HAS_VULKAN
|
||
|
else if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_VULKAN)
|
||
|
SDL_Vulkan_GetDrawableSize(bd->Window, &display_w, &display_h);
|
||
|
#endif
|
||
|
else
|
||
|
SDL_GL_GetDrawableSize(bd->Window, &display_w, &display_h);
|
||
|
io.DisplaySize = ImVec2((float)w, (float)h);
|
||
| ... | ... | |
|
io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
|
||
|
bd->Time = current_time;
|
||
|
|
||
|
if (bd->PendingMouseLeaveFrame && bd->PendingMouseLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
|
||
|
if (bd->MouseLastLeaveFrame && bd->MouseLastLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
|
||
|
{
|
||
|
bd->MouseWindowID = 0;
|
||
|
bd->PendingMouseLeaveFrame = 0;
|
||
|
bd->MouseLastLeaveFrame = 0;
|
||
|
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
|
||
|
}
|
||
|
|
||
Update Dear ImGui to v1.91.5.