From b1e2ca94c7d59e95aa09224b412e13c978e0942c Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Fri, 2 Aug 2024 18:17:21 +0200 Subject: [PATCH] Fix clear scenery window closing (again) (#22456) * Move ClearSceneryToolIsActive into ClearScenery window * Actually apply/paint map selection in clear scenery tool * LandRightsWindow: fix invalidation --- src/openrct2-ui/windows/ClearScenery.cpp | 96 +++++++++++++++++++++--- src/openrct2-ui/windows/LandRights.cpp | 2 +- src/openrct2-ui/windows/TopToolbar.cpp | 15 ---- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/openrct2-ui/windows/ClearScenery.cpp b/src/openrct2-ui/windows/ClearScenery.cpp index 1421bf8d38..1a164f8bc9 100644 --- a/src/openrct2-ui/windows/ClearScenery.cpp +++ b/src/openrct2-ui/windows/ClearScenery.cpp @@ -22,7 +22,7 @@ namespace OpenRCT2::Ui::Windows { - enum WindowClearSceneryWidgetIdx + enum WindowClearSceneryWidgetIdx : WidgetIndex { WIDX_BACKGROUND, WIDX_TITLE, @@ -226,12 +226,80 @@ namespace OpenRCT2::Ui::Windows return ClearAction(range, itemsToClear); } + int8_t ToolUpdateClearLandPaint(const ScreenCoordsXY& screenPos) + { + uint8_t state_changed = 0; + + MapInvalidateSelectionRect(); + gMapSelectFlags &= ~MAP_SELECT_FLAG_ENABLE; + + auto mapTile = ScreenGetMapXY(screenPos, nullptr); + + if (!mapTile.has_value()) + { + return state_changed; + } + + if (!(gMapSelectFlags & MAP_SELECT_FLAG_ENABLE)) + { + gMapSelectFlags |= MAP_SELECT_FLAG_ENABLE; + state_changed++; + } + + if (gMapSelectType != MAP_SELECT_TYPE_FULL) + { + gMapSelectType = MAP_SELECT_TYPE_FULL; + state_changed++; + } + + int16_t tool_size = std::max(1, gLandToolSize); + int16_t tool_length = (tool_size - 1) * 32; + + // Move to tool bottom left + mapTile->x -= (tool_size - 1) * 16; + mapTile->y -= (tool_size - 1) * 16; + mapTile = mapTile->ToTileStart(); + + if (gMapSelectPositionA.x != mapTile->x) + { + gMapSelectPositionA.x = mapTile->x; + state_changed++; + } + + if (gMapSelectPositionA.y != mapTile->y) + { + gMapSelectPositionA.y = mapTile->y; + state_changed++; + } + + mapTile->x += tool_length; + mapTile->y += tool_length; + + if (gMapSelectPositionB.x != mapTile->x) + { + gMapSelectPositionB.x = mapTile->x; + state_changed++; + } + + if (gMapSelectPositionB.y != mapTile->y) + { + gMapSelectPositionB.y = mapTile->y; + state_changed++; + } + + MapInvalidateSelectionRect(); + return state_changed; + } + /** * * rct2: 0x0068E213 */ void ToolUpdateSceneryClear(const ScreenCoordsXY& screenPos) { + if (!ToolUpdateClearLandPaint(screenPos)) + return; + auto action = GetClearAction(); auto result = GameActions::Query(&action); auto cost = (result.Error == GameActions::Status::Ok ? result.Cost : kMoney64Undefined); @@ -307,17 +375,23 @@ namespace OpenRCT2::Ui::Windows WindowBase* ClearSceneryOpen() { - auto* w = static_cast(WindowBringToFrontByClass(WindowClass::ClearScenery)); + return WindowFocusOrCreate( + WindowClass::ClearScenery, ScreenCoordsXY(ContextGetWidth() - WW, 29), WW, WH, 0); + } - if (w != nullptr) - return w; - - w = WindowCreate(WindowClass::ClearScenery, ScreenCoordsXY(ContextGetWidth() - WW, 29), WW, WH, 0); - - if (w != nullptr) - return w; - - return nullptr; + /** + * + * rct2: 0x0066D125 + */ + bool ClearSceneryToolIsActive() + { + if (!(InputTestFlag(INPUT_FLAG_TOOL_ACTIVE))) + return false; + if (gCurrentToolWidget.window_classification != WindowClass::ClearScenery) + return false; + if (gCurrentToolWidget.widget_index != WIDX_BACKGROUND) + return false; + return true; } /** diff --git a/src/openrct2-ui/windows/LandRights.cpp b/src/openrct2-ui/windows/LandRights.cpp index 5d32fb6198..c1d0de2811 100644 --- a/src/openrct2-ui/windows/LandRights.cpp +++ b/src/openrct2-ui/windows/LandRights.cpp @@ -250,7 +250,7 @@ static Widget window_land_rights_widgets[] = { if (_landRightsCost != kMoney64Undefined) { _landRightsCost = kMoney64Undefined; - WindowInvalidateByClass(WindowClass::ClearScenery); + WindowInvalidateByClass(WindowClass::LandRights); } return; } diff --git a/src/openrct2-ui/windows/TopToolbar.cpp b/src/openrct2-ui/windows/TopToolbar.cpp index 580375eba2..e79a212f2e 100644 --- a/src/openrct2-ui/windows/TopToolbar.cpp +++ b/src/openrct2-ui/windows/TopToolbar.cpp @@ -1016,21 +1016,6 @@ namespace OpenRCT2::Ui::Windows return window; } - /** - * - * rct2: 0x0066D125 - */ - bool ClearSceneryToolIsActive() - { - if (!(InputTestFlag(INPUT_FLAG_TOOL_ACTIVE))) - return false; - if (gCurrentToolWidget.window_classification != WindowClass::ClearScenery) - return false; - if (gCurrentToolWidget.widget_index != WIDX_CLEAR_SCENERY) - return false; - return true; - } - void TopToolbar::InitViewMenu(Widget& widget) { using namespace Dropdown;