From 8f72a478f0a048be0671777604b7d789d5d0959a Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 1 Feb 2025 20:54:10 +0100 Subject: [PATCH] Codechange: change 'return existing window' to a template parameter This change is made to make it possible to pass arbitrary other parameters to the constructor of the window instances. --- src/framerate_gui.cpp | 2 +- src/genworld_gui.cpp | 2 +- src/group_gui.cpp | 20 ++++++++++++++++---- src/group_gui.h | 2 +- src/newgrf_debug_gui.cpp | 2 +- src/story_gui.cpp | 2 +- src/window_gui.h | 16 ++++++++-------- 7 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/framerate_gui.cpp b/src/framerate_gui.cpp index b35e040060..e89b43a320 100644 --- a/src/framerate_gui.cpp +++ b/src/framerate_gui.cpp @@ -1030,7 +1030,7 @@ void ShowFramerateWindow() void ShowFrametimeGraphWindow(PerformanceElement elem) { if (elem < PFE_FIRST || elem >= PFE_MAX) return; // maybe warn? - AllocateWindowDescFront(_frametime_graph_window_desc, elem, true); + AllocateWindowDescFront(_frametime_graph_window_desc, elem); } /** Print performance statistics to game console */ diff --git a/src/genworld_gui.cpp b/src/genworld_gui.cpp index 41a6ea1890..2504acdd49 100644 --- a/src/genworld_gui.cpp +++ b/src/genworld_gui.cpp @@ -1030,7 +1030,7 @@ static void _ShowGenerateLandscape(GenerateLandscapeWindowMode mode) } WindowDesc &desc = (mode == GLWM_HEIGHTMAP) ? _heightmap_load_desc : _generate_landscape_desc; - GenerateLandscapeWindow *w = AllocateWindowDescFront(desc, mode, true); + GenerateLandscapeWindow *w = AllocateWindowDescFront(desc, mode); if (mode == GLWM_HEIGHTMAP) { w->x = x; diff --git a/src/group_gui.cpp b/src/group_gui.cpp index c3839b7d52..60a6323eab 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -1182,25 +1182,37 @@ static WindowDesc _vehicle_group_desc[] = { * @param company The company to show the window for. * @param vehicle_type The type of vehicle to show it for. * @param group The group to be selected. Defaults to INVALID_GROUP. - * @param need_existing_window Whether the existing window is needed. Defaults to false. + * @tparam Tneed_existing_window Whether the existing window is needed. */ -void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type, GroupID group, bool need_existing_window) +template +static void ShowCompanyGroupInternal(CompanyID company, VehicleType vehicle_type, GroupID group) { if (!Company::IsValidID(company)) return; assert(vehicle_type < std::size(_vehicle_group_desc)); const WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack(); - VehicleGroupWindow *w = AllocateWindowDescFront(_vehicle_group_desc[vehicle_type], num, need_existing_window); + VehicleGroupWindow *w = AllocateWindowDescFront(_vehicle_group_desc[vehicle_type], num); if (w != nullptr) w->SelectGroup(group); } +/** + * Show the group window for the given company and vehicle type. + * @param company The company to show the window for. + * @param vehicle_type The type of vehicle to show it for. + * @param group The group to be selected. Defaults to INVALID_GROUP. + */ +void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type, GroupID group) +{ + ShowCompanyGroupInternal(company, vehicle_type, group); +} + /** * Show the group window for the given vehicle. * @param v The vehicle to show the window for. */ void ShowCompanyGroupForVehicle(const Vehicle *v) { - ShowCompanyGroup(v->owner, v->type, v->group_id, true); + ShowCompanyGroupInternal(v->owner, v->type, v->group_id); } /** diff --git a/src/group_gui.h b/src/group_gui.h index d2fb2f71a0..816fcc4d50 100644 --- a/src/group_gui.h +++ b/src/group_gui.h @@ -13,7 +13,7 @@ #include "company_type.h" #include "vehicle_type.h" -void ShowCompanyGroup(CompanyID company, VehicleType veh, GroupID group = INVALID_GROUP, bool need_existing_window = false); +void ShowCompanyGroup(CompanyID company, VehicleType veh, GroupID group = INVALID_GROUP); void ShowCompanyGroupForVehicle(const Vehicle *v); void DeleteGroupHighlightOfVehicle(const Vehicle *v); diff --git a/src/newgrf_debug_gui.cpp b/src/newgrf_debug_gui.cpp index a126193fbc..800191434e 100644 --- a/src/newgrf_debug_gui.cpp +++ b/src/newgrf_debug_gui.cpp @@ -723,7 +723,7 @@ void ShowNewGRFInspectWindow(GrfSpecFeature feature, uint index, const uint32_t WindowNumber wno = GetInspectWindowNumber(feature, index); WindowDesc &desc = (feature == GSF_TRAINS || feature == GSF_ROADVEHICLES) ? _newgrf_inspect_chain_desc : _newgrf_inspect_desc; - NewGRFInspectWindow *w = AllocateWindowDescFront(desc, wno, true); + NewGRFInspectWindow *w = AllocateWindowDescFront(desc, wno); w->SetCallerGRFID(grfid); } diff --git a/src/story_gui.cpp b/src/story_gui.cpp index 224562d07e..af7b98bc07 100644 --- a/src/story_gui.cpp +++ b/src/story_gui.cpp @@ -1052,6 +1052,6 @@ void ShowStoryBook(CompanyID company, StoryPageID page_id, bool centered) { if (!Company::IsValidID(company)) company = (CompanyID)INVALID_COMPANY; - StoryBookWindow *w = AllocateWindowDescFront(centered ? _story_book_gs_desc : _story_book_desc, company, true); + StoryBookWindow *w = AllocateWindowDescFront(centered ? _story_book_gs_desc : _story_book_desc, company); if (page_id != INVALID_STORY_PAGE) w->SetSelectedPage(page_id); } diff --git a/src/window_gui.h b/src/window_gui.h index 5a47a21517..6e829652c3 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -1002,18 +1002,18 @@ Window *BringWindowToFrontById(WindowClass cls, ConvertibleThroughBase auto numb /** * Open a new window. - * @tparam Wcls %Window class to use if the window does not exist. + * @tparam Twindow %Window class to use if the window does not exist. + * @tparam Treturn_existing If set, also return the window if it already existed. * @param desc The pointer to the WindowDesc to be created * @param window_number the window number of the new window - * @param return_existing If set, also return the window if it already existed. - * @return %Window pointer of the newly created window, or the existing one if \a return_existing is set, or \c nullptr. + * @return %Window pointer of the newly created window, or the existing one if \a Treturn_existing is set, or \c nullptr. */ -template -Wcls *AllocateWindowDescFront(WindowDesc &desc, int window_number, bool return_existing = false) +template +Twindow *AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number) { - Wcls *w = static_cast(BringWindowToFrontById(desc.cls, window_number)); - if (w != nullptr) return return_existing ? w : nullptr; - return new Wcls(desc, window_number); + Twindow *w = static_cast(BringWindowToFrontById(desc.cls, window_number)); + if (w != nullptr) return Treturn_existing ? w : nullptr; + return new Twindow(desc, window_number); } void RelocateAllWindows(int neww, int newh);