From c44d1151c24830dbcf465cab4235c81084811bd9 Mon Sep 17 00:00:00 2001 From: Bepso <84444906+Bepso@users.noreply.github.com> Date: Wed, 6 Oct 2021 17:46:01 +0200 Subject: [PATCH] Close #13766 : Refactor Clear Scenery to class (#15481) * Add CleanScenery class --- src/openrct2-ui/interface/Window.cpp | 8 + src/openrct2-ui/interface/Window.h | 3 + src/openrct2-ui/windows/ClearScenery.cpp | 359 +++++++++++------------ 3 files changed, 175 insertions(+), 195 deletions(-) diff --git a/src/openrct2-ui/interface/Window.cpp b/src/openrct2-ui/interface/Window.cpp index 94ac25450c..b3134b0ea0 100644 --- a/src/openrct2-ui/interface/Window.cpp +++ b/src/openrct2-ui/interface/Window.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -750,3 +751,10 @@ void Window::Close() { window_close(this); } + +void Window::TextInputOpen( + rct_widgetindex callWidget, rct_string_id title, rct_string_id description, const Formatter& descriptionArgs, + rct_string_id existingText, uintptr_t existingArgs, int32_t maxLength) +{ + window_text_input_open(this, callWidget, title, description, descriptionArgs, existingText, existingArgs, maxLength); +} diff --git a/src/openrct2-ui/interface/Window.h b/src/openrct2-ui/interface/Window.h index 19b0bacc0a..d0c08351d4 100644 --- a/src/openrct2-ui/interface/Window.h +++ b/src/openrct2-ui/interface/Window.h @@ -26,6 +26,9 @@ struct Window : rct_window void SetCheckboxValue(rct_widgetindex widgetIndex, bool value); void DrawWidgets(rct_drawpixelinfo& dpi); void Close(); + void TextInputOpen( + rct_widgetindex callWidget, rct_string_id title, rct_string_id description, const Formatter& descriptionArgs, + rct_string_id existingText, uintptr_t existingArgs, int32_t maxLength); }; void WindowAllWheelInput(); diff --git a/src/openrct2-ui/windows/ClearScenery.cpp b/src/openrct2-ui/windows/ClearScenery.cpp index f870705565..35f75c7a88 100644 --- a/src/openrct2-ui/windows/ClearScenery.cpp +++ b/src/openrct2-ui/windows/ClearScenery.cpp @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2014-2020 OpenRCT2 developers + * Copyright (c) 2014-2021 OpenRCT2 developers * * For a complete list of all authors, please refer to contributors.md * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 @@ -16,8 +16,8 @@ #include #include -// clang-format off -enum WINDOW_CLEAR_SCENERY_WIDGET_IDX { +enum WINDOW_CLEAR_SCENERY_WIDGET_IDX +{ WIDX_BACKGROUND, WIDX_TITLE, WIDX_CLOSE, @@ -28,213 +28,182 @@ enum WINDOW_CLEAR_SCENERY_WIDGET_IDX { WIDX_LARGE_SCENERY, WIDX_FOOTPATH }; - +// clang-format on static constexpr const rct_string_id WINDOW_TITLE = STR_CLEAR_SCENERY; static constexpr const int32_t WW = 98; static constexpr const int32_t WH = 94; +static constexpr ScreenSize CLEAR_SCENERY_BUTTON = { 24, 24 }; + static rct_widget window_clear_scenery_widgets[] = { WINDOW_SHIM(WINDOW_TITLE, WW, WH), - MakeWidget ({27, 17}, {44, 32}, WindowWidgetType::ImgBtn, WindowColour::Primary , SPR_LAND_TOOL_SIZE_0, STR_NONE), // preview box - MakeRemapWidget({28, 18}, {16, 16}, WindowWidgetType::TrnBtn, WindowColour::Secondary, SPR_LAND_TOOL_DECREASE, STR_ADJUST_SMALLER_LAND_TIP), // decrement size - MakeRemapWidget({54, 32}, {16, 16}, WindowWidgetType::TrnBtn, WindowColour::Secondary, SPR_LAND_TOOL_INCREASE, STR_ADJUST_LARGER_LAND_TIP), // increment size - MakeRemapWidget({ 7, 53}, {24, 24}, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_G2_BUTTON_TREES, STR_CLEAR_SCENERY_REMOVE_SMALL_SCENERY_TIP), // small scenery - MakeRemapWidget({37, 53}, {24, 24}, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_G2_BUTTON_LARGE_SCENERY, STR_CLEAR_SCENERY_REMOVE_LARGE_SCENERY_TIP), // large scenery - MakeRemapWidget({67, 53}, {24, 24}, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_G2_BUTTON_FOOTPATH, STR_CLEAR_SCENERY_REMOVE_FOOTPATHS_TIP), // footpaths + MakeWidget( + { 27, 17 }, { 44, 32 }, WindowWidgetType::ImgBtn, WindowColour::Primary, SPR_LAND_TOOL_SIZE_0, STR_NONE), // preview box + MakeRemapWidget( + { 28, 18 }, { 16, 16 }, WindowWidgetType::TrnBtn, WindowColour::Secondary, SPR_LAND_TOOL_DECREASE, + STR_ADJUST_SMALLER_LAND_TIP), // decrement size + MakeRemapWidget( + { 54, 32 }, { 16, 16 }, WindowWidgetType::TrnBtn, WindowColour::Secondary, SPR_LAND_TOOL_INCREASE, + STR_ADJUST_LARGER_LAND_TIP), // increment size + MakeRemapWidget( + { 7, 53 }, CLEAR_SCENERY_BUTTON, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_G2_BUTTON_TREES, + STR_CLEAR_SCENERY_REMOVE_SMALL_SCENERY_TIP), // small scenery + MakeRemapWidget( + { 37, 53 }, CLEAR_SCENERY_BUTTON, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_G2_BUTTON_LARGE_SCENERY, + STR_CLEAR_SCENERY_REMOVE_LARGE_SCENERY_TIP), // large scenery + MakeRemapWidget( + { 67, 53 }, CLEAR_SCENERY_BUTTON, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_G2_BUTTON_FOOTPATH, + STR_CLEAR_SCENERY_REMOVE_FOOTPATHS_TIP), // footpaths WIDGETS_END, }; -static void window_clear_scenery_close(rct_window *w); -static void window_clear_scenery_mouseup(rct_window *w, rct_widgetindex widgetIndex); -static void window_clear_scenery_mousedown(rct_window *w, rct_widgetindex widgetIndex, rct_widget *widget); -static void window_clear_scenery_update(rct_window *w); -static void window_clear_scenery_invalidate(rct_window *w); -static void window_clear_scenery_paint(rct_window *w, rct_drawpixelinfo *dpi); -static void window_clear_scenery_textinput(rct_window *w, rct_widgetindex widgetIndex, char *text); -static void window_clear_scenery_inputsize(rct_window *w); - -static rct_window_event_list window_clear_scenery_events([](auto& events) +class CleanSceneryWindow final : public Window { - events.close = &window_clear_scenery_close; - events.mouse_up = &window_clear_scenery_mouseup; - events.mouse_down = &window_clear_scenery_mousedown; - events.update = &window_clear_scenery_update; - events.text_input = &window_clear_scenery_textinput; - events.invalidate = &window_clear_scenery_invalidate; - events.paint = &window_clear_scenery_paint; -}); -// clang-format on +public: + void OnOpen() override + { + widgets = window_clear_scenery_widgets; + enabled_widgets = (1ULL << WIDX_CLOSE) | (1ULL << WIDX_INCREMENT) | (1ULL << WIDX_DECREMENT) | (1ULL << WIDX_PREVIEW) + | (1ULL << WIDX_SMALL_SCENERY) | (1ULL << WIDX_LARGE_SCENERY) | (1ULL << WIDX_FOOTPATH); + hold_down_widgets = (1ULL << WIDX_INCREMENT) | (1ULL << WIDX_DECREMENT); + WindowInitScrollWidgets(this); + window_push_others_below(this); + + gLandToolSize = 2; + gClearSceneryCost = MONEY64_UNDEFINED; + + gClearSmallScenery = true; + gClearLargeScenery = false; + gClearFootpath = false; + } + + void OnClose() override + { + if (clear_scenery_tool_is_active()) + tool_cancel(); + } + + void OnMouseUp(const rct_widgetindex widgetIndex) override + { + switch (widgetIndex) + { + case WIDX_CLOSE: + Close(); + break; + case WIDX_PREVIEW: + { + Formatter ft; + ft.Add(MINIMUM_TOOL_SIZE); + ft.Add(MAXIMUM_TOOL_SIZE); + TextInputOpen(WIDX_PREVIEW, STR_SELECTION_SIZE, STR_ENTER_SELECTION_SIZE, ft, STR_NONE, STR_NONE, 3); + break; + } + case WIDX_SMALL_SCENERY: + gClearSmallScenery ^= 1; + Invalidate(); + break; + case WIDX_LARGE_SCENERY: + gClearLargeScenery ^= 1; + Invalidate(); + break; + case WIDX_FOOTPATH: + gClearFootpath ^= 1; + Invalidate(); + break; + } + } + + void OnMouseDown(const rct_widgetindex widgetIndex) override + { + switch (widgetIndex) + { + case WIDX_DECREMENT: + // Decrement land tool size, if it stays within the limit + gLandToolSize = std::max(MINIMUM_TOOL_SIZE, gLandToolSize - 1); + + // Invalidate the window + Invalidate(); + break; + case WIDX_INCREMENT: + // Increment land tool size, if it stays within the limit + gLandToolSize = std::min(MAXIMUM_TOOL_SIZE, gLandToolSize + 1); + + // Invalidate the window + Invalidate(); + break; + } + } + + void OnTextInput(const rct_widgetindex widgetIndex, const std::string_view text) override + { + if (widgetIndex != WIDX_PREVIEW || text.empty()) + return; + + char* end; + int32_t size = strtol(std::string(text).c_str(), &end, 10); + if (*end == '\0') + { + size = std::clamp(size, MINIMUM_TOOL_SIZE, MAXIMUM_TOOL_SIZE); + gLandToolSize = size; + Invalidate(); + } + } + + void OnUpdate() override + { + frame_no++; + // Close window if another tool is open + if (!clear_scenery_tool_is_active()) + Close(); + } + + void Invalidate() + { + // Set the preview image button to be pressed down + pressed_widgets = (1ULL << WIDX_PREVIEW) | (gClearSmallScenery ? (1ULL << WIDX_SMALL_SCENERY) : 0) + | (gClearLargeScenery ? (1ULL << WIDX_LARGE_SCENERY) : 0) | (gClearFootpath ? (1ULL << WIDX_FOOTPATH) : 0); + + // Update the preview image (for tool sizes up to 7) + window_clear_scenery_widgets[WIDX_PREVIEW].image = LandTool::SizeToSpriteIndex(gLandToolSize); + } + + void OnDraw(rct_drawpixelinfo& dpi) override + { + DrawWidgets(dpi); + + // Draw number for tool sizes bigger than 7 + ScreenCoordsXY screenCoords = { windowPos.x + window_clear_scenery_widgets[WIDX_PREVIEW].midX(), + windowPos.y + window_clear_scenery_widgets[WIDX_PREVIEW].midY() }; + if (gLandToolSize > MAX_TOOL_SIZE_WITH_SPRITE) + { + auto ft = Formatter(); + ft.Add(gLandToolSize); + DrawTextBasic(&dpi, screenCoords - ScreenCoordsXY{ 0, 2 }, STR_LAND_TOOL_SIZE_VALUE, ft, { TextAlignment::CENTRE }); + } + + // Draw cost amount + if (gClearSceneryCost != MONEY64_UNDEFINED && gClearSceneryCost != 0 && !(gParkFlags & PARK_FLAGS_NO_MONEY)) + { + auto ft = Formatter(); + ft.Add(gClearSceneryCost); + screenCoords.x = window_clear_scenery_widgets[WIDX_PREVIEW].midX() + windowPos.x; + screenCoords.y = window_clear_scenery_widgets[WIDX_PREVIEW].bottom + windowPos.y + 5 + 27; + DrawTextBasic(&dpi, screenCoords, STR_COST_AMOUNT, ft, { TextAlignment::CENTRE }); + } + } +}; -/** - * - * rct2: 0x0068E0A7 - */ rct_window* window_clear_scenery_open() { - rct_window* window; + auto* w = static_cast(window_bring_to_front_by_class(WC_CLEAR_SCENERY)); - // Check if window is already open - window = window_find_by_class(WC_CLEAR_SCENERY); - if (window != nullptr) - return window; + if (w != nullptr) + return w; - window = WindowCreate( - ScreenCoordsXY(context_get_width() - WW, 29), WW, WH, &window_clear_scenery_events, WC_CLEAR_SCENERY, 0); - window->widgets = window_clear_scenery_widgets; - window->enabled_widgets = (1ULL << WIDX_CLOSE) | (1ULL << WIDX_INCREMENT) | (1ULL << WIDX_DECREMENT) - | (1ULL << WIDX_PREVIEW) | (1ULL << WIDX_SMALL_SCENERY) | (1ULL << WIDX_LARGE_SCENERY) | (1ULL << WIDX_FOOTPATH); - window->hold_down_widgets = (1ULL << WIDX_INCREMENT) | (1ULL << WIDX_DECREMENT); - WindowInitScrollWidgets(window); - window_push_others_below(window); + w = WindowCreate(WC_CLEAR_SCENERY, WW, WH, 0); - gLandToolSize = 2; - gClearSceneryCost = MONEY64_UNDEFINED; + if (w != nullptr) + return w; - gClearSmallScenery = true; - gClearLargeScenery = false; - gClearFootpath = false; - - return window; -} - -/** - * - * rct2: 0x006E6B65 - */ -static void window_clear_scenery_close([[maybe_unused]] rct_window* w) -{ - // If the tool wasn't changed, turn tool off - if (clear_scenery_tool_is_active()) - tool_cancel(); -} - -/** - * - * rct2: 0x0068E185 - */ -static void window_clear_scenery_mouseup(rct_window* w, rct_widgetindex widgetIndex) -{ - switch (widgetIndex) - { - case WIDX_CLOSE: - window_close(w); - break; - case WIDX_PREVIEW: - window_clear_scenery_inputsize(w); - break; - case WIDX_SMALL_SCENERY: - gClearSmallScenery ^= 1; - w->Invalidate(); - break; - case WIDX_LARGE_SCENERY: - gClearLargeScenery ^= 1; - w->Invalidate(); - break; - case WIDX_FOOTPATH: - gClearFootpath ^= 1; - w->Invalidate(); - break; - } -} - -static void window_clear_scenery_mousedown(rct_window* w, rct_widgetindex widgetIndex, [[maybe_unused]] rct_widget* widget) -{ - switch (widgetIndex) - { - case WIDX_DECREMENT: - // Decrement land tool size, if it stays within the limit - gLandToolSize = std::max(MINIMUM_TOOL_SIZE, gLandToolSize - 1); - - // Invalidate the window - w->Invalidate(); - break; - case WIDX_INCREMENT: - // Increment land tool size, if it stays within the limit - gLandToolSize = std::min(MAXIMUM_TOOL_SIZE, gLandToolSize + 1); - - // Invalidate the window - w->Invalidate(); - break; - } -} - -static void window_clear_scenery_textinput(rct_window* w, rct_widgetindex widgetIndex, char* text) -{ - int32_t size; - char* end; - - if (widgetIndex != WIDX_PREVIEW || text == nullptr) - return; - - size = strtol(text, &end, 10); - if (*end == '\0') - { - size = std::max(MINIMUM_TOOL_SIZE, size); - size = std::min(MAXIMUM_TOOL_SIZE, size); - gLandToolSize = size; - w->Invalidate(); - } -} - -static void window_clear_scenery_inputsize(rct_window* w) -{ - Formatter ft; - ft.Add(MINIMUM_TOOL_SIZE); - ft.Add(MAXIMUM_TOOL_SIZE); - window_text_input_open(w, WIDX_PREVIEW, STR_SELECTION_SIZE, STR_ENTER_SELECTION_SIZE, ft, STR_NONE, STR_NONE, 3); -} - -/** - * - * rct2: 0x0068E205 - */ -static void window_clear_scenery_update(rct_window* w) -{ - w->frame_no++; - // Close window if another tool is open - if (!clear_scenery_tool_is_active()) - window_close(w); -} - -/** - * - * rct2: 0x0068E115 - */ -static void window_clear_scenery_invalidate(rct_window* w) -{ - // Set the preview image button to be pressed down - w->pressed_widgets = (1ULL << WIDX_PREVIEW) | (gClearSmallScenery ? (1ULL << WIDX_SMALL_SCENERY) : 0) - | (gClearLargeScenery ? (1ULL << WIDX_LARGE_SCENERY) : 0) | (gClearFootpath ? (1ULL << WIDX_FOOTPATH) : 0); - - // Update the preview image (for tool sizes up to 7) - window_clear_scenery_widgets[WIDX_PREVIEW].image = LandTool::SizeToSpriteIndex(gLandToolSize); -} - -/** - * - * rct2: 0x0068E130 - */ -static void window_clear_scenery_paint(rct_window* w, rct_drawpixelinfo* dpi) -{ - WindowDrawWidgets(w, dpi); - - // Draw number for tool sizes bigger than 7 - ScreenCoordsXY screenCoords = { w->windowPos.x + window_clear_scenery_widgets[WIDX_PREVIEW].midX(), - w->windowPos.y + window_clear_scenery_widgets[WIDX_PREVIEW].midY() }; - if (gLandToolSize > MAX_TOOL_SIZE_WITH_SPRITE) - { - auto ft = Formatter(); - ft.Add(gLandToolSize); - DrawTextBasic(dpi, screenCoords - ScreenCoordsXY{ 0, 2 }, STR_LAND_TOOL_SIZE_VALUE, ft, { TextAlignment::CENTRE }); - } - - // Draw cost amount - if (gClearSceneryCost != MONEY64_UNDEFINED && gClearSceneryCost != 0 && !(gParkFlags & PARK_FLAGS_NO_MONEY)) - { - auto ft = Formatter(); - ft.Add(gClearSceneryCost); - screenCoords.x = window_clear_scenery_widgets[WIDX_PREVIEW].midX() + w->windowPos.x; - screenCoords.y = window_clear_scenery_widgets[WIDX_PREVIEW].bottom + w->windowPos.y + 5 + 27; - DrawTextBasic(dpi, screenCoords, STR_COST_AMOUNT, ft, { TextAlignment::CENTRE }); - } + return nullptr; }