Skip to content

Commit 591a18a

Browse files
madebrocornut
authored andcommitted
Backends: SDL2, SDL3: ignore events of other SDL windows. (#7853)
1 parent 29fadad commit 591a18a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

backends/imgui_impl_sdl2.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,17 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
321321
{
322322
case SDL_MOUSEMOTION:
323323
{
324+
if (event->motion.windowID != SDL_GetWindowID(bd->Window))
325+
return false;
324326
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
325327
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
326328
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
327329
return true;
328330
}
329331
case SDL_MOUSEWHEEL:
330332
{
333+
if (event->wheel.windowID != SDL_GetWindowID(bd->Window))
334+
return false;
331335
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
332336
#if SDL_VERSION_ATLEAST(2,0,18) // If this fails to compile on Emscripten: update to latest Emscripten!
333337
float wheel_x = -event->wheel.preciseX;
@@ -346,6 +350,8 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
346350
case SDL_MOUSEBUTTONDOWN:
347351
case SDL_MOUSEBUTTONUP:
348352
{
353+
if (event->button.windowID != SDL_GetWindowID(bd->Window))
354+
return false;
349355
int mouse_button = -1;
350356
if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
351357
if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
@@ -361,12 +367,16 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
361367
}
362368
case SDL_TEXTINPUT:
363369
{
370+
if (event->text.windowID != SDL_GetWindowID(bd->Window))
371+
return false;
364372
io.AddInputCharactersUTF8(event->text.text);
365373
return true;
366374
}
367375
case SDL_KEYDOWN:
368376
case SDL_KEYUP:
369377
{
378+
if (event->key.windowID != SDL_GetWindowID(bd->Window))
379+
return false;
370380
ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
371381
ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(event->key.keysym.sym, event->key.keysym.scancode);
372382
io.AddKeyEvent(key, (event->type == SDL_KEYDOWN));
@@ -375,6 +385,8 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
375385
}
376386
case SDL_WINDOWEVENT:
377387
{
388+
if (event->window.windowID != SDL_GetWindowID(bd->Window))
389+
return false;
378390
// - 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.
379391
// - However we won't get a correct LEAVE event for a captured window.
380392
// - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,

backends/imgui_impl_sdl3.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,17 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
307307
{
308308
case SDL_EVENT_MOUSE_MOTION:
309309
{
310+
if (event->motion.windowID != SDL_GetWindowID(bd->Window))
311+
return false;
310312
ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
311313
io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
312314
io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
313315
return true;
314316
}
315317
case SDL_EVENT_MOUSE_WHEEL:
316318
{
319+
if (event->wheel.windowID != SDL_GetWindowID(bd->Window))
320+
return false;
317321
//IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
318322
float wheel_x = -event->wheel.x;
319323
float wheel_y = event->wheel.y;
@@ -327,6 +331,8 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
327331
case SDL_EVENT_MOUSE_BUTTON_DOWN:
328332
case SDL_EVENT_MOUSE_BUTTON_UP:
329333
{
334+
if (event->button.windowID != SDL_GetWindowID(bd->Window))
335+
return false;
330336
int mouse_button = -1;
331337
if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
332338
if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
@@ -342,12 +348,16 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
342348
}
343349
case SDL_EVENT_TEXT_INPUT:
344350
{
351+
if (event->text.windowID != SDL_GetWindowID(bd->Window))
352+
return false;
345353
io.AddInputCharactersUTF8(event->text.text);
346354
return true;
347355
}
348356
case SDL_EVENT_KEY_DOWN:
349357
case SDL_EVENT_KEY_UP:
350358
{
359+
if (event->key.windowID != SDL_GetWindowID(bd->Window))
360+
return false;
351361
//IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%d: key=%d, scancode=%d, mod=%X\n", (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP", event->key.key, event->key.scancode, event->key.mod);
352362
ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod);
353363
ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode);
@@ -357,6 +367,8 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
357367
}
358368
case SDL_EVENT_WINDOW_MOUSE_ENTER:
359369
{
370+
if (event->window.windowID != SDL_GetWindowID(bd->Window))
371+
return false;
360372
bd->MouseWindowID = event->window.windowID;
361373
bd->MousePendingLeaveFrame = 0;
362374
return true;
@@ -367,13 +379,19 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
367379
// FIXME: Unconfirmed whether this is still needed with SDL3.
368380
case SDL_EVENT_WINDOW_MOUSE_LEAVE:
369381
{
382+
if (event->window.windowID != SDL_GetWindowID(bd->Window))
383+
return false;
370384
bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1;
371385
return true;
372386
}
373387
case SDL_EVENT_WINDOW_FOCUS_GAINED:
388+
if (event->window.windowID != SDL_GetWindowID(bd->Window))
389+
return false;
374390
io.AddFocusEvent(true);
375391
return true;
376392
case SDL_EVENT_WINDOW_FOCUS_LOST:
393+
if (event->window.windowID != SDL_GetWindowID(bd->Window))
394+
return false;
377395
io.AddFocusEvent(false);
378396
return true;
379397
case SDL_EVENT_GAMEPAD_ADDED:

0 commit comments

Comments
 (0)