Add: allow making heightmap screenshot via console
[openttd-github.git] / src / town_gui.cpp
blob1a7085c96462396cd699ba5ee09f507dba6998ee
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/geometry_func.hpp"
31 #include "genworld.h"
32 #include "stringfilter_type.h"
33 #include "widgets/dropdown_func.h"
34 #include "town_kdtree.h"
36 #include "widgets/town_widget.h"
38 #include "table/strings.h"
40 #include "safeguards.h"
42 TownKdtree _town_local_authority_kdtree(&Kdtree_TownXYFunc);
44 typedef GUIList<const Town*> GUITownList;
46 static const NWidgetPart _nested_town_authority_widgets[] = {
47 NWidget(NWID_HORIZONTAL),
48 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
49 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
50 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),
51 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
52 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
53 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
54 EndContainer(),
55 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
56 NWidget(NWID_HORIZONTAL),
57 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(),
58 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TA_SCROLLBAR),
59 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 Scrollbar *vscroll;
73 uint displayed_actions_on_previous_painting; ///< Actions that were available on the previous call to OnPaint()
75 /**
76 * Get the position of the Nth set bit.
78 * If there is no Nth bit set return -1
80 * @param bits The value to search in
81 * @param n The Nth set bit from which we want to know the position
82 * @return The position of the Nth set bit
84 static int GetNthSetBit(uint32 bits, int n)
86 if (n >= 0) {
87 uint i;
88 FOR_EACH_SET_BIT(i, bits) {
89 n--;
90 if (n < 0) return i;
93 return -1;
96 public:
97 TownAuthorityWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc), sel_index(-1), displayed_actions_on_previous_painting(0)
99 this->town = Town::Get(window_number);
100 this->InitNested(window_number);
101 this->vscroll = this->GetScrollbar(WID_TA_SCROLLBAR);
102 this->vscroll->SetCapacity((this->GetWidget<NWidgetBase>(WID_TA_COMMAND_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
105 void OnPaint() override
107 int numact;
108 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
109 if (buttons != displayed_actions_on_previous_painting) this->SetDirty();
110 displayed_actions_on_previous_painting = buttons;
112 this->vscroll->SetCount(numact + 1);
114 if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
115 this->sel_index = -1;
118 this->SetWidgetLoweredState(WID_TA_ZONE_BUTTON, this->town->show_zone);
119 this->SetWidgetDisabledState(WID_TA_EXECUTE, this->sel_index == -1);
121 this->DrawWidgets();
122 if (!this->IsShaded()) this->DrawRatings();
125 /** Draw the contents of the ratings panel. May request a resize of the window if the contents does not fit. */
126 void DrawRatings()
128 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_RATING_INFO);
129 uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
130 uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
132 uint y = nwid->pos_y + WD_FRAMERECT_TOP;
134 DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
135 y += FONT_HEIGHT_NORMAL;
137 Dimension icon_size = GetSpriteSize(SPR_COMPANY_ICON);
138 int icon_width = icon_size.width;
139 int icon_y_offset = (FONT_HEIGHT_NORMAL - icon_size.height) / 2;
141 Dimension exclusive_size = GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT);
142 int exclusive_width = exclusive_size.width;
143 int exclusive_y_offset = (FONT_HEIGHT_NORMAL - exclusive_size.height) / 2;
145 bool rtl = _current_text_dir == TD_RTL;
146 uint text_left = left + (rtl ? 0 : icon_width + exclusive_width + 4);
147 uint text_right = right - (rtl ? icon_width + exclusive_width + 4 : 0);
148 uint icon_left = rtl ? right - icon_width : left;
149 uint exclusive_left = rtl ? right - icon_width - exclusive_width - 2 : left + icon_width + 2;
151 /* Draw list of companies */
152 for (const Company *c : Company::Iterate()) {
153 if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
154 DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
156 SetDParam(0, c->index);
157 SetDParam(1, c->index);
159 int r = this->town->ratings[c->index];
160 StringID str = STR_CARGO_RATING_APPALLING;
161 if (r > RATING_APPALLING) str++;
162 if (r > RATING_VERYPOOR) str++;
163 if (r > RATING_POOR) str++;
164 if (r > RATING_MEDIOCRE) str++;
165 if (r > RATING_GOOD) str++;
166 if (r > RATING_VERYGOOD) str++;
167 if (r > RATING_EXCELLENT) str++;
169 SetDParam(2, str);
170 if (this->town->exclusivity == c->index) {
171 DrawSprite(SPR_EXCLUSIVE_TRANSPORT, COMPANY_SPRITE_COLOUR(c->index), exclusive_left, y + exclusive_y_offset);
174 DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
175 y += FONT_HEIGHT_NORMAL;
179 y = y + WD_FRAMERECT_BOTTOM - nwid->pos_y; // Compute needed size of the widget.
180 if (y > nwid->current_y) {
181 /* If the company list is too big to fit, mark ourself dirty and draw again. */
182 ResizeWindow(this, 0, y - nwid->current_y, false);
186 void SetStringParameters(int widget) const override
188 if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
191 void DrawWidget(const Rect &r, int widget) const override
193 switch (widget) {
194 case WID_TA_ACTION_INFO:
195 if (this->sel_index != -1) {
196 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8);
197 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
198 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
200 break;
201 case WID_TA_COMMAND_LIST: {
202 int numact;
203 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
204 int y = r.top + WD_FRAMERECT_TOP;
205 int pos = this->vscroll->GetPosition();
207 if (--pos < 0) {
208 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
209 y += FONT_HEIGHT_NORMAL;
212 for (int i = 0; buttons; i++, buttons >>= 1) {
213 if (pos <= -5) break; ///< Draw only the 5 fitting lines
215 if ((buttons & 1) && --pos < 0) {
216 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y,
217 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, this->sel_index == i ? TC_WHITE : TC_ORANGE);
218 y += FONT_HEIGHT_NORMAL;
221 break;
226 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
228 switch (widget) {
229 case WID_TA_ACTION_INFO: {
230 assert(size->width > padding.width && size->height > padding.height);
231 size->width -= WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
232 size->height -= WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
233 Dimension d = {0, 0};
234 for (int i = 0; i < TACT_COUNT; i++) {
235 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
236 d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
238 *size = maxdim(*size, d);
239 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
240 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
241 break;
244 case WID_TA_COMMAND_LIST:
245 size->height = WD_FRAMERECT_TOP + 5 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
246 size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
247 for (uint i = 0; i < TACT_COUNT; i++ ) {
248 size->width = std::max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
250 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
251 break;
253 case WID_TA_RATING_INFO:
254 resize->height = FONT_HEIGHT_NORMAL;
255 size->height = WD_FRAMERECT_TOP + 9 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
256 break;
260 void OnClick(Point pt, int widget, int click_count) override
262 switch (widget) {
263 case WID_TA_ZONE_BUTTON: {
264 bool new_show_state = !this->town->show_zone;
265 TownID index = this->town->index;
267 new_show_state ? _town_local_authority_kdtree.Insert(index) : _town_local_authority_kdtree.Remove(index);
269 this->town->show_zone = new_show_state;
270 this->SetWidgetLoweredState(widget, new_show_state);
271 MarkWholeScreenDirty();
272 break;
275 case WID_TA_COMMAND_LIST: {
276 int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL);
277 if (!IsInsideMM(y, 0, 5)) return;
279 y = GetNthSetBit(GetMaskOfTownActions(nullptr, _local_company, this->town), y + this->vscroll->GetPosition() - 1);
280 if (y >= 0) {
281 this->sel_index = y;
282 this->SetDirty();
284 /* When double-clicking, continue */
285 if (click_count == 1 || y < 0) break;
286 FALLTHROUGH;
289 case WID_TA_EXECUTE:
290 DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_ERROR_CAN_T_DO_THIS));
291 break;
295 void OnHundredthTick() override
297 this->SetDirty();
301 static WindowDesc _town_authority_desc(
302 WDP_AUTO, "view_town_authority", 317, 222,
303 WC_TOWN_AUTHORITY, WC_NONE,
305 _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
308 static void ShowTownAuthorityWindow(uint town)
310 AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
314 /* Town view window. */
315 struct TownViewWindow : Window {
316 private:
317 Town *town; ///< Town displayed by the window.
319 public:
320 static const int WID_TV_HEIGHT_NORMAL = 150;
322 TownViewWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
324 this->CreateNestedTree();
326 this->town = Town::Get(window_number);
327 if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
329 this->FinishInitNested(window_number);
331 this->flags |= WF_DISABLE_VP_SCROLL;
332 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
333 nvp->InitializeViewport(this, this->town->xy, ZOOM_LVL_NEWS);
335 /* disable renaming town in network games if you are not the server */
336 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME, _networking && !_network_server);
339 ~TownViewWindow()
341 SetViewportCatchmentTown(Town::Get(this->window_number), false);
344 void SetStringParameters(int widget) const override
346 if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
349 void OnPaint() override
351 extern const Town *_viewport_highlight_town;
352 this->SetWidgetLoweredState(WID_TV_CATCHMENT, _viewport_highlight_town == this->town);
354 this->DrawWidgets();
357 void DrawWidget(const Rect &r, int widget) const override
359 if (widget != WID_TV_INFO) return;
361 uint y = r.top + WD_FRAMERECT_TOP;
363 SetDParam(0, this->town->cache.population);
364 SetDParam(1, this->town->cache.num_houses);
365 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
367 SetDParam(0, 1 << CT_PASSENGERS);
368 SetDParam(1, this->town->supplied[CT_PASSENGERS].old_act);
369 SetDParam(2, this->town->supplied[CT_PASSENGERS].old_max);
370 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
372 SetDParam(0, 1 << CT_MAIL);
373 SetDParam(1, this->town->supplied[CT_MAIL].old_act);
374 SetDParam(2, this->town->supplied[CT_MAIL].old_max);
375 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
377 bool first = true;
378 for (int i = TE_BEGIN; i < TE_END; i++) {
379 if (this->town->goal[i] == 0) continue;
380 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
381 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
383 if (first) {
384 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
385 first = false;
388 bool rtl = _current_text_dir == TD_RTL;
389 uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
390 uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
392 const CargoSpec *cargo = FindFirstCargoWithTownEffect((TownEffect)i);
393 assert(cargo != nullptr);
395 StringID string;
397 if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
398 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
399 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
400 if (this->town->received[i].old_act == 0) {
401 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
403 if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
404 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
408 SetDParam(0, cargo->name);
409 } else {
410 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
411 if (this->town->received[i].old_act < this->town->goal[i]) {
412 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
415 SetDParam(0, cargo->Index());
416 SetDParam(1, this->town->received[i].old_act);
417 SetDParam(2, cargo->Index());
418 SetDParam(3, this->town->goal[i]);
420 DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
423 if (HasBit(this->town->flags, TOWN_IS_GROWING)) {
424 SetDParam(0, RoundDivSU(this->town->growth_rate + 1, DAY_TICKS));
425 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);
426 } else {
427 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
430 /* only show the town noise, if the noise option is activated. */
431 if (_settings_game.economy.station_noise_level) {
432 SetDParam(0, this->town->noise_reached);
433 SetDParam(1, this->town->MaxTownNoise());
434 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
437 if (!this->town->text.empty()) {
438 SetDParamStr(0, this->town->text.c_str());
439 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
443 void OnClick(Point pt, int widget, int click_count) override
445 switch (widget) {
446 case WID_TV_CENTER_VIEW: // scroll to location
447 if (_ctrl_pressed) {
448 ShowExtraViewportWindow(this->town->xy);
449 } else {
450 ScrollMainWindowToTile(this->town->xy);
452 break;
454 case WID_TV_SHOW_AUTHORITY: // town authority
455 ShowTownAuthorityWindow(this->window_number);
456 break;
458 case WID_TV_CHANGE_NAME: // rename
459 SetDParam(0, this->window_number);
460 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);
461 break;
463 case WID_TV_CATCHMENT:
464 SetViewportCatchmentTown(Town::Get(this->window_number), !this->IsWidgetLowered(WID_TV_CATCHMENT));
465 break;
467 case WID_TV_EXPAND: { // expand town - only available on Scenario editor
468 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
469 static bool _warn_town_no_roads = false;
471 if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
472 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
473 _warn_town_no_roads = true;
476 DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
477 break;
480 case WID_TV_DELETE: // delete town - only available on Scenario editor
481 DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
482 break;
486 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
488 switch (widget) {
489 case WID_TV_INFO:
490 size->height = GetDesiredInfoHeight(size->width);
491 break;
496 * Gets the desired height for the information panel.
497 * @return the desired height in pixels.
499 uint GetDesiredInfoHeight(int width) const
501 uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
503 bool first = true;
504 for (int i = TE_BEGIN; i < TE_END; i++) {
505 if (this->town->goal[i] == 0) continue;
506 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
507 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
509 if (first) {
510 aimed_height += FONT_HEIGHT_NORMAL;
511 first = false;
513 aimed_height += FONT_HEIGHT_NORMAL;
515 aimed_height += FONT_HEIGHT_NORMAL;
517 if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
519 if (!this->town->text.empty()) {
520 SetDParamStr(0, this->town->text.c_str());
521 aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT);
524 return aimed_height;
527 void ResizeWindowAsNeeded()
529 const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
530 uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
531 if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
532 this->ReInit();
536 void OnResize() override
538 if (this->viewport != nullptr) {
539 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
540 nvp->UpdateViewportCoordinates(this);
542 ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
547 * Some data on this window has become invalid.
548 * @param data Information about the changed data.
549 * @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.
551 void OnInvalidateData(int data = 0, bool gui_scope = true) override
553 if (!gui_scope) return;
554 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
555 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
556 this->ResizeWindowAsNeeded();
559 void OnQueryTextFinished(char *str) override
561 if (str == nullptr) return;
563 DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), nullptr, str);
567 static const NWidgetPart _nested_town_game_view_widgets[] = {
568 NWidget(NWID_HORIZONTAL),
569 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
570 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
571 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
572 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
573 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
574 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
575 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
576 EndContainer(),
577 NWidget(WWT_PANEL, COLOUR_BROWN),
578 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
579 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
580 EndContainer(),
581 EndContainer(),
582 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
583 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
584 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),
585 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
586 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
587 EndContainer(),
590 static WindowDesc _town_game_view_desc(
591 WDP_AUTO, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
592 WC_TOWN_VIEW, WC_NONE,
594 _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
597 static const NWidgetPart _nested_town_editor_view_widgets[] = {
598 NWidget(NWID_HORIZONTAL),
599 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
600 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
601 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
602 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
603 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
604 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
605 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
606 EndContainer(),
607 NWidget(WWT_PANEL, COLOUR_BROWN),
608 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
609 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
610 EndContainer(),
611 EndContainer(),
612 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
613 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
614 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),
615 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),
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_editor_view_desc(
622 WDP_AUTO, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
623 WC_TOWN_VIEW, WC_NONE,
625 _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
628 void ShowTownViewWindow(TownID town)
630 if (_game_mode == GM_EDITOR) {
631 AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
632 } else {
633 AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
637 static const NWidgetPart _nested_town_directory_widgets[] = {
638 NWidget(NWID_HORIZONTAL),
639 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
640 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
641 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
642 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
643 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
644 EndContainer(),
645 NWidget(NWID_HORIZONTAL),
646 NWidget(NWID_VERTICAL),
647 NWidget(NWID_HORIZONTAL),
648 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
649 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
650 NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_TD_FILTER), SetFill(35, 12), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
651 EndContainer(),
652 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
653 SetFill(1, 0), SetResize(0, 10), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
654 NWidget(WWT_PANEL, COLOUR_BROWN),
655 NWidget(WWT_TEXT, COLOUR_BROWN, WID_TD_WORLD_POPULATION), SetPadding(2, 0, 0, 2), SetMinimalSize(196, 12), SetFill(1, 0), SetDataTip(STR_TOWN_POPULATION, STR_NULL),
656 EndContainer(),
657 EndContainer(),
658 NWidget(NWID_VERTICAL),
659 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
660 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
661 EndContainer(),
662 EndContainer(),
665 /** Town directory window class. */
666 struct TownDirectoryWindow : public Window {
667 private:
668 /* Runtime saved values */
669 static Listing last_sorting;
671 /* Constants for sorting towns */
672 static const StringID sorter_names[];
673 static GUITownList::SortFunction * const sorter_funcs[];
675 StringFilter string_filter; ///< Filter for towns
676 QueryString townname_editbox; ///< Filter editbox
678 GUITownList towns;
680 Scrollbar *vscroll;
682 void BuildSortTownList()
684 if (this->towns.NeedRebuild()) {
685 this->towns.clear();
687 for (const Town *t : Town::Iterate()) {
688 if (this->string_filter.IsEmpty()) {
689 this->towns.push_back(t);
690 continue;
692 this->string_filter.ResetState();
693 this->string_filter.AddLine(t->GetCachedName());
694 if (this->string_filter.GetState()) this->towns.push_back(t);
697 this->towns.shrink_to_fit();
698 this->towns.RebuildDone();
699 this->vscroll->SetCount((uint)this->towns.size()); // Update scrollbar as well.
701 /* Always sort the towns. */
702 this->towns.Sort();
703 this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns.
706 /** Sort by town name */
707 static bool TownNameSorter(const Town * const &a, const Town * const &b)
709 return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting).
712 /** Sort by population (default descending, as big towns are of the most interest). */
713 static bool TownPopulationSorter(const Town * const &a, const Town * const &b)
715 uint32 a_population = a->cache.population;
716 uint32 b_population = b->cache.population;
717 if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
718 return a_population < b_population;
721 /** Sort by town rating */
722 static bool TownRatingSorter(const Town * const &a, const Town * const &b)
724 bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'.
726 /* Towns without rating are always after towns with rating. */
727 if (HasBit(a->have_ratings, _local_company)) {
728 if (HasBit(b->have_ratings, _local_company)) {
729 int16 a_rating = a->ratings[_local_company];
730 int16 b_rating = b->ratings[_local_company];
731 if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
732 return a_rating < b_rating;
734 return before;
736 if (HasBit(b->have_ratings, _local_company)) return !before;
738 /* Sort unrated towns always on ascending town name. */
739 if (before) return TownDirectoryWindow::TownNameSorter(a, b);
740 return !TownDirectoryWindow::TownNameSorter(a, b);
743 public:
744 TownDirectoryWindow(WindowDesc *desc) : Window(desc), townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS)
746 this->CreateNestedTree();
748 this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
750 this->towns.SetListing(this->last_sorting);
751 this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
752 this->towns.ForceRebuild();
753 this->BuildSortTownList();
755 this->FinishInitNested(0);
757 this->querystrings[WID_TD_FILTER] = &this->townname_editbox;
758 this->townname_editbox.cancel_button = QueryString::ACTION_CLEAR;
761 void SetStringParameters(int widget) const override
763 switch (widget) {
764 case WID_TD_WORLD_POPULATION:
765 SetDParam(0, GetWorldPopulation());
766 break;
768 case WID_TD_SORT_CRITERIA:
769 SetDParam(0, TownDirectoryWindow::sorter_names[this->towns.SortType()]);
770 break;
775 * Get the string to draw the town name.
776 * @param t Town to draw.
777 * @return The string to use.
779 static StringID GetTownString(const Town *t)
781 return t->larger_town ? STR_TOWN_DIRECTORY_CITY : STR_TOWN_DIRECTORY_TOWN;
784 void DrawWidget(const Rect &r, int widget) const override
786 switch (widget) {
787 case WID_TD_SORT_ORDER:
788 this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
789 break;
791 case WID_TD_LIST: {
792 int n = 0;
793 int y = r.top + WD_FRAMERECT_TOP;
794 if (this->towns.size() == 0) { // No towns available.
795 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
796 break;
799 /* At least one town available. */
800 bool rtl = _current_text_dir == TD_RTL;
801 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
802 int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
803 int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
804 int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
806 for (uint i = this->vscroll->GetPosition(); i < this->towns.size(); i++) {
807 const Town *t = this->towns[i];
808 assert(t->xy != INVALID_TILE);
810 /* Draw rating icon. */
811 if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
812 DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
813 } else {
814 SpriteID icon = SPR_TOWN_RATING_APALLING;
815 if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
816 if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
817 DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
820 SetDParam(0, t->index);
821 SetDParam(1, t->cache.population);
822 DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, GetTownString(t));
824 y += this->resize.step_height;
825 if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
827 break;
832 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
834 switch (widget) {
835 case WID_TD_SORT_ORDER: {
836 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
837 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
838 d.height += padding.height;
839 *size = maxdim(*size, d);
840 break;
842 case WID_TD_SORT_CRITERIA: {
843 Dimension d = {0, 0};
844 for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
845 d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i]));
847 d.width += padding.width;
848 d.height += padding.height;
849 *size = maxdim(*size, d);
850 break;
852 case WID_TD_LIST: {
853 Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
854 for (uint i = 0; i < this->towns.size(); i++) {
855 const Town *t = this->towns[i];
857 assert(t != nullptr);
859 SetDParam(0, t->index);
860 SetDParamMaxDigits(1, 8);
861 d = maxdim(d, GetStringBoundingBox(GetTownString(t)));
863 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
864 d.width += icon_size.width + 2;
865 d.height = std::max(d.height, icon_size.height);
866 resize->height = d.height;
867 d.height *= 5;
868 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
869 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
870 *size = maxdim(*size, d);
871 break;
873 case WID_TD_WORLD_POPULATION: {
874 SetDParamMaxDigits(0, 10);
875 Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
876 d.width += padding.width;
877 d.height += padding.height;
878 *size = maxdim(*size, d);
879 break;
884 void OnClick(Point pt, int widget, int click_count) override
886 switch (widget) {
887 case WID_TD_SORT_ORDER: // Click on sort order button
888 if (this->towns.SortType() != 2) { // A different sort than by rating.
889 this->towns.ToggleSortOrder();
890 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
891 } else {
892 /* Some parts are always sorted ascending on name. */
893 this->last_sorting.order = !this->last_sorting.order;
894 this->towns.SetListing(this->last_sorting);
895 this->towns.ForceResort();
896 this->towns.Sort();
898 this->SetDirty();
899 break;
901 case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown
902 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names, this->towns.SortType(), WID_TD_SORT_CRITERIA, 0, 0);
903 break;
905 case WID_TD_LIST: { // Click on Town Matrix
906 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
907 if (id_v >= this->towns.size()) return; // click out of town bounds
909 const Town *t = this->towns[id_v];
910 assert(t != nullptr);
911 if (_ctrl_pressed) {
912 ShowExtraViewportWindow(t->xy);
913 } else {
914 ScrollMainWindowToTile(t->xy);
916 break;
921 void OnDropdownSelect(int widget, int index) override
923 if (widget != WID_TD_SORT_CRITERIA) return;
925 if (this->towns.SortType() != index) {
926 this->towns.SetSortType(index);
927 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
928 this->BuildSortTownList();
932 void OnPaint() override
934 if (this->towns.NeedRebuild()) this->BuildSortTownList();
935 this->DrawWidgets();
938 void OnHundredthTick() override
940 this->BuildSortTownList();
941 this->SetDirty();
944 void OnResize() override
946 this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
949 void OnEditboxChanged(int wid) override
951 if (wid == WID_TD_FILTER) {
952 this->string_filter.SetFilterTerm(this->townname_editbox.text.buf);
953 this->InvalidateData(TDIWD_FORCE_REBUILD);
958 * Some data on this window has become invalid.
959 * @param data Information about the changed data.
960 * @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.
962 void OnInvalidateData(int data = 0, bool gui_scope = true) override
964 switch (data) {
965 case TDIWD_FORCE_REBUILD:
966 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
967 this->towns.ForceRebuild();
968 break;
970 case TDIWD_POPULATION_CHANGE:
971 if (this->towns.SortType() == 1) this->towns.ForceResort();
972 break;
974 default:
975 this->towns.ForceResort();
980 Listing TownDirectoryWindow::last_sorting = {false, 0};
982 /** Names of the sorting functions. */
983 const StringID TownDirectoryWindow::sorter_names[] = {
984 STR_SORT_BY_NAME,
985 STR_SORT_BY_POPULATION,
986 STR_SORT_BY_RATING,
987 INVALID_STRING_ID
990 /** Available town directory sorting functions. */
991 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
992 &TownNameSorter,
993 &TownPopulationSorter,
994 &TownRatingSorter,
997 static WindowDesc _town_directory_desc(
998 WDP_AUTO, "list_towns", 208, 202,
999 WC_TOWN_DIRECTORY, WC_NONE,
1001 _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
1004 void ShowTownDirectory()
1006 if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
1007 new TownDirectoryWindow(&_town_directory_desc);
1010 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1012 if (result.Failed()) return;
1014 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
1015 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
1018 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1020 if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
1023 static const NWidgetPart _nested_found_town_widgets[] = {
1024 NWidget(NWID_HORIZONTAL),
1025 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1026 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1027 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1028 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
1029 EndContainer(),
1030 /* Construct new town(s) buttons. */
1031 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1032 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1033 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1034 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1035 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1036 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1037 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
1038 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
1039 /* Town name selection. */
1040 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
1041 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
1042 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
1043 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
1044 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
1045 /* Town size selection. */
1046 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1047 NWidget(NWID_SPACER), SetFill(1, 0),
1048 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
1049 NWidget(NWID_SPACER), SetFill(1, 0),
1050 EndContainer(),
1051 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1052 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
1053 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1054 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
1055 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1056 EndContainer(),
1057 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1058 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1059 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
1060 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1061 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
1062 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1063 EndContainer(),
1064 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1065 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1066 SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
1067 /* Town roads selection. */
1068 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1069 NWidget(NWID_SPACER), SetFill(1, 0),
1070 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
1071 NWidget(NWID_SPACER), SetFill(1, 0),
1072 EndContainer(),
1073 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1074 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),
1075 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),
1076 EndContainer(),
1077 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1078 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1079 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),
1080 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),
1081 EndContainer(),
1082 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1083 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1084 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
1085 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1086 EndContainer(),
1089 /** Found a town window class. */
1090 struct FoundTownWindow : Window {
1091 private:
1092 TownSize town_size; ///< Selected town size
1093 TownLayout town_layout; ///< Selected town layout
1094 bool city; ///< Are we building a city?
1095 QueryString townname_editbox; ///< Townname editbox
1096 bool townnamevalid; ///< Is generated town name valid?
1097 uint32 townnameparts; ///< Generated town name
1098 TownNameParams params; ///< Town name parameters
1100 public:
1101 FoundTownWindow(WindowDesc *desc, WindowNumber window_number) :
1102 Window(desc),
1103 town_size(TSZ_MEDIUM),
1104 town_layout(_settings_game.economy.town_layout),
1105 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
1106 params(_settings_game.game_creation.town_name)
1108 this->InitNested(window_number);
1109 this->querystrings[WID_TF_TOWN_NAME_EDITBOX] = &this->townname_editbox;
1110 this->RandomTownName();
1111 this->UpdateButtons(true);
1114 void RandomTownName()
1116 this->townnamevalid = GenerateTownName(&this->townnameparts);
1118 if (!this->townnamevalid) {
1119 this->townname_editbox.text.DeleteAll();
1120 } else {
1121 GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
1122 this->townname_editbox.text.UpdateSize();
1124 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
1126 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
1129 void UpdateButtons(bool check_availability)
1131 if (check_availability && _game_mode != GM_EDITOR) {
1132 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
1133 this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
1134 WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
1135 if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
1138 for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
1139 this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
1142 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1144 for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
1145 this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
1148 this->SetDirty();
1151 void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
1153 const char *name = nullptr;
1155 if (!this->townnamevalid) {
1156 name = this->townname_editbox.text.buf;
1157 } else {
1158 /* If user changed the name, send it */
1159 char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
1160 GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
1161 if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
1164 bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
1165 townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
1167 /* Rerandomise name, if success and no cost-estimation. */
1168 if (success && !_shift_pressed) this->RandomTownName();
1171 void OnClick(Point pt, int widget, int click_count) override
1173 switch (widget) {
1174 case WID_TF_NEW_TOWN:
1175 HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
1176 break;
1178 case WID_TF_RANDOM_TOWN:
1179 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
1180 break;
1182 case WID_TF_TOWN_NAME_RANDOM:
1183 this->RandomTownName();
1184 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
1185 break;
1187 case WID_TF_MANY_RANDOM_TOWNS:
1188 _generating_world = true;
1189 UpdateNearestTownForRoadTiles(true);
1190 if (!GenerateTowns(this->town_layout)) {
1191 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
1193 UpdateNearestTownForRoadTiles(false);
1194 _generating_world = false;
1195 break;
1197 case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
1198 this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
1199 this->UpdateButtons(false);
1200 break;
1202 case WID_TF_CITY:
1203 this->city ^= true;
1204 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1205 this->SetDirty();
1206 break;
1208 case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
1209 case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
1210 this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
1211 this->UpdateButtons(false);
1212 break;
1216 void OnPlaceObject(Point pt, TileIndex tile) override
1218 this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
1221 void OnPlaceObjectAbort() override
1223 this->RaiseButtons();
1224 this->UpdateButtons(false);
1228 * Some data on this window has become invalid.
1229 * @param data Information about the changed data.
1230 * @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.
1232 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1234 if (!gui_scope) return;
1235 this->UpdateButtons(true);
1239 static WindowDesc _found_town_desc(
1240 WDP_AUTO, "build_town", 160, 162,
1241 WC_FOUND_TOWN, WC_NONE,
1242 WDF_CONSTRUCTION,
1243 _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
1246 void ShowFoundTownWindow()
1248 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
1249 AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
1252 void InitializeTownGui()
1254 _town_local_authority_kdtree.Clear();