From 596c4945e32d43fe7b8a19ed994152aaf3d2e504 Mon Sep 17 00:00:00 2001 From: brentfpage Date: Tue, 29 Sep 2026 22:26:15 -0700 Subject: [PATCH] add support for SDL pinch events --- backends/imgui_impl_sdl3.cpp | 7 +++++++ imgui.cpp | 25 +++++++++++++++++++++++++ imgui.h | 3 +++ 3 files changed, 35 insertions(+) diff --git a/backends/imgui_impl_sdl3.cpp b/backends/imgui_impl_sdl3.cpp index 05f6498f1..07f75259c 100644 --- a/backends/imgui_impl_sdl3.cpp +++ b/backends/imgui_impl_sdl3.cpp @@ -477,6 +477,13 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event) bd->WantUpdateGamepadsList = true; return true; } + case SDL_EVENT_PINCH_UPDATE: + { + io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen); + io.AddPinchUpdateEvent(event->pinch.scale, event->pinch.span_x, event->pinch.span_y, event->pinch.focus_x, event->pinch.focus_y); + return true; + } + default: break; } diff --git a/imgui.cpp b/imgui.cpp index 57ed579af..1b9910849 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -2073,6 +2073,31 @@ void ImGuiIO::AddFocusEvent(bool focused) g.InputEventsQueue.push_back(e); } +// Queue a pinch status update. Represent the pinch event as a wheel event, using MouseWheel.Wheel(X|Y) for the pinch x|y amounts. Pass the pinch centroid into PosOther. +void ImGuiIO::AddPinchUpdateEvent(float scale, float span_x, float span_y, float focus_x, float focus_y) +{ + IM_ASSERT(Ctx != NULL); + ImGuiContext& g = *Ctx; + + if (!AppAcceptingEvents || (scale == 1.0f)) + return; + ImGuiInputEvent e; + e.Type = ImGuiInputEventType_MouseWheel; + e.Source = ImGuiInputSource_Mouse; + e.EventId = g.InputEventsNextEventId++; + float norm = ImSqrt(ImPow(span_x,2) + ImPow(span_y,2)); + e.MouseWheel.WheelX = (1-scale)*100*span_x/norm; + e.MouseWheel.WheelY = (1-scale)*100*span_y/norm; + e.MouseWheel.MouseSource = g.InputEventsNextMouseSource; + g.InputEventsQueue.push_back(e); + + ImVec2 finger_pos_middle(focus_x, focus_y); + g.IO.PosOther = finger_pos_middle; +} + + + + ImGuiPlatformIO::ImGuiPlatformIO() { // Most fields are initialized with zero diff --git a/imgui.h b/imgui.h index a108888a2..aafd0ac13 100644 --- a/imgui.h +++ b/imgui.h @@ -2584,6 +2584,8 @@ struct ImGuiIO IMGUI_API void AddMouseWheelEvent(float wheel_x, float wheel_y); // Queue a mouse wheel update. wheel_y<0: scroll down, wheel_y>0: scroll up, wheel_x<0: scroll right, wheel_x>0: scroll left. IMGUI_API void AddMouseSourceEvent(ImGuiMouseSource source); // Queue a mouse source change (Mouse/TouchScreen/Pen) IMGUI_API void AddFocusEvent(bool focused); // Queue a gain/loss of focus for the application (generally based on OS/platform focus of your window) + IMGUI_API void AddPinchUpdateEvent(float scale, float span_x, float span_y, float focus_x, float focus_y); // Queue a pinch status update + IMGUI_API void AddInputCharacter(unsigned int c); // Queue a new character input IMGUI_API void AddInputCharacterUTF16(ImWchar16 c); // Queue a new character input from a UTF-16 character, it can be a surrogate IMGUI_API void AddInputCharactersUTF8(const char* str); // Queue a new characters input from a UTF-8 string @@ -2658,6 +2660,7 @@ struct ImGuiIO bool AppAcceptingEvents; // Only modify via SetAppAcceptingEvents() ImWchar16 InputQueueSurrogate; // For AddInputCharacterUTF16() ImVector InputQueueCharacters; // Queue of _characters_ input (obtained by platform backend). Fill using AddInputCharacter() helper. + ImVec2 PosOther; // Convenience slot for a screen position, e.g., the centroid of the two finger positions relevant to a pinch gesture on mobile // Legacy: before 1.87, we required backend to fill io.KeyMap[] (imgui->native map) during initialization and io.KeysDown[] (native indices) every frame. // This is still temporarily supported as a legacy feature. However the new preferred scheme is for backend to call io.AddKeyEvent().