1
0
Fork 0

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.
pull/13445/head
Rubidium 2025-02-01 20:54:10 +01:00 committed by rubidium42
parent 9238cb7270
commit 8f72a478f0
7 changed files with 29 additions and 17 deletions

View File

@ -1030,7 +1030,7 @@ void ShowFramerateWindow()
void ShowFrametimeGraphWindow(PerformanceElement elem)
{
if (elem < PFE_FIRST || elem >= PFE_MAX) return; // maybe warn?
AllocateWindowDescFront<FrametimeGraphWindow>(_frametime_graph_window_desc, elem, true);
AllocateWindowDescFront<FrametimeGraphWindow>(_frametime_graph_window_desc, elem);
}
/** Print performance statistics to game console */

View File

@ -1030,7 +1030,7 @@ static void _ShowGenerateLandscape(GenerateLandscapeWindowMode mode)
}
WindowDesc &desc = (mode == GLWM_HEIGHTMAP) ? _heightmap_load_desc : _generate_landscape_desc;
GenerateLandscapeWindow *w = AllocateWindowDescFront<GenerateLandscapeWindow>(desc, mode, true);
GenerateLandscapeWindow *w = AllocateWindowDescFront<GenerateLandscapeWindow, true>(desc, mode);
if (mode == GLWM_HEIGHTMAP) {
w->x = x;

View File

@ -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 <bool Tneed_existing_window>
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<VehicleGroupWindow>(_vehicle_group_desc[vehicle_type], num, need_existing_window);
VehicleGroupWindow *w = AllocateWindowDescFront<VehicleGroupWindow, Tneed_existing_window>(_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<false>(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<true>(v->owner, v->type, v->group_id);
}
/**

View File

@ -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);

View File

@ -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<NewGRFInspectWindow>(desc, wno, true);
NewGRFInspectWindow *w = AllocateWindowDescFront<NewGRFInspectWindow, true>(desc, wno);
w->SetCallerGRFID(grfid);
}

View File

@ -1052,6 +1052,6 @@ void ShowStoryBook(CompanyID company, StoryPageID page_id, bool centered)
{
if (!Company::IsValidID(company)) company = (CompanyID)INVALID_COMPANY;
StoryBookWindow *w = AllocateWindowDescFront<StoryBookWindow>(centered ? _story_book_gs_desc : _story_book_desc, company, true);
StoryBookWindow *w = AllocateWindowDescFront<StoryBookWindow, true>(centered ? _story_book_gs_desc : _story_book_desc, company);
if (page_id != INVALID_STORY_PAGE) w->SetSelectedPage(page_id);
}

View File

@ -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 <typename Wcls>
Wcls *AllocateWindowDescFront(WindowDesc &desc, int window_number, bool return_existing = false)
template <typename Twindow, bool Treturn_existing = false>
Twindow *AllocateWindowDescFront(WindowDesc &desc, WindowNumber window_number)
{
Wcls *w = static_cast<Wcls *>(BringWindowToFrontById(desc.cls, window_number));
if (w != nullptr) return return_existing ? w : nullptr;
return new Wcls(desc, window_number);
Twindow *w = static_cast<Twindow *>(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);