2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file town_gui.cpp GUI for towns. */
12 #include "viewport_func.h"
15 #include "command_func.h"
16 #include "company_func.h"
17 #include "company_base.h"
18 #include "company_gui.h"
19 #include "network/network.h"
20 #include "string_func.h"
21 #include "strings_func.h"
22 #include "sound_func.h"
23 #include "tilehighlight_func.h"
24 #include "sortlist_type.h"
26 #include "landscape.h"
27 #include "querystring_gui.h"
28 #include "window_func.h"
29 #include "townname_func.h"
30 #include "core/backup_type.hpp"
31 #include "core/geometry_func.hpp"
33 #include "stringfilter_type.h"
34 #include "widgets/dropdown_func.h"
35 #include "town_kdtree.h"
37 #include "widgets/town_widget.h"
39 #include "table/strings.h"
41 #include "safeguards.h"
43 TownKdtree
_town_local_authority_kdtree(&Kdtree_TownXYFunc
);
45 typedef GUIList
<const Town
*> GUITownList
;
47 static const NWidgetPart _nested_town_authority_widgets
[] = {
48 NWidget(NWID_HORIZONTAL
),
49 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
50 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TA_CAPTION
), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
51 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_TA_ZONE_BUTTON
), SetMinimalSize(50, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
+ 2), SetDataTip(STR_LOCAL_AUTHORITY_ZONE
, STR_LOCAL_AUTHORITY_ZONE_TOOLTIP
),
52 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
53 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
54 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
56 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TA_RATING_INFO
), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
57 NWidget(NWID_HORIZONTAL
),
58 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TA_COMMAND_LIST
), SetMinimalSize(305, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP
), SetScrollbar(WID_TA_SCROLLBAR
), EndContainer(),
59 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_TA_SCROLLBAR
),
61 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TA_ACTION_INFO
), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
62 NWidget(NWID_HORIZONTAL
),
63 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TA_EXECUTE
), SetMinimalSize(317, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_LOCAL_AUTHORITY_DO_IT_BUTTON
, STR_LOCAL_AUTHORITY_DO_IT_TOOLTIP
),
64 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
68 /** Town authority window. */
69 struct TownAuthorityWindow
: Window
{
71 Town
*town
; ///< Town being displayed.
72 int sel_index
; ///< Currently selected town action, \c 0 to \c TACT_COUNT-1, \c -1 means no action selected.
74 uint displayed_actions_on_previous_painting
; ///< Actions that were available on the previous call to OnPaint()
77 * Get the position of the Nth set bit.
79 * If there is no Nth bit set return -1
81 * @param bits The value to search in
82 * @param n The Nth set bit from which we want to know the position
83 * @return The position of the Nth set bit
85 static int GetNthSetBit(uint32 bits
, int n
)
88 for (uint i
: SetBitIterator(bits
)) {
97 TownAuthorityWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
), sel_index(-1), displayed_actions_on_previous_painting(0)
99 this->town
= Town::Get(window_number
);
100 this->InitNested(window_number
);
101 this->vscroll
= this->GetScrollbar(WID_TA_SCROLLBAR
);
102 this->vscroll
->SetCapacity((this->GetWidget
<NWidgetBase
>(WID_TA_COMMAND_LIST
)->current_y
- WD_FRAMERECT_TOP
- WD_FRAMERECT_BOTTOM
) / FONT_HEIGHT_NORMAL
);
105 void OnPaint() override
108 uint buttons
= GetMaskOfTownActions(&numact
, _local_company
, this->town
);
109 if (buttons
!= displayed_actions_on_previous_painting
) this->SetDirty();
110 displayed_actions_on_previous_painting
= buttons
;
112 this->vscroll
->SetCount(numact
+ 1);
114 if (this->sel_index
!= -1 && !HasBit(buttons
, this->sel_index
)) {
115 this->sel_index
= -1;
118 this->SetWidgetLoweredState(WID_TA_ZONE_BUTTON
, this->town
->show_zone
);
119 this->SetWidgetDisabledState(WID_TA_EXECUTE
, this->sel_index
== -1);
122 if (!this->IsShaded()) this->DrawRatings();
125 /** Draw the contents of the ratings panel. May request a resize of the window if the contents does not fit. */
128 NWidgetBase
*nwid
= this->GetWidget
<NWidgetBase
>(WID_TA_RATING_INFO
);
129 uint left
= nwid
->pos_x
+ WD_FRAMERECT_LEFT
;
130 uint right
= nwid
->pos_x
+ nwid
->current_x
- 1 - WD_FRAMERECT_RIGHT
;
132 uint y
= nwid
->pos_y
+ WD_FRAMERECT_TOP
;
134 DrawString(left
, right
, y
, STR_LOCAL_AUTHORITY_COMPANY_RATINGS
);
135 y
+= FONT_HEIGHT_NORMAL
;
137 Dimension icon_size
= GetSpriteSize(SPR_COMPANY_ICON
);
138 int icon_width
= icon_size
.width
;
139 int icon_y_offset
= (FONT_HEIGHT_NORMAL
- icon_size
.height
) / 2;
141 Dimension exclusive_size
= GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT
);
142 int exclusive_width
= exclusive_size
.width
;
143 int exclusive_y_offset
= (FONT_HEIGHT_NORMAL
- exclusive_size
.height
) / 2;
145 bool rtl
= _current_text_dir
== TD_RTL
;
146 uint text_left
= left
+ (rtl
? 0 : icon_width
+ exclusive_width
+ 4);
147 uint text_right
= right
- (rtl
? icon_width
+ exclusive_width
+ 4 : 0);
148 uint icon_left
= rtl
? right
- icon_width
: left
;
149 uint exclusive_left
= rtl
? right
- icon_width
- exclusive_width
- 2 : left
+ icon_width
+ 2;
151 /* Draw list of companies */
152 for (const Company
*c
: Company::Iterate()) {
153 if ((HasBit(this->town
->have_ratings
, c
->index
) || this->town
->exclusivity
== c
->index
)) {
154 DrawCompanyIcon(c
->index
, icon_left
, y
+ icon_y_offset
);
156 SetDParam(0, c
->index
);
157 SetDParam(1, c
->index
);
159 int r
= this->town
->ratings
[c
->index
];
160 StringID str
= STR_CARGO_RATING_APPALLING
;
161 if (r
> RATING_APPALLING
) str
++;
162 if (r
> RATING_VERYPOOR
) str
++;
163 if (r
> RATING_POOR
) str
++;
164 if (r
> RATING_MEDIOCRE
) str
++;
165 if (r
> RATING_GOOD
) str
++;
166 if (r
> RATING_VERYGOOD
) str
++;
167 if (r
> RATING_EXCELLENT
) str
++;
170 if (this->town
->exclusivity
== c
->index
) {
171 DrawSprite(SPR_EXCLUSIVE_TRANSPORT
, COMPANY_SPRITE_COLOUR(c
->index
), exclusive_left
, y
+ exclusive_y_offset
);
174 DrawString(text_left
, text_right
, y
, STR_LOCAL_AUTHORITY_COMPANY_RATING
);
175 y
+= FONT_HEIGHT_NORMAL
;
179 y
= y
+ WD_FRAMERECT_BOTTOM
- nwid
->pos_y
; // Compute needed size of the widget.
180 if (y
> nwid
->current_y
) {
181 /* If the company list is too big to fit, mark ourself dirty and draw again. */
182 ResizeWindow(this, 0, y
- nwid
->current_y
, false);
186 void SetStringParameters(int widget
) const override
188 if (widget
== WID_TA_CAPTION
) SetDParam(0, this->window_number
);
191 void DrawWidget(const Rect
&r
, int widget
) const override
194 case WID_TA_ACTION_INFO
:
195 if (this->sel_index
!= -1) {
196 SetDParam(0, _price
[PR_TOWN_ACTION
] * _town_action_costs
[this->sel_index
] >> 8);
197 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, r
.bottom
- WD_FRAMERECT_BOTTOM
,
198 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING
+ this->sel_index
);
201 case WID_TA_COMMAND_LIST
: {
203 uint buttons
= GetMaskOfTownActions(&numact
, _local_company
, this->town
);
204 int y
= r
.top
+ WD_FRAMERECT_TOP
;
205 int pos
= this->vscroll
->GetPosition();
208 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_LOCAL_AUTHORITY_ACTIONS_TITLE
);
209 y
+= FONT_HEIGHT_NORMAL
;
212 for (int i
= 0; buttons
; i
++, buttons
>>= 1) {
213 if (pos
<= -5) break; ///< Draw only the 5 fitting lines
215 if ((buttons
& 1) && --pos
< 0) {
216 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
,
217 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN
+ i
, this->sel_index
== i
? TC_WHITE
: TC_ORANGE
);
218 y
+= FONT_HEIGHT_NORMAL
;
226 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
229 case WID_TA_ACTION_INFO
: {
230 assert(size
->width
> padding
.width
&& size
->height
> padding
.height
);
231 size
->width
-= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
232 size
->height
-= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
233 Dimension d
= {0, 0};
234 for (int i
= 0; i
< TACT_COUNT
; i
++) {
235 SetDParam(0, _price
[PR_TOWN_ACTION
] * _town_action_costs
[i
] >> 8);
236 d
= maxdim(d
, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING
+ i
, *size
));
238 *size
= maxdim(*size
, d
);
239 size
->width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
240 size
->height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
244 case WID_TA_COMMAND_LIST
:
245 size
->height
= WD_FRAMERECT_TOP
+ 5 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
246 size
->width
= GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE
).width
;
247 for (uint i
= 0; i
< TACT_COUNT
; i
++ ) {
248 size
->width
= std::max(size
->width
, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN
+ i
).width
);
250 size
->width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
253 case WID_TA_RATING_INFO
:
254 resize
->height
= FONT_HEIGHT_NORMAL
;
255 size
->height
= WD_FRAMERECT_TOP
+ 9 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
260 void OnClick(Point pt
, int widget
, int click_count
) override
263 case WID_TA_ZONE_BUTTON
: {
264 bool new_show_state
= !this->town
->show_zone
;
265 TownID index
= this->town
->index
;
267 new_show_state
? _town_local_authority_kdtree
.Insert(index
) : _town_local_authority_kdtree
.Remove(index
);
269 this->town
->show_zone
= new_show_state
;
270 this->SetWidgetLoweredState(widget
, new_show_state
);
271 MarkWholeScreenDirty();
275 case WID_TA_COMMAND_LIST
: {
276 int y
= this->GetRowFromWidget(pt
.y
, WID_TA_COMMAND_LIST
, 1, FONT_HEIGHT_NORMAL
);
277 if (!IsInsideMM(y
, 0, 5)) return;
279 y
= GetNthSetBit(GetMaskOfTownActions(nullptr, _local_company
, this->town
), y
+ this->vscroll
->GetPosition() - 1);
284 /* When double-clicking, continue */
285 if (click_count
== 1 || y
< 0) break;
290 DoCommandP(this->town
->xy
, this->window_number
, this->sel_index
, CMD_DO_TOWN_ACTION
| CMD_MSG(STR_ERROR_CAN_T_DO_THIS
));
295 void OnHundredthTick() override
301 static WindowDesc
_town_authority_desc(
302 WDP_AUTO
, "view_town_authority", 317, 222,
303 WC_TOWN_AUTHORITY
, WC_NONE
,
305 _nested_town_authority_widgets
, lengthof(_nested_town_authority_widgets
)
308 static void ShowTownAuthorityWindow(uint town
)
310 AllocateWindowDescFront
<TownAuthorityWindow
>(&_town_authority_desc
, town
);
314 /* Town view window. */
315 struct TownViewWindow
: Window
{
317 Town
*town
; ///< Town displayed by the window.
320 static const int WID_TV_HEIGHT_NORMAL
= 150;
322 TownViewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
324 this->CreateNestedTree();
326 this->town
= Town::Get(window_number
);
327 if (this->town
->larger_town
) this->GetWidget
<NWidgetCore
>(WID_TV_CAPTION
)->widget_data
= STR_TOWN_VIEW_CITY_CAPTION
;
329 this->FinishInitNested(window_number
);
331 this->flags
|= WF_DISABLE_VP_SCROLL
;
332 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_TV_VIEWPORT
);
333 nvp
->InitializeViewport(this, this->town
->xy
, ZOOM_LVL_NEWS
);
335 /* disable renaming town in network games if you are not the server */
336 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME
, _networking
&& !_network_server
);
339 void Close() override
341 SetViewportCatchmentTown(Town::Get(this->window_number
), false);
342 this->Window::Close();
345 void SetStringParameters(int widget
) const override
347 if (widget
== WID_TV_CAPTION
) SetDParam(0, this->town
->index
);
350 void OnPaint() override
352 extern const Town
*_viewport_highlight_town
;
353 this->SetWidgetLoweredState(WID_TV_CATCHMENT
, _viewport_highlight_town
== this->town
);
358 void DrawWidget(const Rect
&r
, int widget
) const override
360 if (widget
!= WID_TV_INFO
) return;
362 uint y
= r
.top
+ WD_FRAMERECT_TOP
;
364 SetDParam(0, this->town
->cache
.population
);
365 SetDParam(1, this->town
->cache
.num_houses
);
366 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
, STR_TOWN_VIEW_POPULATION_HOUSES
);
368 SetDParam(0, 1 << CT_PASSENGERS
);
369 SetDParam(1, this->town
->supplied
[CT_PASSENGERS
].old_act
);
370 SetDParam(2, this->town
->supplied
[CT_PASSENGERS
].old_max
);
371 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX
);
373 SetDParam(0, 1 << CT_MAIL
);
374 SetDParam(1, this->town
->supplied
[CT_MAIL
].old_act
);
375 SetDParam(2, this->town
->supplied
[CT_MAIL
].old_max
);
376 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_CARGO_LAST_MONTH_MAX
);
379 for (int i
= TE_BEGIN
; i
< TE_END
; i
++) {
380 if (this->town
->goal
[i
] == 0) continue;
381 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& (TileHeight(this->town
->xy
) < LowestSnowLine() || this->town
->cache
.population
<= 90)) continue;
382 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
&& (GetTropicZone(this->town
->xy
) != TROPICZONE_DESERT
|| this->town
->cache
.population
<= 60)) continue;
385 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH
);
389 bool rtl
= _current_text_dir
== TD_RTL
;
390 uint cargo_text_left
= r
.left
+ WD_FRAMERECT_LEFT
+ (rtl
? 0 : 20);
391 uint cargo_text_right
= r
.right
- WD_FRAMERECT_RIGHT
- (rtl
? 20 : 0);
393 const CargoSpec
*cargo
= FindFirstCargoWithTownEffect((TownEffect
)i
);
394 assert(cargo
!= nullptr);
398 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
|| this->town
->goal
[i
] == TOWN_GROWTH_WINTER
) {
399 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
400 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL
;
401 if (this->town
->received
[i
].old_act
== 0) {
402 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL
;
404 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& TileHeight(this->town
->xy
) < GetSnowLine()) {
405 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER
;
409 SetDParam(0, cargo
->name
);
411 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED
;
412 if (this->town
->received
[i
].old_act
< this->town
->goal
[i
]) {
413 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED
;
416 SetDParam(0, cargo
->Index());
417 SetDParam(1, this->town
->received
[i
].old_act
);
418 SetDParam(2, cargo
->Index());
419 SetDParam(3, this->town
->goal
[i
]);
421 DrawString(cargo_text_left
, cargo_text_right
, y
+= FONT_HEIGHT_NORMAL
, string
);
424 if (HasBit(this->town
->flags
, TOWN_IS_GROWING
)) {
425 SetDParam(0, RoundDivSU(this->town
->growth_rate
+ 1, DAY_TICKS
));
426 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, this->town
->fund_buildings_months
== 0 ? STR_TOWN_VIEW_TOWN_GROWS_EVERY
: STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED
);
428 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_TOWN_GROW_STOPPED
);
431 /* only show the town noise, if the noise option is activated. */
432 if (_settings_game
.economy
.station_noise_level
) {
433 SetDParam(0, this->town
->noise_reached
);
434 SetDParam(1, this->town
->MaxTownNoise());
435 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_NOISE_IN_TOWN
);
438 if (!this->town
->text
.empty()) {
439 SetDParamStr(0, this->town
->text
);
440 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
+= FONT_HEIGHT_NORMAL
, UINT16_MAX
, STR_JUST_RAW_STRING
, TC_BLACK
);
444 void OnClick(Point pt
, int widget
, int click_count
) override
447 case WID_TV_CENTER_VIEW
: // scroll to location
449 ShowExtraViewportWindow(this->town
->xy
);
451 ScrollMainWindowToTile(this->town
->xy
);
455 case WID_TV_SHOW_AUTHORITY
: // town authority
456 ShowTownAuthorityWindow(this->window_number
);
459 case WID_TV_CHANGE_NAME
: // rename
460 SetDParam(0, this->window_number
);
461 ShowQueryString(STR_TOWN_NAME
, STR_TOWN_VIEW_RENAME_TOWN_BUTTON
, MAX_LENGTH_TOWN_NAME_CHARS
, this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
464 case WID_TV_CATCHMENT
:
465 SetViewportCatchmentTown(Town::Get(this->window_number
), !this->IsWidgetLowered(WID_TV_CATCHMENT
));
468 case WID_TV_EXPAND
: { // expand town - only available on Scenario editor
469 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
470 static bool _warn_town_no_roads
= false;
472 if (!_settings_game
.economy
.allow_town_roads
&& !_warn_town_no_roads
) {
473 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS
, INVALID_STRING_ID
, WL_WARNING
);
474 _warn_town_no_roads
= true;
477 DoCommandP(0, this->window_number
, 0, CMD_EXPAND_TOWN
| CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN
));
481 case WID_TV_DELETE
: // delete town - only available on Scenario editor
482 DoCommandP(0, this->window_number
, 0, CMD_DELETE_TOWN
| CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE
));
487 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
491 size
->height
= GetDesiredInfoHeight(size
->width
);
497 * Gets the desired height for the information panel.
498 * @return the desired height in pixels.
500 uint
GetDesiredInfoHeight(int width
) const
502 uint aimed_height
= 3 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
505 for (int i
= TE_BEGIN
; i
< TE_END
; i
++) {
506 if (this->town
->goal
[i
] == 0) continue;
507 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& (TileHeight(this->town
->xy
) < LowestSnowLine() || this->town
->cache
.population
<= 90)) continue;
508 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
&& (GetTropicZone(this->town
->xy
) != TROPICZONE_DESERT
|| this->town
->cache
.population
<= 60)) continue;
511 aimed_height
+= FONT_HEIGHT_NORMAL
;
514 aimed_height
+= FONT_HEIGHT_NORMAL
;
516 aimed_height
+= FONT_HEIGHT_NORMAL
;
518 if (_settings_game
.economy
.station_noise_level
) aimed_height
+= FONT_HEIGHT_NORMAL
;
520 if (!this->town
->text
.empty()) {
521 SetDParamStr(0, this->town
->text
);
522 aimed_height
+= GetStringHeight(STR_JUST_RAW_STRING
, width
- WD_FRAMERECT_LEFT
- WD_FRAMERECT_RIGHT
);
528 void ResizeWindowAsNeeded()
530 const NWidgetBase
*nwid_info
= this->GetWidget
<NWidgetBase
>(WID_TV_INFO
);
531 uint aimed_height
= GetDesiredInfoHeight(nwid_info
->current_x
);
532 if (aimed_height
> nwid_info
->current_y
|| (aimed_height
< nwid_info
->current_y
&& nwid_info
->current_y
> nwid_info
->smallest_y
)) {
537 void OnResize() override
539 if (this->viewport
!= nullptr) {
540 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_TV_VIEWPORT
);
541 nvp
->UpdateViewportCoordinates(this);
543 ScrollWindowToTile(this->town
->xy
, this, true); // Re-center viewport.
548 * Some data on this window has become invalid.
549 * @param data Information about the changed data.
550 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
552 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
554 if (!gui_scope
) return;
555 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
556 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
557 this->ResizeWindowAsNeeded();
560 void OnQueryTextFinished(char *str
) override
562 if (str
== nullptr) return;
564 DoCommandP(0, this->window_number
, 0, CMD_RENAME_TOWN
| CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN
), nullptr, str
);
568 static const NWidgetPart _nested_town_game_view_widgets
[] = {
569 NWidget(NWID_HORIZONTAL
),
570 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
571 NWidget(WWT_PUSHIMGBTN
, COLOUR_BROWN
, WID_TV_CHANGE_NAME
), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME
, STR_TOWN_VIEW_RENAME_TOOLTIP
),
572 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TV_CAPTION
), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
573 NWidget(WWT_PUSHIMGBTN
, COLOUR_BROWN
, WID_TV_CENTER_VIEW
), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION
, STR_TOWN_VIEW_CENTER_TOOLTIP
),
574 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
575 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
576 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
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),
583 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TV_INFO
), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
584 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
585 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_SHOW_AUTHORITY
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON
, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP
),
586 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_TV_CATCHMENT
), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT
, STR_TOOLTIP_CATCHMENT
),
587 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
591 static WindowDesc
_town_game_view_desc(
592 WDP_AUTO
, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL
,
593 WC_TOWN_VIEW
, WC_NONE
,
595 _nested_town_game_view_widgets
, lengthof(_nested_town_game_view_widgets
)
598 static const NWidgetPart _nested_town_editor_view_widgets
[] = {
599 NWidget(NWID_HORIZONTAL
),
600 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
601 NWidget(WWT_PUSHIMGBTN
, COLOUR_BROWN
, WID_TV_CHANGE_NAME
), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME
, STR_TOWN_VIEW_RENAME_TOOLTIP
),
602 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TV_CAPTION
), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
603 NWidget(WWT_PUSHIMGBTN
, COLOUR_BROWN
, WID_TV_CENTER_VIEW
), SetMinimalSize(12, 14), SetDataTip(SPR_GOTO_LOCATION
, STR_TOWN_VIEW_CENTER_TOOLTIP
),
604 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
605 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
606 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
608 NWidget(WWT_PANEL
, COLOUR_BROWN
),
609 NWidget(WWT_INSET
, COLOUR_BROWN
), SetPadding(2, 2, 2, 2),
610 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_TV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1),
613 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TV_INFO
), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
614 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
615 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_EXPAND
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_EXPAND_BUTTON
, STR_TOWN_VIEW_EXPAND_TOOLTIP
),
616 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_DELETE
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON
, STR_TOWN_VIEW_DELETE_TOOLTIP
),
617 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_TV_CATCHMENT
), SetMinimalSize(40, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_CATCHMENT
, STR_TOOLTIP_CATCHMENT
),
618 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
622 static WindowDesc
_town_editor_view_desc(
623 WDP_AUTO
, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL
,
624 WC_TOWN_VIEW
, WC_NONE
,
626 _nested_town_editor_view_widgets
, lengthof(_nested_town_editor_view_widgets
)
629 void ShowTownViewWindow(TownID town
)
631 if (_game_mode
== GM_EDITOR
) {
632 AllocateWindowDescFront
<TownViewWindow
>(&_town_editor_view_desc
, town
);
634 AllocateWindowDescFront
<TownViewWindow
>(&_town_game_view_desc
, town
);
638 static const NWidgetPart _nested_town_directory_widgets
[] = {
639 NWidget(NWID_HORIZONTAL
),
640 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
641 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_TOWN_DIRECTORY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
642 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
643 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
644 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
646 NWidget(NWID_HORIZONTAL
),
647 NWidget(NWID_VERTICAL
),
648 NWidget(NWID_HORIZONTAL
),
649 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_TD_SORT_ORDER
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
650 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_TD_SORT_CRITERIA
), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
651 NWidget(WWT_EDITBOX
, COLOUR_BROWN
, WID_TD_FILTER
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
653 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TD_LIST
), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP
),
654 SetFill(1, 0), SetResize(1, 1), SetScrollbar(WID_TD_SCROLLBAR
), EndContainer(),
655 NWidget(WWT_PANEL
, COLOUR_BROWN
),
656 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_TD_WORLD_POPULATION
), SetPadding(2, 0, 2, 2), SetMinimalTextLines(1, 0), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TOWN_POPULATION
, STR_NULL
),
659 NWidget(NWID_VERTICAL
),
660 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_TD_SCROLLBAR
),
661 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
666 /** Town directory window class. */
667 struct TownDirectoryWindow
: public Window
{
669 /* Runtime saved values */
670 static Listing last_sorting
;
672 /* Constants for sorting towns */
673 static const StringID sorter_names
[];
674 static GUITownList::SortFunction
* const sorter_funcs
[];
676 StringFilter string_filter
; ///< Filter for towns
677 QueryString townname_editbox
; ///< Filter editbox
683 void BuildSortTownList()
685 if (this->towns
.NeedRebuild()) {
688 for (const Town
*t
: Town::Iterate()) {
689 if (this->string_filter
.IsEmpty()) {
690 this->towns
.push_back(t
);
693 this->string_filter
.ResetState();
694 this->string_filter
.AddLine(t
->GetCachedName());
695 if (this->string_filter
.GetState()) this->towns
.push_back(t
);
698 this->towns
.shrink_to_fit();
699 this->towns
.RebuildDone();
700 this->vscroll
->SetCount((uint
)this->towns
.size()); // Update scrollbar as well.
702 /* Always sort the towns. */
704 this->SetWidgetDirty(WID_TD_LIST
); // Force repaint of the displayed towns.
707 /** Sort by town name */
708 static bool TownNameSorter(const Town
* const &a
, const Town
* const &b
)
710 return strnatcmp(a
->GetCachedName(), b
->GetCachedName()) < 0; // Sort by name (natural sorting).
713 /** Sort by population (default descending, as big towns are of the most interest). */
714 static bool TownPopulationSorter(const Town
* const &a
, const Town
* const &b
)
716 uint32 a_population
= a
->cache
.population
;
717 uint32 b_population
= b
->cache
.population
;
718 if (a_population
== b_population
) return TownDirectoryWindow::TownNameSorter(a
, b
);
719 return a_population
< b_population
;
722 /** Sort by town rating */
723 static bool TownRatingSorter(const Town
* const &a
, const Town
* const &b
)
725 bool before
= !TownDirectoryWindow::last_sorting
.order
; // Value to get 'a' before 'b'.
727 /* Towns without rating are always after towns with rating. */
728 if (HasBit(a
->have_ratings
, _local_company
)) {
729 if (HasBit(b
->have_ratings
, _local_company
)) {
730 int16 a_rating
= a
->ratings
[_local_company
];
731 int16 b_rating
= b
->ratings
[_local_company
];
732 if (a_rating
== b_rating
) return TownDirectoryWindow::TownNameSorter(a
, b
);
733 return a_rating
< b_rating
;
737 if (HasBit(b
->have_ratings
, _local_company
)) return !before
;
739 /* Sort unrated towns always on ascending town name. */
740 if (before
) return TownDirectoryWindow::TownNameSorter(a
, b
);
741 return !TownDirectoryWindow::TownNameSorter(a
, b
);
745 TownDirectoryWindow(WindowDesc
*desc
) : Window(desc
), townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS
* MAX_CHAR_LENGTH
, MAX_LENGTH_TOWN_NAME_CHARS
)
747 this->CreateNestedTree();
749 this->vscroll
= this->GetScrollbar(WID_TD_SCROLLBAR
);
751 this->towns
.SetListing(this->last_sorting
);
752 this->towns
.SetSortFuncs(TownDirectoryWindow::sorter_funcs
);
753 this->towns
.ForceRebuild();
754 this->BuildSortTownList();
756 this->FinishInitNested(0);
758 this->querystrings
[WID_TD_FILTER
] = &this->townname_editbox
;
759 this->townname_editbox
.cancel_button
= QueryString::ACTION_CLEAR
;
762 void SetStringParameters(int widget
) const override
765 case WID_TD_WORLD_POPULATION
:
766 SetDParam(0, GetWorldPopulation());
769 case WID_TD_SORT_CRITERIA
:
770 SetDParam(0, TownDirectoryWindow::sorter_names
[this->towns
.SortType()]);
776 * Get the string to draw the town name.
777 * @param t Town to draw.
778 * @return The string to use.
780 static StringID
GetTownString(const Town
*t
)
782 return t
->larger_town
? STR_TOWN_DIRECTORY_CITY
: STR_TOWN_DIRECTORY_TOWN
;
785 void DrawWidget(const Rect
&r
, int widget
) const override
788 case WID_TD_SORT_ORDER
:
789 this->DrawSortButtonState(widget
, this->towns
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
794 int y
= r
.top
+ WD_FRAMERECT_TOP
;
795 if (this->towns
.size() == 0) { // No towns available.
796 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
, y
, STR_TOWN_DIRECTORY_NONE
);
800 /* At least one town available. */
801 bool rtl
= _current_text_dir
== TD_RTL
;
802 Dimension icon_size
= GetSpriteSize(SPR_TOWN_RATING_GOOD
);
803 int text_left
= r
.left
+ WD_FRAMERECT_LEFT
+ (rtl
? 0 : icon_size
.width
+ 2);
804 int text_right
= r
.right
- WD_FRAMERECT_RIGHT
- (rtl
? icon_size
.width
+ 2 : 0);
805 int icon_x
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
- icon_size
.width
: r
.left
+ WD_FRAMERECT_LEFT
;
807 for (uint i
= this->vscroll
->GetPosition(); i
< this->towns
.size(); i
++) {
808 const Town
*t
= this->towns
[i
];
809 assert(t
->xy
!= INVALID_TILE
);
811 /* Draw rating icon. */
812 if (_game_mode
== GM_EDITOR
|| !HasBit(t
->have_ratings
, _local_company
)) {
813 DrawSprite(SPR_TOWN_RATING_NA
, PAL_NONE
, icon_x
, y
+ (this->resize
.step_height
- icon_size
.height
) / 2);
815 SpriteID icon
= SPR_TOWN_RATING_APALLING
;
816 if (t
->ratings
[_local_company
] > RATING_VERYPOOR
) icon
= SPR_TOWN_RATING_MEDIOCRE
;
817 if (t
->ratings
[_local_company
] > RATING_GOOD
) icon
= SPR_TOWN_RATING_GOOD
;
818 DrawSprite(icon
, PAL_NONE
, icon_x
, y
+ (this->resize
.step_height
- icon_size
.height
) / 2);
821 SetDParam(0, t
->index
);
822 SetDParam(1, t
->cache
.population
);
823 DrawString(text_left
, text_right
, y
+ (this->resize
.step_height
- FONT_HEIGHT_NORMAL
) / 2, GetTownString(t
));
825 y
+= this->resize
.step_height
;
826 if (++n
== this->vscroll
->GetCapacity()) break; // max number of towns in 1 window
833 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
836 case WID_TD_SORT_ORDER
: {
837 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
838 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
839 d
.height
+= padding
.height
;
840 *size
= maxdim(*size
, d
);
843 case WID_TD_SORT_CRITERIA
: {
844 Dimension d
= {0, 0};
845 for (uint i
= 0; TownDirectoryWindow::sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
846 d
= maxdim(d
, GetStringBoundingBox(TownDirectoryWindow::sorter_names
[i
]));
848 d
.width
+= padding
.width
;
849 d
.height
+= padding
.height
;
850 *size
= maxdim(*size
, d
);
854 Dimension d
= GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE
);
855 for (uint i
= 0; i
< this->towns
.size(); i
++) {
856 const Town
*t
= this->towns
[i
];
858 assert(t
!= nullptr);
860 SetDParam(0, t
->index
);
861 SetDParamMaxDigits(1, 8);
862 d
= maxdim(d
, GetStringBoundingBox(GetTownString(t
)));
864 Dimension icon_size
= GetSpriteSize(SPR_TOWN_RATING_GOOD
);
865 d
.width
+= icon_size
.width
+ 2;
866 d
.height
= std::max(d
.height
, icon_size
.height
);
867 resize
->height
= d
.height
;
869 d
.width
+= padding
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
870 d
.height
+= padding
.height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
871 *size
= maxdim(*size
, d
);
874 case WID_TD_WORLD_POPULATION
: {
875 SetDParamMaxDigits(0, 10);
876 Dimension d
= GetStringBoundingBox(STR_TOWN_POPULATION
);
877 d
.width
+= padding
.width
;
878 d
.height
+= padding
.height
;
879 *size
= maxdim(*size
, d
);
885 void OnClick(Point pt
, int widget
, int click_count
) override
888 case WID_TD_SORT_ORDER
: // Click on sort order button
889 if (this->towns
.SortType() != 2) { // A different sort than by rating.
890 this->towns
.ToggleSortOrder();
891 this->last_sorting
= this->towns
.GetListing(); // Store new sorting order.
893 /* Some parts are always sorted ascending on name. */
894 this->last_sorting
.order
= !this->last_sorting
.order
;
895 this->towns
.SetListing(this->last_sorting
);
896 this->towns
.ForceResort();
902 case WID_TD_SORT_CRITERIA
: // Click on sort criteria dropdown
903 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names
, this->towns
.SortType(), WID_TD_SORT_CRITERIA
, 0, 0);
906 case WID_TD_LIST
: { // Click on Town Matrix
907 uint id_v
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_TD_LIST
, WD_FRAMERECT_TOP
);
908 if (id_v
>= this->towns
.size()) return; // click out of town bounds
910 const Town
*t
= this->towns
[id_v
];
911 assert(t
!= nullptr);
913 ShowExtraViewportWindow(t
->xy
);
915 ScrollMainWindowToTile(t
->xy
);
922 void OnDropdownSelect(int widget
, int index
) override
924 if (widget
!= WID_TD_SORT_CRITERIA
) return;
926 if (this->towns
.SortType() != index
) {
927 this->towns
.SetSortType(index
);
928 this->last_sorting
= this->towns
.GetListing(); // Store new sorting order.
929 this->BuildSortTownList();
933 void OnPaint() override
935 if (this->towns
.NeedRebuild()) this->BuildSortTownList();
939 void OnHundredthTick() override
941 this->BuildSortTownList();
945 void OnResize() override
947 this->vscroll
->SetCapacityFromWidget(this, WID_TD_LIST
);
950 void OnEditboxChanged(int wid
) override
952 if (wid
== WID_TD_FILTER
) {
953 this->string_filter
.SetFilterTerm(this->townname_editbox
.text
.buf
);
954 this->InvalidateData(TDIWD_FORCE_REBUILD
);
959 * Some data on this window has become invalid.
960 * @param data Information about the changed data.
961 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
963 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
966 case TDIWD_FORCE_REBUILD
:
967 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
968 this->towns
.ForceRebuild();
971 case TDIWD_POPULATION_CHANGE
:
972 if (this->towns
.SortType() == 1) this->towns
.ForceResort();
976 this->towns
.ForceResort();
981 Listing
TownDirectoryWindow::last_sorting
= {false, 0};
983 /** Names of the sorting functions. */
984 const StringID
TownDirectoryWindow::sorter_names
[] = {
986 STR_SORT_BY_POPULATION
,
991 /** Available town directory sorting functions. */
992 GUITownList::SortFunction
* const TownDirectoryWindow::sorter_funcs
[] = {
994 &TownPopulationSorter
,
998 static WindowDesc
_town_directory_desc(
999 WDP_AUTO
, "list_towns", 208, 202,
1000 WC_TOWN_DIRECTORY
, WC_NONE
,
1002 _nested_town_directory_widgets
, lengthof(_nested_town_directory_widgets
)
1005 void ShowTownDirectory()
1007 if (BringWindowToFrontById(WC_TOWN_DIRECTORY
, 0)) return;
1008 new TownDirectoryWindow(&_town_directory_desc
);
1011 void CcFoundTown(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
1013 if (result
.Failed()) return;
1015 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER
, tile
);
1016 if (!_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
1019 void CcFoundRandomTown(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
, uint32 cmd
)
1021 if (result
.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id
)->xy
);
1024 static const NWidgetPart _nested_found_town_widgets
[] = {
1025 NWidget(NWID_HORIZONTAL
),
1026 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
1027 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_FOUND_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1028 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
1029 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
1031 /* Construct new town(s) buttons. */
1032 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
1033 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
1034 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_NEW_TOWN
), SetMinimalSize(156, 12), SetFill(1, 0),
1035 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON
, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP
), SetPadding(0, 2, 1, 2),
1036 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_RANDOM_TOWN
), SetMinimalSize(156, 12), SetFill(1, 0),
1037 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON
, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP
), SetPadding(0, 2, 1, 2),
1038 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_MANY_RANDOM_TOWNS
), SetMinimalSize(156, 12), SetFill(1, 0),
1039 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS
, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP
), SetPadding(0, 2, 0, 2),
1040 /* Town name selection. */
1041 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE
, STR_NULL
),
1042 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_TF_TOWN_NAME_EDITBOX
), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
1043 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE
, STR_FOUND_TOWN_NAME_EDITOR_HELP
),
1044 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_TOWN_NAME_RANDOM
), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
1045 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON
, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP
),
1046 /* Town size selection. */
1047 NWidget(NWID_HORIZONTAL
), SetPIP(2, 0, 2),
1048 NWidget(NWID_SPACER
), SetFill(1, 0),
1049 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE
, STR_NULL
),
1050 NWidget(NWID_SPACER
), SetFill(1, 0),
1052 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1053 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_SMALL
), SetMinimalSize(78, 12), SetFill(1, 0),
1054 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1055 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_MEDIUM
), SetMinimalSize(78, 12), SetFill(1, 0),
1056 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1058 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1059 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1060 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_LARGE
), SetMinimalSize(78, 12), SetFill(1, 0),
1061 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1062 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_RANDOM
), SetMinimalSize(78, 12), SetFill(1, 0),
1063 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1065 NWidget(NWID_SPACER
), SetMinimalSize(0, 3),
1066 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_CITY
), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1067 SetDataTip(STR_FOUND_TOWN_CITY
, STR_FOUND_TOWN_CITY_TOOLTIP
), SetFill(1, 0),
1068 /* Town roads selection. */
1069 NWidget(NWID_HORIZONTAL
), SetPIP(2, 0, 2),
1070 NWidget(NWID_SPACER
), SetFill(1, 0),
1071 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT
, STR_NULL
),
1072 NWidget(NWID_SPACER
), SetFill(1, 0),
1074 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1075 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_ORIGINAL
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_ORIGINAL
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1076 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_BETTER
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_BETTER_ROADS
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1078 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1079 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1080 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_GRID2
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_2X2_GRID
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1081 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_GRID3
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_3X3_GRID
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1083 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1084 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_RANDOM
), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1085 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
), SetFill(1, 0),
1086 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
1090 /** Found a town window class. */
1091 struct FoundTownWindow
: Window
{
1093 TownSize town_size
; ///< Selected town size
1094 TownLayout town_layout
; ///< Selected town layout
1095 bool city
; ///< Are we building a city?
1096 QueryString townname_editbox
; ///< Townname editbox
1097 bool townnamevalid
; ///< Is generated town name valid?
1098 uint32 townnameparts
; ///< Generated town name
1099 TownNameParams params
; ///< Town name parameters
1102 FoundTownWindow(WindowDesc
*desc
, WindowNumber window_number
) :
1104 town_size(TSZ_MEDIUM
),
1105 town_layout(_settings_game
.economy
.town_layout
),
1106 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS
* MAX_CHAR_LENGTH
, MAX_LENGTH_TOWN_NAME_CHARS
),
1107 params(_settings_game
.game_creation
.town_name
)
1109 this->InitNested(window_number
);
1110 this->querystrings
[WID_TF_TOWN_NAME_EDITBOX
] = &this->townname_editbox
;
1111 this->RandomTownName();
1112 this->UpdateButtons(true);
1115 void RandomTownName()
1117 this->townnamevalid
= GenerateTownName(&this->townnameparts
);
1119 if (!this->townnamevalid
) {
1120 this->townname_editbox
.text
.DeleteAll();
1122 GetTownName(this->townname_editbox
.text
.buf
, &this->params
, this->townnameparts
, &this->townname_editbox
.text
.buf
[this->townname_editbox
.text
.max_bytes
- 1]);
1123 this->townname_editbox
.text
.UpdateSize();
1125 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX
);
1127 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX
);
1130 void UpdateButtons(bool check_availability
)
1132 if (check_availability
&& _game_mode
!= GM_EDITOR
) {
1133 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN
, WID_TF_MANY_RANDOM_TOWNS
, WID_TF_SIZE_LARGE
, WIDGET_LIST_END
);
1134 this->SetWidgetsDisabledState(_settings_game
.economy
.found_town
!= TF_CUSTOM_LAYOUT
,
1135 WID_TF_LAYOUT_ORIGINAL
, WID_TF_LAYOUT_BETTER
, WID_TF_LAYOUT_GRID2
, WID_TF_LAYOUT_GRID3
, WID_TF_LAYOUT_RANDOM
, WIDGET_LIST_END
);
1136 if (_settings_game
.economy
.found_town
!= TF_CUSTOM_LAYOUT
) town_layout
= _settings_game
.economy
.town_layout
;
1139 for (int i
= WID_TF_SIZE_SMALL
; i
<= WID_TF_SIZE_RANDOM
; i
++) {
1140 this->SetWidgetLoweredState(i
, i
== WID_TF_SIZE_SMALL
+ this->town_size
);
1143 this->SetWidgetLoweredState(WID_TF_CITY
, this->city
);
1145 for (int i
= WID_TF_LAYOUT_ORIGINAL
; i
<= WID_TF_LAYOUT_RANDOM
; i
++) {
1146 this->SetWidgetLoweredState(i
, i
== WID_TF_LAYOUT_ORIGINAL
+ this->town_layout
);
1152 void ExecuteFoundTownCommand(TileIndex tile
, bool random
, StringID errstr
, CommandCallback cc
)
1156 if (!this->townnamevalid
) {
1157 name
= this->townname_editbox
.text
.buf
;
1159 /* If user changed the name, send it */
1160 char buf
[MAX_LENGTH_TOWN_NAME_CHARS
* MAX_CHAR_LENGTH
];
1161 GetTownName(buf
, &this->params
, this->townnameparts
, lastof(buf
));
1162 if (strcmp(buf
, this->townname_editbox
.text
.buf
) != 0) name
= this->townname_editbox
.text
.buf
;
1165 bool success
= DoCommandP(tile
, this->town_size
| this->city
<< 2 | this->town_layout
<< 3 | random
<< 6,
1166 townnameparts
, CMD_FOUND_TOWN
| CMD_MSG(errstr
), cc
, name
);
1168 /* Rerandomise name, if success and no cost-estimation. */
1169 if (success
&& !_shift_pressed
) this->RandomTownName();
1172 void OnClick(Point pt
, int widget
, int click_count
) override
1175 case WID_TF_NEW_TOWN
:
1176 HandlePlacePushButton(this, WID_TF_NEW_TOWN
, SPR_CURSOR_TOWN
, HT_RECT
);
1179 case WID_TF_RANDOM_TOWN
:
1180 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN
, CcFoundRandomTown
);
1183 case WID_TF_TOWN_NAME_RANDOM
:
1184 this->RandomTownName();
1185 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX
);
1188 case WID_TF_MANY_RANDOM_TOWNS
: {
1189 Backup
<bool> old_generating_world(_generating_world
, true, FILE_LINE
);
1190 UpdateNearestTownForRoadTiles(true);
1191 if (!GenerateTowns(this->town_layout
)) {
1192 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN
, STR_ERROR_NO_SPACE_FOR_TOWN
, WL_INFO
);
1194 UpdateNearestTownForRoadTiles(false);
1195 old_generating_world
.Restore();
1199 case WID_TF_SIZE_SMALL
: case WID_TF_SIZE_MEDIUM
: case WID_TF_SIZE_LARGE
: case WID_TF_SIZE_RANDOM
:
1200 this->town_size
= (TownSize
)(widget
- WID_TF_SIZE_SMALL
);
1201 this->UpdateButtons(false);
1206 this->SetWidgetLoweredState(WID_TF_CITY
, this->city
);
1210 case WID_TF_LAYOUT_ORIGINAL
: case WID_TF_LAYOUT_BETTER
: case WID_TF_LAYOUT_GRID2
:
1211 case WID_TF_LAYOUT_GRID3
: case WID_TF_LAYOUT_RANDOM
:
1212 this->town_layout
= (TownLayout
)(widget
- WID_TF_LAYOUT_ORIGINAL
);
1213 this->UpdateButtons(false);
1218 void OnPlaceObject(Point pt
, TileIndex tile
) override
1220 this->ExecuteFoundTownCommand(tile
, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE
, CcFoundTown
);
1223 void OnPlaceObjectAbort() override
1225 this->RaiseButtons();
1226 this->UpdateButtons(false);
1230 * Some data on this window has become invalid.
1231 * @param data Information about the changed data.
1232 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1234 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
1236 if (!gui_scope
) return;
1237 this->UpdateButtons(true);
1241 static WindowDesc
_found_town_desc(
1242 WDP_AUTO
, "build_town", 160, 162,
1243 WC_FOUND_TOWN
, WC_NONE
,
1245 _nested_found_town_widgets
, lengthof(_nested_found_town_widgets
)
1248 void ShowFoundTownWindow()
1250 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
1251 AllocateWindowDescFront
<FoundTownWindow
>(&_found_town_desc
, 0);
1254 void InitializeTownGui()
1256 _town_local_authority_kdtree
.Clear();