add support for SDL pinch events

This commit is contained in:
brentfpage 2026-09-29 22:26:15 -07:00
parent aa0181478b
commit 596c4945e3
3 changed files with 35 additions and 0 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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<ImWchar> 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().