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/>.
10 /** @file town_gui.cpp GUI for towns. */
14 #include "viewport_func.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"
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"
34 #include "widgets/dropdown_func.h"
36 #include "widgets/town_widget.h"
38 #include "table/strings.h"
40 #include "safeguards.h"
42 typedef GUIList
<const Town
*> GUITownList
;
44 static const NWidgetPart _nested_town_authority_widgets
[] = {
45 NWidget(NWID_HORIZONTAL
),
46 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
47 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TA_CAPTION
), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
48 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
49 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
50 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
52 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TA_RATING_INFO
), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
53 NWidget(NWID_HORIZONTAL
),
54 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TA_COMMAND_LIST
), SetMinimalSize(305, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP
), SetScrollbar(WID_TA_SCROLLBAR
), EndContainer(),
55 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_TA_SCROLLBAR
),
57 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TA_ACTION_INFO
), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
58 NWidget(NWID_HORIZONTAL
),
59 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TA_EXECUTE
), SetMinimalSize(317, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_LOCAL_AUTHORITY_DO_IT_BUTTON
, STR_LOCAL_AUTHORITY_DO_IT_TOOLTIP
),
60 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
64 /** Town authority window. */
65 struct TownAuthorityWindow
: Window
{
67 Town
*town
; ///< Town being displayed.
68 int sel_index
; ///< Currently selected town action, \c 0 to \c TACT_COUNT-1, \c -1 means no action selected.
70 uint displayed_actions_on_previous_painting
; ///< Actions that were available on the previous call to OnPaint()
73 * Get the position of the Nth set bit.
75 * If there is no Nth bit set return -1
77 * @param bits The value to search in
78 * @param n The Nth set bit from which we want to know the position
79 * @return The position of the Nth set bit
81 static int GetNthSetBit(uint32 bits
, int n
)
85 FOR_EACH_SET_BIT(i
, bits
) {
94 TownAuthorityWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
), sel_index(-1), displayed_actions_on_previous_painting(0)
96 this->town
= Town::Get(window_number
);
97 this->InitNested(window_number
);
98 this->vscroll
= this->GetScrollbar(WID_TA_SCROLLBAR
);
99 this->vscroll
->SetCapacity((this->GetWidget
<NWidgetBase
>(WID_TA_COMMAND_LIST
)->current_y
- WD_FRAMERECT_TOP
- WD_FRAMERECT_BOTTOM
) / FONT_HEIGHT_NORMAL
);
102 void OnPaint() override
105 uint buttons
= GetMaskOfTownActions(&numact
, _local_company
, this->town
);
106 if (buttons
!= displayed_actions_on_previous_painting
) this->SetDirty();
107 displayed_actions_on_previous_painting
= buttons
;
109 this->vscroll
->SetCount(numact
+ 1);
111 if (this->sel_index
!= -1 && !HasBit(buttons
, this->sel_index
)) {
112 this->sel_index
= -1;
115 this->SetWidgetDisabledState(WID_TA_EXECUTE
, this->sel_index
== -1);
118 if (!this->IsShaded()) this->DrawRatings();
121 /** Draw the contents of the ratings panel. May request a resize of the window if the contents does not fit. */
124 NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(WID_TA_RATING_INFO
);
125 uint left
= nwid
->pos_x
+ WD_FRAMERECT_LEFT
;
126 uint right
= nwid
->pos_x
+ nwid
->current_x
- 1 - WD_FRAMERECT_RIGHT
;
128 uint y
= nwid
->pos_y
+ WD_FRAMERECT_TOP
;
130 DrawString(left
, right
, y
, STR_LOCAL_AUTHORITY_COMPANY_RATINGS
);
131 y
+= FONT_HEIGHT_NORMAL
;
133 Dimension icon_size
= GetSpriteSize(SPR_COMPANY_ICON
);
134 int icon_width
= icon_size
.width
;
135 int icon_y_offset
= (FONT_HEIGHT_NORMAL
- icon_size
.height
) / 2;
137 Dimension exclusive_size
= GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT
);
138 int exclusive_width
= exclusive_size
.width
;
139 int exclusive_y_offset
= (FONT_HEIGHT_NORMAL
- exclusive_size
.height
) / 2;
141 bool rtl
= _current_text_dir
== TD_RTL
;
142 uint text_left
= left
+ (rtl
? 0 : icon_width
+ exclusive_width
+ 4);
143 uint text_right
= right
- (rtl
? icon_width
+ exclusive_width
+ 4 : 0);
144 uint icon_left
= rtl
? right
- icon_width
: left
;
145 uint exclusive_left
= rtl
? right
- icon_width
- exclusive_width
- 2 : left
+ icon_width
+ 2;
147 /* Draw list of companies */
149 FOR_ALL_COMPANIES(c
) {
150 if ((HasBit(this->town
->have_ratings
, c
->index
) || this->town
->exclusivity
== c
->index
)) {
151 DrawCompanyIcon(c
->index
, icon_left
, y
+ icon_y_offset
);
153 SetDParam(0, c
->index
);
154 SetDParam(1, c
->index
);
156 int r
= this->town
->ratings
[c
->index
];
157 StringID str
= STR_CARGO_RATING_APPALLING
;
158 if (r
> RATING_APPALLING
) str
++;
159 if (r
> RATING_VERYPOOR
) str
++;
160 if (r
> RATING_POOR
) str
++;
161 if (r
> RATING_MEDIOCRE
) str
++;
162 if (r
> RATING_GOOD
) str
++;
163 if (r
> RATING_VERYGOOD
) str
++;
164 if (r
> RATING_EXCELLENT
) str
++;
167 if (this->town
->exclusivity
== c
->index
) {
168 DrawSprite(SPR_EXCLUSIVE_TRANSPORT
, COMPANY_SPRITE_COLOUR(c
->index
), exclusive_left
, y
+ exclusive_y_offset
);
171 DrawString(text_left
, text_right
, y
, STR_LOCAL_AUTHORITY_COMPANY_RATING
);
172 y
+= FONT_HEIGHT_NORMAL
;
176 y
= y
+ WD_FRAMERECT_BOTTOM
- nwid
->pos_y
; // Compute needed size of the widget.
177 if (y
> nwid
->current_y
) {
178 /* If the company list is too big to fit, mark ourself dirty and draw again. */
179 ResizeWindow(this, 0, y
- nwid
->current_y
, false);
183 void SetStringParameters(int widget
) const override
185 if (widget
== WID_TA_CAPTION
) SetDParam(0, this->window_number
);
188 void DrawWidget(const Rect
&r
, int widget
) const override
191 case WID_TA_ACTION_INFO
:
192 if (this->sel_index
!= -1) {
193 SetDParam(0, _price
[PR_TOWN_ACTION
] * _town_action_costs
[this->sel_index
] >> 8);
194 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, r
.bottom
- WD_FRAMERECT_BOTTOM
,
195 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING
+ this->sel_index
);
198 case WID_TA_COMMAND_LIST
: {
200 uint buttons
= GetMaskOfTownActions(&numact
, _local_company
, this->town
);
201 int y
= r
.top
+ WD_FRAMERECT_TOP
;
202 int pos
= this->vscroll
->GetPosition();
205 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_LOCAL_AUTHORITY_ACTIONS_TITLE
);
206 y
+= FONT_HEIGHT_NORMAL
;
209 for (int i
= 0; buttons
; i
++, buttons
>>= 1) {
210 if (pos
<= -5) break; ///< Draw only the 5 fitting lines
212 if ((buttons
& 1) && --pos
< 0) {
213 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
,
214 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN
+ i
, this->sel_index
== i
? TC_WHITE
: TC_ORANGE
);
215 y
+= FONT_HEIGHT_NORMAL
;
223 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
226 case WID_TA_ACTION_INFO
: {
227 assert(size
->width
> padding
.width
&& size
->height
> padding
.height
);
228 size
->width
-= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
229 size
->height
-= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
230 Dimension d
= {0, 0};
231 for (int i
= 0; i
< TACT_COUNT
; i
++) {
232 SetDParam(0, _price
[PR_TOWN_ACTION
] * _town_action_costs
[i
] >> 8);
233 d
= maxdim(d
, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING
+ i
, *size
));
235 *size
= maxdim(*size
, d
);
236 size
->width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
237 size
->height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
241 case WID_TA_COMMAND_LIST
:
242 size
->height
= WD_FRAMERECT_TOP
+ 5 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
243 size
->width
= GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE
).width
;
244 for (uint i
= 0; i
< TACT_COUNT
; i
++ ) {
245 size
->width
= max(size
->width
, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN
+ i
).width
);
247 size
->width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
250 case WID_TA_RATING_INFO
:
251 resize
->height
= FONT_HEIGHT_NORMAL
;
252 size
->height
= WD_FRAMERECT_TOP
+ 9 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
257 void OnClick(Point pt
, int widget
, int click_count
) override
260 case WID_TA_COMMAND_LIST
: {
261 int y
= this->GetRowFromWidget(pt
.y
, WID_TA_COMMAND_LIST
, 1, FONT_HEIGHT_NORMAL
);
262 if (!IsInsideMM(y
, 0, 5)) return;
264 y
= GetNthSetBit(GetMaskOfTownActions(nullptr, _local_company
, this->town
), y
+ this->vscroll
->GetPosition() - 1);
269 /* When double-clicking, continue */
270 if (click_count
== 1 || y
< 0) break;
275 DoCommandP(this->town
->xy
, this->window_number
, this->sel_index
, CMD_DO_TOWN_ACTION
| CMD_MSG(STR_ERROR_CAN_T_DO_THIS
));
280 void OnHundredthTick() override
286 static WindowDesc
_town_authority_desc(
287 WDP_AUTO
, "view_town_authority", 317, 222,
288 WC_TOWN_AUTHORITY
, WC_NONE
,
290 _nested_town_authority_widgets
, lengthof(_nested_town_authority_widgets
)
293 static void ShowTownAuthorityWindow(uint town
)
295 AllocateWindowDescFront
<TownAuthorityWindow
>(&_town_authority_desc
, town
);
299 /* Town view window. */
300 struct TownViewWindow
: Window
{
302 Town
*town
; ///< Town displayed by the window.
305 static const int WID_TV_HEIGHT_NORMAL
= 150;
307 TownViewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
309 this->CreateNestedTree();
311 this->town
= Town::Get(window_number
);
312 if (this->town
->larger_town
) this->GetWidget
<NWidgetCore
>(WID_TV_CAPTION
)->widget_data
= STR_TOWN_VIEW_CITY_CAPTION
;
314 this->FinishInitNested(window_number
);
316 this->flags
|= WF_DISABLE_VP_SCROLL
;
317 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_TV_VIEWPORT
);
318 nvp
->InitializeViewport(this, this->town
->xy
, ZOOM_LVL_NEWS
);
320 /* disable renaming town in network games if you are not the server */
321 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME
, _networking
&& !_network_server
);
324 void SetStringParameters(int widget
) const override
326 if (widget
== WID_TV_CAPTION
) SetDParam(0, this->town
->index
);
329 void DrawWidget(const Rect
&r
, int widget
) const override
331 if (widget
!= WID_TV_INFO
) return;
333 uint y
= r
.top
+ WD_FRAMERECT_TOP
;
335 SetDParam(0, this->town
->cache
.population
);
336 SetDParam(1, this->town
->cache
.num_houses
);
337 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
, STR_TOWN_VIEW_POPULATION_HOUSES
);
339 SetDParam(0, 1 << CT_PASSENGERS
);
340 SetDParam(1, this->town
->supplied
[CT_PASSENGERS
].old_act
);
341 SetDParam(2, 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_CARGO_LAST_MONTH_MAX
);
344 SetDParam(0, 1 << CT_MAIL
);
345 SetDParam(1, this->town
->supplied
[CT_MAIL
].old_act
);
346 SetDParam(2, this->town
->supplied
[CT_MAIL
].old_max
);
347 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX
);
350 for (int i
= TE_BEGIN
; i
< TE_END
; i
++) {
351 if (this->town
->goal
[i
] == 0) continue;
352 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& (TileHeight(this->town
->xy
) < LowestSnowLine() || this->town
->cache
.population
<= 90)) continue;
353 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
&& (GetTropicZone(this->town
->xy
) != TROPICZONE_DESERT
|| this->town
->cache
.population
<= 60)) continue;
356 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH
);
360 bool rtl
= _current_text_dir
== TD_RTL
;
361 uint cargo_text_left
= r
.left
+ WD_FRAMERECT_LEFT
+ (rtl
? 0 : 20);
362 uint cargo_text_right
= r
.right
- WD_FRAMERECT_RIGHT
- (rtl
? 20 : 0);
364 const CargoSpec
*cargo
= FindFirstCargoWithTownEffect((TownEffect
)i
);
365 assert(cargo
!= nullptr);
369 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
|| this->town
->goal
[i
] == TOWN_GROWTH_WINTER
) {
370 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
371 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL
;
372 if (this->town
->received
[i
].old_act
== 0) {
373 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL
;
375 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& TileHeight(this->town
->xy
) < GetSnowLine()) {
376 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER
;
380 SetDParam(0, cargo
->name
);
382 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED
;
383 if (this->town
->received
[i
].old_act
< this->town
->goal
[i
]) {
384 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED
;
387 SetDParam(0, cargo
->Index());
388 SetDParam(1, this->town
->received
[i
].old_act
);
389 SetDParam(2, cargo
->Index());
390 SetDParam(3, this->town
->goal
[i
]);
392 DrawString(cargo_text_left
, cargo_text_right
, y
+= FONT_HEIGHT_NORMAL
, string
);
395 if (HasBit(this->town
->flags
, TOWN_IS_GROWING
)) {
396 SetDParam(0, RoundDivSU(this->town
->growth_rate
+ 1, DAY_TICKS
));
397 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
);
399 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_TOWN_GROW_STOPPED
);
402 /* only show the town noise, if the noise option is activated. */
403 if (_settings_game
.economy
.station_noise_level
) {
404 SetDParam(0, this->town
->noise_reached
);
405 SetDParam(1, this->town
->MaxTownNoise());
406 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_NOISE_IN_TOWN
);
409 if (this->town
->text
!= nullptr) {
410 SetDParamStr(0, this->town
->text
);
411 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
+= FONT_HEIGHT_NORMAL
, UINT16_MAX
, STR_JUST_RAW_STRING
, TC_BLACK
);
415 void OnClick(Point pt
, int widget
, int click_count
) override
418 case WID_TV_CENTER_VIEW
: // scroll to location
420 ShowExtraViewPortWindow(this->town
->xy
);
422 ScrollMainWindowToTile(this->town
->xy
);
426 case WID_TV_SHOW_AUTHORITY
: // town authority
427 ShowTownAuthorityWindow(this->window_number
);
430 case WID_TV_CHANGE_NAME
: // rename
431 SetDParam(0, this->window_number
);
432 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
);
435 case WID_TV_EXPAND
: { // expand town - only available on Scenario editor
436 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
437 static bool _warn_town_no_roads
= false;
439 if (!_settings_game
.economy
.allow_town_roads
&& !_warn_town_no_roads
) {
440 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS
, INVALID_STRING_ID
, WL_WARNING
);
441 _warn_town_no_roads
= true;
444 DoCommandP(0, this->window_number
, 0, CMD_EXPAND_TOWN
| CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN
));
448 case WID_TV_DELETE
: // delete town - only available on Scenario editor
449 DoCommandP(0, this->window_number
, 0, CMD_DELETE_TOWN
| CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE
));
454 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
458 size
->height
= GetDesiredInfoHeight(size
->width
);
464 * Gets the desired height for the information panel.
465 * @return the desired height in pixels.
467 uint
GetDesiredInfoHeight(int width
) const
469 uint aimed_height
= 3 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
472 for (int i
= TE_BEGIN
; i
< TE_END
; i
++) {
473 if (this->town
->goal
[i
] == 0) continue;
474 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& (TileHeight(this->town
->xy
) < LowestSnowLine() || this->town
->cache
.population
<= 90)) continue;
475 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
&& (GetTropicZone(this->town
->xy
) != TROPICZONE_DESERT
|| this->town
->cache
.population
<= 60)) continue;
478 aimed_height
+= FONT_HEIGHT_NORMAL
;
481 aimed_height
+= FONT_HEIGHT_NORMAL
;
483 aimed_height
+= FONT_HEIGHT_NORMAL
;
485 if (_settings_game
.economy
.station_noise_level
) aimed_height
+= FONT_HEIGHT_NORMAL
;
487 if (this->town
->text
!= nullptr) {
488 SetDParamStr(0, this->town
->text
);
489 aimed_height
+= GetStringHeight(STR_JUST_RAW_STRING
, width
- WD_FRAMERECT_LEFT
- WD_FRAMERECT_RIGHT
);
495 void ResizeWindowAsNeeded()
497 const NWidgetBase
*nwid_info
= this->GetWidget
<NWidgetBase
>(WID_TV_INFO
);
498 uint aimed_height
= GetDesiredInfoHeight(nwid_info
->current_x
);
499 if (aimed_height
> nwid_info
->current_y
|| (aimed_height
< nwid_info
->current_y
&& nwid_info
->current_y
> nwid_info
->smallest_y
)) {
504 void OnResize() override
506 if (this->viewport
!= nullptr) {
507 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_TV_VIEWPORT
);
508 nvp
->UpdateViewportCoordinates(this);
510 ScrollWindowToTile(this->town
->xy
, this, true); // Re-center viewport.
515 * Some data on this window has become invalid.
516 * @param data Information about the changed data.
517 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
519 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
521 if (!gui_scope
) return;
522 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
523 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
524 this->ResizeWindowAsNeeded();
527 void OnQueryTextFinished(char *str
) override
529 if (str
== nullptr) return;
531 DoCommandP(0, this->window_number
, 0, CMD_RENAME_TOWN
| CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN
), nullptr, str
);
535 static const NWidgetPart _nested_town_game_view_widgets
[] = {
536 NWidget(NWID_HORIZONTAL
),
537 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
538 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TV_CAPTION
), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
539 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
540 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
541 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
543 NWidget(WWT_PANEL
, COLOUR_BROWN
),
544 NWidget(WWT_INSET
, COLOUR_BROWN
), SetPadding(2, 2, 2, 2),
545 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_TV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
548 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TV_INFO
), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
549 NWidget(NWID_HORIZONTAL
),
550 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
551 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
),
552 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
),
553 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
),
555 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
559 static WindowDesc
_town_game_view_desc(
560 WDP_AUTO
, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL
,
561 WC_TOWN_VIEW
, WC_NONE
,
563 _nested_town_game_view_widgets
, lengthof(_nested_town_game_view_widgets
)
566 static const NWidgetPart _nested_town_editor_view_widgets
[] = {
567 NWidget(NWID_HORIZONTAL
),
568 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
569 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TV_CAPTION
), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
570 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_CHANGE_NAME
), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME
, STR_TOWN_VIEW_RENAME_TOOLTIP
),
571 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
572 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
573 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
575 NWidget(WWT_PANEL
, COLOUR_BROWN
),
576 NWidget(WWT_INSET
, COLOUR_BROWN
), SetPadding(2, 2, 2, 2),
577 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_TV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
580 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TV_INFO
), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
581 NWidget(NWID_HORIZONTAL
),
582 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
583 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
),
584 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
),
585 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
),
587 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
591 static WindowDesc
_town_editor_view_desc(
592 WDP_AUTO
, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL
,
593 WC_TOWN_VIEW
, WC_NONE
,
595 _nested_town_editor_view_widgets
, lengthof(_nested_town_editor_view_widgets
)
598 void ShowTownViewWindow(TownID town
)
600 if (_game_mode
== GM_EDITOR
) {
601 AllocateWindowDescFront
<TownViewWindow
>(&_town_editor_view_desc
, town
);
603 AllocateWindowDescFront
<TownViewWindow
>(&_town_game_view_desc
, town
);
607 static const NWidgetPart _nested_town_directory_widgets
[] = {
608 NWidget(NWID_HORIZONTAL
),
609 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
610 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_TOWN_DIRECTORY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
611 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
612 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
613 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
615 NWidget(NWID_HORIZONTAL
),
616 NWidget(NWID_VERTICAL
),
617 NWidget(NWID_HORIZONTAL
),
618 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_TD_SORT_ORDER
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
619 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_TD_SORT_CRITERIA
), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
620 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetResize(1, 0), EndContainer(),
622 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TD_LIST
), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP
),
623 SetFill(1, 0), SetResize(0, 10), SetScrollbar(WID_TD_SCROLLBAR
), EndContainer(),
624 NWidget(WWT_PANEL
, COLOUR_BROWN
),
625 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
),
628 NWidget(NWID_VERTICAL
),
629 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_TD_SCROLLBAR
),
630 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
635 /** Town directory window class. */
636 struct TownDirectoryWindow
: public Window
{
638 /* Runtime saved values */
639 static Listing last_sorting
;
640 static const Town
*last_town
;
642 /* Constants for sorting towns */
643 static const StringID sorter_names
[];
644 static GUITownList::SortFunction
* const sorter_funcs
[];
650 void BuildSortTownList()
652 if (this->towns
.NeedRebuild()) {
657 this->towns
.push_back(t
);
660 this->towns
.shrink_to_fit();
661 this->towns
.RebuildDone();
662 this->vscroll
->SetCount((uint
)this->towns
.size()); // Update scrollbar as well.
664 /* Always sort the towns. */
665 this->last_town
= nullptr;
667 this->SetWidgetDirty(WID_TD_LIST
); // Force repaint of the displayed towns.
670 /** Sort by town name */
671 static int CDECL
TownNameSorter(const Town
* const *a
, const Town
* const *b
)
673 static char buf_cache
[64];
678 SetDParam(0, ta
->index
);
679 GetString(buf
, STR_TOWN_NAME
, lastof(buf
));
681 /* If 'b' is the same town as in the last round, use the cached value
682 * We do this to speed stuff up ('b' is called with the same value a lot of
683 * times after each other) */
684 if (tb
!= last_town
) {
686 SetDParam(0, tb
->index
);
687 GetString(buf_cache
, STR_TOWN_NAME
, lastof(buf_cache
));
690 return strnatcmp(buf
, buf_cache
); // Sort by name (natural sorting).
693 /** Sort by population (default descending, as big towns are of the most interest). */
694 static int CDECL
TownPopulationSorter(const Town
* const *a
, const Town
* const *b
)
696 uint32 a_population
= (*a
)->cache
.population
;
697 uint32 b_population
= (*b
)->cache
.population
;
698 if (a_population
== b_population
) return TownDirectoryWindow::TownNameSorter(a
, b
);
699 return (a_population
< b_population
) ? -1 : 1;
702 /** Sort by town rating */
703 static int CDECL
TownRatingSorter(const Town
* const *a
, const Town
* const *b
)
705 int before
= TownDirectoryWindow::last_sorting
.order
? 1 : -1; // Value to get 'a' before 'b'.
707 /* Towns without rating are always after towns with rating. */
708 if (HasBit((*a
)->have_ratings
, _local_company
)) {
709 if (HasBit((*b
)->have_ratings
, _local_company
)) {
710 int16 a_rating
= (*a
)->ratings
[_local_company
];
711 int16 b_rating
= (*b
)->ratings
[_local_company
];
712 if (a_rating
== b_rating
) return TownDirectoryWindow::TownNameSorter(a
, b
);
713 return (a_rating
< b_rating
) ? -1 : 1;
717 if (HasBit((*b
)->have_ratings
, _local_company
)) return -before
;
718 return -before
* TownDirectoryWindow::TownNameSorter(a
, b
); // Sort unrated towns always on ascending town name.
722 TownDirectoryWindow(WindowDesc
*desc
) : Window(desc
)
724 this->CreateNestedTree();
726 this->vscroll
= this->GetScrollbar(WID_TD_SCROLLBAR
);
728 this->towns
.SetListing(this->last_sorting
);
729 this->towns
.SetSortFuncs(TownDirectoryWindow::sorter_funcs
);
730 this->towns
.ForceRebuild();
731 this->BuildSortTownList();
733 this->FinishInitNested(0);
736 void SetStringParameters(int widget
) const override
739 case WID_TD_WORLD_POPULATION
:
740 SetDParam(0, GetWorldPopulation());
743 case WID_TD_SORT_CRITERIA
:
744 SetDParam(0, TownDirectoryWindow::sorter_names
[this->towns
.SortType()]);
750 * Get the string to draw the town name.
751 * @param t Town to draw.
752 * @return The string to use.
754 static StringID
GetTownString(const Town
*t
)
756 return t
->larger_town
? STR_TOWN_DIRECTORY_CITY
: STR_TOWN_DIRECTORY_TOWN
;
759 void DrawWidget(const Rect
&r
, int widget
) const override
762 case WID_TD_SORT_ORDER
:
763 this->DrawSortButtonState(widget
, this->towns
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
768 int y
= r
.top
+ WD_FRAMERECT_TOP
;
769 if (this->towns
.size() == 0) { // No towns available.
770 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
, y
, STR_TOWN_DIRECTORY_NONE
);
774 /* At least one town available. */
775 bool rtl
= _current_text_dir
== TD_RTL
;
776 Dimension icon_size
= GetSpriteSize(SPR_TOWN_RATING_GOOD
);
777 int text_left
= r
.left
+ WD_FRAMERECT_LEFT
+ (rtl
? 0 : icon_size
.width
+ 2);
778 int text_right
= r
.right
- WD_FRAMERECT_RIGHT
- (rtl
? icon_size
.width
+ 2 : 0);
779 int icon_x
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
- icon_size
.width
: r
.left
+ WD_FRAMERECT_LEFT
;
781 for (uint i
= this->vscroll
->GetPosition(); i
< this->towns
.size(); i
++) {
782 const Town
*t
= this->towns
[i
];
783 assert(t
->xy
!= INVALID_TILE
);
785 /* Draw rating icon. */
786 if (_game_mode
== GM_EDITOR
|| !HasBit(t
->have_ratings
, _local_company
)) {
787 DrawSprite(SPR_TOWN_RATING_NA
, PAL_NONE
, icon_x
, y
+ (this->resize
.step_height
- icon_size
.height
) / 2);
789 SpriteID icon
= SPR_TOWN_RATING_APALLING
;
790 if (t
->ratings
[_local_company
] > RATING_VERYPOOR
) icon
= SPR_TOWN_RATING_MEDIOCRE
;
791 if (t
->ratings
[_local_company
] > RATING_GOOD
) icon
= SPR_TOWN_RATING_GOOD
;
792 DrawSprite(icon
, PAL_NONE
, icon_x
, y
+ (this->resize
.step_height
- icon_size
.height
) / 2);
795 SetDParam(0, t
->index
);
796 SetDParam(1, t
->cache
.population
);
797 DrawString(text_left
, text_right
, y
+ (this->resize
.step_height
- FONT_HEIGHT_NORMAL
) / 2, GetTownString(t
));
799 y
+= this->resize
.step_height
;
800 if (++n
== this->vscroll
->GetCapacity()) break; // max number of towns in 1 window
807 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
810 case WID_TD_SORT_ORDER
: {
811 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
812 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
813 d
.height
+= padding
.height
;
814 *size
= maxdim(*size
, d
);
817 case WID_TD_SORT_CRITERIA
: {
818 Dimension d
= {0, 0};
819 for (uint i
= 0; TownDirectoryWindow::sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
820 d
= maxdim(d
, GetStringBoundingBox(TownDirectoryWindow::sorter_names
[i
]));
822 d
.width
+= padding
.width
;
823 d
.height
+= padding
.height
;
824 *size
= maxdim(*size
, d
);
828 Dimension d
= GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE
);
829 for (uint i
= 0; i
< this->towns
.size(); i
++) {
830 const Town
*t
= this->towns
[i
];
832 assert(t
!= nullptr);
834 SetDParam(0, t
->index
);
835 SetDParamMaxDigits(1, 8);
836 d
= maxdim(d
, GetStringBoundingBox(GetTownString(t
)));
838 Dimension icon_size
= GetSpriteSize(SPR_TOWN_RATING_GOOD
);
839 d
.width
+= icon_size
.width
+ 2;
840 d
.height
= max(d
.height
, icon_size
.height
);
841 resize
->height
= d
.height
;
843 d
.width
+= padding
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
844 d
.height
+= padding
.height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
845 *size
= maxdim(*size
, d
);
848 case WID_TD_WORLD_POPULATION
: {
849 SetDParamMaxDigits(0, 10);
850 Dimension d
= GetStringBoundingBox(STR_TOWN_POPULATION
);
851 d
.width
+= padding
.width
;
852 d
.height
+= padding
.height
;
853 *size
= maxdim(*size
, d
);
859 void OnClick(Point pt
, int widget
, int click_count
) override
862 case WID_TD_SORT_ORDER
: // Click on sort order button
863 if (this->towns
.SortType() != 2) { // A different sort than by rating.
864 this->towns
.ToggleSortOrder();
865 this->last_sorting
= this->towns
.GetListing(); // Store new sorting order.
867 /* Some parts are always sorted ascending on name. */
868 this->last_sorting
.order
= !this->last_sorting
.order
;
869 this->towns
.SetListing(this->last_sorting
);
870 this->towns
.ForceResort();
876 case WID_TD_SORT_CRITERIA
: // Click on sort criteria dropdown
877 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names
, this->towns
.SortType(), WID_TD_SORT_CRITERIA
, 0, 0);
880 case WID_TD_LIST
: { // Click on Town Matrix
881 uint id_v
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_TD_LIST
, WD_FRAMERECT_TOP
);
882 if (id_v
>= this->towns
.size()) return; // click out of town bounds
884 const Town
*t
= this->towns
[id_v
];
885 assert(t
!= nullptr);
887 ShowExtraViewPortWindow(t
->xy
);
889 ScrollMainWindowToTile(t
->xy
);
896 void OnDropdownSelect(int widget
, int index
) override
898 if (widget
!= WID_TD_SORT_CRITERIA
) return;
900 if (this->towns
.SortType() != index
) {
901 this->towns
.SetSortType(index
);
902 this->last_sorting
= this->towns
.GetListing(); // Store new sorting order.
903 this->BuildSortTownList();
907 void OnPaint() override
909 if (this->towns
.NeedRebuild()) this->BuildSortTownList();
913 void OnHundredthTick() override
915 this->BuildSortTownList();
919 void OnResize() override
921 this->vscroll
->SetCapacityFromWidget(this, WID_TD_LIST
);
925 * Some data on this window has become invalid.
926 * @param data Information about the changed data.
927 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
929 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
932 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
933 this->towns
.ForceRebuild();
935 this->towns
.ForceResort();
940 Listing
TownDirectoryWindow::last_sorting
= {false, 0};
941 const Town
*TownDirectoryWindow::last_town
= nullptr;
943 /** Names of the sorting functions. */
944 const StringID
TownDirectoryWindow::sorter_names
[] = {
946 STR_SORT_BY_POPULATION
,
951 /** Available town directory sorting functions. */
952 GUITownList::SortFunction
* const TownDirectoryWindow::sorter_funcs
[] = {
954 &TownPopulationSorter
,
958 static WindowDesc
_town_directory_desc(
959 WDP_AUTO
, "list_towns", 208, 202,
960 WC_TOWN_DIRECTORY
, WC_NONE
,
962 _nested_town_directory_widgets
, lengthof(_nested_town_directory_widgets
)
965 void ShowTownDirectory()
967 if (BringWindowToFrontById(WC_TOWN_DIRECTORY
, 0)) return;
968 new TownDirectoryWindow(&_town_directory_desc
);
971 void CcFoundTown(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
973 if (result
.Failed()) return;
975 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_1F_SPLAT_OTHER
, tile
);
976 if (!_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
979 void CcFoundRandomTown(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
981 if (result
.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id
)->xy
);
984 static const NWidgetPart _nested_found_town_widgets
[] = {
985 NWidget(NWID_HORIZONTAL
),
986 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
987 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_FOUND_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
988 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
989 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
991 /* Construct new town(s) buttons. */
992 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
993 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
994 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_NEW_TOWN
), SetMinimalSize(156, 12), SetFill(1, 0),
995 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON
, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP
), SetPadding(0, 2, 1, 2),
996 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_RANDOM_TOWN
), SetMinimalSize(156, 12), SetFill(1, 0),
997 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON
, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP
), SetPadding(0, 2, 1, 2),
998 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_MANY_RANDOM_TOWNS
), SetMinimalSize(156, 12), SetFill(1, 0),
999 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS
, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP
), SetPadding(0, 2, 0, 2),
1000 /* Town name selection. */
1001 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE
, STR_NULL
),
1002 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_TF_TOWN_NAME_EDITBOX
), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
1003 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE
, STR_FOUND_TOWN_NAME_EDITOR_HELP
),
1004 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_TOWN_NAME_RANDOM
), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
1005 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON
, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP
),
1006 /* Town size selection. */
1007 NWidget(NWID_HORIZONTAL
), SetPIP(2, 0, 2),
1008 NWidget(NWID_SPACER
), SetFill(1, 0),
1009 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE
, STR_NULL
),
1010 NWidget(NWID_SPACER
), SetFill(1, 0),
1012 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1013 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_SMALL
), SetMinimalSize(78, 12), SetFill(1, 0),
1014 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1015 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_MEDIUM
), SetMinimalSize(78, 12), SetFill(1, 0),
1016 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1018 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1019 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1020 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_LARGE
), SetMinimalSize(78, 12), SetFill(1, 0),
1021 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1022 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_RANDOM
), SetMinimalSize(78, 12), SetFill(1, 0),
1023 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1025 NWidget(NWID_SPACER
), SetMinimalSize(0, 3),
1026 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_CITY
), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1027 SetDataTip(STR_FOUND_TOWN_CITY
, STR_FOUND_TOWN_CITY_TOOLTIP
), SetFill(1, 0),
1028 /* Town roads selection. */
1029 NWidget(NWID_HORIZONTAL
), SetPIP(2, 0, 2),
1030 NWidget(NWID_SPACER
), SetFill(1, 0),
1031 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT
, STR_NULL
),
1032 NWidget(NWID_SPACER
), SetFill(1, 0),
1034 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1035 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
),
1036 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
),
1038 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1039 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1040 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
),
1041 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
),
1043 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1044 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_RANDOM
), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1045 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
), SetFill(1, 0),
1046 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
1050 /** Found a town window class. */
1051 struct FoundTownWindow
: Window
{
1053 TownSize town_size
; ///< Selected town size
1054 TownLayout town_layout
; ///< Selected town layout
1055 bool city
; ///< Are we building a city?
1056 QueryString townname_editbox
; ///< Townname editbox
1057 bool townnamevalid
; ///< Is generated town name valid?
1058 uint32 townnameparts
; ///< Generated town name
1059 TownNameParams params
; ///< Town name parameters
1062 FoundTownWindow(WindowDesc
*desc
, WindowNumber window_number
) :
1064 town_size(TSZ_MEDIUM
),
1065 town_layout(_settings_game
.economy
.town_layout
),
1066 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS
* MAX_CHAR_LENGTH
, MAX_LENGTH_TOWN_NAME_CHARS
),
1067 params(_settings_game
.game_creation
.town_name
)
1069 this->InitNested(window_number
);
1070 this->querystrings
[WID_TF_TOWN_NAME_EDITBOX
] = &this->townname_editbox
;
1071 this->RandomTownName();
1072 this->UpdateButtons(true);
1075 void RandomTownName()
1077 this->townnamevalid
= GenerateTownName(&this->townnameparts
);
1079 if (!this->townnamevalid
) {
1080 this->townname_editbox
.text
.DeleteAll();
1082 GetTownName(this->townname_editbox
.text
.buf
, &this->params
, this->townnameparts
, &this->townname_editbox
.text
.buf
[this->townname_editbox
.text
.max_bytes
- 1]);
1083 this->townname_editbox
.text
.UpdateSize();
1085 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX
);
1087 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX
);
1090 void UpdateButtons(bool check_availability
)
1092 if (check_availability
&& _game_mode
!= GM_EDITOR
) {
1093 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN
, WID_TF_MANY_RANDOM_TOWNS
, WID_TF_SIZE_LARGE
, WIDGET_LIST_END
);
1094 this->SetWidgetsDisabledState(_settings_game
.economy
.found_town
!= TF_CUSTOM_LAYOUT
,
1095 WID_TF_LAYOUT_ORIGINAL
, WID_TF_LAYOUT_BETTER
, WID_TF_LAYOUT_GRID2
, WID_TF_LAYOUT_GRID3
, WID_TF_LAYOUT_RANDOM
, WIDGET_LIST_END
);
1096 if (_settings_game
.economy
.found_town
!= TF_CUSTOM_LAYOUT
) town_layout
= _settings_game
.economy
.town_layout
;
1099 for (int i
= WID_TF_SIZE_SMALL
; i
<= WID_TF_SIZE_RANDOM
; i
++) {
1100 this->SetWidgetLoweredState(i
, i
== WID_TF_SIZE_SMALL
+ this->town_size
);
1103 this->SetWidgetLoweredState(WID_TF_CITY
, this->city
);
1105 for (int i
= WID_TF_LAYOUT_ORIGINAL
; i
<= WID_TF_LAYOUT_RANDOM
; i
++) {
1106 this->SetWidgetLoweredState(i
, i
== WID_TF_LAYOUT_ORIGINAL
+ this->town_layout
);
1112 void ExecuteFoundTownCommand(TileIndex tile
, bool random
, StringID errstr
, CommandCallback cc
)
1114 const char *name
= nullptr;
1116 if (!this->townnamevalid
) {
1117 name
= this->townname_editbox
.text
.buf
;
1119 /* If user changed the name, send it */
1120 char buf
[MAX_LENGTH_TOWN_NAME_CHARS
* MAX_CHAR_LENGTH
];
1121 GetTownName(buf
, &this->params
, this->townnameparts
, lastof(buf
));
1122 if (strcmp(buf
, this->townname_editbox
.text
.buf
) != 0) name
= this->townname_editbox
.text
.buf
;
1125 bool success
= DoCommandP(tile
, this->town_size
| this->city
<< 2 | this->town_layout
<< 3 | random
<< 6,
1126 townnameparts
, CMD_FOUND_TOWN
| CMD_MSG(errstr
), cc
, name
);
1128 /* Rerandomise name, if success and no cost-estimation. */
1129 if (success
&& !_shift_pressed
) this->RandomTownName();
1132 void OnClick(Point pt
, int widget
, int click_count
) override
1135 case WID_TF_NEW_TOWN
:
1136 HandlePlacePushButton(this, WID_TF_NEW_TOWN
, SPR_CURSOR_TOWN
, HT_RECT
);
1139 case WID_TF_RANDOM_TOWN
:
1140 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN
, CcFoundRandomTown
);
1143 case WID_TF_TOWN_NAME_RANDOM
:
1144 this->RandomTownName();
1145 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX
);
1148 case WID_TF_MANY_RANDOM_TOWNS
:
1149 _generating_world
= true;
1150 UpdateNearestTownForRoadTiles(true);
1151 if (!GenerateTowns(this->town_layout
)) {
1152 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN
, STR_ERROR_NO_SPACE_FOR_TOWN
, WL_INFO
);
1154 UpdateNearestTownForRoadTiles(false);
1155 _generating_world
= false;
1158 case WID_TF_SIZE_SMALL
: case WID_TF_SIZE_MEDIUM
: case WID_TF_SIZE_LARGE
: case WID_TF_SIZE_RANDOM
:
1159 this->town_size
= (TownSize
)(widget
- WID_TF_SIZE_SMALL
);
1160 this->UpdateButtons(false);
1165 this->SetWidgetLoweredState(WID_TF_CITY
, this->city
);
1169 case WID_TF_LAYOUT_ORIGINAL
: case WID_TF_LAYOUT_BETTER
: case WID_TF_LAYOUT_GRID2
:
1170 case WID_TF_LAYOUT_GRID3
: case WID_TF_LAYOUT_RANDOM
:
1171 this->town_layout
= (TownLayout
)(widget
- WID_TF_LAYOUT_ORIGINAL
);
1172 this->UpdateButtons(false);
1177 void OnPlaceObject(Point pt
, TileIndex tile
) override
1179 this->ExecuteFoundTownCommand(tile
, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE
, CcFoundTown
);
1182 void OnPlaceObjectAbort() override
1184 this->RaiseButtons();
1185 this->UpdateButtons(false);
1189 * Some data on this window has become invalid.
1190 * @param data Information about the changed data.
1191 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1193 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1195 if (!gui_scope
) return;
1196 this->UpdateButtons(true);
1200 static WindowDesc
_found_town_desc(
1201 WDP_AUTO
, "build_town", 160, 162,
1202 WC_FOUND_TOWN
, WC_NONE
,
1204 _nested_found_town_widgets
, lengthof(_nested_found_town_widgets
)
1207 void ShowFoundTownWindow()
1209 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
1210 AllocateWindowDescFront
<FoundTownWindow
>(&_found_town_desc
, 0);