Change: Remove scrollbar from town authority actions panel (#9928)
[openttd-github.git] / src / town_gui.cpp
blob95a08a8e13f31e128009d8d482d69d83b71b2da4
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file town_gui.cpp GUI for towns. */
10 #include "stdafx.h"
11 #include "town.h"
12 #include "viewport_func.h"
13 #include "error.h"
14 #include "gui.h"
15 #include "command_func.h"
16 #include "company_func.h"
17 #include "company_base.h"
18 #include "company_gui.h"
19 #include "network/network.h"
20 #include "string_func.h"
21 #include "strings_func.h"
22 #include "sound_func.h"
23 #include "tilehighlight_func.h"
24 #include "sortlist_type.h"
25 #include "road_cmd.h"
26 #include "landscape.h"
27 #include "querystring_gui.h"
28 #include "window_func.h"
29 #include "townname_func.h"
30 #include "core/backup_type.hpp"
31 #include "core/geometry_func.hpp"
32 #include "genworld.h"
33 #include "stringfilter_type.h"
34 #include "widgets/dropdown_func.h"
35 #include "town_kdtree.h"
36 #include "town_cmd.h"
38 #include "widgets/town_widget.h"
40 #include "table/strings.h"
42 #include "safeguards.h"
43 #include "zoom_func.h"
45 TownKdtree _town_local_authority_kdtree(&Kdtree_TownXYFunc);
47 typedef GUIList<const Town*> GUITownList;
49 static const NWidgetPart _nested_town_authority_widgets[] = {
50 NWidget(NWID_HORIZONTAL),
51 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
52 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
53 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TA_ZONE_BUTTON), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_LOCAL_AUTHORITY_ZONE, STR_LOCAL_AUTHORITY_ZONE_TOOLTIP),
54 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
55 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
56 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
57 EndContainer(),
58 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
59 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_COMMAND_LIST), SetMinimalSize(317, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP), EndContainer(),
60 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
61 NWidget(NWID_HORIZONTAL),
62 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TA_EXECUTE), SetMinimalSize(317, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_LOCAL_AUTHORITY_DO_IT_BUTTON, STR_LOCAL_AUTHORITY_DO_IT_TOOLTIP),
63 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
64 EndContainer()
67 /** Town authority window. */
68 struct TownAuthorityWindow : Window {
69 private:
70 Town *town; ///< Town being displayed.
71 int sel_index; ///< Currently selected town action, \c 0 to \c TACT_COUNT-1, \c -1 means no action selected.
72 uint displayed_actions_on_previous_painting; ///< Actions that were available on the previous call to OnPaint()
73 TownActions enabled_actions; ///< Actions that are enabled in settings.
74 TownActions available_actions; ///< Actions that are available to execute for the current company.
76 /**
77 * Get the position of the Nth set bit.
79 * If there is no Nth bit set return -1
81 * @param n The Nth set bit from which we want to know the position
82 * @return The position of the Nth set bit, or -1 if no Nth bit set.
84 int GetNthSetBit(int n)
86 if (n >= 0) {
87 for (uint i : SetBitIterator(this->enabled_actions)) {
88 n--;
89 if (n < 0) return i;
92 return -1;
95 /**
96 * Gets all town authority actions enabled in settings.
98 * @return Bitmask of actions enabled in the settings.
100 static TownActions GetEnabledActions()
102 TownActions enabled = TACT_ALL;
104 if (!_settings_game.economy.fund_roads) CLRBITS(enabled, TACT_ROAD_REBUILD);
105 if (!_settings_game.economy.fund_buildings) CLRBITS(enabled, TACT_FUND_BUILDINGS);
106 if (!_settings_game.economy.exclusive_rights) CLRBITS(enabled, TACT_BUY_RIGHTS);
107 if (!_settings_game.economy.bribe) CLRBITS(enabled, TACT_BRIBE);
109 return enabled;
112 public:
113 TownAuthorityWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc), sel_index(-1), displayed_actions_on_previous_painting(0), available_actions(TACT_NONE)
115 this->town = Town::Get(window_number);
116 this->enabled_actions = GetEnabledActions();
117 this->InitNested(window_number);
120 void OnPaint() override
122 this->available_actions = GetMaskOfTownActions(_local_company, this->town);
123 if (this->available_actions != displayed_actions_on_previous_painting) this->SetDirty();
124 displayed_actions_on_previous_painting = this->available_actions;
126 this->SetWidgetLoweredState(WID_TA_ZONE_BUTTON, this->town->show_zone);
127 this->SetWidgetDisabledState(WID_TA_EXECUTE, (this->sel_index == -1) || !HasBit(this->available_actions, this->sel_index));
129 this->DrawWidgets();
130 if (!this->IsShaded())
132 this->DrawRatings();
133 this->DrawActions();
137 /** Draw the contents of the ratings panel. May request a resize of the window if the contents does not fit. */
138 void DrawRatings()
140 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_RATING_INFO);
141 uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
142 uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
144 uint y = nwid->pos_y + WD_FRAMERECT_TOP;
146 DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
147 y += FONT_HEIGHT_NORMAL;
149 Dimension icon_size = GetSpriteSize(SPR_COMPANY_ICON);
150 int icon_width = icon_size.width;
151 int icon_y_offset = (FONT_HEIGHT_NORMAL - icon_size.height) / 2;
153 Dimension exclusive_size = GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT);
154 int exclusive_width = exclusive_size.width;
155 int exclusive_y_offset = (FONT_HEIGHT_NORMAL - exclusive_size.height) / 2;
157 bool rtl = _current_text_dir == TD_RTL;
158 uint text_left = left + (rtl ? 0 : icon_width + exclusive_width + 4);
159 uint text_right = right - (rtl ? icon_width + exclusive_width + 4 : 0);
160 uint icon_left = rtl ? right - icon_width : left;
161 uint exclusive_left = rtl ? right - icon_width - exclusive_width - 2 : left + icon_width + 2;
163 /* Draw list of companies */
164 for (const Company *c : Company::Iterate()) {
165 if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
166 DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
168 SetDParam(0, c->index);
169 SetDParam(1, c->index);
171 int r = this->town->ratings[c->index];
172 StringID str = STR_CARGO_RATING_APPALLING;
173 if (r > RATING_APPALLING) str++;
174 if (r > RATING_VERYPOOR) str++;
175 if (r > RATING_POOR) str++;
176 if (r > RATING_MEDIOCRE) str++;
177 if (r > RATING_GOOD) str++;
178 if (r > RATING_VERYGOOD) str++;
179 if (r > RATING_EXCELLENT) str++;
181 SetDParam(2, str);
182 if (this->town->exclusivity == c->index) {
183 DrawSprite(SPR_EXCLUSIVE_TRANSPORT, COMPANY_SPRITE_COLOUR(c->index), exclusive_left, y + exclusive_y_offset);
186 DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
187 y += FONT_HEIGHT_NORMAL;
191 y = y + WD_FRAMERECT_BOTTOM - nwid->pos_y; // Compute needed size of the widget.
192 if (y > nwid->current_y) {
193 /* If the company list is too big to fit, mark ourself dirty and draw again. */
194 ResizeWindow(this, 0, y - nwid->current_y, false);
198 /** Draws the contents of the actions panel. May re-initialise window to resize panel, if the list does not fit. */
199 void DrawActions()
201 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_COMMAND_LIST);
202 uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
203 uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
204 uint y = nwid->pos_y + WD_FRAMERECT_TOP;
206 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
207 y += FONT_HEIGHT_NORMAL;
209 /* Draw list of actions */
210 for (int i = 0; i < TACT_COUNT; i++) {
211 /* Don't show actions if disabled in settings. */
212 if (!HasBit(this->enabled_actions, i)) continue;
214 /* Set colour of action based on ability to execute and if selected. */
215 TextColour action_colour = TC_GREY | TC_NO_SHADE;
216 if (HasBit(this->available_actions, i)) action_colour = TC_ORANGE;
217 if (this->sel_index == i) action_colour = TC_WHITE;
219 DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, action_colour);
220 y += FONT_HEIGHT_NORMAL;
224 void SetStringParameters(int widget) const override
226 if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
229 void DrawWidget(const Rect &r, int widget) const override
231 switch (widget) {
232 case WID_TA_ACTION_INFO:
233 if (this->sel_index != -1) {
234 Money action_cost = _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8;
235 bool affordable = action_cost < Company::GetIfValid(_local_company)->money;
237 SetDParam(0, action_cost);
238 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
239 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index,
240 affordable ? TC_YELLOW : TC_RED);
242 break;
246 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
248 switch (widget) {
249 case WID_TA_ACTION_INFO: {
250 assert(size->width > padding.width && size->height > padding.height);
251 size->width -= WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
252 size->height -= WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
253 Dimension d = {0, 0};
254 for (int i = 0; i < TACT_COUNT; i++) {
255 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
256 d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
258 *size = maxdim(*size, d);
259 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
260 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
261 break;
264 case WID_TA_COMMAND_LIST:
265 size->height = WD_FRAMERECT_TOP + (TACT_COUNT + 1) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
266 size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
267 for (uint i = 0; i < TACT_COUNT; i++ ) {
268 size->width = std::max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
270 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
271 break;
273 case WID_TA_RATING_INFO:
274 resize->height = FONT_HEIGHT_NORMAL;
275 size->height = WD_FRAMERECT_TOP + 9 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
276 break;
280 void OnClick(Point pt, int widget, int click_count) override
282 switch (widget) {
283 case WID_TA_ZONE_BUTTON: {
284 bool new_show_state = !this->town->show_zone;
285 TownID index = this->town->index;
287 new_show_state ? _town_local_authority_kdtree.Insert(index) : _town_local_authority_kdtree.Remove(index);
289 this->town->show_zone = new_show_state;
290 this->SetWidgetLoweredState(widget, new_show_state);
291 MarkWholeScreenDirty();
292 break;
295 case WID_TA_COMMAND_LIST: {
296 int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL) - 1;
298 y = GetNthSetBit(y);
299 if (y >= 0) {
300 this->sel_index = y;
301 this->SetDirty();
304 /* When double-clicking, continue */
305 if (click_count == 1 || y < 0) break;
306 FALLTHROUGH;
309 case WID_TA_EXECUTE:
310 Command<CMD_DO_TOWN_ACTION>::Post(STR_ERROR_CAN_T_DO_THIS, this->town->xy, this->window_number, this->sel_index);
311 break;
315 void OnHundredthTick() override
317 this->SetDirty();
320 void OnInvalidateData(int data = 0, bool gui_scope = true) override
322 if (!gui_scope) return;
324 this->enabled_actions = this->GetEnabledActions();
325 if (!HasBit(this->enabled_actions, this->sel_index)) {
326 this->sel_index = -1;
331 static WindowDesc _town_authority_desc(
332 WDP_AUTO, "view_town_authority", 317, 222,
333 WC_TOWN_AUTHORITY, WC_NONE,
335 _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
338 static void ShowTownAuthorityWindow(uint town)
340 AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
344 /* Town view window. */
345 struct TownViewWindow : Window {
346 private:
347 Town *town; ///< Town displayed by the window.
349 public:
350 static const int WID_TV_HEIGHT_NORMAL = 150;
352 TownViewWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
354 this->CreateNestedTree();
356 this->town = Town::Get(window_number);
357 if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
359 this->FinishInitNested(window_number);
361 this->flags |= WF_DISABLE_VP_SCROLL;
362 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
363 nvp->InitializeViewport(this, this->town->xy, ScaleZoomGUI(ZOOM_LVL_TOWN));
365 /* disable renaming town in network games if you are not the server */
366 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME, _networking && !_network_server);
369 void Close() override
371 SetViewportCatchmentTown(Town::Get(this->window_number), false);
372 this->Window::Close();
375 void SetStringParameters(int widget) const override
377 if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
380 void OnPaint() override
382 extern const Town *_viewport_highlight_town;
383 this->SetWidgetLoweredState(WID_TV_CATCHMENT, _viewport_highlight_town == this->town);
385 this->DrawWidgets();
388 void DrawWidget(const Rect &r, int widget) const override
390 if (widget != WID_TV_INFO) return;
392 uint y = r.top + WD_FRAMERECT_TOP;
394 SetDParam(0, this->town->cache.population);
395 SetDParam(1, this->town->cache.num_houses);
396 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
398 SetDParam(0, 1 << CT_PASSENGERS);
399 SetDParam(1, this->town->supplied[CT_PASSENGERS].old_act);
400 SetDParam(2, this->town->supplied[CT_PASSENGERS].old_max);
401 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
403 SetDParam(0, 1 << CT_MAIL);
404 SetDParam(1, this->town->supplied[CT_MAIL].old_act);
405 SetDParam(2, this->town->supplied[CT_MAIL].old_max);
406 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
408 bool first = true;
409 for (int i = TE_BEGIN; i < TE_END; i++) {
410 if (this->town->goal[i] == 0) continue;
411 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
412 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
414 if (first) {
415 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
416 first = false;
419 bool rtl = _current_text_dir == TD_RTL;
420 uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
421 uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
423 const CargoSpec *cargo = FindFirstCargoWithTownEffect((TownEffect)i);
424 assert(cargo != nullptr);
426 StringID string;
428 if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
429 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
430 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
431 if (this->town->received[i].old_act == 0) {
432 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
434 if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
435 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
439 SetDParam(0, cargo->name);
440 } else {
441 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
442 if (this->town->received[i].old_act < this->town->goal[i]) {
443 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
446 SetDParam(0, cargo->Index());
447 SetDParam(1, this->town->received[i].old_act);
448 SetDParam(2, cargo->Index());
449 SetDParam(3, this->town->goal[i]);
451 DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
454 if (HasBit(this->town->flags, TOWN_IS_GROWING)) {
455 SetDParam(0, RoundDivSU(this->town->growth_rate + 1, DAY_TICKS));
456 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, this->town->fund_buildings_months == 0 ? STR_TOWN_VIEW_TOWN_GROWS_EVERY : STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED);
457 } else {
458 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
461 /* only show the town noise, if the noise option is activated. */
462 if (_settings_game.economy.station_noise_level) {
463 SetDParam(0, this->town->noise_reached);
464 SetDParam(1, this->town->MaxTownNoise());
465 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
468 if (!this->town->text.empty()) {
469 SetDParamStr(0, this->town->text);
470 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
474 void OnClick(Point pt, int widget, int click_count) override
476 switch (widget) {
477 case WID_TV_CENTER_VIEW: // scroll to location
478 if (_ctrl_pressed) {
479 ShowExtraViewportWindow(this->town->xy);
480 } else {
481 ScrollMainWindowToTile(this->town->xy);
483 break;
485 case WID_TV_SHOW_AUTHORITY: // town authority
486 ShowTownAuthorityWindow(this->window_number);
487 break;
489 case WID_TV_CHANGE_NAME: // rename
490 SetDParam(0, this->window_number);
491 ShowQueryString(STR_TOWN_NAME, STR_TOWN_VIEW_RENAME_TOWN_BUTTON, MAX_LENGTH_TOWN_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
492 break;
494 case WID_TV_CATCHMENT:
495 SetViewportCatchmentTown(Town::Get(this->window_number), !this->IsWidgetLowered(WID_TV_CATCHMENT));
496 break;
498 case WID_TV_EXPAND: { // expand town - only available on Scenario editor
499 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
500 static bool _warn_town_no_roads = false;
502 if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
503 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
504 _warn_town_no_roads = true;
507 Command<CMD_EXPAND_TOWN>::Post(STR_ERROR_CAN_T_EXPAND_TOWN, this->window_number, 0);
508 break;
511 case WID_TV_DELETE: // delete town - only available on Scenario editor
512 Command<CMD_DELETE_TOWN>::Post(STR_ERROR_TOWN_CAN_T_DELETE, this->window_number);
513 break;
517 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
519 switch (widget) {
520 case WID_TV_INFO:
521 size->height = GetDesiredInfoHeight(size->width);
522 break;
527 * Gets the desired height for the information panel.
528 * @return the desired height in pixels.
530 uint GetDesiredInfoHeight(int width) const
532 uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
534 bool first = true;
535 for (int i = TE_BEGIN; i < TE_END; i++) {
536 if (this->town->goal[i] == 0) continue;
537 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
538 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
540 if (first) {
541 aimed_height += FONT_HEIGHT_NORMAL;
542 first = false;
544 aimed_height += FONT_HEIGHT_NORMAL;
546 aimed_height += FONT_HEIGHT_NORMAL;
548 if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
550 if (!this->town->text.empty()) {
551 SetDParamStr(0, this->town->text);
552 aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT);
555 return aimed_height;
558 void ResizeWindowAsNeeded()
560 const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
561 uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
562 if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
563 this->ReInit();
567 void OnResize() override
569 if (this->viewport != nullptr) {
570 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
571 nvp->UpdateViewportCoordinates(this);
573 ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
578 * Some data on this window has become invalid.
579 * @param data Information about the changed data.
580 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
582 void OnInvalidateData(int data = 0, bool gui_scope = true) override
584 if (!gui_scope) return;
585 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
586 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
587 this->ResizeWindowAsNeeded();
590 void OnQueryTextFinished(char *str) override
592 if (str == nullptr) return;
594 Command<CMD_RENAME_TOWN>::Post(STR_ERROR_CAN_T_RENAME_TOWN, this->window_number, str);
598 static const NWidgetPart _nested_town_game_view_widgets[] = {
599 NWidget(NWID_HORIZONTAL),
600 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
601 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
602 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
603 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
604 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
605 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
606 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
607 EndContainer(),
608 NWidget(WWT_PANEL, COLOUR_BROWN),
609 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
610 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1),
611 EndContainer(),
612 EndContainer(),
613 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
614 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
615 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_SHOW_AUTHORITY), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP),
616 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
617 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
618 EndContainer(),
621 static WindowDesc _town_game_view_desc(
622 WDP_AUTO, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
623 WC_TOWN_VIEW, WC_NONE,
625 _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
628 static const NWidgetPart _nested_town_editor_view_widgets[] = {
629 NWidget(NWID_HORIZONTAL),
630 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
631 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
632 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
633 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
634 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
635 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
636 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
637 EndContainer(),
638 NWidget(WWT_PANEL, COLOUR_BROWN),
639 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
640 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1),
641 EndContainer(),
642 EndContainer(),
643 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
644 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
645 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_EXPAND), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_EXPAND_BUTTON, STR_TOWN_VIEW_EXPAND_TOOLTIP),
646 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_DELETE), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_TOWN_VIEW_DELETE_TOOLTIP),
647 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
648 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
649 EndContainer(),
652 static WindowDesc _town_editor_view_desc(
653 WDP_AUTO, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
654 WC_TOWN_VIEW, WC_NONE,
656 _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
659 void ShowTownViewWindow(TownID town)
661 if (_game_mode == GM_EDITOR) {
662 AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
663 } else {
664 AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
668 static const NWidgetPart _nested_town_directory_widgets[] = {
669 NWidget(NWID_HORIZONTAL),
670 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
671 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
672 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
673 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
674 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
675 EndContainer(),
676 NWidget(NWID_HORIZONTAL),
677 NWidget(NWID_VERTICAL),
678 NWidget(NWID_HORIZONTAL),
679 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
680 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
681 NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_TD_FILTER), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
682 EndContainer(),
683 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
684 SetFill(1, 0), SetResize(1, 1), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
685 NWidget(WWT_PANEL, COLOUR_BROWN),
686 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),
687 EndContainer(),
688 EndContainer(),
689 NWidget(NWID_VERTICAL),
690 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
691 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
692 EndContainer(),
693 EndContainer(),
696 /** Town directory window class. */
697 struct TownDirectoryWindow : public Window {
698 private:
699 /* Runtime saved values */
700 static Listing last_sorting;
702 /* Constants for sorting towns */
703 static const StringID sorter_names[];
704 static GUITownList::SortFunction * const sorter_funcs[];
706 StringFilter string_filter; ///< Filter for towns
707 QueryString townname_editbox; ///< Filter editbox
709 GUITownList towns;
711 Scrollbar *vscroll;
713 void BuildSortTownList()
715 if (this->towns.NeedRebuild()) {
716 this->towns.clear();
718 for (const Town *t : Town::Iterate()) {
719 if (this->string_filter.IsEmpty()) {
720 this->towns.push_back(t);
721 continue;
723 this->string_filter.ResetState();
724 this->string_filter.AddLine(t->GetCachedName());
725 if (this->string_filter.GetState()) this->towns.push_back(t);
728 this->towns.shrink_to_fit();
729 this->towns.RebuildDone();
730 this->vscroll->SetCount((uint)this->towns.size()); // Update scrollbar as well.
732 /* Always sort the towns. */
733 this->towns.Sort();
734 this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns.
737 /** Sort by town name */
738 static bool TownNameSorter(const Town * const &a, const Town * const &b)
740 return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting).
743 /** Sort by population (default descending, as big towns are of the most interest). */
744 static bool TownPopulationSorter(const Town * const &a, const Town * const &b)
746 uint32 a_population = a->cache.population;
747 uint32 b_population = b->cache.population;
748 if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
749 return a_population < b_population;
752 /** Sort by town rating */
753 static bool TownRatingSorter(const Town * const &a, const Town * const &b)
755 bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'.
757 /* Towns without rating are always after towns with rating. */
758 if (HasBit(a->have_ratings, _local_company)) {
759 if (HasBit(b->have_ratings, _local_company)) {
760 int16 a_rating = a->ratings[_local_company];
761 int16 b_rating = b->ratings[_local_company];
762 if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
763 return a_rating < b_rating;
765 return before;
767 if (HasBit(b->have_ratings, _local_company)) return !before;
769 /* Sort unrated towns always on ascending town name. */
770 if (before) return TownDirectoryWindow::TownNameSorter(a, b);
771 return !TownDirectoryWindow::TownNameSorter(a, b);
774 public:
775 TownDirectoryWindow(WindowDesc *desc) : Window(desc), townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS)
777 this->CreateNestedTree();
779 this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
781 this->towns.SetListing(this->last_sorting);
782 this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
783 this->towns.ForceRebuild();
784 this->BuildSortTownList();
786 this->FinishInitNested(0);
788 this->querystrings[WID_TD_FILTER] = &this->townname_editbox;
789 this->townname_editbox.cancel_button = QueryString::ACTION_CLEAR;
792 void SetStringParameters(int widget) const override
794 switch (widget) {
795 case WID_TD_WORLD_POPULATION:
796 SetDParam(0, GetWorldPopulation());
797 break;
799 case WID_TD_SORT_CRITERIA:
800 SetDParam(0, TownDirectoryWindow::sorter_names[this->towns.SortType()]);
801 break;
806 * Get the string to draw the town name.
807 * @param t Town to draw.
808 * @return The string to use.
810 static StringID GetTownString(const Town *t)
812 return t->larger_town ? STR_TOWN_DIRECTORY_CITY : STR_TOWN_DIRECTORY_TOWN;
815 void DrawWidget(const Rect &r, int widget) const override
817 switch (widget) {
818 case WID_TD_SORT_ORDER:
819 this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
820 break;
822 case WID_TD_LIST: {
823 int n = 0;
824 int y = r.top + WD_FRAMERECT_TOP;
825 if (this->towns.size() == 0) { // No towns available.
826 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
827 break;
830 /* At least one town available. */
831 bool rtl = _current_text_dir == TD_RTL;
832 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
833 int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
834 int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
835 int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
837 for (uint i = this->vscroll->GetPosition(); i < this->towns.size(); i++) {
838 const Town *t = this->towns[i];
839 assert(t->xy != INVALID_TILE);
841 /* Draw rating icon. */
842 if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
843 DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
844 } else {
845 SpriteID icon = SPR_TOWN_RATING_APALLING;
846 if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
847 if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
848 DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
851 SetDParam(0, t->index);
852 SetDParam(1, t->cache.population);
853 DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, GetTownString(t));
855 y += this->resize.step_height;
856 if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
858 break;
863 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
865 switch (widget) {
866 case WID_TD_SORT_ORDER: {
867 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
868 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
869 d.height += padding.height;
870 *size = maxdim(*size, d);
871 break;
873 case WID_TD_SORT_CRITERIA: {
874 Dimension d = {0, 0};
875 for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
876 d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i]));
878 d.width += padding.width;
879 d.height += padding.height;
880 *size = maxdim(*size, d);
881 break;
883 case WID_TD_LIST: {
884 Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
885 for (uint i = 0; i < this->towns.size(); i++) {
886 const Town *t = this->towns[i];
888 assert(t != nullptr);
890 SetDParam(0, t->index);
891 SetDParamMaxDigits(1, 8);
892 d = maxdim(d, GetStringBoundingBox(GetTownString(t)));
894 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
895 d.width += icon_size.width + 2;
896 d.height = std::max(d.height, icon_size.height);
897 resize->height = d.height;
898 d.height *= 5;
899 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
900 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
901 *size = maxdim(*size, d);
902 break;
904 case WID_TD_WORLD_POPULATION: {
905 SetDParamMaxDigits(0, 10);
906 Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
907 d.width += padding.width;
908 d.height += padding.height;
909 *size = maxdim(*size, d);
910 break;
915 void OnClick(Point pt, int widget, int click_count) override
917 switch (widget) {
918 case WID_TD_SORT_ORDER: // Click on sort order button
919 if (this->towns.SortType() != 2) { // A different sort than by rating.
920 this->towns.ToggleSortOrder();
921 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
922 } else {
923 /* Some parts are always sorted ascending on name. */
924 this->last_sorting.order = !this->last_sorting.order;
925 this->towns.SetListing(this->last_sorting);
926 this->towns.ForceResort();
927 this->towns.Sort();
929 this->SetDirty();
930 break;
932 case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown
933 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names, this->towns.SortType(), WID_TD_SORT_CRITERIA, 0, 0);
934 break;
936 case WID_TD_LIST: { // Click on Town Matrix
937 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
938 if (id_v >= this->towns.size()) return; // click out of town bounds
940 const Town *t = this->towns[id_v];
941 assert(t != nullptr);
942 if (_ctrl_pressed) {
943 ShowExtraViewportWindow(t->xy);
944 } else {
945 ScrollMainWindowToTile(t->xy);
947 break;
952 void OnDropdownSelect(int widget, int index) override
954 if (widget != WID_TD_SORT_CRITERIA) return;
956 if (this->towns.SortType() != index) {
957 this->towns.SetSortType(index);
958 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
959 this->BuildSortTownList();
963 void OnPaint() override
965 if (this->towns.NeedRebuild()) this->BuildSortTownList();
966 this->DrawWidgets();
969 void OnHundredthTick() override
971 this->BuildSortTownList();
972 this->SetDirty();
975 void OnResize() override
977 this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
980 void OnEditboxChanged(int wid) override
982 if (wid == WID_TD_FILTER) {
983 this->string_filter.SetFilterTerm(this->townname_editbox.text.buf);
984 this->InvalidateData(TDIWD_FORCE_REBUILD);
989 * Some data on this window has become invalid.
990 * @param data Information about the changed data.
991 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
993 void OnInvalidateData(int data = 0, bool gui_scope = true) override
995 switch (data) {
996 case TDIWD_FORCE_REBUILD:
997 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
998 this->towns.ForceRebuild();
999 break;
1001 case TDIWD_POPULATION_CHANGE:
1002 if (this->towns.SortType() == 1) this->towns.ForceResort();
1003 break;
1005 default:
1006 this->towns.ForceResort();
1011 Listing TownDirectoryWindow::last_sorting = {false, 0};
1013 /** Names of the sorting functions. */
1014 const StringID TownDirectoryWindow::sorter_names[] = {
1015 STR_SORT_BY_NAME,
1016 STR_SORT_BY_POPULATION,
1017 STR_SORT_BY_RATING,
1018 INVALID_STRING_ID
1021 /** Available town directory sorting functions. */
1022 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
1023 &TownNameSorter,
1024 &TownPopulationSorter,
1025 &TownRatingSorter,
1028 static WindowDesc _town_directory_desc(
1029 WDP_AUTO, "list_towns", 208, 202,
1030 WC_TOWN_DIRECTORY, WC_NONE,
1032 _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
1035 void ShowTownDirectory()
1037 if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
1038 new TownDirectoryWindow(&_town_directory_desc);
1041 void CcFoundTown(Commands cmd, const CommandCost &result, TileIndex tile)
1043 if (result.Failed()) return;
1045 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
1046 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
1049 void CcFoundRandomTown(Commands cmd, const CommandCost &result, Money, TownID town_id)
1051 if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(town_id)->xy);
1054 static const NWidgetPart _nested_found_town_widgets[] = {
1055 NWidget(NWID_HORIZONTAL),
1056 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1057 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1058 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1059 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
1060 EndContainer(),
1061 /* Construct new town(s) buttons. */
1062 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1063 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1064 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1065 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1066 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1067 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1068 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
1069 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
1070 /* Town name selection. */
1071 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
1072 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
1073 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
1074 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
1075 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
1076 /* Town size selection. */
1077 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1078 NWidget(NWID_SPACER), SetFill(1, 0),
1079 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
1080 NWidget(NWID_SPACER), SetFill(1, 0),
1081 EndContainer(),
1082 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1083 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
1084 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1085 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
1086 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1087 EndContainer(),
1088 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1089 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1090 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
1091 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1092 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
1093 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1094 EndContainer(),
1095 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1096 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1097 SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
1098 /* Town roads selection. */
1099 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1100 NWidget(NWID_SPACER), SetFill(1, 0),
1101 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
1102 NWidget(NWID_SPACER), SetFill(1, 0),
1103 EndContainer(),
1104 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1105 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_ORIGINAL), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_ORIGINAL, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
1106 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_BETTER), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_BETTER_ROADS, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
1107 EndContainer(),
1108 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1109 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1110 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_GRID2), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_2X2_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
1111 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_GRID3), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_3X3_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
1112 EndContainer(),
1113 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1114 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1115 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
1116 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1117 EndContainer(),
1120 /** Found a town window class. */
1121 struct FoundTownWindow : Window {
1122 private:
1123 TownSize town_size; ///< Selected town size
1124 TownLayout town_layout; ///< Selected town layout
1125 bool city; ///< Are we building a city?
1126 QueryString townname_editbox; ///< Townname editbox
1127 bool townnamevalid; ///< Is generated town name valid?
1128 uint32 townnameparts; ///< Generated town name
1129 TownNameParams params; ///< Town name parameters
1131 public:
1132 FoundTownWindow(WindowDesc *desc, WindowNumber window_number) :
1133 Window(desc),
1134 town_size(TSZ_MEDIUM),
1135 town_layout(_settings_game.economy.town_layout),
1136 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
1137 params(_settings_game.game_creation.town_name)
1139 this->InitNested(window_number);
1140 this->querystrings[WID_TF_TOWN_NAME_EDITBOX] = &this->townname_editbox;
1141 this->RandomTownName();
1142 this->UpdateButtons(true);
1145 void RandomTownName()
1147 this->townnamevalid = GenerateTownName(&this->townnameparts);
1149 if (!this->townnamevalid) {
1150 this->townname_editbox.text.DeleteAll();
1151 } else {
1152 GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
1153 this->townname_editbox.text.UpdateSize();
1155 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
1157 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
1160 void UpdateButtons(bool check_availability)
1162 if (check_availability && _game_mode != GM_EDITOR) {
1163 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
1164 this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
1165 WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
1166 if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
1169 for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
1170 this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
1173 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1175 for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
1176 this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
1179 this->SetDirty();
1182 template <typename Tcallback>
1183 void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, Tcallback cc)
1185 std::string name;
1187 if (!this->townnamevalid) {
1188 name = this->townname_editbox.text.buf;
1189 } else {
1190 /* If user changed the name, send it */
1191 char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
1192 GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
1193 if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
1196 bool success = Command<CMD_FOUND_TOWN>::Post(errstr, cc,
1197 tile, this->town_size, this->city, this->town_layout, random, townnameparts, name);
1199 /* Rerandomise name, if success and no cost-estimation. */
1200 if (success && !_shift_pressed) this->RandomTownName();
1203 void OnClick(Point pt, int widget, int click_count) override
1205 switch (widget) {
1206 case WID_TF_NEW_TOWN:
1207 HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
1208 break;
1210 case WID_TF_RANDOM_TOWN:
1211 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
1212 break;
1214 case WID_TF_TOWN_NAME_RANDOM:
1215 this->RandomTownName();
1216 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
1217 break;
1219 case WID_TF_MANY_RANDOM_TOWNS: {
1220 Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
1221 UpdateNearestTownForRoadTiles(true);
1222 if (!GenerateTowns(this->town_layout)) {
1223 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
1225 UpdateNearestTownForRoadTiles(false);
1226 old_generating_world.Restore();
1227 break;
1230 case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
1231 this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
1232 this->UpdateButtons(false);
1233 break;
1235 case WID_TF_CITY:
1236 this->city ^= true;
1237 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1238 this->SetDirty();
1239 break;
1241 case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
1242 case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
1243 this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
1244 this->UpdateButtons(false);
1245 break;
1249 void OnPlaceObject(Point pt, TileIndex tile) override
1251 this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
1254 void OnPlaceObjectAbort() override
1256 this->RaiseButtons();
1257 this->UpdateButtons(false);
1261 * Some data on this window has become invalid.
1262 * @param data Information about the changed data.
1263 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1265 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1267 if (!gui_scope) return;
1268 this->UpdateButtons(true);
1272 static WindowDesc _found_town_desc(
1273 WDP_AUTO, "build_town", 160, 162,
1274 WC_FOUND_TOWN, WC_NONE,
1275 WDF_CONSTRUCTION,
1276 _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
1279 void ShowFoundTownWindow()
1281 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
1282 AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
1285 void InitializeTownGui()
1287 _town_local_authority_kdtree.Clear();