1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-19 04:29:09 +00:00

Feature: Count the number of towns and cities in town directory

This commit is contained in:
Tyler Trahan
2023-08-06 14:18:19 -04:00
parent 9a602ff304
commit a36ae53583
5 changed files with 47 additions and 2 deletions

View File

@@ -3510,7 +3510,9 @@ STR_TOWN_DIRECTORY_NONE :{ORANGE}- None
STR_TOWN_DIRECTORY_TOWN :{ORANGE}{TOWN}{BLACK} ({COMMA})
STR_TOWN_DIRECTORY_CITY :{ORANGE}{TOWN}{YELLOW} (City){BLACK} ({COMMA})
STR_TOWN_DIRECTORY_LIST_TOOLTIP :{BLACK}Town names - click on name to centre main view on town. Ctrl+Click opens a new viewport on town location
STR_TOWN_POPULATION :{BLACK}World population: {COMMA}
STR_NUM_TOWNS :{BLACK}Towns: {ORANGE}{COMMA}
STR_NUM_CITIES :{BLACK}Cities: {ORANGE}{COMMA}
STR_TOWN_POPULATION :{BLACK}World population: {ORANGE}{COMMA}
# Town view window
STR_TOWN_VIEW_TOWN_CAPTION :{WHITE}{TOWN}

View File

@@ -143,6 +143,7 @@ private:
void FillCachedName() const;
};
uint CountTowns(bool cities);
uint32_t GetWorldPopulation();
void UpdateAllTownVirtCoords();

View File

@@ -456,6 +456,20 @@ uint32_t GetWorldPopulation()
return pop;
}
/**
* Get the number of towns or cities on the map.
* @param cities Count cities instead of towns.
* @return The number of towns or cities.
*/
uint CountTowns(bool cities)
{
uint total = 0;
for (const Town *t : Town::Iterate()) {
if (cities == t->larger_town) total++;
}
return total;
}
/**
* Remove stations from nearby station list if a town is no longer in the catchment area of each.
* To improve performance only checks stations that cover the provided house area (doesn't need to contain an actual house).

View File

@@ -692,7 +692,9 @@ static const NWidgetPart _nested_town_directory_widgets[] = {
NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
SetFill(1, 0), SetResize(1, 1), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
NWidget(WWT_PANEL, COLOUR_BROWN),
NWidget(WWT_TEXT, COLOUR_BROWN, WID_TD_WORLD_POPULATION), SetPadding(2, 0, 2, 2), SetMinimalTextLines(1, 0), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TOWN_POPULATION, STR_NULL),
NWidget(WWT_TEXT, COLOUR_BROWN, WID_TD_NUM_TOWNS), SetPadding(2, 0, 0, 2), SetMinimalTextLines(1, 0), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NUM_TOWNS, STR_NULL),
NWidget(WWT_TEXT, COLOUR_BROWN, WID_TD_NUM_CITIES), SetPadding(0, 0, 0, 2), SetMinimalTextLines(1, 0), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_NUM_CITIES, STR_NULL),
NWidget(WWT_TEXT, COLOUR_BROWN, WID_TD_WORLD_POPULATION), SetPadding(0, 0, 2, 2), SetMinimalTextLines(1, 0), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TOWN_POPULATION, STR_NULL),
EndContainer(),
EndContainer(),
NWidget(NWID_VERTICAL),
@@ -801,6 +803,14 @@ public:
void SetStringParameters(int widget) const override
{
switch (widget) {
case WID_TD_NUM_TOWNS:
SetDParam(0, CountTowns(false));
break;
case WID_TD_NUM_CITIES:
SetDParam(0, CountTowns(true));
break;
case WID_TD_WORLD_POPULATION:
SetDParam(0, GetWorldPopulation());
break;
@@ -909,6 +919,22 @@ public:
*size = maxdim(*size, d);
break;
}
case WID_TD_NUM_TOWNS: {
SetDParamMaxDigits(0, 5);
Dimension d = GetStringBoundingBox(STR_NUM_TOWNS);
d.width += padding.width;
d.height += padding.height;
*size = maxdim(*size, d);
break;
}
case WID_TD_NUM_CITIES: {
SetDParamMaxDigits(0, 5);
Dimension d = GetStringBoundingBox(STR_NUM_CITIES);
d.width += padding.width;
d.height += padding.height;
*size = maxdim(*size, d);
break;
}
case WID_TD_WORLD_POPULATION: {
SetDParamMaxDigits(0, 10);
Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);

View File

@@ -17,6 +17,8 @@ enum TownDirectoryWidgets {
WID_TD_FILTER, ///< Filter of name.
WID_TD_LIST, ///< List of towns.
WID_TD_SCROLLBAR, ///< Scrollbar for the town list.
WID_TD_NUM_TOWNS, ///< Total number of towns.
WID_TD_NUM_CITIES, ///< Total number of cities.
WID_TD_WORLD_POPULATION, ///< The world's population.
};