From 6c5aeae01b87cbb40b195c50499927068e4a2bd0 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 13 Dec 2023 02:19:16 +0000 Subject: [PATCH] Fix #11577: Extra viewport opened in wrong location. (#11578) ExtraViewportWindow calls IninitializeViewport() with focus as 0, which is ambiguous as focus should be either a TileIndex or a VehicleID. Instead, pass the tile and let InitializeViewport() handle setting all the coordinates. --- src/viewport.cpp | 17 ++++++++++++++--- src/viewport_gui.cpp | 19 +------------------ 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/viewport.cpp b/src/viewport.cpp index 5d1b4f634c..797f16aaae 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -242,11 +242,22 @@ void InitializeWindowViewport(Window *w, int x, int y, veh = Vehicle::Get(vp->follow_vehicle); pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos); } else { - x = TileX(std::get(focus)) * TILE_SIZE; - y = TileY(std::get(focus)) * TILE_SIZE; + TileIndex tile = std::get(focus); + if (tile == INVALID_TILE) { + /* No tile? Use center of main viewport. */ + const Window *mw = GetMainWindow(); + /* center on same place as main window (zoom is maximum, no adjustment needed) */ + pt.x = mw->viewport->scrollpos_x + mw->viewport->virtual_width / 2; + pt.x -= vp->virtual_width / 2; + pt.y = mw->viewport->scrollpos_y + mw->viewport->virtual_height / 2; + pt.y -= vp->virtual_height / 2; + } else { + x = TileX(tile) * TILE_SIZE; + y = TileY(tile) * TILE_SIZE; + pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y)); + } vp->follow_vehicle = INVALID_VEHICLE; - pt = MapXYZToViewport(vp, x, y, GetSlopePixelZ(x, y)); } vp->scrollpos_x = pt.x; diff --git a/src/viewport_gui.cpp b/src/viewport_gui.cpp index 1f5178aff2..ccf02c31fc 100644 --- a/src/viewport_gui.cpp +++ b/src/viewport_gui.cpp @@ -57,25 +57,8 @@ public: this->InitNested(window_number); NWidgetViewport *nvp = this->GetWidget(WID_EV_VIEWPORT); - nvp->InitializeViewport(this, 0, ScaleZoomGUI(ZOOM_LVL_VIEWPORT)); + nvp->InitializeViewport(this, tile, ScaleZoomGUI(ZOOM_LVL_VIEWPORT)); if (_settings_client.gui.zoom_min == viewport->zoom) this->DisableWidget(WID_EV_ZOOM_IN); - - Point pt; - if (tile == INVALID_TILE) { - /* No tile? Use center of main viewport. */ - const Window *w = GetMainWindow(); - - /* center on same place as main window (zoom is maximum, no adjustment needed) */ - pt.x = w->viewport->scrollpos_x + w->viewport->virtual_width / 2; - pt.y = w->viewport->scrollpos_y + w->viewport->virtual_height / 2; - } else { - pt = RemapCoords(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, TilePixelHeight(tile)); - } - - this->viewport->scrollpos_x = pt.x - this->viewport->virtual_width / 2; - this->viewport->scrollpos_y = pt.y - this->viewport->virtual_height / 2; - this->viewport->dest_scrollpos_x = this->viewport->scrollpos_x; - this->viewport->dest_scrollpos_y = this->viewport->scrollpos_y; } void SetStringParameters(int widget) const override