From 6b961be93bf58718a40c57dc539e4cc2f6304a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Sun, 15 Jun 2025 11:48:46 +0200 Subject: [PATCH] Add: Select viewport under the cursor for gamepad scrolling --- src/window.cpp | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/window.cpp b/src/window.cpp index 65e9dbb2bd..550a02ca72 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3612,16 +3612,36 @@ void HandleGamepadScrolling(int stick_x, int stick_y, int max_axis_value) return; } - /* Apply scrolling to the main viewport */ + /* Apply scrolling based on cursor position */ if (_game_mode != GM_MENU && _game_mode != GM_BOOTSTRAP) { - Window *main_window = GetMainWindow(); - if (main_window != nullptr && main_window->viewport != nullptr) { + Window *target_window = nullptr; + + /* Check if cursor is over a window with a viewport */ + Window *w = FindWindowFromPt(_cursor.pos.x, _cursor.pos.y); + if (w != nullptr && w->viewport != nullptr) { + /* Check if cursor is actually over the viewport area within the window */ + Point pt = { _cursor.pos.x - w->left, _cursor.pos.y - w->top }; + if (pt.x >= w->viewport->left - w->left && + pt.x < w->viewport->left - w->left + w->viewport->width && + pt.y >= w->viewport->top - w->top && + pt.y < w->viewport->top - w->top + w->viewport->height) { + target_window = w; + } + } + + /* If no viewport under cursor, use main window */ + if (target_window == nullptr) { + target_window = GetMainWindow(); + } + + /* Apply scrolling to the target viewport */ + if (target_window != nullptr && target_window->viewport != nullptr) { /* Cancel vehicle following when gamepad scrolling */ - main_window->viewport->CancelFollow(*main_window); + target_window->viewport->CancelFollow(*target_window); /* Apply the scroll using the same method as keyboard scrolling */ - main_window->viewport->dest_scrollpos_x += ScaleByZoom(delta_x, main_window->viewport->zoom); - main_window->viewport->dest_scrollpos_y += ScaleByZoom(delta_y, main_window->viewport->zoom); + target_window->viewport->dest_scrollpos_x += ScaleByZoom(delta_x, target_window->viewport->zoom); + target_window->viewport->dest_scrollpos_y += ScaleByZoom(delta_y, target_window->viewport->zoom); } } }