From 787acb903b005320bef9eddab19588629856f8ef Mon Sep 17 00:00:00 2001 From: Tulio Leao Date: Sat, 29 Oct 2022 17:34:39 -0300 Subject: [PATCH] Close #13810: Refactor TrackDesignManage window to class (#18314) * Close #13810: Refactor TrackDesignManage window to class * Remove redundant condition on TrackDesignManageWindow::OnTextInput * Make early exit ifs on TrackDesignManageWindow::OnTextInput else ifs * Apply suggestions from code review Co-authored-by: Hielke Morsink Co-authored-by: Hielke Morsink --- src/openrct2-ui/windows/TrackDesignManage.cpp | 159 ++++++++---------- 1 file changed, 69 insertions(+), 90 deletions(-) diff --git a/src/openrct2-ui/windows/TrackDesignManage.cpp b/src/openrct2-ui/windows/TrackDesignManage.cpp index 4ec5f22ff1..9a33fbda8c 100644 --- a/src/openrct2-ui/windows/TrackDesignManage.cpp +++ b/src/openrct2-ui/windows/TrackDesignManage.cpp @@ -54,38 +54,35 @@ static rct_widget window_track_delete_prompt_widgets[] = { #pragma endregion -#pragma region Events - -static void WindowTrackManageClose(rct_window *w); -static void WindowTrackManageMouseup(rct_window *w, WidgetIndex widgetIndex); -static void WindowTrackManageTextinput(rct_window *w, WidgetIndex widgetIndex, char *text); -static void WindowTrackManagePaint(rct_window *w, rct_drawpixelinfo *dpi); - -static void WindowTrackDeletePromptMouseup(rct_window *w, WidgetIndex widgetIndex); -static void WindowTrackDeletePromptPaint(rct_window *w, rct_drawpixelinfo *dpi); - -// 0x009940EC -static WindowEventList window_track_manage_events([](auto& events) +class TrackDesignManageWindow final : public Window { - events.close = &WindowTrackManageClose; - events.mouse_up = &WindowTrackManageMouseup; - events.text_input = &WindowTrackManageTextinput; - events.paint = &WindowTrackManagePaint; -}); + private: + TrackDesignFileRef* _trackDesignFileReference{nullptr}; -// 0x0099415C -static WindowEventList window_track_delete_prompt_events([](auto& events) + public: + TrackDesignManageWindow(TrackDesignFileRef* tdFileRef) : _trackDesignFileReference(tdFileRef){} + + void OnOpen() override; + void OnClose() override; + void OnMouseUp(WidgetIndex widgetIndex) override; + void OnTextInput(WidgetIndex widgetIndex, std::string_view text) override; + void OnDraw(rct_drawpixelinfo& dpi) override; +}; + +class TrackDeletePromptWindow final : public Window { - events.mouse_up = &WindowTrackDeletePromptMouseup; - events.paint = &WindowTrackDeletePromptPaint; -}); -// clang-format on + private: + TrackDesignFileRef* _trackDesignFileReference{nullptr}; -#pragma endregion + public: + TrackDeletePromptWindow(TrackDesignFileRef* tdFileRef) : _trackDesignFileReference(tdFileRef){} -static TrackDesignFileRef* _trackDesignFileReference; + void OnOpen() override; + void OnMouseUp(WidgetIndex widgetIndex) override; + void OnDraw(rct_drawpixelinfo& dpi) override; +}; -static void WindowTrackDeletePromptOpen(); +static void WindowTrackDeletePromptOpen(TrackDesignFileRef* tdFileRef); static void WindowTrackDesignListReloadTracks(); /** @@ -95,28 +92,27 @@ static void WindowTrackDesignListReloadTracks(); rct_window* WindowTrackManageOpen(TrackDesignFileRef* tdFileRef) { window_close_by_class(WindowClass::ManageTrackDesign); + auto trackDesignManageWindow = std::make_unique(tdFileRef); + auto* window = WindowCreate( + std::move(trackDesignManageWindow), WindowClass::ManageTrackDesign, {}, WW, WH, + WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_CENTRE_SCREEN | WF_AUTO_POSITION); - rct_window* w = WindowCreateCentred( - WW, WH, &window_track_manage_events, WindowClass::ManageTrackDesign, WF_STICK_TO_FRONT | WF_TRANSPARENT); - w->widgets = window_track_manage_widgets; - WindowInitScrollWidgets(*w); + return window; +} - rct_window* trackDesignListWindow = window_find_by_class(WindowClass::TrackDesignList); +void TrackDesignManageWindow::OnOpen() +{ + widgets = window_track_manage_widgets; + WindowInitScrollWidgets(*this); + + auto* trackDesignListWindow = window_find_by_class(WindowClass::TrackDesignList); if (trackDesignListWindow != nullptr) { trackDesignListWindow->track_list.track_list_being_updated = true; } - - _trackDesignFileReference = tdFileRef; - - return w; } -/** - * - * rct2: 0x006D364C - */ -static void WindowTrackManageClose(rct_window* w) +void TrackDesignManageWindow::OnClose() { rct_window* trackDesignListWindow = window_find_by_class(WindowClass::TrackDesignList); if (trackDesignListWindow != nullptr) @@ -125,56 +121,46 @@ static void WindowTrackManageClose(rct_window* w) } } -/** - * - * rct2: 0x006D3523 - */ -static void WindowTrackManageMouseup(rct_window* w, WidgetIndex widgetIndex) +void TrackDesignManageWindow::OnMouseUp(WidgetIndex widgetIndex) { switch (widgetIndex) { case WIDX_CLOSE: window_close_by_class(WindowClass::TrackDeletePrompt); - window_close(*w); + Close(); break; case WIDX_RENAME: WindowTextInputRawOpen( - w, widgetIndex, STR_TRACK_DESIGN_RENAME_TITLE, STR_TRACK_DESIGN_RENAME_DESC, {}, + this, widgetIndex, STR_TRACK_DESIGN_RENAME_TITLE, STR_TRACK_DESIGN_RENAME_DESC, {}, _trackDesignFileReference->name.c_str(), TrackDesignNameMaxLength); break; case WIDX_DELETE: - WindowTrackDeletePromptOpen(); + WindowTrackDeletePromptOpen(_trackDesignFileReference); break; } } -/** - * - * rct2: 0x006D3523 - */ -static void WindowTrackManageTextinput(rct_window* w, WidgetIndex widgetIndex, char* text) +void TrackDesignManageWindow::OnTextInput(WidgetIndex widgetIndex, std::string_view text) { - if (widgetIndex != WIDX_RENAME || str_is_null_or_empty(text)) + if (widgetIndex != WIDX_RENAME) { return; } - - if (str_is_null_or_empty(text)) + else if (text.empty()) { context_show_error(STR_CANT_RENAME_TRACK_DESIGN, STR_NONE, {}); return; } - - if (!Platform::IsFilenameValid(text)) + else if (!Platform::IsFilenameValid(text)) { context_show_error(STR_CANT_RENAME_TRACK_DESIGN, STR_NEW_NAME_CONTAINS_INVALID_CHARACTERS, {}); return; } - if (track_repository_rename(_trackDesignFileReference->path, text)) + if (track_repository_rename(_trackDesignFileReference->path, std::string(text))) { window_close_by_class(WindowClass::TrackDeletePrompt); - window_close(*w); + Close(); WindowTrackDesignListReloadTracks(); } else @@ -183,51 +169,48 @@ static void WindowTrackManageTextinput(rct_window* w, WidgetIndex widgetIndex, c } } -/** - * - * rct2: 0x006D3523 - */ -static void WindowTrackManagePaint(rct_window* w, rct_drawpixelinfo* dpi) +void TrackDesignManageWindow::OnDraw(rct_drawpixelinfo& dpi) { Formatter::Common().Add(_trackDesignFileReference->name.c_str()); - WindowDrawWidgets(*w, dpi); + DrawWidgets(dpi); } /** * * rct2: 0x006D35CD */ -static void WindowTrackDeletePromptOpen() +static void WindowTrackDeletePromptOpen(TrackDesignFileRef* tdFileRef) { window_close_by_class(WindowClass::TrackDeletePrompt); int32_t screenWidth = context_get_width(); int32_t screenHeight = context_get_height(); - rct_window* w = WindowCreate( - ScreenCoordsXY( - std::max(TOP_TOOLBAR_HEIGHT + 1, (screenWidth - WW_DELETE_PROMPT) / 2), (screenHeight - WH_DELETE_PROMPT) / 2), - WW_DELETE_PROMPT, WH_DELETE_PROMPT, &window_track_delete_prompt_events, WindowClass::TrackDeletePrompt, - WF_STICK_TO_FRONT); - w->widgets = window_track_delete_prompt_widgets; - WindowInitScrollWidgets(*w); - w->flags |= WF_TRANSPARENT; + auto trackDeletePromptWindow = std::make_unique(tdFileRef); + WindowCreate( + std::move(trackDeletePromptWindow), WindowClass::TrackDeletePrompt, ScreenCoordsXY( + std::max(TOP_TOOLBAR_HEIGHT + 1, (screenWidth - WW_DELETE_PROMPT) / 2), (screenHeight - WH_DELETE_PROMPT) / 2), WW_DELETE_PROMPT, WH_DELETE_PROMPT, + WF_STICK_TO_FRONT | WF_TRANSPARENT); } -/** - * - * rct2: 0x006D3823 - */ -static void WindowTrackDeletePromptMouseup(rct_window* w, WidgetIndex widgetIndex) +void TrackDeletePromptWindow::OnOpen() +{ + widgets = window_track_delete_prompt_widgets; + WindowInitScrollWidgets(*this); +} + +void TrackDeletePromptWindow::OnMouseUp(WidgetIndex widgetIndex) { switch (widgetIndex) { case WIDX_CLOSE: case WIDX_PROMPT_CANCEL: - window_close(*w); + Close(); break; case WIDX_PROMPT_DELETE: - window_close(*w); - if (track_repository_delete(_trackDesignFileReference->path)) + // tdPath has to be saved before window is closed, as that would blank it out. + auto tdPath = _trackDesignFileReference->path; + Close(); + if (track_repository_delete(tdPath)) { window_close_by_class(WindowClass::ManageTrackDesign); WindowTrackDesignListReloadTracks(); @@ -240,18 +223,14 @@ static void WindowTrackDeletePromptMouseup(rct_window* w, WidgetIndex widgetInde } } -/** - * - * rct2: 0x006D37EE - */ -static void WindowTrackDeletePromptPaint(rct_window* w, rct_drawpixelinfo* dpi) +void TrackDeletePromptWindow::OnDraw(rct_drawpixelinfo& dpi) { - WindowDrawWidgets(*w, dpi); + DrawWidgets(dpi); auto ft = Formatter(); ft.Add(_trackDesignFileReference->name.c_str()); DrawTextWrapped( - dpi, { w->windowPos.x + (WW_DELETE_PROMPT / 2), w->windowPos.y + ((WH_DELETE_PROMPT / 2) - 9) }, (WW_DELETE_PROMPT - 4), + &dpi, { windowPos.x + (WW_DELETE_PROMPT / 2), windowPos.y + ((WH_DELETE_PROMPT / 2) - 9) }, (WW_DELETE_PROMPT - 4), STR_ARE_YOU_SURE_YOU_WANT_TO_PERMANENTLY_DELETE_TRACK, ft, { TextAlignment::CENTRE }); }