(svn r27013) -Codechange: extract the functionality to determine the number of digits to make space for when drawing an unit number into a separate function

This commit is contained in:
rubidium 2014-10-14 11:23:41 +00:00
parent 65e3925afb
commit a72dd0480c
2 changed files with 26 additions and 15 deletions

View File

@ -98,6 +98,30 @@ const StringID BaseVehicleListWindow::vehicle_depot_name[] = {
STR_VEHICLE_LIST_SEND_AIRCRAFT_TO_HANGAR
};
/**
* Get the number of digits the biggest unit number of a set of vehicles has.
* @param vehicles The list of vehicles.
* @return The number of digits to allocate space for.
*/
uint GetUnitNumberDigits(VehicleList &vehicles)
{
uint unitnumber = 0;
for (const Vehicle **v = vehicles.Begin(); v != vehicles.End(); v++) {
unitnumber = max<uint>(unitnumber, (*v)->unitnumber);
}
if (unitnumber >= 10000) return 5;
if (unitnumber >= 1000) return 4;
if (unitnumber >= 100) return 3;
/*
* When the smallest unit number is less than 10, it is
* quite likely that it will expand to become more than
* 10 quite soon.
*/
return 2;
}
void BaseVehicleListWindow::BuildVehicleList()
{
if (!this->vehicles.NeedRebuild()) return;
@ -106,21 +130,7 @@ void BaseVehicleListWindow::BuildVehicleList()
GenerateVehicleSortList(&this->vehicles, this->vli);
uint unitnumber = 0;
for (const Vehicle **v = this->vehicles.Begin(); v != this->vehicles.End(); v++) {
unitnumber = max<uint>(unitnumber, (*v)->unitnumber);
}
/* Because 111 is much less wide than e.g. 999 we use the
* wider numbers to determine the width instead of just
* the random number that it seems to be. */
if (unitnumber >= 1000) {
this->unitnumber_digits = 4;
} else if (unitnumber >= 100) {
this->unitnumber_digits = 3;
} else {
this->unitnumber_digits = 2;
}
this->unitnumber_digits = GetUnitNumberDigits(this->vehicles);
this->vehicles.RebuildDone();
this->vscroll->SetCount(this->vehicles.Length());

View File

@ -55,5 +55,6 @@ typedef SmallVector<const Vehicle *, 32> VehicleList;
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &identifier);
void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engine_list, VehicleList *wagon_list, bool individual_wagons = false);
uint GetUnitNumberDigits(VehicleList &vehicles);
#endif /* VEHICLELIST_H */