From 83fcfa6e68d724922f9a2808418f169f6b5fc9f6 Mon Sep 17 00:00:00 2001 From: Jaime Centeno Date: Mon, 1 Feb 2021 07:33:22 -0600 Subject: [PATCH] Refactored water window code into WaterWindow class Created a new WaterWindow class that inherits from Window class and overrides On methods. Added a new generic method for WindowFocusOnCreate that takes position as an argument. --- src/openrct2-ui/windows/Water.cpp | 314 +++++++++++++----------------- src/openrct2/interface/Window.h | 10 + 2 files changed, 150 insertions(+), 174 deletions(-) diff --git a/src/openrct2-ui/windows/Water.cpp b/src/openrct2-ui/windows/Water.cpp index b0d16a903a..e0fdfc73e9 100644 --- a/src/openrct2-ui/windows/Water.cpp +++ b/src/openrct2-ui/windows/Water.cpp @@ -37,182 +37,148 @@ static rct_widget window_water_widgets[] = { MakeRemapWidget({43, 32}, {16, 16}, WindowWidgetType::TrnBtn, WindowColour::Tertiary, SPR_LAND_TOOL_INCREASE, STR_ADJUST_LARGER_WATER_TIP), // increment size { WIDGETS_END }, }; - -static void window_water_close(rct_window *w); -static void window_water_mouseup(rct_window *w, rct_widgetindex widgetIndex); -static void window_water_mousedown(rct_window *w, rct_widgetindex widgetIndex, rct_widget *widget); -static void window_water_update(rct_window *w); -static void window_water_invalidate(rct_window *w); -static void window_water_paint(rct_window *w, rct_drawpixelinfo *dpi); -static void window_water_textinput(rct_window *w, rct_widgetindex widgetIndex, char *text); -static void window_water_inputsize(rct_window *w); - -static rct_window_event_list window_water_events([](auto& events) -{ - events.close = &window_water_close; - events.mouse_up = &window_water_mouseup; - events.mouse_down = &window_water_mousedown; - events.update = &window_water_update; - events.text_input = &window_water_textinput; - events.invalidate = &window_water_invalidate; - events.paint = &window_water_paint; -}); // clang-format on -/** - * - * rct2: 0x006E6A40 - */ +class WaterWindow final : public Window +{ +public: + void OnOpen() override + { + widgets = window_water_widgets; + enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_DECREMENT) | (1 << WIDX_INCREMENT) | (1 << WIDX_PREVIEW); + hold_down_widgets = (1 << WIDX_INCREMENT) | (1 << WIDX_DECREMENT); + WindowInitScrollWidgets(this); + window_push_others_below(this); + + gLandToolSize = 1; + gWaterToolRaiseCost = MONEY32_UNDEFINED; + gWaterToolLowerCost = MONEY32_UNDEFINED; + } + + void OnClose() override + { + // If the tool wasn't changed, turn tool off + if (water_tool_is_active()) + { + tool_cancel(); + } + } + + void OnMouseUp(rct_widgetindex widgetIndex) override + { + switch (widgetIndex) + { + case WIDX_CLOSE: + Close(); + break; + case WIDX_PREVIEW: + InputSize(); + break; + } + } + + void OnMouseDown(rct_widgetindex widgetIndex) override + { + switch (widgetIndex) + { + case WIDX_DECREMENT: + // Decrement land tool size + gLandToolSize = std::max(MINIMUM_TOOL_SIZE, gLandToolSize - 1); + + // Invalidate the window + Invalidate(); + break; + case WIDX_INCREMENT: + // Increment land tool size + gLandToolSize = std::min(MAXIMUM_TOOL_SIZE, gLandToolSize + 1); + + // Invalidate the window + Invalidate(); + break; + } + } + + void OnUpdate() override + { + // Close window if another tool is open + if (!water_tool_is_active()) + { + Close(); + } + } + + void OnTextInput(rct_widgetindex widgetIndex, std::string_view text) override + { + int32_t size; + char* end; + + if (widgetIndex != WIDX_PREVIEW) + { + return; + } + + size = strtol(std::string(text).c_str(), &end, 10); + if (*end == '\0') + { + size = std::max(MINIMUM_TOOL_SIZE, size); + size = std::min(MAXIMUM_TOOL_SIZE, size); + gLandToolSize = size; + + Invalidate(); + } + } + + void OnPrepareDraw() override + { + // Set the preview image button to be pressed down + SetWidgetPressed(WIDX_PREVIEW, true); + + // Update the preview image + widgets[WIDX_PREVIEW].image = LandTool::SizeToSpriteIndex(gLandToolSize); + } + + void OnDraw(rct_drawpixelinfo& dpi) override + { + auto screenCoords = ScreenCoordsXY{ windowPos.x + window_water_widgets[WIDX_PREVIEW].midX(), + windowPos.y + window_water_widgets[WIDX_PREVIEW].midY() }; + + DrawWidgets(dpi); + // Draw number for tool sizes bigger than 7 + if (gLandToolSize > MAX_TOOL_SIZE_WITH_SPRITE) + { + gfx_draw_string_centred( + &dpi, STR_LAND_TOOL_SIZE_VALUE, screenCoords - ScreenCoordsXY{ 0, 2 }, COLOUR_BLACK, &gLandToolSize); + } + + if (!(gParkFlags & PARK_FLAGS_NO_MONEY)) + { + // Draw raise cost amount + screenCoords = { window_water_widgets[WIDX_PREVIEW].midX() + windowPos.x, + window_water_widgets[WIDX_PREVIEW].bottom + windowPos.y + 5 }; + if (gWaterToolRaiseCost != MONEY32_UNDEFINED && gWaterToolRaiseCost != 0) + { + gfx_draw_string_centred(&dpi, STR_RAISE_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolRaiseCost); + } + screenCoords.y += 10; + + // Draw lower cost amount + if (gWaterToolLowerCost != MONEY32_UNDEFINED && gWaterToolLowerCost != 0) + { + gfx_draw_string_centred(&dpi, STR_LOWER_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolLowerCost); + } + } + } + +private: + void InputSize() + { + TextInputDescriptionArgs[0] = MINIMUM_TOOL_SIZE; + TextInputDescriptionArgs[1] = MAXIMUM_TOOL_SIZE; + window_text_input_open(this, WIDX_PREVIEW, STR_SELECTION_SIZE, STR_ENTER_SELECTION_SIZE, STR_NONE, STR_NONE, 3); + } +}; + rct_window* window_water_open() { - rct_window* window; - - // Check if window is already open - window = window_find_by_class(WC_WATER); - if (window != nullptr) - return window; - - window = WindowCreate(ScreenCoordsXY(context_get_width() - 76, 29), 76, 77, &window_water_events, WC_WATER, 0); - window->widgets = window_water_widgets; - window->enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_DECREMENT) | (1 << WIDX_INCREMENT) | (1 << WIDX_PREVIEW); - window->hold_down_widgets = (1 << WIDX_INCREMENT) | (1 << WIDX_DECREMENT); - WindowInitScrollWidgets(window); - window_push_others_below(window); - - gLandToolSize = 1; - gWaterToolRaiseCost = MONEY32_UNDEFINED; - gWaterToolLowerCost = MONEY32_UNDEFINED; - - return window; -} - -/** - * - * rct2: 0x006E6B65 - */ -static void window_water_close(rct_window* w) -{ - // If the tool wasn't changed, turn tool off - if (water_tool_is_active()) - tool_cancel(); -} - -/** - * - * rct2: 0x006E6B4E - */ -static void window_water_mouseup(rct_window* w, rct_widgetindex widgetIndex) -{ - switch (widgetIndex) - { - case WIDX_CLOSE: - window_close(w); - break; - case WIDX_PREVIEW: - window_water_inputsize(w); - break; - } -} - -static void window_water_mousedown(rct_window* w, rct_widgetindex widgetIndex, rct_widget* widget) -{ - switch (widgetIndex) - { - case WIDX_DECREMENT: - // Decrement land tool size - gLandToolSize = std::max(MINIMUM_TOOL_SIZE, gLandToolSize - 1); - - // Invalidate the window - w->Invalidate(); - break; - case WIDX_INCREMENT: - // Increment land tool size - gLandToolSize = std::min(MAXIMUM_TOOL_SIZE, gLandToolSize + 1); - - // Invalidate the window - w->Invalidate(); - break; - } -} - -static void window_water_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_water_inputsize(rct_window* w) -{ - TextInputDescriptionArgs[0] = MINIMUM_TOOL_SIZE; - TextInputDescriptionArgs[1] = MAXIMUM_TOOL_SIZE; - window_text_input_open(w, WIDX_PREVIEW, STR_SELECTION_SIZE, STR_ENTER_SELECTION_SIZE, STR_NONE, STR_NONE, 3); -} - -/** - * - * rct2: 0x006E6BCE - */ -static void window_water_update(rct_window* w) -{ - // Close window if another tool is open - if (!water_tool_is_active()) - window_close(w); -} - -/** - * - * rct2: 0x006E6AB8 - */ -static void window_water_invalidate(rct_window* w) -{ - // Set the preview image button to be pressed down - w->pressed_widgets |= (1 << WIDX_PREVIEW); - - // Update the preview image - window_water_widgets[WIDX_PREVIEW].image = LandTool::SizeToSpriteIndex(gLandToolSize); -} - -/** - * - * rct2: 0x006E6ACF - */ -static void window_water_paint(rct_window* w, rct_drawpixelinfo* dpi) -{ - auto screenCoords = ScreenCoordsXY{ w->windowPos.x + window_water_widgets[WIDX_PREVIEW].midX(), - w->windowPos.y + window_water_widgets[WIDX_PREVIEW].midY() }; - - WindowDrawWidgets(w, dpi); - // Draw number for tool sizes bigger than 7 - if (gLandToolSize > MAX_TOOL_SIZE_WITH_SPRITE) - { - gfx_draw_string_centred( - dpi, STR_LAND_TOOL_SIZE_VALUE, screenCoords - ScreenCoordsXY{ 0, 2 }, COLOUR_BLACK, &gLandToolSize); - } - - if (!(gParkFlags & PARK_FLAGS_NO_MONEY)) - { - // Draw raise cost amount - screenCoords = { window_water_widgets[WIDX_PREVIEW].midX() + w->windowPos.x, - window_water_widgets[WIDX_PREVIEW].bottom + w->windowPos.y + 5 }; - if (gWaterToolRaiseCost != MONEY32_UNDEFINED && gWaterToolRaiseCost != 0) - gfx_draw_string_centred(dpi, STR_RAISE_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolRaiseCost); - screenCoords.y += 10; - - // Draw lower cost amount - if (gWaterToolLowerCost != MONEY32_UNDEFINED && gWaterToolLowerCost != 0) - gfx_draw_string_centred(dpi, STR_LOWER_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolLowerCost); - } + return WindowFocusOrCreate(WC_WATER, ScreenCoordsXY(context_get_width() - WW, 29), WW, WH, 0); } diff --git a/src/openrct2/interface/Window.h b/src/openrct2/interface/Window.h index 1993e2d178..a73881f434 100644 --- a/src/openrct2/interface/Window.h +++ b/src/openrct2/interface/Window.h @@ -708,6 +708,16 @@ T* WindowCreate(rct_windowclass cls, int32_t width, int32_t height, uint32_t fla return static_cast(WindowCreate(std::make_unique(), cls, {}, width, height, flags | WF_AUTO_POSITION)); } template::value>::type* = nullptr> +T* WindowFocusOrCreate(rct_windowclass cls, const ScreenCoordsXY& pos, int32_t width, int32_t height, uint32_t flags = 0) +{ + auto* w = window_bring_to_front_by_class(cls); + if (w == nullptr) + { + w = WindowCreate(cls, pos, width, height, flags); + } + return static_cast(w); +} +template::value>::type* = nullptr> T* WindowFocusOrCreate(rct_windowclass cls, int32_t width, int32_t height, uint32_t flags = 0) { auto* w = window_bring_to_front_by_class(cls);