Feature: Wide rivers
[openttd-github.git] / src / town_gui.cpp
blob216de7e79a77b4bee0d746311f46479dadab22f8
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(NWID_HORIZONTAL),
60 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_COMMAND_LIST), SetMinimalSize(305, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP), SetScrollbar(WID_TA_SCROLLBAR), EndContainer(),
61 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TA_SCROLLBAR),
62 EndContainer(),
63 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
64 NWidget(NWID_HORIZONTAL),
65 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),
66 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
67 EndContainer()
70 /** Town authority window. */
71 struct TownAuthorityWindow : Window {
72 private:
73 Town *town; ///< Town being displayed.
74 int sel_index; ///< Currently selected town action, \c 0 to \c TACT_COUNT-1, \c -1 means no action selected.
75 Scrollbar *vscroll;
76 uint displayed_actions_on_previous_painting; ///< Actions that were available on the previous call to OnPaint()
78 /**
79 * Get the position of the Nth set bit.
81 * If there is no Nth bit set return -1
83 * @param bits The value to search in
84 * @param n The Nth set bit from which we want to know the position
85 * @return The position of the Nth set bit
87 static int GetNthSetBit(uint32 bits, int n)
89 if (n >= 0) {
90 for (uint i : SetBitIterator(bits)) {
91 n--;
92 if (n < 0) return i;
95 return -1;
98 public:
99 TownAuthorityWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc), sel_index(-1), displayed_actions_on_previous_painting(0)
101 this->town = Town::Get(window_number);
102 this->InitNested(window_number);
103 this->vscroll = this->GetScrollbar(WID_TA_SCROLLBAR);
104 this->vscroll->SetCapacity((this->GetWidget<NWidgetBase>(WID_TA_COMMAND_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
107 void OnPaint() override
109 int numact;
110 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
111 if (buttons != displayed_actions_on_previous_painting) this->SetDirty();
112 displayed_actions_on_previous_painting = buttons;
114 this->vscroll->SetCount(numact + 1);
116 if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
117 this->sel_index = -1;
120 this->SetWidgetLoweredState(WID_TA_ZONE_BUTTON, this->town->show_zone);
121 this->SetWidgetDisabledState(WID_TA_EXECUTE, this->sel_index == -1);
123 this->DrawWidgets();
124 if (!this->IsShaded()) this->DrawRatings();
127 /** Draw the contents of the ratings panel. May request a resize of the window if the contents does not fit. */
128 void DrawRatings()
130 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_RATING_INFO);
131 uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
132 uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
134 uint y = nwid->pos_y + WD_FRAMERECT_TOP;
136 DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
137 y += FONT_HEIGHT_NORMAL;
139 Dimension icon_size = GetSpriteSize(SPR_COMPANY_ICON);
140 int icon_width = icon_size.width;
141 int icon_y_offset = (FONT_HEIGHT_NORMAL - icon_size.height) / 2;
143 Dimension exclusive_size = GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT);
144 int exclusive_width = exclusive_size.width;
145 int exclusive_y_offset = (FONT_HEIGHT_NORMAL - exclusive_size.height) / 2;
147 bool rtl = _current_text_dir == TD_RTL;
148 uint text_left = left + (rtl ? 0 : icon_width + exclusive_width + 4);
149 uint text_right = right - (rtl ? icon_width + exclusive_width + 4 : 0);
150 uint icon_left = rtl ? right - icon_width : left;
151 uint exclusive_left = rtl ? right - icon_width - exclusive_width - 2 : left + icon_width + 2;
153 /* Draw list of companies */
154 for (const Company *c : Company::Iterate()) {
155 if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
156 DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
158 SetDParam(0, c->index);
159 SetDParam(1, c->index);
161 int r = this->town->ratings[c->index];
162 StringID str = STR_CARGO_RATING_APPALLING;
163 if (r > RATING_APPALLING) str++;
164 if (r > RATING_VERYPOOR) str++;
165 if (r > RATING_POOR) str++;
166 if (r > RATING_MEDIOCRE) str++;
167 if (r > RATING_GOOD) str++;
168 if (r > RATING_VERYGOOD) str++;
169 if (r > RATING_EXCELLENT) str++;
171 SetDParam(2, str);
172 if (this->town->exclusivity == c->index) {
173 DrawSprite(SPR_EXCLUSIVE_TRANSPORT, COMPANY_SPRITE_COLOUR(c->index), exclusive_left, y + exclusive_y_offset);
176 DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
177 y += FONT_HEIGHT_NORMAL;
181 y = y + WD_FRAMERECT_BOTTOM - nwid->pos_y; // Compute needed size of the widget.
182 if (y > nwid->current_y) {
183 /* If the company list is too big to fit, mark ourself dirty and draw again. */
184 ResizeWindow(this, 0, y - nwid->current_y, false);
188 void SetStringParameters(int widget) const override
190 if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
193 void DrawWidget(const Rect &r, int widget) const override
195 switch (widget) {
196 case WID_TA_ACTION_INFO:
197 if (this->sel_index != -1) {
198 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8);
199 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
200 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
202 break;
203 case WID_TA_COMMAND_LIST: {
204 int numact;
205 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
206 int y = r.top + WD_FRAMERECT_TOP;
207 int pos = this->vscroll->GetPosition();
209 if (--pos < 0) {
210 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
211 y += FONT_HEIGHT_NORMAL;
214 for (int i = 0; buttons; i++, buttons >>= 1) {
215 if (pos <= -5) break; ///< Draw only the 5 fitting lines
217 if ((buttons & 1) && --pos < 0) {
218 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y,
219 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, this->sel_index == i ? TC_WHITE : TC_ORANGE);
220 y += FONT_HEIGHT_NORMAL;
223 break;
228 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
230 switch (widget) {
231 case WID_TA_ACTION_INFO: {
232 assert(size->width > padding.width && size->height > padding.height);
233 size->width -= WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
234 size->height -= WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
235 Dimension d = {0, 0};
236 for (int i = 0; i < TACT_COUNT; i++) {
237 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
238 d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
240 *size = maxdim(*size, d);
241 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
242 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
243 break;
246 case WID_TA_COMMAND_LIST:
247 size->height = WD_FRAMERECT_TOP + 5 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
248 size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
249 for (uint i = 0; i < TACT_COUNT; i++ ) {
250 size->width = std::max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
252 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
253 break;
255 case WID_TA_RATING_INFO:
256 resize->height = FONT_HEIGHT_NORMAL;
257 size->height = WD_FRAMERECT_TOP + 9 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
258 break;
262 void OnClick(Point pt, int widget, int click_count) override
264 switch (widget) {
265 case WID_TA_ZONE_BUTTON: {
266 bool new_show_state = !this->town->show_zone;
267 TownID index = this->town->index;
269 new_show_state ? _town_local_authority_kdtree.Insert(index) : _town_local_authority_kdtree.Remove(index);
271 this->town->show_zone = new_show_state;
272 this->SetWidgetLoweredState(widget, new_show_state);
273 MarkWholeScreenDirty();
274 break;
277 case WID_TA_COMMAND_LIST: {
278 int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL);
279 if (!IsInsideMM(y, 0, 5)) return;
281 y = GetNthSetBit(GetMaskOfTownActions(nullptr, _local_company, this->town), y + this->vscroll->GetPosition() - 1);
282 if (y >= 0) {
283 this->sel_index = y;
284 this->SetDirty();
286 /* When double-clicking, continue */
287 if (click_count == 1 || y < 0) break;
288 FALLTHROUGH;
291 case WID_TA_EXECUTE:
292 Command<CMD_DO_TOWN_ACTION>::Post(STR_ERROR_CAN_T_DO_THIS, this->town->xy, this->window_number, this->sel_index);
293 break;
297 void OnHundredthTick() override
299 this->SetDirty();
303 static WindowDesc _town_authority_desc(
304 WDP_AUTO, "view_town_authority", 317, 222,
305 WC_TOWN_AUTHORITY, WC_NONE,
307 _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
310 static void ShowTownAuthorityWindow(uint town)
312 AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
316 /* Town view window. */
317 struct TownViewWindow : Window {
318 private:
319 Town *town; ///< Town displayed by the window.
321 public:
322 static const int WID_TV_HEIGHT_NORMAL = 150;
324 TownViewWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
326 this->CreateNestedTree();
328 this->town = Town::Get(window_number);
329 if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
331 this->FinishInitNested(window_number);
333 this->flags |= WF_DISABLE_VP_SCROLL;
334 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
335 nvp->InitializeViewport(this, this->town->xy, ScaleZoomGUI(ZOOM_LVL_TOWN));
337 /* disable renaming town in network games if you are not the server */
338 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME, _networking && !_network_server);
341 void Close() override
343 SetViewportCatchmentTown(Town::Get(this->window_number), false);
344 this->Window::Close();
347 void SetStringParameters(int widget) const override
349 if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
352 void OnPaint() override
354 extern const Town *_viewport_highlight_town;
355 this->SetWidgetLoweredState(WID_TV_CATCHMENT, _viewport_highlight_town == this->town);
357 this->DrawWidgets();
360 void DrawWidget(const Rect &r, int widget) const override
362 if (widget != WID_TV_INFO) return;
364 uint y = r.top + WD_FRAMERECT_TOP;
366 SetDParam(0, this->town->cache.population);
367 SetDParam(1, this->town->cache.num_houses);
368 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
370 SetDParam(0, 1 << CT_PASSENGERS);
371 SetDParam(1, this->town->supplied[CT_PASSENGERS].old_act);
372 SetDParam(2, this->town->supplied[CT_PASSENGERS].old_max);
373 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
375 SetDParam(0, 1 << CT_MAIL);
376 SetDParam(1, this->town->supplied[CT_MAIL].old_act);
377 SetDParam(2, this->town->supplied[CT_MAIL].old_max);
378 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
380 bool first = true;
381 for (int i = TE_BEGIN; i < TE_END; i++) {
382 if (this->town->goal[i] == 0) continue;
383 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
384 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
386 if (first) {
387 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
388 first = false;
391 bool rtl = _current_text_dir == TD_RTL;
392 uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
393 uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
395 const CargoSpec *cargo = FindFirstCargoWithTownEffect((TownEffect)i);
396 assert(cargo != nullptr);
398 StringID string;
400 if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
401 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
402 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
403 if (this->town->received[i].old_act == 0) {
404 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
406 if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
407 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
411 SetDParam(0, cargo->name);
412 } else {
413 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
414 if (this->town->received[i].old_act < this->town->goal[i]) {
415 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
418 SetDParam(0, cargo->Index());
419 SetDParam(1, this->town->received[i].old_act);
420 SetDParam(2, cargo->Index());
421 SetDParam(3, this->town->goal[i]);
423 DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
426 if (HasBit(this->town->flags, TOWN_IS_GROWING)) {
427 SetDParam(0, RoundDivSU(this->town->growth_rate + 1, DAY_TICKS));
428 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);
429 } else {
430 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
433 /* only show the town noise, if the noise option is activated. */
434 if (_settings_game.economy.station_noise_level) {
435 SetDParam(0, this->town->noise_reached);
436 SetDParam(1, this->town->MaxTownNoise());
437 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
440 if (!this->town->text.empty()) {
441 SetDParamStr(0, this->town->text);
442 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
446 void OnClick(Point pt, int widget, int click_count) override
448 switch (widget) {
449 case WID_TV_CENTER_VIEW: // scroll to location
450 if (_ctrl_pressed) {
451 ShowExtraViewportWindow(this->town->xy);
452 } else {
453 ScrollMainWindowToTile(this->town->xy);
455 break;
457 case WID_TV_SHOW_AUTHORITY: // town authority
458 ShowTownAuthorityWindow(this->window_number);
459 break;
461 case WID_TV_CHANGE_NAME: // rename
462 SetDParam(0, this->window_number);
463 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);
464 break;
466 case WID_TV_CATCHMENT:
467 SetViewportCatchmentTown(Town::Get(this->window_number), !this->IsWidgetLowered(WID_TV_CATCHMENT));
468 break;
470 case WID_TV_EXPAND: { // expand town - only available on Scenario editor
471 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
472 static bool _warn_town_no_roads = false;
474 if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
475 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
476 _warn_town_no_roads = true;
479 Command<CMD_EXPAND_TOWN>::Post(STR_ERROR_CAN_T_EXPAND_TOWN, this->window_number, 0);
480 break;
483 case WID_TV_DELETE: // delete town - only available on Scenario editor
484 Command<CMD_DELETE_TOWN>::Post(STR_ERROR_TOWN_CAN_T_DELETE, this->window_number);
485 break;
489 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
491 switch (widget) {
492 case WID_TV_INFO:
493 size->height = GetDesiredInfoHeight(size->width);
494 break;
499 * Gets the desired height for the information panel.
500 * @return the desired height in pixels.
502 uint GetDesiredInfoHeight(int width) const
504 uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
506 bool first = true;
507 for (int i = TE_BEGIN; i < TE_END; i++) {
508 if (this->town->goal[i] == 0) continue;
509 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
510 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
512 if (first) {
513 aimed_height += FONT_HEIGHT_NORMAL;
514 first = false;
516 aimed_height += FONT_HEIGHT_NORMAL;
518 aimed_height += FONT_HEIGHT_NORMAL;
520 if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
522 if (!this->town->text.empty()) {
523 SetDParamStr(0, this->town->text);
524 aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT);
527 return aimed_height;
530 void ResizeWindowAsNeeded()
532 const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
533 uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
534 if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
535 this->ReInit();
539 void OnResize() override
541 if (this->viewport != nullptr) {
542 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
543 nvp->UpdateViewportCoordinates(this);
545 ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
550 * Some data on this window has become invalid.
551 * @param data Information about the changed data.
552 * @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.
554 void OnInvalidateData(int data = 0, bool gui_scope = true) override
556 if (!gui_scope) return;
557 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
558 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
559 this->ResizeWindowAsNeeded();
562 void OnQueryTextFinished(char *str) override
564 if (str == nullptr) return;
566 Command<CMD_RENAME_TOWN>::Post(STR_ERROR_CAN_T_RENAME_TOWN, this->window_number, str);
570 static const NWidgetPart _nested_town_game_view_widgets[] = {
571 NWidget(NWID_HORIZONTAL),
572 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
573 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
574 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
575 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
576 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
577 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
578 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
579 EndContainer(),
580 NWidget(WWT_PANEL, COLOUR_BROWN),
581 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
582 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1),
583 EndContainer(),
584 EndContainer(),
585 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
586 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
587 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),
588 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
589 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
590 EndContainer(),
593 static WindowDesc _town_game_view_desc(
594 WDP_AUTO, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
595 WC_TOWN_VIEW, WC_NONE,
597 _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
600 static const NWidgetPart _nested_town_editor_view_widgets[] = {
601 NWidget(NWID_HORIZONTAL),
602 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
603 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
604 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
605 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
606 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
607 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
608 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
609 EndContainer(),
610 NWidget(WWT_PANEL, COLOUR_BROWN),
611 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
612 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1),
613 EndContainer(),
614 EndContainer(),
615 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
616 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
617 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),
618 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),
619 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
620 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
621 EndContainer(),
624 static WindowDesc _town_editor_view_desc(
625 WDP_AUTO, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
626 WC_TOWN_VIEW, WC_NONE,
628 _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
631 void ShowTownViewWindow(TownID town)
633 if (_game_mode == GM_EDITOR) {
634 AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
635 } else {
636 AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
640 static const NWidgetPart _nested_town_directory_widgets[] = {
641 NWidget(NWID_HORIZONTAL),
642 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
643 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
644 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
645 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
646 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
647 EndContainer(),
648 NWidget(NWID_HORIZONTAL),
649 NWidget(NWID_VERTICAL),
650 NWidget(NWID_HORIZONTAL),
651 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
652 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
653 NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_TD_FILTER), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
654 EndContainer(),
655 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
656 SetFill(1, 0), SetResize(1, 1), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
657 NWidget(WWT_PANEL, COLOUR_BROWN),
658 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),
659 EndContainer(),
660 EndContainer(),
661 NWidget(NWID_VERTICAL),
662 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
663 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
664 EndContainer(),
665 EndContainer(),
668 /** Town directory window class. */
669 struct TownDirectoryWindow : public Window {
670 private:
671 /* Runtime saved values */
672 static Listing last_sorting;
674 /* Constants for sorting towns */
675 static const StringID sorter_names[];
676 static GUITownList::SortFunction * const sorter_funcs[];
678 StringFilter string_filter; ///< Filter for towns
679 QueryString townname_editbox; ///< Filter editbox
681 GUITownList towns;
683 Scrollbar *vscroll;
685 void BuildSortTownList()
687 if (this->towns.NeedRebuild()) {
688 this->towns.clear();
690 for (const Town *t : Town::Iterate()) {
691 if (this->string_filter.IsEmpty()) {
692 this->towns.push_back(t);
693 continue;
695 this->string_filter.ResetState();
696 this->string_filter.AddLine(t->GetCachedName());
697 if (this->string_filter.GetState()) this->towns.push_back(t);
700 this->towns.shrink_to_fit();
701 this->towns.RebuildDone();
702 this->vscroll->SetCount((uint)this->towns.size()); // Update scrollbar as well.
704 /* Always sort the towns. */
705 this->towns.Sort();
706 this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns.
709 /** Sort by town name */
710 static bool TownNameSorter(const Town * const &a, const Town * const &b)
712 return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting).
715 /** Sort by population (default descending, as big towns are of the most interest). */
716 static bool TownPopulationSorter(const Town * const &a, const Town * const &b)
718 uint32 a_population = a->cache.population;
719 uint32 b_population = b->cache.population;
720 if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
721 return a_population < b_population;
724 /** Sort by town rating */
725 static bool TownRatingSorter(const Town * const &a, const Town * const &b)
727 bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'.
729 /* Towns without rating are always after towns with rating. */
730 if (HasBit(a->have_ratings, _local_company)) {
731 if (HasBit(b->have_ratings, _local_company)) {
732 int16 a_rating = a->ratings[_local_company];
733 int16 b_rating = b->ratings[_local_company];
734 if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
735 return a_rating < b_rating;
737 return before;
739 if (HasBit(b->have_ratings, _local_company)) return !before;
741 /* Sort unrated towns always on ascending town name. */
742 if (before) return TownDirectoryWindow::TownNameSorter(a, b);
743 return !TownDirectoryWindow::TownNameSorter(a, b);
746 public:
747 TownDirectoryWindow(WindowDesc *desc) : Window(desc), townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS)
749 this->CreateNestedTree();
751 this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
753 this->towns.SetListing(this->last_sorting);
754 this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
755 this->towns.ForceRebuild();
756 this->BuildSortTownList();
758 this->FinishInitNested(0);
760 this->querystrings[WID_TD_FILTER] = &this->townname_editbox;
761 this->townname_editbox.cancel_button = QueryString::ACTION_CLEAR;
764 void SetStringParameters(int widget) const override
766 switch (widget) {
767 case WID_TD_WORLD_POPULATION:
768 SetDParam(0, GetWorldPopulation());
769 break;
771 case WID_TD_SORT_CRITERIA:
772 SetDParam(0, TownDirectoryWindow::sorter_names[this->towns.SortType()]);
773 break;
778 * Get the string to draw the town name.
779 * @param t Town to draw.
780 * @return The string to use.
782 static StringID GetTownString(const Town *t)
784 return t->larger_town ? STR_TOWN_DIRECTORY_CITY : STR_TOWN_DIRECTORY_TOWN;
787 void DrawWidget(const Rect &r, int widget) const override
789 switch (widget) {
790 case WID_TD_SORT_ORDER:
791 this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
792 break;
794 case WID_TD_LIST: {
795 int n = 0;
796 int y = r.top + WD_FRAMERECT_TOP;
797 if (this->towns.size() == 0) { // No towns available.
798 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
799 break;
802 /* At least one town available. */
803 bool rtl = _current_text_dir == TD_RTL;
804 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
805 int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
806 int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
807 int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
809 for (uint i = this->vscroll->GetPosition(); i < this->towns.size(); i++) {
810 const Town *t = this->towns[i];
811 assert(t->xy != INVALID_TILE);
813 /* Draw rating icon. */
814 if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
815 DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
816 } else {
817 SpriteID icon = SPR_TOWN_RATING_APALLING;
818 if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
819 if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
820 DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
823 SetDParam(0, t->index);
824 SetDParam(1, t->cache.population);
825 DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, GetTownString(t));
827 y += this->resize.step_height;
828 if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
830 break;
835 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
837 switch (widget) {
838 case WID_TD_SORT_ORDER: {
839 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
840 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
841 d.height += padding.height;
842 *size = maxdim(*size, d);
843 break;
845 case WID_TD_SORT_CRITERIA: {
846 Dimension d = {0, 0};
847 for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
848 d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i]));
850 d.width += padding.width;
851 d.height += padding.height;
852 *size = maxdim(*size, d);
853 break;
855 case WID_TD_LIST: {
856 Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
857 for (uint i = 0; i < this->towns.size(); i++) {
858 const Town *t = this->towns[i];
860 assert(t != nullptr);
862 SetDParam(0, t->index);
863 SetDParamMaxDigits(1, 8);
864 d = maxdim(d, GetStringBoundingBox(GetTownString(t)));
866 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
867 d.width += icon_size.width + 2;
868 d.height = std::max(d.height, icon_size.height);
869 resize->height = d.height;
870 d.height *= 5;
871 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
872 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
873 *size = maxdim(*size, d);
874 break;
876 case WID_TD_WORLD_POPULATION: {
877 SetDParamMaxDigits(0, 10);
878 Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
879 d.width += padding.width;
880 d.height += padding.height;
881 *size = maxdim(*size, d);
882 break;
887 void OnClick(Point pt, int widget, int click_count) override
889 switch (widget) {
890 case WID_TD_SORT_ORDER: // Click on sort order button
891 if (this->towns.SortType() != 2) { // A different sort than by rating.
892 this->towns.ToggleSortOrder();
893 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
894 } else {
895 /* Some parts are always sorted ascending on name. */
896 this->last_sorting.order = !this->last_sorting.order;
897 this->towns.SetListing(this->last_sorting);
898 this->towns.ForceResort();
899 this->towns.Sort();
901 this->SetDirty();
902 break;
904 case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown
905 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names, this->towns.SortType(), WID_TD_SORT_CRITERIA, 0, 0);
906 break;
908 case WID_TD_LIST: { // Click on Town Matrix
909 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
910 if (id_v >= this->towns.size()) return; // click out of town bounds
912 const Town *t = this->towns[id_v];
913 assert(t != nullptr);
914 if (_ctrl_pressed) {
915 ShowExtraViewportWindow(t->xy);
916 } else {
917 ScrollMainWindowToTile(t->xy);
919 break;
924 void OnDropdownSelect(int widget, int index) override
926 if (widget != WID_TD_SORT_CRITERIA) return;
928 if (this->towns.SortType() != index) {
929 this->towns.SetSortType(index);
930 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
931 this->BuildSortTownList();
935 void OnPaint() override
937 if (this->towns.NeedRebuild()) this->BuildSortTownList();
938 this->DrawWidgets();
941 void OnHundredthTick() override
943 this->BuildSortTownList();
944 this->SetDirty();
947 void OnResize() override
949 this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
952 void OnEditboxChanged(int wid) override
954 if (wid == WID_TD_FILTER) {
955 this->string_filter.SetFilterTerm(this->townname_editbox.text.buf);
956 this->InvalidateData(TDIWD_FORCE_REBUILD);
961 * Some data on this window has become invalid.
962 * @param data Information about the changed data.
963 * @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.
965 void OnInvalidateData(int data = 0, bool gui_scope = true) override
967 switch (data) {
968 case TDIWD_FORCE_REBUILD:
969 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
970 this->towns.ForceRebuild();
971 break;
973 case TDIWD_POPULATION_CHANGE:
974 if (this->towns.SortType() == 1) this->towns.ForceResort();
975 break;
977 default:
978 this->towns.ForceResort();
983 Listing TownDirectoryWindow::last_sorting = {false, 0};
985 /** Names of the sorting functions. */
986 const StringID TownDirectoryWindow::sorter_names[] = {
987 STR_SORT_BY_NAME,
988 STR_SORT_BY_POPULATION,
989 STR_SORT_BY_RATING,
990 INVALID_STRING_ID
993 /** Available town directory sorting functions. */
994 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
995 &TownNameSorter,
996 &TownPopulationSorter,
997 &TownRatingSorter,
1000 static WindowDesc _town_directory_desc(
1001 WDP_AUTO, "list_towns", 208, 202,
1002 WC_TOWN_DIRECTORY, WC_NONE,
1004 _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
1007 void ShowTownDirectory()
1009 if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
1010 new TownDirectoryWindow(&_town_directory_desc);
1013 void CcFoundTown(Commands cmd, const CommandCost &result, TileIndex tile)
1015 if (result.Failed()) return;
1017 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
1018 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
1021 void CcFoundRandomTown(Commands cmd, const CommandCost &result, Money, TownID town_id)
1023 if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(town_id)->xy);
1026 static const NWidgetPart _nested_found_town_widgets[] = {
1027 NWidget(NWID_HORIZONTAL),
1028 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1029 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1030 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1031 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
1032 EndContainer(),
1033 /* Construct new town(s) buttons. */
1034 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1035 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1036 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1037 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1038 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1039 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1040 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
1041 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
1042 /* Town name selection. */
1043 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
1044 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
1045 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
1046 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
1047 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
1048 /* Town size selection. */
1049 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1050 NWidget(NWID_SPACER), SetFill(1, 0),
1051 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
1052 NWidget(NWID_SPACER), SetFill(1, 0),
1053 EndContainer(),
1054 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1055 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
1056 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1057 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
1058 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1059 EndContainer(),
1060 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1061 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1062 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
1063 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1064 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
1065 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1066 EndContainer(),
1067 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1068 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1069 SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
1070 /* Town roads selection. */
1071 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1072 NWidget(NWID_SPACER), SetFill(1, 0),
1073 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
1074 NWidget(NWID_SPACER), SetFill(1, 0),
1075 EndContainer(),
1076 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1077 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),
1078 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),
1079 EndContainer(),
1080 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1081 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1082 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),
1083 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),
1084 EndContainer(),
1085 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1086 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1087 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
1088 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1089 EndContainer(),
1092 /** Found a town window class. */
1093 struct FoundTownWindow : Window {
1094 private:
1095 TownSize town_size; ///< Selected town size
1096 TownLayout town_layout; ///< Selected town layout
1097 bool city; ///< Are we building a city?
1098 QueryString townname_editbox; ///< Townname editbox
1099 bool townnamevalid; ///< Is generated town name valid?
1100 uint32 townnameparts; ///< Generated town name
1101 TownNameParams params; ///< Town name parameters
1103 public:
1104 FoundTownWindow(WindowDesc *desc, WindowNumber window_number) :
1105 Window(desc),
1106 town_size(TSZ_MEDIUM),
1107 town_layout(_settings_game.economy.town_layout),
1108 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
1109 params(_settings_game.game_creation.town_name)
1111 this->InitNested(window_number);
1112 this->querystrings[WID_TF_TOWN_NAME_EDITBOX] = &this->townname_editbox;
1113 this->RandomTownName();
1114 this->UpdateButtons(true);
1117 void RandomTownName()
1119 this->townnamevalid = GenerateTownName(&this->townnameparts);
1121 if (!this->townnamevalid) {
1122 this->townname_editbox.text.DeleteAll();
1123 } else {
1124 GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
1125 this->townname_editbox.text.UpdateSize();
1127 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
1129 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
1132 void UpdateButtons(bool check_availability)
1134 if (check_availability && _game_mode != GM_EDITOR) {
1135 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
1136 this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
1137 WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
1138 if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
1141 for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
1142 this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
1145 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1147 for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
1148 this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
1151 this->SetDirty();
1154 template <typename Tcallback>
1155 void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, Tcallback cc)
1157 std::string name;
1159 if (!this->townnamevalid) {
1160 name = this->townname_editbox.text.buf;
1161 } else {
1162 /* If user changed the name, send it */
1163 char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
1164 GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
1165 if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
1168 bool success = Command<CMD_FOUND_TOWN>::Post(errstr, cc,
1169 tile, this->town_size, this->city, this->town_layout, random, townnameparts, name);
1171 /* Rerandomise name, if success and no cost-estimation. */
1172 if (success && !_shift_pressed) this->RandomTownName();
1175 void OnClick(Point pt, int widget, int click_count) override
1177 switch (widget) {
1178 case WID_TF_NEW_TOWN:
1179 HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
1180 break;
1182 case WID_TF_RANDOM_TOWN:
1183 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
1184 break;
1186 case WID_TF_TOWN_NAME_RANDOM:
1187 this->RandomTownName();
1188 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
1189 break;
1191 case WID_TF_MANY_RANDOM_TOWNS: {
1192 Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
1193 UpdateNearestTownForRoadTiles(true);
1194 if (!GenerateTowns(this->town_layout)) {
1195 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
1197 UpdateNearestTownForRoadTiles(false);
1198 old_generating_world.Restore();
1199 break;
1202 case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
1203 this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
1204 this->UpdateButtons(false);
1205 break;
1207 case WID_TF_CITY:
1208 this->city ^= true;
1209 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1210 this->SetDirty();
1211 break;
1213 case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
1214 case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
1215 this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
1216 this->UpdateButtons(false);
1217 break;
1221 void OnPlaceObject(Point pt, TileIndex tile) override
1223 this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
1226 void OnPlaceObjectAbort() override
1228 this->RaiseButtons();
1229 this->UpdateButtons(false);
1233 * Some data on this window has become invalid.
1234 * @param data Information about the changed data.
1235 * @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.
1237 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1239 if (!gui_scope) return;
1240 this->UpdateButtons(true);
1244 static WindowDesc _found_town_desc(
1245 WDP_AUTO, "build_town", 160, 162,
1246 WC_FOUND_TOWN, WC_NONE,
1247 WDF_CONSTRUCTION,
1248 _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
1251 void ShowFoundTownWindow()
1253 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
1254 AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
1257 void InitializeTownGui()
1259 _town_local_authority_kdtree.Clear();