Skip to content

Commit c5f6721

Browse files
rokupsocornut
authored andcommitted
Backends: SDL: Fix multi-viewport dragging issue with SDL on some systems. (v2 for master, using bd->MouseButtonsDown == 0) (ocornut#5012, ocornut#5082)
# Conflicts: # backends/imgui_impl_sdl.cpp # docs/CHANGELOG.txt
1 parent 7602277 commit c5f6721

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

backends/imgui_impl_sdl.cpp

+20-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
// CHANGELOG
2020
// (minor and older changes stripped away, please see git history for details)
21+
// 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.
2122
// 2022-03-22: Inputs: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2).
2223
// 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.
23-
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago)with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
24+
// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
2425
// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
2526
// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
2627
// 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates.
@@ -85,6 +86,7 @@ struct ImGui_ImplSDL2_Data
8586
Uint64 Time;
8687
int MouseButtonsDown;
8788
SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
89+
int PendingMouseLeaveFrame;
8890
char* ClipboardTextData;
8991
bool MouseCanUseGlobalState;
9092

@@ -292,9 +294,17 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
292294
}
293295
case SDL_WINDOWEVENT:
294296
{
295-
if (event->window.event == SDL_WINDOWEVENT_LEAVE)
296-
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
297-
if (event->window.event == SDL_WINDOWEVENT_FOCUS_GAINED)
297+
// - 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.
298+
// - However we won't get a correct LEAVE event for a captured window.
299+
// - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
300+
// causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
301+
// we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
302+
Uint8 window_event = event->window.event;
303+
if (window_event == SDL_WINDOWEVENT_ENTER)
304+
bd->PendingMouseLeaveFrame = 0;
305+
if (window_event == SDL_WINDOWEVENT_LEAVE)
306+
bd->PendingMouseLeaveFrame = ImGui::GetFrameCount() + 1;
307+
if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
298308
io.AddFocusEvent(true);
299309
else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
300310
io.AddFocusEvent(false);
@@ -540,6 +550,12 @@ void ImGui_ImplSDL2_NewFrame()
540550
io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
541551
bd->Time = current_time;
542552

553+
if (bd->PendingMouseLeaveFrame && bd->PendingMouseLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
554+
{
555+
io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
556+
bd->PendingMouseLeaveFrame = 0;
557+
}
558+
543559
ImGui_ImplSDL2_UpdateMouseData();
544560
ImGui_ImplSDL2_UpdateMouseCursor();
545561

docs/CHANGELOG.txt

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Other Changes:
6969
- Misc: Updated stb_rect_pack.h from 1.00 to 1.01 (minor). (#5075)
7070
- Misc: binary_to_compressed_c tool: Added -nostatic option. (#5021) [@podsvirov]
7171
- ImVector: Fixed erase() with empty range. (#5009) [@thedmd]
72+
- Backends: SDL: Fixed dragging out viewport broken on some SDL setups. (#5012) [@rokups]
7273
- Backends: SDL: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2). (#5125) [@sgiurgiu]
7374
- Examples: Emscripten: Fix building for latest Emscripten specs. (#3632)
7475

0 commit comments

Comments
 (0)