From 056af36cdfefbdb42f78dbbca96a6a20fe4423b6 Mon Sep 17 00:00:00 2001 From: Hielke Morsink Date: Tue, 4 Oct 2022 23:59:46 +0200 Subject: [PATCH] Move initialization to constructor --- src/openrct2-ui/windows/LoadSave.cpp | 54 +++++++++++++++------------- src/openrct2/interface/Window.h | 8 +++-- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/openrct2-ui/windows/LoadSave.cpp b/src/openrct2-ui/windows/LoadSave.cpp index df4e4c4677..6023ab5f6f 100644 --- a/src/openrct2-ui/windows/LoadSave.cpp +++ b/src/openrct2-ui/windows/LoadSave.cpp @@ -396,7 +396,7 @@ static void Select(const char* path) } } -static u8string Browse(bool isSave) +static u8string OpenSystemFileBrowser(bool isSave) { OpenRCT2::Ui::FileDialogDesc desc = {}; u8string extension{}; @@ -484,9 +484,32 @@ static u8string Browse(bool isSave) class LoadSaveWindow final : public Window { public: - LoadSaveWindow() + LoadSaveWindow(int32_t type) { widgets = window_loadsave_widgets; + + min_width = WW; + min_height = WH / 2; + max_width = WW * 2; + max_height = WH * 2; + + const auto uiContext = OpenRCT2::GetContext()->GetUiContext(); + if (!uiContext->HasFilePicker()) + { + disabled_widgets |= (1uLL << WIDX_BROWSE); + window_loadsave_widgets[WIDX_BROWSE].type = WindowWidgetType::Empty; + } + + const bool isSave = (type & 0x01) == LOADSAVETYPE_SAVE; + const auto path = GetDir(type); + + const char* pattern = GetFilterPatternByType(type, isSave); + PopulateList(isSave, path, pattern); + no_list_items = static_cast(_listItems.size()); + selected_list_item = -1; + + InitScrollWidgets(); + ComputeMaxDateWidth(); } private: @@ -789,7 +812,7 @@ public: case WIDX_BROWSE: { - u8string path = Browse(isSave); + u8string path = OpenSystemFileBrowser(isSave); if (!path.empty()) { Select(path.c_str()); @@ -1014,7 +1037,7 @@ rct_window* WindowLoadsaveOpen( auto hasFilePicker = OpenRCT2::GetContext()->GetUiContext()->HasFilePicker(); if (gConfigGeneral.UseNativeBrowseDialog && hasFilePicker) { - const u8string path = Browse(isSave); + const u8string path = OpenSystemFileBrowser(isSave); if (!path.empty()) { Select(path.c_str()); @@ -1028,26 +1051,9 @@ rct_window* WindowLoadsaveOpen( if (w == nullptr) { w = WindowCreate( - WindowClass::Loadsave, WW, WH, WF_STICK_TO_FRONT | WF_RESIZABLE | WF_AUTO_POSITION | WF_CENTRE_SCREEN); - w->widgets = window_loadsave_widgets; - - w->min_width = WW; - w->min_height = WH / 2; - w->max_width = WW * 2; - w->max_height = WH * 2; - - if (!hasFilePicker) - { - w->disabled_widgets |= (1uLL << WIDX_BROWSE); - window_loadsave_widgets[WIDX_BROWSE].type = WindowWidgetType::Empty; - } + WindowClass::Loadsave, WW, WH, WF_STICK_TO_FRONT | WF_RESIZABLE | WF_AUTO_POSITION | WF_CENTRE_SCREEN, type); } - const char* pattern = GetFilterPatternByType(type, isSave); - w->PopulateList(isSave, path.c_str(), pattern); - w->no_list_items = static_cast(_listItems.size()); - w->selected_list_item = -1; - switch (type & 0x0E) { case LOADSAVETYPE_GAME: @@ -1074,11 +1080,9 @@ rct_window* WindowLoadsaveOpen( default: openrct2_assert(true, "Unsupported load/save type: %d", type & 0x0F); + break; } - WindowInitScrollWidgets(*w); - // WindowLoadsaveComputeMaxDateWidth(); - return w; } diff --git a/src/openrct2/interface/Window.h b/src/openrct2/interface/Window.h index 02909a72e8..f062327028 100644 --- a/src/openrct2/interface/Window.h +++ b/src/openrct2/interface/Window.h @@ -22,6 +22,7 @@ #include #include #include +#include #include struct rct_drawpixelinfo; @@ -567,10 +568,11 @@ T* WindowCreate(WindowClass cls, const ScreenCoordsXY& pos = {}, int32_t width = { return static_cast(WindowCreate(std::make_unique(), cls, pos, width, height, flags)); } -template::value>::type* = nullptr> -T* WindowCreate(WindowClass cls, int32_t width, int32_t height, uint32_t flags = 0) +template::value>::type* = nullptr> +T* WindowCreate(WindowClass cls, int32_t width, int32_t height, uint32_t flags = 0, TArgs&&... args) { - return static_cast(WindowCreate(std::make_unique(), cls, {}, width, height, flags | WF_AUTO_POSITION)); + return static_cast( + WindowCreate(std::make_unique(std::forward(args)...), cls, {}, width, height, flags | WF_AUTO_POSITION)); } template::value>::type* = nullptr> T* WindowFocusOrCreate(WindowClass cls, const ScreenCoordsXY& pos, int32_t width, int32_t height, uint32_t flags = 0)