OpenTTD
town_gui.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 "stringfilter_type.h"
35 #include "widgets/dropdown_func.h"
36 #include "town_kdtree.h"
37 
38 #include "widgets/town_widget.h"
39 
40 #include "table/strings.h"
41 
42 #include "safeguards.h"
43 
44 TownKdtree _town_local_authority_kdtree(&Kdtree_TownXYFunc);
45 
47 
48 static const NWidgetPart _nested_town_authority_widgets[] = {
50  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
51  NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
52  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),
53  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
54  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
55  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
56  EndContainer(),
57  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
59  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_COMMAND_LIST), SetMinimalSize(305, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP), SetScrollbar(WID_TA_SCROLLBAR), EndContainer(),
61  EndContainer(),
62  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
64  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),
65  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
66  EndContainer()
67 };
68 
71 private:
73  int sel_index;
74  Scrollbar *vscroll;
76 
86  static int GetNthSetBit(uint32 bits, int n)
87  {
88  if (n >= 0) {
89  uint i;
90  FOR_EACH_SET_BIT(i, bits) {
91  n--;
92  if (n < 0) return i;
93  }
94  }
95  return -1;
96  }
97 
98 public:
100  {
101  this->town = Town::Get(window_number);
102  this->InitNested(window_number);
103  this->vscroll = this->GetScrollbar(WID_TA_SCROLLBAR);
104  this->vscroll->SetCapacity((this->GetWidget<NWidgetBase>(WID_TA_COMMAND_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
105  }
106 
107  void OnPaint() override
108  {
109  int numact;
110  uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
111  if (buttons != displayed_actions_on_previous_painting) this->SetDirty();
112  displayed_actions_on_previous_painting = buttons;
113 
114  this->vscroll->SetCount(numact + 1);
115 
116  if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
117  this->sel_index = -1;
118  }
119 
121  this->SetWidgetDisabledState(WID_TA_EXECUTE, this->sel_index == -1);
122 
123  this->DrawWidgets();
124  if (!this->IsShaded()) this->DrawRatings();
125  }
126 
128  void DrawRatings()
129  {
130  NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_RATING_INFO);
131  uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
132  uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
133 
134  uint y = nwid->pos_y + WD_FRAMERECT_TOP;
135 
136  DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
137  y += FONT_HEIGHT_NORMAL;
138 
139  Dimension icon_size = GetSpriteSize(SPR_COMPANY_ICON);
140  int icon_width = icon_size.width;
141  int icon_y_offset = (FONT_HEIGHT_NORMAL - icon_size.height) / 2;
142 
143  Dimension exclusive_size = GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT);
144  int exclusive_width = exclusive_size.width;
145  int exclusive_y_offset = (FONT_HEIGHT_NORMAL - exclusive_size.height) / 2;
146 
147  bool rtl = _current_text_dir == TD_RTL;
148  uint text_left = left + (rtl ? 0 : icon_width + exclusive_width + 4);
149  uint text_right = right - (rtl ? icon_width + exclusive_width + 4 : 0);
150  uint icon_left = rtl ? right - icon_width : left;
151  uint exclusive_left = rtl ? right - icon_width - exclusive_width - 2 : left + icon_width + 2;
152 
153  /* Draw list of companies */
154  const Company *c;
155  FOR_ALL_COMPANIES(c) {
156  if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
157  DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
158 
159  SetDParam(0, c->index);
160  SetDParam(1, c->index);
161 
162  int r = this->town->ratings[c->index];
163  StringID str = STR_CARGO_RATING_APPALLING;
164  if (r > RATING_APPALLING) str++;
165  if (r > RATING_VERYPOOR) str++;
166  if (r > RATING_POOR) str++;
167  if (r > RATING_MEDIOCRE) str++;
168  if (r > RATING_GOOD) str++;
169  if (r > RATING_VERYGOOD) str++;
170  if (r > RATING_EXCELLENT) str++;
171 
172  SetDParam(2, str);
173  if (this->town->exclusivity == c->index) {
174  DrawSprite(SPR_EXCLUSIVE_TRANSPORT, COMPANY_SPRITE_COLOUR(c->index), exclusive_left, y + exclusive_y_offset);
175  }
176 
177  DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
178  y += FONT_HEIGHT_NORMAL;
179  }
180  }
181 
182  y = y + WD_FRAMERECT_BOTTOM - nwid->pos_y; // Compute needed size of the widget.
183  if (y > nwid->current_y) {
184  /* If the company list is too big to fit, mark ourself dirty and draw again. */
185  ResizeWindow(this, 0, y - nwid->current_y, false);
186  }
187  }
188 
189  void SetStringParameters(int widget) const override
190  {
191  if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
192  }
193 
194  void DrawWidget(const Rect &r, int widget) const override
195  {
196  switch (widget) {
197  case WID_TA_ACTION_INFO:
198  if (this->sel_index != -1) {
199  SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8);
201  STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
202  }
203  break;
204  case WID_TA_COMMAND_LIST: {
205  int numact;
206  uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
207  int y = r.top + WD_FRAMERECT_TOP;
208  int pos = this->vscroll->GetPosition();
209 
210  if (--pos < 0) {
211  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
212  y += FONT_HEIGHT_NORMAL;
213  }
214 
215  for (int i = 0; buttons; i++, buttons >>= 1) {
216  if (pos <= -5) break;
217 
218  if ((buttons & 1) && --pos < 0) {
219  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y,
220  STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, this->sel_index == i ? TC_WHITE : TC_ORANGE);
221  y += FONT_HEIGHT_NORMAL;
222  }
223  }
224  break;
225  }
226  }
227  }
228 
229  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
230  {
231  switch (widget) {
232  case WID_TA_ACTION_INFO: {
233  assert(size->width > padding.width && size->height > padding.height);
234  size->width -= WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
235  size->height -= WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
236  Dimension d = {0, 0};
237  for (int i = 0; i < TACT_COUNT; i++) {
238  SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
239  d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
240  }
241  *size = maxdim(*size, d);
242  size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
243  size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
244  break;
245  }
246 
247  case WID_TA_COMMAND_LIST:
249  size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
250  for (uint i = 0; i < TACT_COUNT; i++ ) {
251  size->width = max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
252  }
253  size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
254  break;
255 
256  case WID_TA_RATING_INFO:
257  resize->height = FONT_HEIGHT_NORMAL;
259  break;
260  }
261  }
262 
263  void OnClick(Point pt, int widget, int click_count) override
264  {
265  switch (widget) {
266  case WID_TA_ZONE_BUTTON: {
267  bool new_show_state = !this->town->show_zone;
268  TownID index = this->town->index;
269 
270  new_show_state ? _town_local_authority_kdtree.Insert(index) : _town_local_authority_kdtree.Remove(index);
271 
272  this->town->show_zone = new_show_state;
273  this->SetWidgetLoweredState(widget, new_show_state);
275  break;
276  }
277 
278  case WID_TA_COMMAND_LIST: {
280  if (!IsInsideMM(y, 0, 5)) return;
281 
282  y = GetNthSetBit(GetMaskOfTownActions(nullptr, _local_company, this->town), y + this->vscroll->GetPosition() - 1);
283  if (y >= 0) {
284  this->sel_index = y;
285  this->SetDirty();
286  }
287  /* When double-clicking, continue */
288  if (click_count == 1 || y < 0) break;
289  FALLTHROUGH;
290  }
291 
292  case WID_TA_EXECUTE:
293  DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_ERROR_CAN_T_DO_THIS));
294  break;
295  }
296  }
297 
298  void OnHundredthTick() override
299  {
300  this->SetDirty();
301  }
302 };
303 
304 static WindowDesc _town_authority_desc(
305  WDP_AUTO, "view_town_authority", 317, 222,
307  0,
308  _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
309 );
310 
311 static void ShowTownAuthorityWindow(uint town)
312 {
313  AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
314 }
315 
316 
317 /* Town view window. */
319 private:
321 
322 public:
323  static const int WID_TV_HEIGHT_NORMAL = 150;
324 
326  {
327  this->CreateNestedTree();
328 
329  this->town = Town::Get(window_number);
330  if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
331 
332  this->FinishInitNested(window_number);
333 
334  this->flags |= WF_DISABLE_VP_SCROLL;
335  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
336  nvp->InitializeViewport(this, this->town->xy, ZOOM_LVL_NEWS);
337 
338  /* disable renaming town in network games if you are not the server */
340  }
341 
342  ~TownViewWindow()
343  {
345  }
346 
347  void SetStringParameters(int widget) const override
348  {
349  if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
350  }
351 
352  void OnPaint() override
353  {
354  extern const Town *_viewport_highlight_town;
355  this->SetWidgetLoweredState(WID_TV_CATCHMENT, _viewport_highlight_town == this->town);
356 
357  this->DrawWidgets();
358  }
359 
360  void DrawWidget(const Rect &r, int widget) const override
361  {
362  if (widget != WID_TV_INFO) return;
363 
364  uint y = r.top + WD_FRAMERECT_TOP;
365 
366  SetDParam(0, this->town->cache.population);
367  SetDParam(1, this->town->cache.num_houses);
368  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
369 
370  SetDParam(0, 1 << CT_PASSENGERS);
371  SetDParam(1, this->town->supplied[CT_PASSENGERS].old_act);
372  SetDParam(2, this->town->supplied[CT_PASSENGERS].old_max);
373  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
374 
375  SetDParam(0, 1 << CT_MAIL);
376  SetDParam(1, this->town->supplied[CT_MAIL].old_act);
377  SetDParam(2, this->town->supplied[CT_MAIL].old_max);
378  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX);
379 
380  bool first = true;
381  for (int i = TE_BEGIN; i < TE_END; i++) {
382  if (this->town->goal[i] == 0) continue;
383  if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
384  if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
385 
386  if (first) {
387  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
388  first = false;
389  }
390 
391  bool rtl = _current_text_dir == TD_RTL;
392  uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
393  uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
394 
396  assert(cargo != nullptr);
397 
398  StringID string;
399 
400  if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
401  /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
402  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
403  if (this->town->received[i].old_act == 0) {
404  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
405 
406  if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
407  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
408  }
409  }
410 
411  SetDParam(0, cargo->name);
412  } else {
413  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
414  if (this->town->received[i].old_act < this->town->goal[i]) {
415  string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
416  }
417 
418  SetDParam(0, cargo->Index());
419  SetDParam(1, this->town->received[i].old_act);
420  SetDParam(2, cargo->Index());
421  SetDParam(3, this->town->goal[i]);
422  }
423  DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
424  }
425 
426  if (HasBit(this->town->flags, TOWN_IS_GROWING)) {
427  SetDParam(0, RoundDivSU(this->town->growth_rate + 1, DAY_TICKS));
428  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, this->town->fund_buildings_months == 0 ? STR_TOWN_VIEW_TOWN_GROWS_EVERY : STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED);
429  } else {
430  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
431  }
432 
433  /* only show the town noise, if the noise option is activated. */
435  SetDParam(0, this->town->noise_reached);
436  SetDParam(1, this->town->MaxTownNoise());
437  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
438  }
439 
440  if (this->town->text != nullptr) {
441  SetDParamStr(0, this->town->text);
442  DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
443  }
444  }
445 
446  void OnClick(Point pt, int widget, int click_count) override
447  {
448  switch (widget) {
449  case WID_TV_CENTER_VIEW: // scroll to location
450  if (_ctrl_pressed) {
451  ShowExtraViewPortWindow(this->town->xy);
452  } else {
453  ScrollMainWindowToTile(this->town->xy);
454  }
455  break;
456 
457  case WID_TV_SHOW_AUTHORITY: // town authority
458  ShowTownAuthorityWindow(this->window_number);
459  break;
460 
461  case WID_TV_CHANGE_NAME: // rename
462  SetDParam(0, this->window_number);
463  ShowQueryString(STR_TOWN_NAME, STR_TOWN_VIEW_RENAME_TOWN_BUTTON, MAX_LENGTH_TOWN_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
464  break;
465 
466  case WID_TV_CATCHMENT:
468  break;
469 
470  case WID_TV_EXPAND: { // expand town - only available on Scenario editor
471  /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
472  static bool _warn_town_no_roads = false;
473 
474  if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
475  ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
476  _warn_town_no_roads = true;
477  }
478 
479  DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
480  break;
481  }
482 
483  case WID_TV_DELETE: // delete town - only available on Scenario editor
484  DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
485  break;
486  }
487  }
488 
489  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
490  {
491  switch (widget) {
492  case WID_TV_INFO:
493  size->height = GetDesiredInfoHeight(size->width);
494  break;
495  }
496  }
497 
502  uint GetDesiredInfoHeight(int width) const
503  {
504  uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
505 
506  bool first = true;
507  for (int i = TE_BEGIN; i < TE_END; i++) {
508  if (this->town->goal[i] == 0) continue;
509  if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
510  if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
511 
512  if (first) {
513  aimed_height += FONT_HEIGHT_NORMAL;
514  first = false;
515  }
516  aimed_height += FONT_HEIGHT_NORMAL;
517  }
518  aimed_height += FONT_HEIGHT_NORMAL;
519 
521 
522  if (this->town->text != nullptr) {
523  SetDParamStr(0, this->town->text);
524  aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT);
525  }
526 
527  return aimed_height;
528  }
529 
530  void ResizeWindowAsNeeded()
531  {
532  const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
533  uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
534  if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
535  this->ReInit();
536  }
537  }
538 
539  void OnResize() override
540  {
541  if (this->viewport != nullptr) {
542  NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
543  nvp->UpdateViewportCoordinates(this);
544 
545  ScrollWindowToTile(this->town->xy, this, true); // Re-center viewport.
546  }
547  }
548 
554  void OnInvalidateData(int data = 0, bool gui_scope = true) override
555  {
556  if (!gui_scope) return;
557  /* Called when setting station noise or required cargoes have changed, in order to resize the window */
558  this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
559  this->ResizeWindowAsNeeded();
560  }
561 
562  void OnQueryTextFinished(char *str) override
563  {
564  if (str == nullptr) return;
565 
566  DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), nullptr, str);
567  }
568 };
569 
570 static const NWidgetPart _nested_town_game_view_widgets[] = {
572  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
573  NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
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), SetPadding(1, 1, 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(),
586  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),
587  NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_SHOW_AUTHORITY), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP),
588  NWidget(WWT_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),
589  EndContainer(),
590  NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
591  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
592  EndContainer(),
593 };
594 
595 static WindowDesc _town_game_view_desc(
596  WDP_AUTO, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
598  0,
599  _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
600 );
601 
602 static const NWidgetPart _nested_town_editor_view_widgets[] = {
604  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
605  NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
606  NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
607  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
608  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
609  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
610  EndContainer(),
611  NWidget(WWT_PANEL, COLOUR_BROWN),
612  NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
613  NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
614  EndContainer(),
615  EndContainer(),
616  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
619  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),
620  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),
621  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),
622  EndContainer(),
623  NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
624  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
625  EndContainer(),
626 };
627 
628 static WindowDesc _town_editor_view_desc(
629  WDP_AUTO, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
631  0,
632  _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
633 );
634 
635 void ShowTownViewWindow(TownID town)
636 {
637  if (_game_mode == GM_EDITOR) {
638  AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
639  } else {
640  AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
641  }
642 }
643 
644 static const NWidgetPart _nested_town_directory_widgets[] = {
646  NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
647  NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
648  NWidget(WWT_SHADEBOX, COLOUR_BROWN),
649  NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
650  NWidget(WWT_STICKYBOX, COLOUR_BROWN),
651  EndContainer(),
655  NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TD_SORT_ORDER), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
656  NWidget(WWT_DROPDOWN, COLOUR_BROWN, WID_TD_SORT_CRITERIA), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
657  NWidget(WWT_EDITBOX, COLOUR_BROWN, WID_TD_FILTER), SetFill(35, 12), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
658  EndContainer(),
659  NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
661  NWidget(WWT_PANEL, COLOUR_BROWN),
662  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),
663  EndContainer(),
664  EndContainer(),
666  NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
667  NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
668  EndContainer(),
669  EndContainer(),
670 };
671 
673 struct TownDirectoryWindow : public Window {
674 private:
675  /* Runtime saved values */
676  static Listing last_sorting;
677  static const Town *last_town;
678 
679  /* Constants for sorting towns */
680  static const StringID sorter_names[];
681  static GUITownList::SortFunction * const sorter_funcs[];
682 
685 
686  GUITownList towns;
687 
688  Scrollbar *vscroll;
689 
690  void BuildSortTownList()
691  {
692  if (this->towns.NeedRebuild()) {
693  this->towns.clear();
694 
695  const Town *t;
696  FOR_ALL_TOWNS(t) {
697  this->towns.push_back(t);
698  }
699 
700  this->towns.shrink_to_fit();
701  this->towns.RebuildDone();
702  this->vscroll->SetCount((uint)this->towns.size()); // Update scrollbar as well.
703  }
704  /* Always sort the towns. */
705  this->last_town = nullptr;
706  this->towns.Sort();
707  this->SetWidgetDirty(WID_TD_LIST); // Force repaint of the displayed towns.
708  }
709 
711  static bool TownNameSorter(const Town * const &a, const Town * const &b)
712  {
713  static char buf_cache[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
715 
716  SetDParam(0, a->index);
717  GetString(buf, STR_TOWN_NAME, lastof(buf));
718 
719  /* If 'b' is the same town as in the last round, use the cached value
720  * We do this to speed stuff up ('b' is called with the same value a lot of
721  * times after each other) */
722  if (b != last_town) {
723  last_town = b;
724  SetDParam(0, b->index);
725  GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
726  }
727 
728  return strnatcmp(buf, buf_cache) < 0; // Sort by name (natural sorting).
729  }
730 
732  static bool TownPopulationSorter(const Town * const &a, const Town * const &b)
733  {
734  uint32 a_population = a->cache.population;
735  uint32 b_population = b->cache.population;
736  if (a_population == b_population) return TownDirectoryWindow::TownNameSorter(a, b);
737  return a_population < b_population;
738  }
739 
741  static bool TownRatingSorter(const Town * const &a, const Town * const &b)
742  {
743  bool before = !TownDirectoryWindow::last_sorting.order; // Value to get 'a' before 'b'.
744 
745  /* Towns without rating are always after towns with rating. */
748  int16 a_rating = a->ratings[_local_company];
749  int16 b_rating = b->ratings[_local_company];
750  if (a_rating == b_rating) return TownDirectoryWindow::TownNameSorter(a, b);
751  return a_rating < b_rating;
752  }
753  return before;
754  }
755  if (HasBit(b->have_ratings, _local_company)) return !before;
756 
757  /* Sort unrated towns always on ascending town name. */
758  if (before) return TownDirectoryWindow::TownNameSorter(a, b);
760  }
761 
762 public:
764  {
765  this->CreateNestedTree();
766 
767  this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
768 
769  this->towns.SetListing(this->last_sorting);
771  this->towns.ForceRebuild();
772  this->BuildSortTownList();
773 
774  this->FinishInitNested(0);
775 
776  this->querystrings[WID_TD_FILTER] = &this->townname_editbox;
777  this->townname_editbox.cancel_button = QueryString::ACTION_CLEAR;
778  }
779 
780  void SetStringParameters(int widget) const override
781  {
782  switch (widget) {
785  break;
786 
789  break;
790  }
791  }
792 
798  static StringID GetTownString(const Town *t)
799  {
800  return t->larger_town ? STR_TOWN_DIRECTORY_CITY : STR_TOWN_DIRECTORY_TOWN;
801  }
802 
803  void DrawWidget(const Rect &r, int widget) const override
804  {
805  switch (widget) {
806  case WID_TD_SORT_ORDER:
807  this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
808  break;
809 
810  case WID_TD_LIST: {
811  int n = 0;
812  int y = r.top + WD_FRAMERECT_TOP;
813  if (this->towns.size() == 0) { // No towns available.
814  DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
815  break;
816  }
817 
818  /* At least one town available. */
819  bool rtl = _current_text_dir == TD_RTL;
820  Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
821  int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
822  int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
823  int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
824 
825  for (uint i = this->vscroll->GetPosition(); i < this->towns.size(); i++) {
826  const Town *t = this->towns[i];
827  assert(t->xy != INVALID_TILE);
828 
829  /* Draw rating icon. */
830  if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
831  DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
832  } else {
833  SpriteID icon = SPR_TOWN_RATING_APALLING;
834  if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
835  if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
836  DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
837  }
838 
839  SetDParam(0, t->index);
840  SetDParam(1, t->cache.population);
841  DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, GetTownString(t));
842 
843  y += this->resize.step_height;
844  if (++n == this->vscroll->GetCapacity()) break; // max number of towns in 1 window
845  }
846  break;
847  }
848  }
849  }
850 
851  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
852  {
853  switch (widget) {
854  case WID_TD_SORT_ORDER: {
855  Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
856  d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
857  d.height += padding.height;
858  *size = maxdim(*size, d);
859  break;
860  }
861  case WID_TD_SORT_CRITERIA: {
862  Dimension d = {0, 0};
863  for (uint i = 0; TownDirectoryWindow::sorter_names[i] != INVALID_STRING_ID; i++) {
865  }
866  d.width += padding.width;
867  d.height += padding.height;
868  *size = maxdim(*size, d);
869  break;
870  }
871  case WID_TD_LIST: {
872  Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
873  for (uint i = 0; i < this->towns.size(); i++) {
874  const Town *t = this->towns[i];
875 
876  assert(t != nullptr);
877 
878  SetDParam(0, t->index);
879  SetDParamMaxDigits(1, 8);
880  d = maxdim(d, GetStringBoundingBox(GetTownString(t)));
881  }
882  Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
883  d.width += icon_size.width + 2;
884  d.height = max(d.height, icon_size.height);
885  resize->height = d.height;
886  d.height *= 5;
887  d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
888  d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
889  *size = maxdim(*size, d);
890  break;
891  }
893  SetDParamMaxDigits(0, 10);
894  Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
895  d.width += padding.width;
896  d.height += padding.height;
897  *size = maxdim(*size, d);
898  break;
899  }
900  }
901  }
902 
903  void OnClick(Point pt, int widget, int click_count) override
904  {
905  switch (widget) {
906  case WID_TD_SORT_ORDER: // Click on sort order button
907  if (this->towns.SortType() != 2) { // A different sort than by rating.
908  this->towns.ToggleSortOrder();
909  this->last_sorting = this->towns.GetListing(); // Store new sorting order.
910  } else {
911  /* Some parts are always sorted ascending on name. */
912  this->last_sorting.order = !this->last_sorting.order;
913  this->towns.SetListing(this->last_sorting);
914  this->towns.ForceResort();
915  this->towns.Sort();
916  }
917  this->SetDirty();
918  break;
919 
920  case WID_TD_SORT_CRITERIA: // Click on sort criteria dropdown
922  break;
923 
924  case WID_TD_LIST: { // Click on Town Matrix
925  uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
926  if (id_v >= this->towns.size()) return; // click out of town bounds
927 
928  const Town *t = this->towns[id_v];
929  assert(t != nullptr);
930  if (_ctrl_pressed) {
932  } else {
934  }
935  break;
936  }
937  }
938  }
939 
940  void OnDropdownSelect(int widget, int index) override
941  {
942  if (widget != WID_TD_SORT_CRITERIA) return;
943 
944  if (this->towns.SortType() != index) {
945  this->towns.SetSortType(index);
946  this->last_sorting = this->towns.GetListing(); // Store new sorting order.
947  this->BuildSortTownList();
948  }
949  }
950 
951  void OnPaint() override
952  {
953  if (this->towns.NeedRebuild()) this->BuildSortTownList();
954  this->DrawWidgets();
955  }
956 
957  void OnHundredthTick() override
958  {
959  this->BuildSortTownList();
960  this->SetDirty();
961  }
962 
963  void OnResize() override
964  {
965  this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
966  }
967 
968  void OnEditboxChanged(int wid) override
969  {
970  if (wid == WID_TD_FILTER) {
971  this->string_filter.SetFilterTerm(this->townname_editbox.text.buf);
973  }
974  }
975 
981  void OnInvalidateData(int data = 0, bool gui_scope = true) override
982  {
984 
985  switch (data) {
986  case TDIWD_FORCE_REBUILD:
987  /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
988  this->towns.ForceRebuild();
989  break;
990 
992  if (this->string_filter.IsEmpty()) {
993  this->towns.ForceRebuild();
994  } else {
995  this->towns.clear();
996 
997  const Town *t;
998  FOR_ALL_TOWNS(t) {
999  this->string_filter.ResetState();
1000 
1001  SetDParam(0, t->index);
1002  GetString(buf, STR_TOWN_NAME, lastof(buf));
1003 
1004  this->string_filter.AddLine(buf);
1005  if (this->string_filter.GetState()) this->towns.push_back(t);
1006  }
1007 
1008  this->towns.SetListing(this->last_sorting);
1009  this->towns.ForceResort();
1010  this->towns.Sort();
1011  this->towns.shrink_to_fit();
1012  this->towns.RebuildDone();
1013  this->vscroll->SetCount((int)this->towns.size()); // Update scrollbar as well.
1014  }
1015  break;
1016  default:
1017  this->towns.ForceResort();
1018  }
1019  }
1020 };
1021 
1022 Listing TownDirectoryWindow::last_sorting = {false, 0};
1023 const Town *TownDirectoryWindow::last_town = nullptr;
1024 
1027  STR_SORT_BY_NAME,
1028  STR_SORT_BY_POPULATION,
1029  STR_SORT_BY_RATING,
1031 };
1032 
1035  &TownNameSorter,
1036  &TownPopulationSorter,
1037  &TownRatingSorter,
1038 };
1039 
1040 static WindowDesc _town_directory_desc(
1041  WDP_AUTO, "list_towns", 208, 202,
1043  0,
1044  _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
1045 );
1046 
1047 void ShowTownDirectory()
1048 {
1049  if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
1050  new TownDirectoryWindow(&_town_directory_desc);
1051 }
1052 
1053 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1054 {
1055  if (result.Failed()) return;
1056 
1057  if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
1059 }
1060 
1061 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
1062 {
1063  if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
1064 }
1065 
1066 static const NWidgetPart _nested_found_town_widgets[] = {
1068  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
1069  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1070  NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
1071  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
1072  EndContainer(),
1073  /* Construct new town(s) buttons. */
1074  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
1076  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1077  SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1078  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
1079  SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
1080  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
1081  SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
1082  /* Town name selection. */
1083  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
1084  NWidget(WWT_EDITBOX, COLOUR_GREY, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
1085  SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
1086  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
1087  SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
1088  /* Town size selection. */
1089  NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1090  NWidget(NWID_SPACER), SetFill(1, 0),
1091  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
1092  NWidget(NWID_SPACER), SetFill(1, 0),
1093  EndContainer(),
1095  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
1096  SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1097  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
1098  SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1099  EndContainer(),
1102  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
1103  SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1104  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
1105  SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
1106  EndContainer(),
1108  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1109  SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
1110  /* Town roads selection. */
1111  NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
1112  NWidget(NWID_SPACER), SetFill(1, 0),
1113  NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
1114  NWidget(NWID_SPACER), SetFill(1, 0),
1115  EndContainer(),
1117  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),
1118  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),
1119  EndContainer(),
1122  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),
1123  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),
1124  EndContainer(),
1126  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1127  SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
1129  EndContainer(),
1130 };
1131 
1134 private:
1137  bool city;
1140  uint32 townnameparts;
1142 
1143 public:
1145  Window(desc),
1146  town_size(TSZ_MEDIUM),
1147  town_layout(_settings_game.economy.town_layout),
1149  params(_settings_game.game_creation.town_name)
1150  {
1151  this->InitNested(window_number);
1152  this->querystrings[WID_TF_TOWN_NAME_EDITBOX] = &this->townname_editbox;
1153  this->RandomTownName();
1154  this->UpdateButtons(true);
1155  }
1156 
1157  void RandomTownName()
1158  {
1159  this->townnamevalid = GenerateTownName(&this->townnameparts);
1160 
1161  if (!this->townnamevalid) {
1162  this->townname_editbox.text.DeleteAll();
1163  } else {
1164  GetTownName(this->townname_editbox.text.buf, &this->params, this->townnameparts, &this->townname_editbox.text.buf[this->townname_editbox.text.max_bytes - 1]);
1165  this->townname_editbox.text.UpdateSize();
1166  }
1168 
1170  }
1171 
1172  void UpdateButtons(bool check_availability)
1173  {
1174  if (check_availability && _game_mode != GM_EDITOR) {
1179  }
1180 
1181  for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
1182  this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
1183  }
1184 
1185  this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1186 
1187  for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
1188  this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
1189  }
1190 
1191  this->SetDirty();
1192  }
1193 
1194  void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
1195  {
1196  const char *name = nullptr;
1197 
1198  if (!this->townnamevalid) {
1199  name = this->townname_editbox.text.buf;
1200  } else {
1201  /* If user changed the name, send it */
1203  GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
1204  if (strcmp(buf, this->townname_editbox.text.buf) != 0) name = this->townname_editbox.text.buf;
1205  }
1206 
1207  bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
1208  townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
1209 
1210  /* Rerandomise name, if success and no cost-estimation. */
1211  if (success && !_shift_pressed) this->RandomTownName();
1212  }
1213 
1214  void OnClick(Point pt, int widget, int click_count) override
1215  {
1216  switch (widget) {
1217  case WID_TF_NEW_TOWN:
1218  HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
1219  break;
1220 
1221  case WID_TF_RANDOM_TOWN:
1222  this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
1223  break;
1224 
1226  this->RandomTownName();
1228  break;
1229 
1231  _generating_world = true;
1233  if (!GenerateTowns(this->town_layout)) {
1234  ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
1235  }
1237  _generating_world = false;
1238  break;
1239 
1241  this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
1242  this->UpdateButtons(false);
1243  break;
1244 
1245  case WID_TF_CITY:
1246  this->city ^= true;
1247  this->SetWidgetLoweredState(WID_TF_CITY, this->city);
1248  this->SetDirty();
1249  break;
1250 
1253  this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
1254  this->UpdateButtons(false);
1255  break;
1256  }
1257  }
1258 
1259  void OnPlaceObject(Point pt, TileIndex tile) override
1260  {
1261  this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
1262  }
1263 
1264  void OnPlaceObjectAbort() override
1265  {
1266  this->RaiseButtons();
1267  this->UpdateButtons(false);
1268  }
1269 
1275  void OnInvalidateData(int data = 0, bool gui_scope = true) override
1276  {
1277  if (!gui_scope) return;
1278  this->UpdateButtons(true);
1279  }
1280 };
1281 
1282 static WindowDesc _found_town_desc(
1283  WDP_AUTO, "build_town", 160, 162,
1286  _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
1287 );
1288 
1289 void ShowFoundTownWindow()
1290 {
1291  if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
1292  AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
1293 }
1294 
1295 void InitializeTownGui()
1296 {
1297  _town_local_authority_kdtree.Clear();
1298 }
Nested widget containing a viewport.
Definition: widget_type.h:81
Selection for a small town.
Definition: town_widget.h:56
Functions related to OTTD&#39;s strings.
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: town_gui.cpp:489
static const uint TOWN_GROWTH_WINTER
The town only needs this cargo in the winter (any amount)
Definition: town.h:36
Base types for having sorted lists in GUIs.
void RebuildDone()
Notify the sortlist that the rebuild is done.
Direction of sort dropdown.
Definition: town_widget.h:17
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:81
Scrollbar of the list of commands.
Definition: town_widget.h:31
Definition of stuff that is very close to a company, like the company struct itself.
void UpdateNearestTownForRoadTiles(bool invalidate)
Updates cached nearest town for all road tiles.
Definition: road_cmd.cpp:1829
bool _networking
are we in networking mode?
Definition: network.cpp:54
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: town_gui.cpp:446
static TropicZone GetTropicZone(TileIndex tile)
Get the tropic zone.
Definition: tile_map.h:240
TransportedCargoStat< uint16 > received[NUM_TE]
Cargo statistics about received cargotypes.
Definition: town.h:80
ResizeInfo resize
Resize information.
Definition: window_gui.h:324
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:930
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1851
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:394
rename a town
Definition: command_type.h:263
Selection for a large town.
Definition: town_widget.h:58
void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition: window_gui.h:455
uint displayed_actions_on_previous_painting
Actions that were available on the previous call to OnPaint()
Definition: town_gui.cpp:75
Tile is desert.
Definition: tile_type.h:73
byte LowestSnowLine()
Get the lowest possible snow line height, either variable or static.
Definition: landscape.cpp:670
Turn on/off showing local authority zone.
Definition: town_widget.h:28
High level window description.
Definition: window_gui.h:168
EconomySettings economy
settings to change the economy
WindowFlags flags
Window flags.
Definition: window_gui.h:312
int left
x position of left edge of the window
Definition: window_gui.h:319
Expand this town (scenario editor only).
Definition: town_widget.h:45
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:604
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
Declarations for accessing the k-d tree of towns.
Centered label.
Definition: widget_type.h:57
Scrollbar data structure.
Definition: widget_type.h:589
void DrawRatings()
Draw the contents of the ratings panel.
Definition: town_gui.cpp:128
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:581
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:64
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition: widget.cpp:1936
Horizontal container.
Definition: widget_type.h:75
byte flags
See TownFlags.
Definition: town.h:66
void ResetState()
Reset the matching state to process a new item.
int sel_index
Currently selected town action, 0 to TACT_COUNT-1, -1 means no action selected.
Definition: town_gui.cpp:73
void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets,...)
Sets the enabled/disabled status of a list of widgets.
Definition: window.cpp:520
void SetSortFuncs(SortFunction *const *n_funcs)
Hand the array of sort function pointers to the sort list.
bool GetState() const
Get the matching state of the current item.
void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
Definition: misc_gui.cpp:1121
Specification of a cargo type.
Definition: cargotype.h:57
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: town_gui.cpp:903
Medium town.
Definition: town_type.h:23
void OnHundredthTick() override
Called once every 100 (game) ticks.
Definition: town_gui.cpp:957
Filter of name.
Definition: town_widget.h:19
General information about the town.
Definition: town_widget.h:40
void CDECL void DeleteAll()
Delete every character in the textbuffer.
Definition: textbuf.cpp:118
a textbox for typing
Definition: widget_type.h:71
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:68
bool GenerateTownName(uint32 *townnameparts, TownNames *town_names)
Generates valid town name.
Definition: townname.cpp:122
Pressed (inset) panel, most commonly used as combo box text area.
Definition: widget_type.h:51
QueryString townname_editbox
Townname editbox.
Definition: town_gui.cpp:1138
void DrawCompanyIcon(CompanyID c, int x, int y)
Draw the icon of a company.
TownLayout
Town Layouts.
Definition: town_type.h:80
Generate a random town name.
Definition: town_widget.h:55
static const int ACTION_CLEAR
Clear editbox.
TownSize town_size
Selected town size.
Definition: town_gui.cpp:1135
TownFounding found_town
town founding.
Overview with ratings for each company.
Definition: town_widget.h:29
bool city
Are we building a city?
Definition: town_gui.cpp:1137
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:382
int GetStringHeight(const char *str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition: gfx.cpp:547
static const int DAY_TICKS
1 day is 74 ticks; _date_fract used to be uint16 and incremented by 885.
Definition: date_type.h:30
Close box (at top-left of a window)
Definition: widget_type.h:69
bool show_zone
NOSAVE: mark town to show the local authority zone in the viewports.
Definition: town.h:104
static bool TownRatingSorter(const Town *const &a, const Town *const &b)
Sort by town rating.
Definition: town_gui.cpp:741
static NWidgetPart SetMinimalTextLines(uint8 lines, uint8 spacing, FontSize size=FS_NORMAL)
Widget part function for setting the minimal text lines.
Definition: widget_type.h:965
void SetViewportCatchmentTown(const Town *t, bool sel)
Select or deselect town for coverage area highlight.
Definition: viewport.cpp:3497
TownLayout town_layout
select town layout,
String filter and state.
do a action from the town detail window (like advertises or bribe)
Definition: command_type.h:264
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
void ReInit(int rx=0, int ry=0)
Re-initialize a window, and optionally change its size.
Definition: window.cpp:979
Functions related to world/map generation.
void InitializeViewport(Window *w, uint32 follow_flags, ZoomLevel zoom)
Initialize the viewport of the window.
Definition: widget.cpp:1927
void OnDropdownSelect(int widget, int index) override
A dropdown option associated to this window has been selected.
Definition: town_gui.cpp:940
#define FOR_EACH_SET_BIT(bitpos_var, bitset_value)
Do an operation for each set set bit in a value.
bool persistent_buildingtools
keep the building tools active after usage
Common return value for all commands.
Definition: command_type.h:25
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: town_gui.cpp:263
bool GenerateTowns(TownLayout layout)
This function will generate a certain amount of towns, with a certain layout It can be called from th...
Definition: town_cmd.cpp:2139
uint32 GetWorldPopulation()
Determines the world population Basically, count population of all towns, one by one.
Definition: town_cmd.cpp:437
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
Town directory; Window numbers:
Definition: window_type.h:249
bool allow_town_roads
towns are allowed to build roads (always allowed when generating world / in SE)
uint32 population
Current population of people.
Definition: town.h:47
void OnPaint() override
The window must be repainted.
Definition: town_gui.cpp:352
List of towns.
Definition: town_widget.h:20
Nested widget to display a viewport in a window.
Definition: widget_type.h:575
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1812
void SetListing(Listing l)
Import sort conditions.
uint16 noise_reached
level of noise that all the airports are generating
Definition: town.h:68
Pure simple text.
Definition: widget_type.h:58
static GUITownList::SortFunction *const sorter_funcs[]
Available town directory sorting functions.
Definition: town_gui.cpp:681
const byte _town_action_costs[TACT_COUNT]
Factor in the cost of each town action.
Definition: town_cmd.cpp:2958
static int RoundDivSU(int a, uint b)
Computes round(a / b) for signed a and unsigned b.
Definition: math_func.hpp:338
bool NeedRebuild() const
Check if a rebuild is needed.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: town_gui.cpp:194
void SetCapacity(int capacity)
Set the capacity of visible elements.
Definition: widget_type.h:686
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:670
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
Functions related to (drawing on) viewports.
void ForceRebuild()
Force that a rebuild is needed.
Base for the GUIs that have an edit box in them.
StringFilter string_filter
Filter for towns.
Definition: town_gui.cpp:683
uint32 goal[NUM_TE]
Amount of cargo required for the town to grow.
Definition: town.h:81
Data structure for an opened window.
Definition: window_gui.h:278
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:37
void UpdateOSKOriginalText(const Window *parent, int button)
Updates the original text of the OSK so when the &#39;parent&#39; changes the original and you press on cance...
Definition: osk_gui.cpp:427
Caption of window.
Definition: town_widget.h:38
Selection for a medium town.
Definition: town_widget.h:57
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1828
void SetFilterTerm(const char *str)
Set the term to filter on.
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1046
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1841
Criteria of sort dropdown.
Definition: town_widget.h:18
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:281
enable the &#39;Default&#39; button ("\0" is returned)
Definition: textbuf_gui.h:23
Center the main view on this town.
Definition: town_widget.h:41
void OnResize() override
Called after the window got resized.
Definition: town_gui.cpp:539
TownEffect
Town growth effect when delivering cargo.
Definition: cargotype.h:26
Functions related to low-level strings.
List of commands for the player.
Definition: town_widget.h:30
static const int MAX_CHAR_LENGTH
Max. length of UTF-8 encoded unicode character.
Definition: strings_type.h:20
void CommandCallback(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
Define a callback function for the client, after the command is finished.
Definition: command_type.h:472
The world&#39;s population.
Definition: town_widget.h:22
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: town_gui.cpp:1214
TileIndex xy
town center tile
Definition: town.h:56
Invisible widget that takes some space.
Definition: widget_type.h:79
Other information.
Definition: error.h:24
Functions related to errors.
QueryString townname_editbox
Filter editbox.
Definition: town_gui.cpp:684
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:65
Additional information about the action.
Definition: town_widget.h:32
int GetScrolledRowFromWidget(int clickpos, const Window *const w, int widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition: widget.cpp:1959
This window is used for construction; close it whenever changing company.
Definition: window_gui.h:210
TownSize
Supported initial town sizes.
Definition: town_type.h:21
const Town * _viewport_highlight_town
Currently selected town for coverage area highlight.
Definition: viewport.cpp:991
bool IsWidgetLowered(byte widget_index) const
Gets the lowered state of a widget.
Definition: window_gui.h:495
bool townnamevalid
Is generated town name valid?
Definition: town_gui.cpp:1139
SoundSettings sound
sound effect settings
Listing GetListing() const
Export current sort conditions.
bool order
Ascending/descending.
Definition: sortlist_type.h:34
found a town
Definition: command_type.h:262
int16 ratings[MAX_COMPANIES]
ratings of each company for this town
Definition: town.h:77
Town * town
Town being displayed.
Definition: town_gui.cpp:72
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: town_gui.cpp:347
uint smallest_y
Smallest vertical size of the widget in a filled window.
Definition: widget_type.h:172
uint current_y
Current vertical size (after resizing).
Definition: widget_type.h:175
Sort descending.
Definition: window_gui.h:227
void OnPaint() override
The window must be repainted.
Definition: town_gui.cpp:107
SmallMap< int, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition: window_gui.h:330
void RaiseButtons(bool autoraise=false)
Raise the buttons of the window.
Definition: window.cpp:557
char * GetTownName(char *buff, const TownNameParams *par, uint32 townnameparts, const char *last)
Fills buffer with specified town name.
Definition: townname.cpp:51
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:178
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
Data stored about a string that can be modified in the GUI.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:947
bool Succeeded() const
Did this command succeed?
Definition: command_type.h:152
Definition of base types and functions in a cross-platform compatible way.
the length of the string is counted in characters
Definition: textbuf_gui.h:24
A number of safeguards to prevent using unsafe methods.
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Geometry functions.
rectangle (stations, depots, ...)
Allowed, with custom town layout.
Definition: town_type.h:98
Simple depressed panel.
Definition: widget_type.h:50
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: town_gui.cpp:981
void DrawSortButtonState(int widget, SortButtonState state) const
Draw a sort button&#39;s up or down arrow symbol.
Definition: widget.cpp:638
uint step_height
Step-size of height resize changes.
Definition: window_gui.h:220
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:311
GUI Functions related to companies.
delete a town
Definition: command_type.h:269
Number of available town actions.
Definition: town.h:223
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition: widget_type.h:177
void OnQueryTextFinished(char *str) override
The query window opened from this window has closed.
Definition: town_gui.cpp:562
Editor for the town name.
Definition: town_widget.h:54
const CargoSpec * FindFirstCargoWithTownEffect(TownEffect effect)
Determines the first cargo with a certain town effect.
Definition: town_cmd.cpp:2709
expand a town
Definition: command_type.h:268
Randomly place a town.
Definition: town_widget.h:52
void SetDParamMaxDigits(uint n, uint count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:122
int GetRowFromWidget(int clickpos, int widget, int padding, int line_height=-1) const
Compute the row of a widget that a user clicked in.
Definition: window.cpp:203
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:65
Baseclass for nested widgets.
Definition: widget_type.h:126
Found a town; Window numbers:
Definition: window_type.h:424
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:499
Selection for the 3x3 grid town layout.
Definition: town_widget.h:64
Do-it button.
Definition: town_widget.h:33
Toggle catchment area highlight.
Definition: town_widget.h:44
Basic functions/variables used all over the place.
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:534
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
Caption of window.
Definition: town_widget.h:27
bool ScrollWindowToTile(TileIndex tile, Window *w, bool instant)
Scrolls the viewport in a window to a given location.
Definition: viewport.cpp:2406
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
Functions related to sound.
void SetSortType(uint8 n_type)
Set the sorttype of the list.
bool Sort(SortFunction *compare)
Sort the list.
static int SortButtonWidth()
Get width of up/down arrow of sort button state.
Definition: widget.cpp:658
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: town_gui.cpp:229
bool Failed() const
Did this command fail?
Definition: command_type.h:161
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: town_gui.cpp:851
The filename filter has changed (via the editbox)
Definition: town.h:166
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: town_gui.cpp:1275
uint16 MaxTownNoise() const
Calculate the max town noise.
Definition: town.h:125
static const int WIDGET_LIST_END
indicate the end of widgets&#39; list for vararg functions
Definition: widget_type.h:22
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:968
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:38
void ShowExtraViewPortWindow(TileIndex tile=INVALID_TILE)
Show a new Extra Viewport window.
Road related functions.
Selection for the better town layout.
Definition: town_widget.h:62
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:698
void AddLine(const char *str)
Pass another text line from the current item to the filter.
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition: gfx.cpp:587
void ForceResort()
Force a resort next Sort call Reset the resort timer if used too.
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
static StringID GetTownString(const Town *t)
Get the string to draw the town name.
Definition: town_gui.cpp:798
char * text
General text with additional information.
Definition: town.h:83
Randomly place many towns.
Definition: town_widget.h:53
void OnHundredthTick() override
Called once every 100 (game) ticks.
Definition: town_gui.cpp:298
static const uint TOWN_GROWTH_DESERT
The town needs the cargo for growth when on desert (any amount)
Definition: town.h:37
uint16 growth_rate
town growth rate
Definition: town.h:96
Functions related to companies.
char *const buf
buffer in which text is saved
Definition: textbuf_type.h:34
Delete this town (scenario editor only).
Definition: town_widget.h:46
Tstorage old_max
Maximum amount last month.
Definition: town_type.h:115
bool _generating_world
Whether we are generating the map or not.
Definition: genworld.cpp:62
Town directory window class.
Definition: town_gui.cpp:673
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:29
Town authority; Window numbers:
Definition: window_type.h:189
GUISettings gui
settings related to the GUI
Selection for a randomly chosen town layout.
Definition: town_widget.h:65
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:580
bool station_noise_level
build new airports when the town noise level is still within accepted limits
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
Create a new town.
Definition: town_widget.h:51
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
bool HandlePlacePushButton(Window *w, int widget, CursorID cursor, HighLightStyle mode)
This code is shared for the majority of the pushbuttons.
Definition: main_gui.cpp:100
void OnPaint() override
The window must be repainted.
Definition: town_gui.cpp:951
void OnEditboxChanged(int wid) override
The text in an editbox has been edited.
Definition: town_gui.cpp:968
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:50
void ToggleSortOrder()
Toggle the sort order Since that is the worst condition for the sort function reverse the list here...
Change the name of this town.
Definition: town_widget.h:43
void Remove(const T &element)
Remove a single element from the tree, if it exists.
Definition: kdtree.hpp:420
Searching and filtering using a stringterm.
Sort ascending.
Definition: window_gui.h:226
Show the town authority window.
Definition: town_widget.h:42
Town * town
Town displayed by the window.
Definition: town_gui.cpp:320
Vertical container.
Definition: widget_type.h:77
uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t)
Get a list of available actions to do at a town.
Definition: town_cmd.cpp:3205
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
uint32 townnameparts
Generated town name.
Definition: town_gui.cpp:1140
Town view; Window numbers:
Definition: window_type.h:328
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:174
Selection for the 2x2 grid town layout.
Definition: town_widget.h:63
Selection for the original town layout.
Definition: town_widget.h:61
void OnResize() override
Called after the window got resized.
Definition: town_gui.cpp:963
TownCache cache
Container for all cacheable data.
Definition: town.h:58
TownLayout town_layout
Selected town layout.
Definition: town_gui.cpp:1136
Selection for a randomly sized town.
Definition: town_widget.h:59
Town data structure.
Definition: town.h:55
void OnPlaceObject(Point pt, TileIndex tile) override
The user clicked some place on the map when a tile highlight mode has been set.
Definition: town_gui.cpp:1259
bool SetFocusedWidget(int widget_index)
Set focus within this window to the given widget.
Definition: window.cpp:488
bool confirm
Play sound effect on successful constructions or other actions.
Scrollbar for the town list.
Definition: town_widget.h:21
Functions related to OTTD&#39;s landscape.
bool ScrollMainWindowToTile(TileIndex tile, bool instant)
Scrolls the viewport of the main window to a given location.
Definition: viewport.cpp:2417
Non-water non-rail construction.
Definition: sound_type.h:70
static bool TownNameSorter(const Town *const &a, const Town *const &b)
Sort by town name.
Definition: town_gui.cpp:711
End of town effects.
Definition: cargotype.h:34
Functions related to commands.
bool larger_town
if this is a larger town and should grow more quickly
Definition: town.h:101
bool _network_server
network-server is active
Definition: network.cpp:55
Coordinates of a point in 2D.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:767
Struct holding parameters used to generate town name.
Definition: townname_type.h:29
byte GetSnowLine()
Get the current snow line, either variable or static.
Definition: landscape.cpp:646
TownNameParams params
Town name parameters.
Definition: town_gui.cpp:1141
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:235
static uint TileHeight(TileIndex tile)
Returns the height of a tile.
Definition: tile_map.h:31
Data structure describing how to show the list (what sort direction and criteria).
Definition: sortlist_type.h:33
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: town_gui.cpp:780
Drop down list.
Definition: widget_type.h:70
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:622
Town authority window.
Definition: town_gui.cpp:70
static const uint MAX_LENGTH_TOWN_NAME_CHARS
The maximum length of a town name in characters including &#39;\0&#39;.
Definition: town_type.h:110
Window does not do autoscroll,.
Definition: window_gui.h:241
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: town_gui.cpp:554
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:19
Types related to the town widgets.
K-dimensional tree, specialised for 2-dimensional space.
Definition: kdtree.hpp:38
static const StringID sorter_names[]
Names of the sorting functions.
Definition: town_gui.cpp:680
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:63
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:66
Used for DoCommand-like (and some non-fatal AI GUI) errors/information.
Definition: error.h:23
void Clear()
Clear the tree.
Definition: kdtree.hpp:380
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:321
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:983
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: town_gui.cpp:360
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Town name generator stuff.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Base of the town class.
bool IsDescSortOrder() const
Check if the sort order is descending.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: town_gui.cpp:803
#define CMD_MSG(x)
Used to combine a StringID with the command.
Definition: command_type.h:370
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: town_gui.cpp:189
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:707
TransportedCargoStat< uint32 > supplied[NUM_CARGO]
Cargo statistics about supplied cargo.
Definition: town.h:79
void SetCapacityFromWidget(Window *w, int widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget. ...
Definition: widget.cpp:1973
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3374
Specification of a rectangle with absolute coordinates of all edges.
Vertical scrollbar.
Definition: widget_type.h:84
static bool TownPopulationSorter(const Town *const &a, const Town *const &b)
Sort by population (default descending, as big towns are of the most interest).
Definition: town_gui.cpp:732
bool IsShaded() const
Is window shaded currently?
Definition: window_gui.h:526
Text is written right-to-left by default.
Definition: strings_type.h:26
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:314
Functions related to tile highlights.
Window functions not directly related to making/drawing windows.
uint32 num_houses
Amount of houses.
Definition: town.h:46
Find a place automatically.
Definition: window_gui.h:156
Conditions for town growth are met. Grow according to Town::growth_rate.
Definition: town.h:177
void OnPlaceObjectAbort() override
The user cancelled a tile highlight mode that has been set.
Definition: town_gui.cpp:1264
ViewportData * viewport
Pointer to viewport data, if present.
Definition: window_gui.h:328
GUI functions that shouldn&#39;t be here.
bool SortFunction(const T &, const T &)
Signature of sort function.
Definition: sortlist_type.h:51
static int GetNthSetBit(uint32 bits, int n)
Get the position of the Nth set bit.
Definition: town_gui.cpp:86
uint GetDesiredInfoHeight(int width) const
Gets the desired height for the information panel.
Definition: town_gui.cpp:502
void Insert(const T &element)
Insert a single element in the tree.
Definition: kdtree.hpp:401
void ResizeWindow(Window *w, int delta_x, int delta_y, bool clamp_to_screen)
Resize the window.
Definition: window.cpp:2126
CompanyID exclusivity
which company has exclusivity
Definition: town.h:75
Selection for the town&#39;s city state.
Definition: town_widget.h:60
static NWidgetPart SetScrollbar(int index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1095
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
Found a town window class.
Definition: town_gui.cpp:1133
Dimensions (a width and height) of a rectangle in 2D.
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:429
bool IsEmpty() const
Check whether any filter words were entered.
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:62
Window * BringWindowToFrontById(WindowClass cls, WindowNumber number)
Find a window and make it the relative top-window on the screen.
Definition: window.cpp:1243
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:64
View of the center of the town.
Definition: town_widget.h:39
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:832
static NWidgetPart SetPIP(uint8 pre, uint8 inter, uint8 post)
Widget part function for setting a pre/inter/post spaces.
Definition: widget_type.h:1076
Default zoom level for the news messages.
Definition: zoom_type.h:36
int pos_y
Vertical position of top-left corner of the widget in the window.
Definition: widget_type.h:178
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window&#39;s data as invalid (in need of re-computing)
Definition: window.cpp:3240
Tstorage old_act
Actually transported last month.
Definition: town_type.h:117
CompanyMask have_ratings
which companies have a rating
Definition: town.h:73
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1459
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:620
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition: textbuf.cpp:432
(Toggle) Button with text
Definition: widget_type.h:55
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:631
uint8 SortType() const
Get the sorttype of the list.
Definition: sortlist_type.h:96
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201