diff options
author | rubidium <rubidium@openttd.org> | 2014-10-14 11:23:41 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2014-10-14 11:23:41 +0000 |
commit | a72dd0480c14985d10daaa323dfd1937748f54a4 (patch) | |
tree | 51fd047940a65c217abdfae44b2496aaf80e019f | |
parent | 65e3925afb2425b7baa2b1bf435eaf2e114cd85d (diff) | |
download | openttd-a72dd0480c14985d10daaa323dfd1937748f54a4.tar.xz |
(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
-rw-r--r-- | src/vehicle_gui.cpp | 40 | ||||
-rw-r--r-- | src/vehiclelist.h | 1 |
2 files changed, 26 insertions, 15 deletions
diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 10f2cfe39..3d6ad5188 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -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()); diff --git a/src/vehiclelist.h b/src/vehiclelist.h index 03bfb0774..c96fb692a 100644 --- a/src/vehiclelist.h +++ b/src/vehiclelist.h @@ -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 */ |