OpenTTD
town_gui.cpp
Go to the documentation of this file.
1 /* $Id: town_gui.cpp 27893 2017-08-13 18:38:42Z frosch $ */
2 
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  */
9 
12 #include "stdafx.h"
13 #include "town.h"
14 #include "viewport_func.h"
15 #include "error.h"
16 #include "gui.h"
17 #include "command_func.h"
18 #include "company_func.h"
19 #include "company_base.h"
20 #include "company_gui.h"
21 #include "network/network.h"
22 #include "string_func.h"
23 #include "strings_func.h"
24 #include "sound_func.h"
25 #include "tilehighlight_func.h"
26 #include "sortlist_type.h"
27 #include "road_cmd.h"
28 #include "landscape.h"
29 #include "querystring_gui.h"
30 #include "window_func.h"
31 #include "townname_func.h"
32 #include "core/geometry_func.hpp"
33 #include "genworld.h"
34 #include "widgets/dropdown_func.h"
35 
36 #include "widgets/town_widget.h"
37 
38 #include "table/strings.h"
39 
40 #include "safeguards.h"
41 
43 
44 static const NWidgetPart _nested_town_authority_widgets[] = {
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(),
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(),
56  EndContainer(),
57  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
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()
62 };
63 
66 private:
68  int sel_index;
69  Scrollbar *vscroll;
71 
81  static int GetNthSetBit(uint32 bits, int n)
82  {
83  if (n >= 0) {
84  uint i;
85  FOR_EACH_SET_BIT(i, bits) {
86  n--;
87  if (n < 0) return i;
88  }
89  }
90  return -1;
91  }
92 
93 public:
95  {
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);
100  }
101 
102  virtual void OnPaint()
103  {
104  int numact;
105  uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
106  if (buttons != displayed_actions_on_previous_painting) this->SetDirty();
108 
109  this->vscroll->SetCount(numact + 1);
110 
111  if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
112  this->sel_index = -1;
113  }
114 
115  this->SetWidgetDisabledState(WID_TA_EXECUTE, this->sel_index == -1);
116 
117  this->DrawWidgets();
118  if (!this->IsShaded()) this->DrawRatings();
119  }
120 
122  void DrawRatings()
123  {
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;
127 
128  uint y = nwid->pos_y + WD_FRAMERECT_TOP;
129 
130  DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
131  y += FONT_HEIGHT_NORMAL;
132 
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;
136 
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;
140 
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;
146 
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);
152 
153  SetDParam(0, c->index);
154  SetDParam(1, c->index);
155 
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
166 
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);
170  }
171 
172  DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
173  y += FONT_HEIGHT_NORMAL;
174  }
175  }
176 
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, false);
181  }
182  }
183 
184  virtual void SetStringParameters(int widget) const
185  {
186  if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
187  }
188 
189  virtual void DrawWidget(const Rect &r, int widget) const
190  {
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);
196  STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
197  }
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();
204 
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;
208  }
209 
210  for (int i = 0; buttons; i++, buttons >>= 1) {
211  if (pos <= -5) break;
212 
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;
217  }
218  }
219  break;
220  }
221  }
222  }
223 
224  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
225  {
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));
235  }
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;
240  }
241 
242  case WID_TA_COMMAND_LIST:
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);
247  }
248  size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
249  break;
250 
251  case WID_TA_RATING_INFO:
252  resize->height = FONT_HEIGHT_NORMAL;
254  break;
255  }
256  }
257 
258  virtual void OnClick(Point pt, int widget, int click_count)
259  {
260  switch (widget) {
261  case WID_TA_COMMAND_LIST: {
263  if (!IsInsideMM(y, 0, 5)) return;
264 
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();
269  }
270  /* When double-clicking, continue */
271  if (click_count == 1 || y < 0) break;
272  }
273  FALLTHROUGH;
274 
275  case WID_TA_EXECUTE:
276  DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_ERROR_CAN_T_DO_THIS));
277  break;
278  }
279  }
280 
281  virtual void OnHundredthTick()
282  {
283  this->SetDirty();
284  }
285 };
286 
287 static WindowDesc _town_authority_desc(
288  WDP_AUTO, "view_town_authority", 317, 222,
290  0,
291  _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
292 );
293 
294 static void ShowTownAuthorityWindow(uint town)
295 {
296  AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
297 }
298 
299 
300 /* Town view window. */
302 private:
304 
305 public:
306  static const int WID_TV_HEIGHT_NORMAL = 150;
307 
308  TownViewWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
309  {
310  this->CreateNestedTree();
311 
312  this->town = Town::Get(window_number);
313  if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
314 
315  this->FinishInitNested(window_number);
316 
317  this->flags |= WF_DISABLE_VP_SCROLL;
318  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
319  nvp->InitializeViewport(this, this->town->xy, ZOOM_LVL_NEWS);
320 
321  /* disable renaming town in network games if you are not the server */
323  }
324 
325  virtual void SetStringParameters(int widget) const
326  {
327  if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
328  }
329 
330  virtual void DrawWidget(const Rect &r, int widget) const
331  {
332  if (widget != WID_TV_INFO) return;
333 
334  uint y = r.top + WD_FRAMERECT_TOP;
335 
336  SetDParam(0, this->town->cache.population);
337  SetDParam(1, this->town->cache.num_houses);
338  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
339 
340  SetDParam(0, this->town->supplied[CT_PASSENGERS].old_act);
341  SetDParam(1, this->town->supplied[CT_PASSENGERS].old_max);
342  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_PASSENGERS_LAST_MONTH_MAX);
343 
344  SetDParam(0, this->town->supplied[CT_MAIL].old_act);
345  SetDParam(1, this->town->supplied[CT_MAIL].old_max);
346  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_MAIL_LAST_MONTH_MAX);
347 
348  bool first = true;
349  for (int i = TE_BEGIN; i < TE_END; i++) {
350  if (this->town->goal[i] == 0) continue;
351  if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
352  if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
353 
354  if (first) {
355  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
356  first = false;
357  }
358 
359  bool rtl = _current_text_dir == TD_RTL;
360  uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
361  uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
362 
364  assert(cargo != NULL);
365 
366  StringID string;
367 
368  if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
369  /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
370  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
371  if (this->town->received[i].old_act == 0) {
372  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
373 
374  if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
375  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
376  }
377  }
378 
379  SetDParam(0, cargo->name);
380  } else {
381  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
382  if (this->town->received[i].old_act < this->town->goal[i]) {
383  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
384  }
385 
386  SetDParam(0, cargo->Index());
387  SetDParam(1, this->town->received[i].old_act);
388  SetDParam(2, cargo->Index());
389  SetDParam(3, this->town->goal[i]);
390  }
391  DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
392  }
393 
394  if (HasBit(this->town->flags, TOWN_IS_GROWING)) {
396  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);
397  } else {
398  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
399  }
400 
401  /* only show the town noise, if the noise option is activated. */
403  SetDParam(0, this->town->noise_reached);
404  SetDParam(1, this->town->MaxTownNoise());
405  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
406  }
407 
408  if (this->town->text != NULL) {
409  SetDParamStr(0, this->town->text);
410  DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
411  }
412  }
413 
414  virtual void OnClick(Point pt, int widget, int click_count)
415  {
416  switch (widget) {
417  case WID_TV_CENTER_VIEW: // scroll to location
418  if (_ctrl_pressed) {
420  } else {
422  }
423  break;
424 
425  case WID_TV_SHOW_AUTHORITY: // town authority
426  ShowTownAuthorityWindow(this->window_number);
427  break;
428 
429  case WID_TV_CHANGE_NAME: // rename
430  SetDParam(0, this->window_number);
431  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);
432  break;
433 
434  case WID_TV_EXPAND: { // expand town - only available on Scenario editor
435  /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
436  static bool _warn_town_no_roads = false;
437 
438  if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
439  ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
440  _warn_town_no_roads = true;
441  }
442 
443  DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
444  break;
445  }
446 
447  case WID_TV_DELETE: // delete town - only available on Scenario editor
448  DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
449  break;
450  }
451  }
452 
453  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
454  {
455  switch (widget) {
456  case WID_TV_INFO:
457  size->height = GetDesiredInfoHeight(size->width);
458  break;
459  }
460  }
461 
466  uint GetDesiredInfoHeight(int width) const
467  {
468  uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
469 
470  bool first = true;
471  for (int i = TE_BEGIN; i < TE_END; i++) {
472  if (this->town->goal[i] == 0) continue;
473  if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
474  if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
475 
476  if (first) {
477  aimed_height += FONT_HEIGHT_NORMAL;
478  first = false;
479  }
480  aimed_height += FONT_HEIGHT_NORMAL;
481  }
482  aimed_height += FONT_HEIGHT_NORMAL;
483 
485 
486  if (this->town->text != NULL) {
487  SetDParamStr(0, this->town->text);
488  aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT);
489  }
490 
491  return aimed_height;
492  }
493 
494  void ResizeWindowAsNeeded()
495  {
496  const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
497  uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
498  if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
499  this->ReInit();
500  }
501  }
502 
503  virtual void OnResize()
504  {
505  if (this->viewport != NULL) {
506  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
507  nvp->UpdateViewportCoordinates(this);
508 
509  ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
510  }
511  }
512 
518  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
519  {
520  if (!gui_scope) return;
521  /* Called when setting station noise or required cargoes have changed, in order to resize the window */
522  this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
523  this->ResizeWindowAsNeeded();
524  }
525 
526  virtual void OnQueryTextFinished(char *str)
527  {
528  if (str == NULL) return;
529 
530  DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), NULL, str);
531  }
532 };
533 
534 static const NWidgetPart _nested_town_game_view_widgets[] = {
536  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
537  NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
538  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
539  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
540  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
541  EndContainer(),
542  NWidget(WWT_PANEL, COLOUR_BROWN),
543  NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
544  NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
545  EndContainer(),
546  EndContainer(),
547  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
550  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),
551  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),
552  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),
553  EndContainer(),
554  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
555  EndContainer(),
556 };
557 
558 static WindowDesc _town_game_view_desc(
559  WDP_AUTO, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
561  0,
562  _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
563 );
564 
565 static const NWidgetPart _nested_town_editor_view_widgets[] = {
567  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
568  NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
569  NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
570  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
571  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
572  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
573  EndContainer(),
574  NWidget(WWT_PANEL, COLOUR_BROWN),
575  NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
576  NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
577  EndContainer(),
578  EndContainer(),
579  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
582  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),
583  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),
584  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),
585  EndContainer(),
586  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
587  EndContainer(),
588 };
589 
590 static WindowDesc _town_editor_view_desc(
591  WDP_AUTO, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
593  0,
594  _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
595 );
596 
597 void ShowTownViewWindow(TownID town)
598 {
599  if (_game_mode == GM_EDITOR) {
600  AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
601  } else {
602  AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
603  }
604 }
605 
606 static const NWidgetPart _nested_town_directory_widgets[] = {
608  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
609  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
610  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
611  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
612  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
613  EndContainer(),
617  NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
618  NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
619  NWidget(WWT_PANEL, COLOUR_BROWN), SetResize(1, 0), EndContainer(),
620  EndContainer(),
621  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
623  NWidget(WWT_PANEL, COLOUR_BROWN),
624  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),
625  EndContainer(),
626  EndContainer(),
628  NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
629  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
630  EndContainer(),
631  EndContainer(),
632 };
633 
635 struct TownDirectoryWindow : public Window {
636 private:
637  /* Runtime saved values */
638  static Listing last_sorting;
639  static const Town *last_town;
640 
641  /* Constants for sorting towns */
642  static const StringID sorter_names[];
644 
645  GUITownList towns;
646 
647  Scrollbar *vscroll;
648 
649  void BuildSortTownList()
650  {
651  if (this->towns.NeedRebuild()) {
652  this->towns.Clear();
653 
654  const Town *t;
655  FOR_ALL_TOWNS(t) {
656  *this->towns.Append() = t;
657  }
658 
659  this->towns.Compact();
660  this->towns.RebuildDone();
661  this->vscroll->SetCount(this->towns.Length()); // Update scrollbar as well.
662  }
663  /* Always sort the towns. */
664  this->last_town = NULL;
665  this->towns.Sort();
666  this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns.
667  }
668 
670  static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
671  {
672  static char buf_cache[64];
673  const Town *ta = *a;
674  const Town *tb = *b;
675  char buf[64];
676 
677  SetDParam(0, ta->index);
678  GetString(buf, STR_TOWN_NAME, lastof(buf));
679 
680  /* If 'b' is the same town as in the last round, use the cached value
681  * We do this to speed stuff up ('b' is called with the same value a lot of
682  * times after each other) */
683  if (tb != last_town) {
684  last_town = tb;
685  SetDParam(0, tb->index);
686  GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
687  }
688 
689  return strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
690  }
691 
693  static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
694  {
695  uint32 a_population = (*a)->cache.population;
696  uint32 b_population = (*b)->cache.population;
697  if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
698  return (a_population < b_population) ? -1 : 1;
699  }
700 
702  static int CDECL TownRatingSorter(const Town * const *a, const Town * const *b)
703  {
704  int before = TownDirectoryWindow::last_sorting.order ? 1 : -1; // Value to get 'a' before 'b'.
705 
706  /* Towns without rating are always after towns with rating. */
707  if (HasBit((*a)->have_ratings, _local_company)) {
708  if (HasBit((*b)->have_ratings, _local_company)) {
709  int16 a_rating = (*a)->ratings[_local_company];
710  int16 b_rating = (*b)->ratings[_local_company];
711  if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
712  return (a_rating < b_rating) ? -1 : 1;
713  }
714  return before;
715  }
716  if (HasBit((*b)->have_ratings, _local_company)) return -before;
717  return -before * TownDirectoryWindow::TownNameSorter(a, b); // Sort unrated towns always on ascending town name.
718  }
719 
720 public:
721  TownDirectoryWindow(WindowDesc *desc) : Window(desc)
722  {
723  this->CreateNestedTree();
724 
725  this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
726 
727  this->towns.SetListing(this->last_sorting);
729  this->towns.ForceRebuild();
730  this->BuildSortTownList();
731 
732  this->FinishInitNested(0);
733  }
734 
735  virtual void SetStringParameters(int widget) const
736  {
737  switch (widget) {
740  break;
741 
744  break;
745  }
746  }
747 
748  virtual void DrawWidget(const Rect &r, int widget) const
749  {
750  switch (widget) {
751  case WID_TD_SORT_ORDER:
752  this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
753  break;
754 
755  case WID_TD_LIST: {
756  int n = 0;
757  int y = r.top + WD_FRAMERECT_TOP;
758  if (this->towns.Length() == 0) { // No towns available.
759  DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
760  break;
761  }
762 
763  /* At least one town available. */
764  bool rtl = _current_text_dir == TD_RTL;
765  Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
766  int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
767  int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
768  int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
769 
770  for (uint i = this->vscroll->GetPosition(); i < this->towns.Length(); i++) {
771  const Town *t = this->towns[i];
772  assert(t->xy != INVALID_TILE);
773 
774  /* Draw rating icon. */
775  if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
776  DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
777  } else {
778  SpriteID icon = SPR_TOWN_RATING_APALLING;
779  if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
780  if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
781  DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
782  }
783 
784  SetDParam(0, t->index);
785  SetDParam(1, t->cache.population);
786  DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, STR_TOWN_DIRECTORY_TOWN);
787 
788  y += this->resize.step_height;
789  if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
790  }
791  break;
792  }
793  }
794  }
795 
796  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
797  {
798  switch (widget) {
799  case WID_TD_SORT_ORDER: {
800  Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
801  d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
802  d.height += padding.height;
803  *size = maxdim(*size, d);
804  break;
805  }
806  case WID_TD_SORT_CRITERIA: {
807  Dimension d = {0, 0};
808  for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
810  }
811  d.width += padding.width;
812  d.height += padding.height;
813  *size = maxdim(*size, d);
814  break;
815  }
816  case WID_TD_LIST: {
817  Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
818  for (uint i = 0; i < this->towns.Length(); i++) {
819  const Town *t = this->towns[i];
820 
821  assert(t != NULL);
822 
823  SetDParam(0, t->index);
824  SetDParamMaxDigits(1, 8);
825  d = maxdim(d, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN));
826  }
827  Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
828  d.width += icon_size.width + 2;
829  d.height = max(d.height, icon_size.height);
830  resize->height = d.height;
831  d.height *= 5;
832  d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
833  d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
834  *size = maxdim(*size, d);
835  break;
836  }
838  SetDParamMaxDigits(0, 10);
839  Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
840  d.width += padding.width;
841  d.height += padding.height;
842  *size = maxdim(*size, d);
843  break;
844  }
845  }
846  }
847 
848  virtual void OnClick(Point pt, int widget, int click_count)
849  {
850  switch (widget) {
851  case WID_TD_SORT_ORDER: // Click on sort order button
852  if (this->towns.SortType() != 2) { // A different sort than by rating.
853  this->towns.ToggleSortOrder();
854  this->last_sorting = this->towns.GetListing(); // Store new sorting order.
855  } else {
856  /* Some parts are always sorted ascending on name. */
857  this->last_sorting.order = !this->last_sorting.order;
858  this->towns.SetListing(this->last_sorting);
859  this->towns.ForceResort();
860  this->towns.Sort();
861  }
862  this->SetDirty();
863  break;
864 
865  case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown
867  break;
868 
869  case WID_TD_LIST: { // Click on Town Matrix
870  uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
871  if (id_v >= this->towns.Length()) return; // click out of town bounds
872 
873  const Town *t = this->towns[id_v];
874  assert(t != NULL);
875  if (_ctrl_pressed) {
877  } else {
879  }
880  break;
881  }
882  }
883  }
884 
885  virtual void OnDropdownSelect(int widget, int index)
886  {
887  if (widget != WID_TD_SORT_CRITERIA) return;
888 
889  if (this->towns.SortType() != index) {
890  this->towns.SetSortType(index);
891  this->last_sorting = this->towns.GetListing(); // Store new sorting order.
892  this->BuildSortTownList();
893  }
894  }
895 
896  virtual void OnPaint()
897  {
898  if (this->towns.NeedRebuild()) this->BuildSortTownList();
899  this->DrawWidgets();
900  }
901 
902  virtual void OnHundredthTick()
903  {
904  this->BuildSortTownList();
905  this->SetDirty();
906  }
907 
908  virtual void OnResize()
909  {
910  this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
911  }
912 
918  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
919  {
920  if (data == 0) {
921  /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
922  this->towns.ForceRebuild();
923  } else {
924  this->towns.ForceResort();
925  }
926  }
927 };
928 
929 Listing TownDirectoryWindow::last_sorting = {false, 0};
930 const Town *TownDirectoryWindow::last_town = NULL;
931 
934  STR_SORT_BY_NAME,
935  STR_SORT_BY_POPULATION,
936  STR_SORT_BY_RATING,
938 };
939 
942  &TownNameSorter,
943  &TownPopulationSorter,
944  &TownRatingSorter,
945 };
946 
947 static WindowDesc _town_directory_desc(
948  WDP_AUTO, "list_towns", 208, 202,
950  0,
951  _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
952 );
953 
954 void ShowTownDirectory()
955 {
957  new TownDirectoryWindow(&_town_directory_desc);
958 }
959 
960 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
961 {
962  if (result.Failed()) return;
963 
964  if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
966 }
967 
968 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
969 {
970  if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
971 }
972 
973 static const NWidgetPart _nested_found_town_widgets[] = {
975  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
976  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
977  NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
978  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
979  EndContainer(),
980  /* Construct new town(s) buttons. */
981  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
983  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
984  SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
985  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
986  SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
987  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
988  SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
989  /* Town name selection. */
990  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
991  NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
992  SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
993  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
994  SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
995  /* Town size selection. */
996  NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
997  NWidget(NWID_SPACER), SetFill(1, 0),
998  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
999  NWidget(NWID_SPACER), SetFill(1, 0),
1000  EndContainer(),
1002  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
1003  SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1004  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
1005  SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1006  EndContainer(),
1009  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
1010  SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1011  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
1012  SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1013  EndContainer(),
1015  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1016  SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
1017  /* Town roads selection. */
1018  NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1019  NWidget(NWID_SPACER), SetFill(1, 0),
1020  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
1021  NWidget(NWID_SPACER), SetFill(1, 0),
1022  EndContainer(),
1024  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),
1025  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),
1026  EndContainer(),
1029  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),
1030  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),
1031  EndContainer(),
1033  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1034  SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
1036  EndContainer(),
1037 };
1038 
1041 private:
1044  bool city;
1047  uint32 townnameparts;
1049 
1050 public:
1051  FoundTownWindow(WindowDesc *desc, WindowNumber window_number) :
1052  Window(desc),
1056  params(_settings_game.game_creation.town_name)
1057  {
1058  this->InitNested(window_number);
1060  this->RandomTownName();
1061  this->UpdateButtons(true);
1062  }
1063 
1064  void RandomTownName()
1065  {
1067 
1068  if (!this->townnamevalid) {
1069  this->townname_editbox.text.DeleteAll();
1070  } else {
1071  GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
1072  this->townname_editbox.text.UpdateSize();
1073  }
1075 
1077  }
1078 
1079  void UpdateButtons(bool check_availability)
1080  {
1081  if (check_availability && _game_mode != GM_EDITOR) {
1086  }
1087 
1088  for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
1089  this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
1090  }
1091 
1092  this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1093 
1094  for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
1096  }
1097 
1098  this->SetDirty();
1099  }
1100 
1101  void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
1102  {
1103  const char *name = NULL;
1104 
1105  if (!this->townnamevalid) {
1106  name = this->townname_editbox.text.buf;
1107  } else {
1108  /* If user changed the name, send it */
1110  GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
1111  if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
1112  }
1113 
1114  bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
1115  townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
1116 
1117  /* Rerandomise name, if success and no cost-estimation. */
1118  if (success && !_shift_pressed) this->RandomTownName();
1119  }
1120 
1121  virtual void OnClick(Point pt, int widget, int click_count)
1122  {
1123  switch (widget) {
1124  case WID_TF_NEW_TOWN:
1125  HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
1126  break;
1127 
1128  case WID_TF_RANDOM_TOWN:
1129  this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
1130  break;
1131 
1133  this->RandomTownName();
1135  break;
1136 
1138  _generating_world = true;
1140  if (!GenerateTowns(this->town_layout)) {
1141  ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
1142  }
1144  _generating_world = false;
1145  break;
1146 
1148  this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
1149  this->UpdateButtons(false);
1150  break;
1151 
1152  case WID_TF_CITY:
1153  this->city ^= true;
1154  this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1155  this->SetDirty();
1156  break;
1157 
1160  this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
1161  this->UpdateButtons(false);
1162  break;
1163  }
1164  }
1165 
1166  virtual void OnPlaceObject(Point pt, TileIndex tile)
1167  {
1168  this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
1169  }
1170 
1171  virtual void OnPlaceObjectAbort()
1172  {
1173  this->RaiseButtons();
1174  this->UpdateButtons(false);
1175  }
1176 
1182  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1183  {
1184  if (!gui_scope) return;
1185  this->UpdateButtons(true);
1186  }
1187 };
1188 
1189 static WindowDesc _found_town_desc(
1190  WDP_AUTO, "build_town", 160, 162,
1193  _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
1194 );
1195 
1196 void ShowFoundTownWindow()
1197 {
1198  if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
1199  AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
1200 }