Handle invalid strings from game scripts more leniently
[openttd/fttd.git] / src / town_gui.cpp
blob81b19b7244187a1767207e0ee76e5ba26a96e974
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file town_gui.cpp GUI for towns. */
12 #include "stdafx.h"
13 #include "town.h"
14 #include "map/zoneheight.h"
15 #include "viewport_func.h"
16 #include "error.h"
17 #include "gui.h"
18 #include "command_func.h"
19 #include "company_func.h"
20 #include "company_base.h"
21 #include "company_gui.h"
22 #include "network/network.h"
23 #include "string_func.h"
24 #include "strings_func.h"
25 #include "sound_func.h"
26 #include "tilehighlight_func.h"
27 #include "sortlist_type.h"
28 #include "road_cmd.h"
29 #include "landscape.h"
30 #include "querystring_gui.h"
31 #include "window_func.h"
32 #include "townname_func.h"
33 #include "core/geometry_func.hpp"
34 #include "genworld.h"
35 #include "widgets/dropdown_func.h"
36 #include "economy_func.h"
38 #include "widgets/town_widget.h"
40 #include "table/strings.h"
42 typedef GUIList<const Town*> GUITownList;
44 static const NWidgetPart _nested_town_authority_widgets[] = {
45 NWidget(NWID_HORIZONTAL),
46 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
47 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
48 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
49 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
50 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
51 EndContainer(),
52 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
53 NWidget(NWID_HORIZONTAL),
54 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(),
55 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TA_SCROLLBAR),
56 EndContainer(),
57 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
58 NWidget(NWID_HORIZONTAL),
59 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),
60 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
61 EndContainer()
64 /** Town authority window. */
65 struct TownAuthorityWindow : Window {
66 private:
67 Town *town; ///< Town being displayed.
68 int sel_index; ///< Currently selected town action, \c 0 to \c TACT_COUNT-1, \c -1 means no action selected.
69 Scrollbar *vscroll;
70 uint displayed_actions_on_previous_painting; ///< Actions that were available on the previous call to OnPaint()
72 /**
73 * Get the position of the Nth set bit.
75 * If there is no Nth bit set return -1
77 * @param bits The value to search in
78 * @param n The Nth set bit from which we want to know the position
79 * @return The position of the Nth set bit
81 static int GetNthSetBit(uint32 bits, int n)
83 if (n >= 0) {
84 uint i;
85 FOR_EACH_SET_BIT(i, bits) {
86 n--;
87 if (n < 0) return i;
90 return -1;
93 public:
94 TownAuthorityWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc), sel_index(-1), displayed_actions_on_previous_painting(0)
96 this->town = Town::Get(window_number);
97 this->InitNested(window_number);
98 this->vscroll = this->GetScrollbar(WID_TA_SCROLLBAR);
99 this->vscroll->SetCapacity((this->GetWidget<NWidgetBase>(WID_TA_COMMAND_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
102 virtual void OnPaint()
104 int numact;
105 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
106 if (buttons != displayed_actions_on_previous_painting) this->SetDirty();
107 displayed_actions_on_previous_painting = buttons;
109 this->vscroll->SetCount(numact + 1);
111 if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
112 this->sel_index = -1;
115 this->SetWidgetDisabledState(WID_TA_EXECUTE, this->sel_index == -1);
117 this->DrawWidgets();
118 if (!this->IsShaded()) this->DrawRatings();
121 /** Draw the contents of the ratings panel. May request a resize of the window if the contents does not fit. */
122 void DrawRatings()
124 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_RATING_INFO);
125 uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
126 uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
128 uint y = nwid->pos_y + WD_FRAMERECT_TOP;
130 DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
131 y += FONT_HEIGHT_NORMAL;
133 Dimension icon_size = GetSpriteSize(SPR_COMPANY_ICON);
134 int icon_width = icon_size.width;
135 int icon_y_offset = (FONT_HEIGHT_NORMAL - icon_size.height) / 2;
137 Dimension exclusive_size = GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT);
138 int exclusive_width = exclusive_size.width;
139 int exclusive_y_offset = (FONT_HEIGHT_NORMAL - exclusive_size.height) / 2;
141 bool rtl = _current_text_dir == TD_RTL;
142 uint text_left = left + (rtl ? 0 : icon_width + exclusive_width + 4);
143 uint text_right = right - (rtl ? icon_width + exclusive_width + 4 : 0);
144 uint icon_left = rtl ? right - icon_width : left;
145 uint exclusive_left = rtl ? right - icon_width - exclusive_width - 2 : left + icon_width + 2;
147 /* Draw list of companies */
148 const Company *c;
149 FOR_ALL_COMPANIES(c) {
150 if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
151 DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
153 SetDParam(0, c->index);
154 SetDParam(1, c->index);
156 int r = this->town->ratings[c->index];
157 StringID str;
158 (str = STR_CARGO_RATING_APPALLING, r <= RATING_APPALLING) || // Apalling
159 (str++, r <= RATING_VERYPOOR) || // Very Poor
160 (str++, r <= RATING_POOR) || // Poor
161 (str++, r <= RATING_MEDIOCRE) || // Mediocore
162 (str++, r <= RATING_GOOD) || // Good
163 (str++, r <= RATING_VERYGOOD) || // Very Good
164 (str++, r <= RATING_EXCELLENT) || // Excellent
165 (str++, true); // Outstanding
167 SetDParam(2, str);
168 if (this->town->exclusivity == c->index) {
169 DrawSprite(SPR_EXCLUSIVE_TRANSPORT, COMPANY_SPRITE_COLOUR(c->index), exclusive_left, y + exclusive_y_offset);
172 DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
173 y += FONT_HEIGHT_NORMAL;
177 y = y + WD_FRAMERECT_BOTTOM - nwid->pos_y; // Compute needed size of the widget.
178 if (y > nwid->current_y) {
179 /* If the company list is too big to fit, mark ourself dirty and draw again. */
180 ResizeWindow(this, 0, y - nwid->current_y);
184 virtual void SetStringParameters(int widget) const
186 if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
189 virtual void DrawWidget(const Rect &r, int widget) const
191 switch (widget) {
192 case WID_TA_ACTION_INFO:
193 if (this->sel_index != -1) {
194 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8);
195 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
196 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
198 break;
199 case WID_TA_COMMAND_LIST: {
200 int numact;
201 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
202 int y = r.top + WD_FRAMERECT_TOP;
203 int pos = this->vscroll->GetPosition();
205 if (--pos < 0) {
206 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
207 y += FONT_HEIGHT_NORMAL;
210 for (int i = 0; buttons; i++, buttons >>= 1) {
211 if (pos <= -5) break; ///< Draw only the 5 fitting lines
213 if ((buttons & 1) && --pos < 0) {
214 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y,
215 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, this->sel_index == i ? TC_WHITE : TC_ORANGE);
216 y += FONT_HEIGHT_NORMAL;
219 break;
224 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
226 switch (widget) {
227 case WID_TA_ACTION_INFO: {
228 assert(size->width > padding.width && size->height > padding.height);
229 size->width -= WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
230 size->height -= WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
231 Dimension d = {0, 0};
232 for (int i = 0; i < TACT_COUNT; i++) {
233 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
234 d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
236 *size = maxdim(*size, d);
237 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
238 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
239 break;
242 case WID_TA_COMMAND_LIST:
243 size->height = WD_FRAMERECT_TOP + 5 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
244 size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
245 for (uint i = 0; i < TACT_COUNT; i++ ) {
246 size->width = max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
248 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
249 break;
251 case WID_TA_RATING_INFO:
252 resize->height = FONT_HEIGHT_NORMAL;
253 size->height = WD_FRAMERECT_TOP + 9 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
254 break;
258 virtual void OnClick(Point pt, int widget, int click_count)
260 switch (widget) {
261 case WID_TA_COMMAND_LIST: {
262 int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL);
263 if (!IsInsideMM(y, 0, 5)) return;
265 y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_company, this->town), y + this->vscroll->GetPosition() - 1);
266 if (y >= 0) {
267 this->sel_index = y;
268 this->SetDirty();
270 /* FALL THROUGH, when double-clicking. */
271 if (click_count == 1 || y < 0) break;
274 case WID_TA_EXECUTE:
275 DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_ERROR_CAN_T_DO_THIS));
276 break;
280 virtual void OnHundredthTick()
282 this->SetDirty();
286 static WindowDesc _town_authority_desc(
287 WDP_AUTO, "view_town_authority", 317, 222,
288 WC_TOWN_AUTHORITY, WC_NONE,
290 _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
293 static void ShowTownAuthorityWindow(uint town)
295 AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
299 /* Town view window. */
300 struct TownViewWindow : Window {
301 private:
302 Town *town; ///< Town displayed by the window.
304 public:
305 static const int WID_TV_HEIGHT_NORMAL = 150;
307 TownViewWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
309 this->CreateNestedTree();
311 this->town = Town::Get(window_number);
312 if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
314 this->FinishInitNested(window_number);
316 this->flags |= WF_DISABLE_VP_SCROLL;
317 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
318 nvp->InitializeViewport(this, this->town->xy, ZOOM_LVL_NEWS);
320 /* disable renaming town in network games if you are not the server */
321 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME, _networking && !_network_server);
324 virtual void SetStringParameters(int widget) const
326 if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
329 virtual void DrawWidget(const Rect &r, int widget) const
331 if (widget != WID_TV_INFO) return;
333 uint y = r.top + WD_FRAMERECT_TOP;
335 SetDParam(0, this->town->cache.population);
336 SetDParam(1, this->town->cache.num_houses);
337 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
339 SetDParam(0, this->town->supplied[CT_PASSENGERS].old_act);
340 SetDParam(1, this->town->supplied[CT_PASSENGERS].old_max);
341 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_PASSENGERS_LAST_MONTH_MAX);
343 SetDParam(0, this->town->supplied[CT_MAIL].old_act);
344 SetDParam(1, this->town->supplied[CT_MAIL].old_max);
345 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_MAIL_LAST_MONTH_MAX);
347 bool first = true;
348 for (int i = TE_BEGIN; i < TE_END; i++) {
349 if (this->town->goal[i] == 0) continue;
350 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
351 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
353 if (first) {
354 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
355 first = false;
358 bool rtl = _current_text_dir == TD_RTL;
359 uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
360 uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
362 const CargoSpec *cargo = FindFirstCargoWithTownEffect((TownEffect)i);
363 assert(cargo != NULL);
365 StringID string;
367 if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
368 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
369 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
370 if (this->town->received[i].old_act == 0) {
371 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
373 if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
374 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
378 SetDParam(0, cargo->name);
379 } else {
380 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
381 if (this->town->received[i].old_act < this->town->goal[i]) {
382 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
385 SetDParam(0, cargo->Index());
386 SetDParam(1, this->town->received[i].old_act);
387 SetDParam(2, cargo->Index());
388 SetDParam(3, this->town->goal[i]);
390 DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
393 if (HasBit(this->town->flags, TOWN_IS_GROWING)) {
394 SetDParam(0, ((this->town->growth_rate & (~TOWN_GROW_RATE_CUSTOM)) * TOWN_GROWTH_TICKS + DAY_TICKS) / DAY_TICKS);
395 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);
396 } else {
397 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
400 /* only show the town noise, if the noise option is activated. */
401 if (_settings_game.economy.station_noise_level) {
402 SetDParam(0, this->town->noise_reached);
403 SetDParam(1, this->town->MaxTownNoise());
404 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
407 if (this->town->text != NULL) {
408 SetDParamStr(0, this->town->text);
409 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
413 virtual void OnClick(Point pt, int widget, int click_count)
415 switch (widget) {
416 case WID_TV_CENTER_VIEW: // scroll to location
417 if (_ctrl_pressed) {
418 ShowExtraViewPortWindow(this->town->xy);
419 } else {
420 ScrollMainWindowToTile(this->town->xy);
422 break;
424 case WID_TV_SHOW_AUTHORITY: // town authority
425 ShowTownAuthorityWindow(this->window_number);
426 break;
428 case WID_TV_CHANGE_NAME: // rename
429 SetDParam(0, this->window_number);
430 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);
431 break;
433 case WID_TV_EXPAND: { // expand town - only available on Scenario editor
434 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
435 static bool _warn_town_no_roads = false;
437 if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
438 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
439 _warn_town_no_roads = true;
442 DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
443 break;
446 case WID_TV_DELETE: // delete town - only available on Scenario editor
447 DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
448 break;
452 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
454 switch (widget) {
455 case WID_TV_INFO:
456 size->height = GetDesiredInfoHeight(size->width);
457 break;
462 * Gets the desired height for the information panel.
463 * @return the desired height in pixels.
465 uint GetDesiredInfoHeight(int width) const
467 uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
469 bool first = true;
470 for (int i = TE_BEGIN; i < TE_END; i++) {
471 if (this->town->goal[i] == 0) continue;
472 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
473 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
475 if (first) {
476 aimed_height += FONT_HEIGHT_NORMAL;
477 first = false;
479 aimed_height += FONT_HEIGHT_NORMAL;
481 aimed_height += FONT_HEIGHT_NORMAL;
483 if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
485 if (this->town->text != NULL) {
486 SetDParamStr(0, this->town->text);
487 aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width);
490 return aimed_height;
493 void ResizeWindowAsNeeded()
495 const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
496 uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
497 if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
498 this->ReInit();
502 virtual void OnResize()
504 if (this->viewport != NULL) {
505 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
506 nvp->UpdateViewportCoordinates(this);
508 ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
513 * Some data on this window has become invalid.
514 * @param data Information about the changed data.
515 * @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.
517 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
519 if (!gui_scope) return;
520 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
521 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
522 this->ResizeWindowAsNeeded();
525 virtual void OnQueryTextFinished(char *str)
527 if (str == NULL) return;
529 DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), NULL, str);
533 static const NWidgetPart _nested_town_game_view_widgets[] = {
534 NWidget(NWID_HORIZONTAL),
535 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
536 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
537 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
538 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
539 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
540 EndContainer(),
541 NWidget(WWT_PANEL, COLOUR_BROWN),
542 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
543 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
544 EndContainer(),
545 EndContainer(),
546 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
547 NWidget(NWID_HORIZONTAL),
548 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
549 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
550 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),
551 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
552 EndContainer(),
553 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
554 EndContainer(),
557 static WindowDesc _town_game_view_desc(
558 WDP_AUTO, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
559 WC_TOWN_VIEW, WC_NONE,
561 _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
564 static const NWidgetPart _nested_town_editor_view_widgets[] = {
565 NWidget(NWID_HORIZONTAL),
566 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
567 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
568 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
569 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
570 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
571 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
572 EndContainer(),
573 NWidget(WWT_PANEL, COLOUR_BROWN),
574 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
575 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
576 EndContainer(),
577 EndContainer(),
578 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
579 NWidget(NWID_HORIZONTAL),
580 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
581 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
582 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),
583 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),
584 EndContainer(),
585 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
586 EndContainer(),
589 static WindowDesc _town_editor_view_desc(
590 WDP_AUTO, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
591 WC_TOWN_VIEW, WC_NONE,
593 _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
596 void ShowTownViewWindow(TownID town)
598 if (_game_mode == GM_EDITOR) {
599 AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
600 } else {
601 AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
605 static const NWidgetPart _nested_town_directory_widgets[] = {
606 NWidget(NWID_HORIZONTAL),
607 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
608 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
609 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
610 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
611 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
612 EndContainer(),
613 NWidget(NWID_HORIZONTAL),
614 NWidget(NWID_VERTICAL),
615 NWidget(NWID_HORIZONTAL),
616 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
617 NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
618 NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
619 EndContainer(),
620 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
621 SetFill(1, 0), SetResize(0, 10), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
622 NWidget(WWT_PANEL, COLOUR_BROWN),
623 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),
624 EndContainer(),
625 EndContainer(),
626 NWidget(NWID_VERTICAL),
627 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
628 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
629 EndContainer(),
630 EndContainer(),
633 /** Town directory window class. */
634 struct TownDirectoryWindow : public Window {
635 private:
636 /* Runtime saved values */
637 static Listing last_sorting;
638 static const Town *last_town;
640 /* Constants for sorting towns */
641 static const StringID sorter_names[];
642 static GUITownList::SortFunction * const sorter_funcs[];
644 GUITownList towns;
646 Scrollbar *vscroll;
648 void BuildSortTownList()
650 if (this->towns.NeedRebuild()) {
651 this->towns.Clear();
653 const Town *t;
654 FOR_ALL_TOWNS(t) {
655 *this->towns.Append() = t;
658 this->towns.Compact();
659 this->towns.RebuildDone();
660 this->vscroll->SetCount(this->towns.Length()); // Update scrollbar as well.
662 /* Always sort the towns. */
663 this->last_town = NULL;
664 this->towns.Sort();
665 this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns.
668 /** Sort by town name */
669 static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
671 static char buf_cache[64];
672 const Town *ta = *a;
673 const Town *tb = *b;
674 char buf[64];
676 SetDParam(0, ta->index);
677 GetString(buf, STR_TOWN_NAME, lastof(buf));
679 /* If 'b' is the same town as in the last round, use the cached value
680 * We do this to speed stuff up ('b' is called with the same value a lot of
681 * times after each other) */
682 if (tb != last_town) {
683 last_town = tb;
684 SetDParam(0, tb->index);
685 GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
688 return strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
691 /** Sort by population (default descending, as big towns are of the most interest). */
692 static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
694 uint32 a_population = (*a)->cache.population;
695 uint32 b_population = (*b)->cache.population;
696 if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
697 return (a_population < b_population) ? -1 : 1;
700 /** Sort by town rating */
701 static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b)
703 int before = TownDirectoryWindow::last_sorting.order ? 1 : -1; // Value to get 'a' before 'b'.
705 /* Towns without rating are always after towns with rating. */
706 if (HasBit((*a)->have_ratings, _local_company)) {
707 if (HasBit((*b)->have_ratings, _local_company)) {
708 int16 a_rating = (*a)->ratings[_local_company];
709 int16 b_rating = (*b)->ratings[_local_company];
710 if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
711 return (a_rating < b_rating) ? -1 : 1;
713 return before;
715 if (HasBit((*b)->have_ratings, _local_company)) return -before;
716 return -before * TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name.
719 public:
720 TownDirectoryWindow(WindowDesc *desc) : Window(desc)
722 this->CreateNestedTree();
724 this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
726 this->towns.SetListing(this->last_sorting);
727 this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
728 this->towns.ForceRebuild();
729 this->BuildSortTownList();
731 this->FinishInitNested(0);
734 virtual void SetStringParameters(int widget) const
736 switch (widget) {
737 case WID_TD_WORLD_POPULATION:
738 SetDParam(0, GetWorldPopulation());
739 break;
741 case WID_TD_SORT_CRITERIA:
742 SetDParam(0, TownDirectoryWindow::sorter_names[this->towns.SortType()]);
743 break;
747 virtual void DrawWidget(const Rect &r, int widget) const
749 switch (widget) {
750 case WID_TD_SORT_ORDER:
751 this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
752 break;
754 case WID_TD_LIST: {
755 int n = 0;
756 int y = r.top + WD_FRAMERECT_TOP;
757 if (this->towns.Length() == 0) { // No towns available.
758 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
759 break;
762 /* At least one town available. */
763 bool rtl = _current_text_dir == TD_RTL;
764 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
765 int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
766 int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
767 int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
769 for (uint i = this->vscroll->GetPosition(); i < this->towns.Length(); i++) {
770 const Town *t = this->towns[i];
771 assert(t->xy != INVALID_TILE);
773 /* Draw rating icon. */
774 if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
775 DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
776 } else {
777 SpriteID icon = SPR_TOWN_RATING_APALLING;
778 if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
779 if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
780 DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
783 SetDParam(0, t->index);
784 SetDParam(1, t->cache.population);
785 DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, STR_TOWN_DIRECTORY_TOWN);
787 y += this->resize.step_height;
788 if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
790 break;
795 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
797 switch (widget) {
798 case WID_TD_SORT_ORDER: {
799 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
800 d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
801 d.height += padding.height;
802 *size = maxdim(*size, d);
803 break;
805 case WID_TD_SORT_CRITERIA: {
806 Dimension d = {0, 0};
807 for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
808 d = maxdim(d, GetStringBoundingBox(TownDirectoryWindow::sorter_names[i]));
810 d.width += padding.width;
811 d.height += padding.height;
812 *size = maxdim(*size, d);
813 break;
815 case WID_TD_LIST: {
816 Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
817 for (uint i = 0; i < this->towns.Length(); i++) {
818 const Town *t = this->towns[i];
820 assert(t != NULL);
822 SetDParam(0, t->index);
823 SetDParamMaxDigits(1, 8);
824 d = maxdim(d, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN));
826 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
827 d.width += icon_size.width + 2;
828 d.height = max(d.height, icon_size.height);
829 resize->height = d.height;
830 d.height *= 5;
831 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
832 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
833 *size = maxdim(*size, d);
834 break;
836 case WID_TD_WORLD_POPULATION: {
837 SetDParamMaxDigits(0, 10);
838 Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
839 d.width += padding.width;
840 d.height += padding.height;
841 *size = maxdim(*size, d);
842 break;
847 virtual void OnClick(Point pt, int widget, int click_count)
849 switch (widget) {
850 case WID_TD_SORT_ORDER: // Click on sort order button
851 if (this->towns.SortType() != 2) { // A different sort than by rating.
852 this->towns.ToggleSortOrder();
853 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
854 } else {
855 /* Some parts are always sorted ascending on name. */
856 this->last_sorting.order = !this->last_sorting.order;
857 this->towns.SetListing(this->last_sorting);
858 this->towns.ForceResort();
859 this->towns.Sort();
861 this->SetDirty();
862 break;
864 case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown
865 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names, this->towns.SortType(), WID_TD_SORT_CRITERIA, 0, 0);
866 break;
868 case WID_TD_LIST: { // Click on Town Matrix
869 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
870 if (id_v >= this->towns.Length()) return; // click out of town bounds
872 const Town *t = this->towns[id_v];
873 assert(t != NULL);
874 if (_ctrl_pressed) {
875 ShowExtraViewPortWindow(t->xy);
876 } else {
877 ScrollMainWindowToTile(t->xy);
879 break;
884 virtual void OnDropdownSelect(int widget, int index)
886 if (widget != WID_TD_SORT_CRITERIA) return;
888 if (this->towns.SortType() != index) {
889 this->towns.SetSortType(index);
890 this->last_sorting = this->towns.GetListing(); // Store new sorting order.
891 this->BuildSortTownList();
895 virtual void OnPaint()
897 if (this->towns.NeedRebuild()) this->BuildSortTownList();
898 this->DrawWidgets();
901 virtual void OnHundredthTick()
903 this->BuildSortTownList();
904 this->SetDirty();
907 virtual void OnResize()
909 this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
913 * Some data on this window has become invalid.
914 * @param data Information about the changed data.
915 * @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.
917 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
919 if (data == 0) {
920 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
921 this->towns.ForceRebuild();
922 } else {
923 this->towns.ForceResort();
928 Listing TownDirectoryWindow::last_sorting = {false, 0};
929 const Town *TownDirectoryWindow::last_town = NULL;
931 /** Names of the sorting functions. */
932 const StringID TownDirectoryWindow::sorter_names[] = {
933 STR_SORT_BY_NAME,
934 STR_SORT_BY_POPULATION,
935 STR_SORT_BY_RATING,
936 INVALID_STRING_ID
939 /** Available town directory sorting functions. */
940 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
941 &TownNameSorter,
942 &TownPopulationSorter,
943 &TownRatingSorter,
946 static WindowDesc _town_directory_desc(
947 WDP_AUTO, "list_towns", 208, 202,
948 WC_TOWN_DIRECTORY, WC_NONE,
950 _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
953 void ShowTownDirectory()
955 if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
956 new TownDirectoryWindow(&_town_directory_desc);
959 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
961 if (result.Failed()) return;
963 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT, tile);
964 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
967 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
969 if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
972 static const NWidgetPart _nested_found_town_widgets[] = {
973 NWidget(NWID_HORIZONTAL),
974 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
975 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
976 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
977 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
978 EndContainer(),
979 /* Construct new town(s) buttons. */
980 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
981 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
982 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
983 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
984 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
985 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
986 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
987 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
988 /* Town name selection. */
989 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
990 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
991 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
992 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
993 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
994 /* Town size selection. */
995 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
996 NWidget(NWID_SPACER), SetFill(1, 0),
997 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
998 NWidget(NWID_SPACER), SetFill(1, 0),
999 EndContainer(),
1000 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1001 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
1002 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1003 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
1004 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1005 EndContainer(),
1006 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1007 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1008 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
1009 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1010 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
1011 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1012 EndContainer(),
1013 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
1014 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1015 SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
1016 /* Town roads selection. */
1017 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1018 NWidget(NWID_SPACER), SetFill(1, 0),
1019 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
1020 NWidget(NWID_SPACER), SetFill(1, 0),
1021 EndContainer(),
1022 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1023 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),
1024 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),
1025 EndContainer(),
1026 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1027 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
1028 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),
1029 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),
1030 EndContainer(),
1031 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
1032 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1033 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
1034 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
1035 EndContainer(),
1038 /** Found a town window class. */
1039 struct FoundTownWindow : Window {
1040 private:
1041 TownSize town_size; ///< Selected town size
1042 TownLayout town_layout; ///< Selected town layout
1043 bool city; ///< Are we building a city?
1044 QueryString townname_editbox; ///< Townname editbox
1045 bool townnamevalid; ///< Is generated town name valid?
1046 uint32 townnameparts; ///< Generated town name
1047 TownNameParams params; ///< Town name parameters
1049 public:
1050 FoundTownWindow(WindowDesc *desc, WindowNumber window_number) :
1051 Window(desc),
1052 town_size(TSZ_MEDIUM),
1053 town_layout(_settings_game.economy.town_layout),
1054 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
1055 params(_settings_game.game_creation.town_name)
1057 this->InitNested(window_number);
1058 this->querystrings[WID_TF_TOWN_NAME_EDITBOX] = &this->townname_editbox;
1059 this->RandomTownName();
1060 this->UpdateButtons(true);
1063 void RandomTownName()
1065 this->townnamevalid = GenerateTownName(&this->townnameparts);
1067 if (!this->townnamevalid) {
1068 this->townname_editbox.text.DeleteAll();
1069 } else {
1070 GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
1071 this->townname_editbox.text.UpdateSize();
1073 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
1075 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
1078 void UpdateButtons(bool check_availability)
1080 if (check_availability && _game_mode != GM_EDITOR) {
1081 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
1082 this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
1083 WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
1084 if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
1087 for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
1088 this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
1091 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1093 for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
1094 this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
1097 this->SetDirty();
1100 void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
1102 const char *name = NULL;
1104 if (!this->townnamevalid) {
1105 name = this->townname_editbox.text.buf;
1106 } else {
1107 /* If user changed the name, send it */
1108 char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
1109 GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
1110 if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
1113 bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
1114 townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
1116 if (success) this->RandomTownName();
1119 virtual void OnClick(Point pt, int widget, int click_count)
1121 switch (widget) {
1122 case WID_TF_NEW_TOWN:
1123 HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
1124 break;
1126 case WID_TF_RANDOM_TOWN:
1127 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
1128 break;
1130 case WID_TF_TOWN_NAME_RANDOM:
1131 this->RandomTownName();
1132 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
1133 break;
1135 case WID_TF_MANY_RANDOM_TOWNS:
1136 _generating_world = true;
1137 UpdateNearestTownForRoadTiles(true);
1138 if (!GenerateTowns(this->town_layout)) {
1139 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
1141 UpdateNearestTownForRoadTiles(false);
1142 _generating_world = false;
1143 break;
1145 case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
1146 this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
1147 this->UpdateButtons(false);
1148 break;
1150 case WID_TF_CITY:
1151 this->city ^= true;
1152 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1153 this->SetDirty();
1154 break;
1156 case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
1157 case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
1158 this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
1159 this->UpdateButtons(false);
1160 break;
1164 virtual void OnPlaceObject(Point pt, TileIndex tile)
1166 this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
1169 virtual void OnPlaceObjectAbort()
1171 this->RaiseButtons();
1172 this->UpdateButtons(false);
1176 * Some data on this window has become invalid.
1177 * @param data Information about the changed data.
1178 * @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.
1180 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1182 if (!gui_scope) return;
1183 this->UpdateButtons(true);
1187 static WindowDesc _found_town_desc(
1188 WDP_AUTO, "build_town", 160, 162,
1189 WC_FOUND_TOWN, WC_NONE,
1190 WDF_CONSTRUCTION,
1191 _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
1194 void ShowFoundTownWindow()
1196 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
1197 AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);