1
0
Fork 0

Codechange: move large switch case to separate function (#12863)

pull/12865/head
Joan Josep 2024-07-16 21:41:04 +02:00 committed by GitHub
parent d67963e616
commit 891e53c72e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 31 additions and 27 deletions

View File

@ -1606,6 +1606,35 @@ struct BuildVehicleWindow : Window {
return list;
}
void BuildVehicle()
{
EngineID sel_eng = this->sel_engine;
if (sel_eng == INVALID_ENGINE) return;
CargoID cargo = this->cargo_filter_criteria;
if (cargo == CargoFilterCriteria::CF_ANY || cargo == CargoFilterCriteria::CF_ENGINES || cargo == CargoFilterCriteria::CF_NONE) cargo = INVALID_CARGO;
if (this->vehicle_type == VEH_TRAIN && RailVehInfo(sel_eng)->railveh_type == RAILVEH_WAGON) {
Command<CMD_BUILD_VEHICLE>::Post(GetCmdBuildVehMsg(this->vehicle_type), CcBuildWagon, this->window_number, sel_eng, true, cargo, INVALID_CLIENT_ID);
} else {
Command<CMD_BUILD_VEHICLE>::Post(GetCmdBuildVehMsg(this->vehicle_type), CcBuildPrimaryVehicle, this->window_number, sel_eng, true, cargo, INVALID_CLIENT_ID);
}
/* Update last used variant in hierarchy and refresh if necessary. */
bool refresh = false;
EngineID parent = sel_eng;
while (parent != INVALID_ENGINE) {
Engine *e = Engine::Get(parent);
refresh |= (e->display_last_variant != sel_eng);
e->display_last_variant = sel_eng;
parent = e->info.variant_id;
}
if (refresh) {
InvalidateWindowData(WC_REPLACE_VEHICLE, this->vehicle_type, 0); // Update the autoreplace window
InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
}
}
void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
{
switch (widget) {
@ -1668,34 +1697,9 @@ struct BuildVehicleWindow : Window {
break;
}
case WID_BV_BUILD: {
EngineID sel_eng = this->sel_engine;
if (sel_eng != INVALID_ENGINE) {
CargoID cargo = this->cargo_filter_criteria;
if (cargo == CargoFilterCriteria::CF_ANY || cargo == CargoFilterCriteria::CF_ENGINES || cargo == CargoFilterCriteria::CF_NONE) cargo = INVALID_CARGO;
if (this->vehicle_type == VEH_TRAIN && RailVehInfo(sel_eng)->railveh_type == RAILVEH_WAGON) {
Command<CMD_BUILD_VEHICLE>::Post(GetCmdBuildVehMsg(this->vehicle_type), CcBuildWagon, this->window_number, sel_eng, true, cargo, INVALID_CLIENT_ID);
} else {
Command<CMD_BUILD_VEHICLE>::Post(GetCmdBuildVehMsg(this->vehicle_type), CcBuildPrimaryVehicle, this->window_number, sel_eng, true, cargo, INVALID_CLIENT_ID);
}
/* Update last used variant in hierarchy and refresh if necessary. */
bool refresh = false;
EngineID parent = sel_eng;
while (parent != INVALID_ENGINE) {
Engine *e = Engine::Get(parent);
refresh |= (e->display_last_variant != sel_eng);
e->display_last_variant = sel_eng;
parent = e->info.variant_id;
}
if (refresh) {
InvalidateWindowData(WC_REPLACE_VEHICLE, this->vehicle_type, 0); // Update the autoreplace window
InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
return;
}
}
case WID_BV_BUILD:
this->BuildVehicle();
break;
}
case WID_BV_RENAME: {
EngineID sel_eng = this->sel_engine;