Feature: Hide block signals in GUI by default (#8688)
[openttd-github.git] / src / town_gui.cpp
bloba4bb240637d7a58662457d3b13dfc7a3576f39e4
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"
37 #include "widgets/town_widget.h"
39 #include "table/strings.h"
41 #include "safeguards.h"
43 TownKdtree _town_local_authority_kdtree(&Kdtree_TownXYFunc);
45 typedef GUIList<const Town*> GUITownList;
47 static const NWidgetPart _nested_town_authority_widgets[] = {
48 NWidget(NWID_HORIZONTAL),
49 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
50 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
51 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),
52 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
53 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
54 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
55 EndContainer(),
56 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
57 NWidget(NWID_HORIZONTAL),
58 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(),
59 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TA_SCROLLBAR),
60 EndContainer(),
61 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
62 NWidget(NWID_HORIZONTAL),
63 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),
64 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
65 EndContainer()
68 /** Town authority window. */
69 struct TownAuthorityWindow : Window {
70 private:
71 Town *town; ///< Town being displayed.
72 int sel_index; ///< Currently selected town action, \c 0 to \c TACT_COUNT-1, \c -1 means no action selected.
73 Scrollbar *vscroll;
74 uint displayed_actions_on_previous_painting; ///< Actions that were available on the previous call to OnPaint()
76 /**
77 * Get the position of the Nth set bit.
79 * If there is no Nth bit set return -1
81 * @param bits The value to search in
82 * @param n The Nth set bit from which we want to know the position
83 * @return The position of the Nth set bit
85 static int GetNthSetBit(uint32 bits, int n)
87 if (n >= 0) {
88 for (uint i : SetBitIterator(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 void Close() override
341 SetViewportCatchmentTown(Town::Get(this->window_number), false);
342 this->Window::Close();
345 void SetStringParameters(int widget) const override
347 if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
350 void OnPaint() override
352 extern const Town *_viewport_highlight_town;
353 this->SetWidgetLoweredState(WID_TV_CATCHMENT, _viewport_highlight_town == this->town);
355 this->DrawWidgets();
358 void DrawWidget(const Rect &r, int widget) const override
360 if (widget != WID_TV_INFO) return;
362 uint y = r.top + WD_FRAMERECT_TOP;
364 SetDParam(0, this->town->cache.population);
365 SetDParam(1, this->town->cache.num_houses);
366 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
368 SetDParam(0, 1 << CT_PASSENGERS);
369 SetDParam(1, this->town->supplied[CT_PASSENGERS].old_act);
370 SetDParam(2, this->town->supplied[CT_PASSENGERS].old_max);
371 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
373 SetDParam(0, 1 << CT_MAIL);
374 SetDParam(1, this->town->supplied[CT_MAIL].old_act);
375 SetDParam(2, this->town->supplied[CT_MAIL].old_max);
376 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
378 bool first = true;
379 for (int i = TE_BEGIN; i < TE_END; i++) {
380 if (this->town->goal[i] == 0) continue;
381 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
382 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
384 if (first) {
385 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
386 first = false;
389 bool rtl = _current_text_dir == TD_RTL;
390 uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
391 uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
393 const CargoSpec *cargo = FindFirstCargoWithTownEffect((TownEffect)i);
394 assert(cargo != nullptr);
396 StringID string;
398 if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
399 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
400 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
401 if (this->town->received[i].old_act == 0) {
402 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
404 if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
405 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
409 SetDParam(0, cargo->name);
410 } else {
411 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
412 if (this->town->received[i].old_act < this->town->goal[i]) {
413 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
416 SetDParam(0, cargo->Index());
417 SetDParam(1, this->town->received[i].old_act);
418 SetDParam(2, cargo->Index());
419 SetDParam(3, this->town->goal[i]);
421 DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
424 if (HasBit(this->town->flags, TOWN_IS_GROWING)) {
425 SetDParam(0, RoundDivSU(this->town->growth_rate + 1, DAY_TICKS));
426 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);
427 } else {
428 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
431 /* only show the town noise, if the noise option is activated. */
432 if (_settings_game.economy.station_noise_level) {
433 SetDParam(0, this->town->noise_reached);
434 SetDParam(1, this->town->MaxTownNoise());
435 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
438 if (!this->town->text.empty()) {
439 SetDParamStr(0, this->town->text);
440 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
444 void OnClick(Point pt, int widget, int click_count) override
446 switch (widget) {
447 case WID_TV_CENTER_VIEW: // scroll to location
448 if (_ctrl_pressed) {
449 ShowExtraViewportWindow(this->town->xy);
450 } else {
451 ScrollMainWindowToTile(this->town->xy);
453 break;
455 case WID_TV_SHOW_AUTHORITY: // town authority
456 ShowTownAuthorityWindow(this->window_number);
457 break;
459 case WID_TV_CHANGE_NAME: // rename
460 SetDParam(0, this->window_number);
461 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);
462 break;
464 case WID_TV_CATCHMENT:
465 SetViewportCatchmentTown(Town::Get(this->window_number), !this->IsWidgetLowered(WID_TV_CATCHMENT));
466 break;
468 case WID_TV_EXPAND: { // expand town - only available on Scenario editor
469 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
470 static bool _warn_town_no_roads = false;
472 if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
473 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
474 _warn_town_no_roads = true;
477 DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
478 break;
481 case WID_TV_DELETE: // delete town - only available on Scenario editor
482 DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
483 break;
487 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
489 switch (widget) {
490 case WID_TV_INFO:
491 size->height = GetDesiredInfoHeight(size->width);
492 break;
497 * Gets the desired height for the information panel.
498 * @return the desired height in pixels.
500 uint GetDesiredInfoHeight(int width) const
502 uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
504 bool first = true;
505 for (int i = TE_BEGIN; i < TE_END; i++) {
506 if (this->town->goal[i] == 0) continue;
507 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
508 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
510 if (first) {
511 aimed_height += FONT_HEIGHT_NORMAL;
512 first = false;
514 aimed_height += FONT_HEIGHT_NORMAL;
516 aimed_height += FONT_HEIGHT_NORMAL;
518 if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
520 if (!this->town->text.empty()) {
521 SetDParamStr(0, this->town->text);
522 aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT);
525 return aimed_height;
528 void ResizeWindowAsNeeded()
530 const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
531 uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
532 if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
533 this->ReInit();
537 void OnResize() override
539 if (this->viewport != nullptr) {
540 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
541 nvp->UpdateViewportCoordinates(this);
543 ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
548 * Some data on this window has become invalid.
549 * @param data Information about the changed data.
550 * @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.
552 void OnInvalidateData(int data = 0, bool gui_scope = true) override
554 if (!gui_scope) return;
555 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
556 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
557 this->ResizeWindowAsNeeded();
560 void OnQueryTextFinished(char *str) override
562 if (str == nullptr) return;
564 DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), nullptr, str);
568 static const NWidgetPart _nested_town_game_view_widgets[] = {
569 NWidget(NWID_HORIZONTAL),
570 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
571 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
572 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
573 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
574 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
575 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
576 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
577 EndContainer(),
578 NWidget(WWT_PANEL, COLOUR_BROWN),
579 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
580 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1),
581 EndContainer(),
582 EndContainer(),
583 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
584 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
585 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),
586 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
587 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
588 EndContainer(),
591 static WindowDesc _town_game_view_desc(
592 WDP_AUTO, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
593 WC_TOWN_VIEW, WC_NONE,
595 _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
598 static const NWidgetPart _nested_town_editor_view_widgets[] = {
599 NWidget(NWID_HORIZONTAL),
600 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
601 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
602 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
603 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
604 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
605 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
606 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
607 EndContainer(),
608 NWidget(WWT_PANEL, COLOUR_BROWN),
609 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
610 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1),
611 EndContainer(),
612 EndContainer(),
613 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
614 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
615 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_EXPAND), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_EXPAND_BUTTON, STR_TOWN_VIEW_EXPAND_TOOLTIP),
616 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),
617 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
618 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
619 EndContainer(),
622 static WindowDesc _town_editor_view_desc(
623 WDP_AUTO, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
624 WC_TOWN_VIEW, WC_NONE,
626 _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
629 void ShowTownViewWindow(TownID town)
631 if (_game_mode == GM_EDITOR) {
632 AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
633 } else {
634 AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
638 static const NWidgetPart _nested_town_directory_widgets[] = {
639 NWidget(NWID_HORIZONTAL),
640 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
641 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
642 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
643 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
644 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
645 EndContainer(),
646 NWidget(NWID_HORIZONTAL),
647 NWidget(NWID_VERTICAL),
648 NWidget(NWID_HORIZONTAL),
649 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
650 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
651 NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_TD_FILTER), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
652 EndContainer(),
653 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
654 SetFill(1, 0), SetResize(1, 1), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
655 NWidget(WWT_PANEL, COLOUR_BROWN),
656 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),
657 EndContainer(),
658 EndContainer(),
659 NWidget(NWID_VERTICAL),
660 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
661 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
662 EndContainer(),
663 EndContainer(),
666 /** Town directory window class. */
667 struct TownDirectoryWindow : public Window {
668 private:
669 /* Runtime saved values */
670 static Listing last_sorting;
672 /* Constants for sorting towns */
673 static const StringID sorter_names[];
674 static GUITownList::SortFunction * const sorter_funcs[];
676 StringFilter string_filter; ///< Filter for towns
677 QueryString townname_editbox; ///< Filter editbox
679 GUITownList towns;
681 Scrollbar *vscroll;
683 void BuildSortTownList()
685 if (this->towns.NeedRebuild()) {
686 this->towns.clear();
688 for (const Town *t : Town::Iterate()) {
689 if (this->string_filter.IsEmpty()) {
690 this->towns.push_back(t);
691 continue;
693 this->string_filter.ResetState();
694 this->string_filter.AddLine(t->GetCachedName());
695 if (this->string_filter.GetState()) this->towns.push_back(t);
698 this->towns.shrink_to_fit();
699 this->towns.RebuildDone();
700 this->vscroll->SetCount((uint)this->towns.size()); // Update scrollbar as well.
702 /* Always sort the towns. */
703 this->towns.Sort();
704 this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns.
707 /** Sort by town name */
708 static bool TownNameSorter(const Town * const &a, const Town * const &b)
710 return strnatcmp(a->GetCachedName(), b->GetCachedName()) < 0; // Sort by name (natural sorting).
713 /** Sort by population (default descending, as big towns are of the most interest). */
714 static bool TownPopulationSorter(const Town * const &a, const Town * const &b)
716 uint32 a_population = a->cache.population;
717 uint32 b_population = b->cache.population;
718 if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
719 return a_population < b_population;
722 /** Sort by town rating */
723 static bool TownRatingSorter(const Town * const &a, const Town * const &b)
725 bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'.
727 /* Towns without rating are always after towns with rating. */
728 if (HasBit(a->have_ratings, _local_company)) {
729 if (HasBit(b->have_ratings, _local_company)) {
730 int16 a_rating = a->ratings[_local_company];
731 int16 b_rating = b->ratings[_local_company];
732 if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
733 return a_rating < b_rating;
735 return before;
737 if (HasBit(b->have_ratings, _local_company)) return !before;
739 /* Sort unrated towns always on ascending town name. */
740 if (before) return TownDirectoryWindow::TownNameSorter(a, b);
741 return !TownDirectoryWindow::TownNameSorter(a, b);
744 public:
745 TownDirectoryWindow(WindowDesc *desc) : Window(desc), townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS)
747 this->CreateNestedTree();
749 this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
751 this->towns.SetListing(this->last_sorting);
752 this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
753 this->towns.ForceRebuild();
754 this->BuildSortTownList();
756 this->FinishInitNested(0);
758 this->querystrings[WID_TD_FILTER] = &this->townname_editbox;
759 this->townname_editbox.cancel_button = QueryString::ACTION_CLEAR;
762 void SetStringParameters(int widget) const override
764 switch (widget) {
765 case WID_TD_WORLD_POPULATION:
766 SetDParam(0, GetWorldPopulation());
767 break;
769 case WID_TD_SORT_CRITERIA:
770 SetDParam(0, TownDirectoryWindow::sorter_names[this->towns.SortType()]);
771 break;
776 * Get the string to draw the town name.
777 * @param t Town to draw.
778 * @return The string to use.
780 static StringID GetTownString(const Town *t)
782 return t->larger_town ? STR_TOWN_DIRECTORY_CITY : STR_TOWN_DIRECTORY_TOWN;
785 void DrawWidget(const Rect &r, int widget) const override
787 switch (widget) {
788 case WID_TD_SORT_ORDER:
789 this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
790 break;
792 case WID_TD_LIST: {
793 int n = 0;
794 int y = r.top + WD_FRAMERECT_TOP;
795 if (this->towns.size() == 0) { // No towns available.
796 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
797 break;
800 /* At least one town available. */
801 bool rtl = _current_text_dir == TD_RTL;
802 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
803 int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
804 int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
805 int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
807 for (uint i = this->vscroll->GetPosition(); i < this->towns.size(); i++) {
808 const Town *t = this->towns[i];
809 assert(t->xy != INVALID_TILE);
811 /* Draw rating icon. */
812 if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
813 DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
814 } else {
815 SpriteID icon = SPR_TOWN_RATING_APALLING;
816 if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
817 if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
818 DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
821 SetDParam(0, t->index);
822 SetDParam(1, t->cache.population);
823 DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, GetTownString(t));
825 y += this->resize.step_height;
826 if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
828 break;
833 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
835 switch (widget) {
836 case WID_TD_SORT_ORDER: {
837 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
838 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
839 d.height += padding.height;
840 *size = maxdim(*size, d);
841 break;
843 case WID_TD_SORT_CRITERIA: {
844 Dimension d = {0, 0};
845 for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
846 d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i]));
848 d.width += padding.width;
849 d.height += padding.height;
850 *size = maxdim(*size, d);
851 break;
853 case WID_TD_LIST: {
854 Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
855 for (uint i = 0; i < this->towns.size(); i++) {
856 const Town *t = this->towns[i];
858 assert(t != nullptr);
860 SetDParam(0, t->index);
861 SetDParamMaxDigits(1, 8);
862 d = maxdim(d, GetStringBoundingBox(GetTownString(t)));
864 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
865 d.width += icon_size.width + 2;
866 d.height = std::max(d.height, icon_size.height);
867 resize->height = d.height;
868 d.height *= 5;
869 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
870 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
871 *size = maxdim(*size, d);
872 break;
874 case WID_TD_WORLD_POPULATION: {
875 SetDParamMaxDigits(0, 10);
876 Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
877 d.width += padding.width;
878 d.height += padding.height;
879 *size = maxdim(*size, d);
880 break;
885 void OnClick(Point pt, int widget, int click_count) override
887 switch (widget) {
888 case WID_TD_SORT_ORDER: // Click on sort order button
889 if (this->towns.SortType() != 2) { // A different sort than by rating.
890 this->towns.ToggleSortOrder();
891 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
892 } else {
893 /* Some parts are always sorted ascending on name. */
894 this->last_sorting.order = !this->last_sorting.order;
895 this->towns.SetListing(this->last_sorting);
896 this->towns.ForceResort();
897 this->towns.Sort();
899 this->SetDirty();
900 break;
902 case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown
903 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names, this->towns.SortType(), WID_TD_SORT_CRITERIA, 0, 0);
904 break;
906 case WID_TD_LIST: { // Click on Town Matrix
907 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
908 if (id_v >= this->towns.size()) return; // click out of town bounds
910 const Town *t = this->towns[id_v];
911 assert(t != nullptr);
912 if (_ctrl_pressed) {
913 ShowExtraViewportWindow(t->xy);
914 } else {
915 ScrollMainWindowToTile(t->xy);
917 break;
922 void OnDropdownSelect(int widget, int index) override
924 if (widget != WID_TD_SORT_CRITERIA) return;
926 if (this->towns.SortType() != index) {
927 this->towns.SetSortType(index);
928 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
929 this->BuildSortTownList();
933 void OnPaint() override
935 if (this->towns.NeedRebuild()) this->BuildSortTownList();
936 this->DrawWidgets();
939 void OnHundredthTick() override
941 this->BuildSortTownList();
942 this->SetDirty();
945 void OnResize() override
947 this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
950 void OnEditboxChanged(int wid) override
952 if (wid == WID_TD_FILTER) {
953 this->string_filter.SetFilterTerm(this->townname_editbox.text.buf);
954 this->InvalidateData(TDIWD_FORCE_REBUILD);
959 * Some data on this window has become invalid.
960 * @param data Information about the changed data.
961 * @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.
963 void OnInvalidateData(int data = 0, bool gui_scope = true) override
965 switch (data) {
966 case TDIWD_FORCE_REBUILD:
967 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
968 this->towns.ForceRebuild();
969 break;
971 case TDIWD_POPULATION_CHANGE:
972 if (this->towns.SortType() == 1) this->towns.ForceResort();
973 break;
975 default:
976 this->towns.ForceResort();
981 Listing TownDirectoryWindow::last_sorting = {false, 0};
983 /** Names of the sorting functions. */
984 const StringID TownDirectoryWindow::sorter_names[] = {
985 STR_SORT_BY_NAME,
986 STR_SORT_BY_POPULATION,
987 STR_SORT_BY_RATING,
988 INVALID_STRING_ID
991 /** Available town directory sorting functions. */
992 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
993 &TownNameSorter,
994 &TownPopulationSorter,
995 &TownRatingSorter,
998 static WindowDesc _town_directory_desc(
999 WDP_AUTO, "list_towns", 208, 202,
1000 WC_TOWN_DIRECTORY, WC_NONE,
1002 _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
1005 void ShowTownDirectory()
1007 if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
1008 new TownDirectoryWindow(&_town_directory_desc);
1011 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1013 if (result.Failed()) return;
1015 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
1016 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
1019 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1021 if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
1024 static const NWidgetPart _nested_found_town_widgets[] = {
1025 NWidget(NWID_HORIZONTAL),
1026 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1027 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1028 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1029 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
1030 EndContainer(),
1031 /* Construct new town(s) buttons. */
1032 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1033 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1034 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1035 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1036 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1037 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1038 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
1039 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
1040 /* Town name selection. */
1041 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
1042 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
1043 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
1044 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
1045 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
1046 /* Town size selection. */
1047 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1048 NWidget(NWID_SPACER), SetFill(1, 0),
1049 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
1050 NWidget(NWID_SPACER), SetFill(1, 0),
1051 EndContainer(),
1052 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1053 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
1054 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1055 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
1056 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1057 EndContainer(),
1058 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1059 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1060 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
1061 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1062 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
1063 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1064 EndContainer(),
1065 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1066 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1067 SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
1068 /* Town roads selection. */
1069 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1070 NWidget(NWID_SPACER), SetFill(1, 0),
1071 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
1072 NWidget(NWID_SPACER), SetFill(1, 0),
1073 EndContainer(),
1074 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1075 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),
1076 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),
1077 EndContainer(),
1078 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1079 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1080 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),
1081 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),
1082 EndContainer(),
1083 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1084 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1085 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
1086 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1087 EndContainer(),
1090 /** Found a town window class. */
1091 struct FoundTownWindow : Window {
1092 private:
1093 TownSize town_size; ///< Selected town size
1094 TownLayout town_layout; ///< Selected town layout
1095 bool city; ///< Are we building a city?
1096 QueryString townname_editbox; ///< Townname editbox
1097 bool townnamevalid; ///< Is generated town name valid?
1098 uint32 townnameparts; ///< Generated town name
1099 TownNameParams params; ///< Town name parameters
1101 public:
1102 FoundTownWindow(WindowDesc *desc, WindowNumber window_number) :
1103 Window(desc),
1104 town_size(TSZ_MEDIUM),
1105 town_layout(_settings_game.economy.town_layout),
1106 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
1107 params(_settings_game.game_creation.town_name)
1109 this->InitNested(window_number);
1110 this->querystrings[WID_TF_TOWN_NAME_EDITBOX] = &this->townname_editbox;
1111 this->RandomTownName();
1112 this->UpdateButtons(true);
1115 void RandomTownName()
1117 this->townnamevalid = GenerateTownName(&this->townnameparts);
1119 if (!this->townnamevalid) {
1120 this->townname_editbox.text.DeleteAll();
1121 } else {
1122 GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
1123 this->townname_editbox.text.UpdateSize();
1125 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
1127 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
1130 void UpdateButtons(bool check_availability)
1132 if (check_availability && _game_mode != GM_EDITOR) {
1133 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
1134 this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
1135 WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
1136 if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
1139 for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
1140 this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
1143 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1145 for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
1146 this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
1149 this->SetDirty();
1152 void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
1154 std::string name;
1156 if (!this->townnamevalid) {
1157 name = this->townname_editbox.text.buf;
1158 } else {
1159 /* If user changed the name, send it */
1160 char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
1161 GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
1162 if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
1165 bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
1166 townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
1168 /* Rerandomise name, if success and no cost-estimation. */
1169 if (success && !_shift_pressed) this->RandomTownName();
1172 void OnClick(Point pt, int widget, int click_count) override
1174 switch (widget) {
1175 case WID_TF_NEW_TOWN:
1176 HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
1177 break;
1179 case WID_TF_RANDOM_TOWN:
1180 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
1181 break;
1183 case WID_TF_TOWN_NAME_RANDOM:
1184 this->RandomTownName();
1185 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
1186 break;
1188 case WID_TF_MANY_RANDOM_TOWNS: {
1189 Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
1190 UpdateNearestTownForRoadTiles(true);
1191 if (!GenerateTowns(this->town_layout)) {
1192 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
1194 UpdateNearestTownForRoadTiles(false);
1195 old_generating_world.Restore();
1196 break;
1199 case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
1200 this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
1201 this->UpdateButtons(false);
1202 break;
1204 case WID_TF_CITY:
1205 this->city ^= true;
1206 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1207 this->SetDirty();
1208 break;
1210 case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
1211 case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
1212 this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
1213 this->UpdateButtons(false);
1214 break;
1218 void OnPlaceObject(Point pt, TileIndex tile) override
1220 this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
1223 void OnPlaceObjectAbort() override
1225 this->RaiseButtons();
1226 this->UpdateButtons(false);
1230 * Some data on this window has become invalid.
1231 * @param data Information about the changed data.
1232 * @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.
1234 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1236 if (!gui_scope) return;
1237 this->UpdateButtons(true);
1241 static WindowDesc _found_town_desc(
1242 WDP_AUTO, "build_town", 160, 162,
1243 WC_FOUND_TOWN, WC_NONE,
1244 WDF_CONSTRUCTION,
1245 _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
1248 void ShowFoundTownWindow()
1250 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
1251 AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
1254 void InitializeTownGui()
1256 _town_local_authority_kdtree.Clear();