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 virtual void OnPaint()
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
];
158 (str
= STR_CARGO_RATING_APPALLING
, r
<= RATING_APPALLING
) || // Apalling
159 (str
++, r
<= RATING_VERYPOOR
) || // Very Poor
160 (str
++, r
<= RATING_POOR
) || // Poor
161 (str
++, r
<= RATING_MEDIOCRE
) || // Mediocore
162 (str
++, r
<= RATING_GOOD
) || // Good
163 (str
++, r
<= RATING_VERYGOOD
) || // Very Good
164 (str
++, r
<= RATING_EXCELLENT
) || // Excellent
165 (str
++, true); // Outstanding
168 if (this->town
->exclusivity
== c
->index
) {
169 DrawSprite(SPR_EXCLUSIVE_TRANSPORT
, COMPANY_SPRITE_COLOUR(c
->index
), exclusive_left
, y
+ exclusive_y_offset
);
172 DrawString(text_left
, text_right
, y
, STR_LOCAL_AUTHORITY_COMPANY_RATING
);
173 y
+= FONT_HEIGHT_NORMAL
;
177 y
= y
+ WD_FRAMERECT_BOTTOM
- nwid
->pos_y
; // Compute needed size of the widget.
178 if (y
> nwid
->current_y
) {
179 /* If the company list is too big to fit, mark ourself dirty and draw again. */
180 ResizeWindow(this, 0, y
- nwid
->current_y
, false);
184 virtual void SetStringParameters(int widget
) const
186 if (widget
== WID_TA_CAPTION
) SetDParam(0, this->window_number
);
189 virtual void DrawWidget(const Rect
&r
, int widget
) const
192 case WID_TA_ACTION_INFO
:
193 if (this->sel_index
!= -1) {
194 SetDParam(0, _price
[PR_TOWN_ACTION
] * _town_action_costs
[this->sel_index
] >> 8);
195 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_FRAMERECT_TOP
, r
.bottom
- WD_FRAMERECT_BOTTOM
,
196 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING
+ this->sel_index
);
199 case WID_TA_COMMAND_LIST
: {
201 uint buttons
= GetMaskOfTownActions(&numact
, _local_company
, this->town
);
202 int y
= r
.top
+ WD_FRAMERECT_TOP
;
203 int pos
= this->vscroll
->GetPosition();
206 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_LOCAL_AUTHORITY_ACTIONS_TITLE
);
207 y
+= FONT_HEIGHT_NORMAL
;
210 for (int i
= 0; buttons
; i
++, buttons
>>= 1) {
211 if (pos
<= -5) break; ///< Draw only the 5 fitting lines
213 if ((buttons
& 1) && --pos
< 0) {
214 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
,
215 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN
+ i
, this->sel_index
== i
? TC_WHITE
: TC_ORANGE
);
216 y
+= FONT_HEIGHT_NORMAL
;
224 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
227 case WID_TA_ACTION_INFO
: {
228 assert(size
->width
> padding
.width
&& size
->height
> padding
.height
);
229 size
->width
-= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
230 size
->height
-= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
231 Dimension d
= {0, 0};
232 for (int i
= 0; i
< TACT_COUNT
; i
++) {
233 SetDParam(0, _price
[PR_TOWN_ACTION
] * _town_action_costs
[i
] >> 8);
234 d
= maxdim(d
, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING
+ i
, *size
));
236 *size
= maxdim(*size
, d
);
237 size
->width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
238 size
->height
+= WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
242 case WID_TA_COMMAND_LIST
:
243 size
->height
= WD_FRAMERECT_TOP
+ 5 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
244 size
->width
= GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE
).width
;
245 for (uint i
= 0; i
< TACT_COUNT
; i
++ ) {
246 size
->width
= max(size
->width
, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN
+ i
).width
);
248 size
->width
+= WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
251 case WID_TA_RATING_INFO
:
252 resize
->height
= FONT_HEIGHT_NORMAL
;
253 size
->height
= WD_FRAMERECT_TOP
+ 9 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
258 virtual void OnClick(Point pt
, int widget
, int click_count
)
261 case WID_TA_COMMAND_LIST
: {
262 int y
= this->GetRowFromWidget(pt
.y
, WID_TA_COMMAND_LIST
, 1, FONT_HEIGHT_NORMAL
);
263 if (!IsInsideMM(y
, 0, 5)) return;
265 y
= GetNthSetBit(GetMaskOfTownActions(NULL
, _local_company
, this->town
), y
+ this->vscroll
->GetPosition() - 1);
270 /* When double-clicking, continue */
271 if (click_count
== 1 || y
< 0) break;
276 DoCommandP(this->town
->xy
, this->window_number
, this->sel_index
, CMD_DO_TOWN_ACTION
| CMD_MSG(STR_ERROR_CAN_T_DO_THIS
));
281 virtual void OnHundredthTick()
287 static WindowDesc
_town_authority_desc(
288 WDP_AUTO
, "view_town_authority", 317, 222,
289 WC_TOWN_AUTHORITY
, WC_NONE
,
291 _nested_town_authority_widgets
, lengthof(_nested_town_authority_widgets
)
294 static void ShowTownAuthorityWindow(uint town
)
296 AllocateWindowDescFront
<TownAuthorityWindow
>(&_town_authority_desc
, town
);
300 /* Town view window. */
301 struct TownViewWindow
: Window
{
303 Town
*town
; ///< Town displayed by the window.
306 static const int WID_TV_HEIGHT_NORMAL
= 150;
308 TownViewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
310 this->CreateNestedTree();
312 this->town
= Town::Get(window_number
);
313 if (this->town
->larger_town
) this->GetWidget
<NWidgetCore
>(WID_TV_CAPTION
)->widget_data
= STR_TOWN_VIEW_CITY_CAPTION
;
315 this->FinishInitNested(window_number
);
317 this->flags
|= WF_DISABLE_VP_SCROLL
;
318 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_TV_VIEWPORT
);
319 nvp
->InitializeViewport(this, this->town
->xy
, ZOOM_LVL_NEWS
);
321 /* disable renaming town in network games if you are not the server */
322 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME
, _networking
&& !_network_server
);
325 virtual void SetStringParameters(int widget
) const
327 if (widget
== WID_TV_CAPTION
) SetDParam(0, this->town
->index
);
330 virtual void DrawWidget(const Rect
&r
, int widget
) const
332 if (widget
!= WID_TV_INFO
) return;
334 uint y
= r
.top
+ WD_FRAMERECT_TOP
;
336 SetDParam(0, this->town
->cache
.population
);
337 SetDParam(1, this->town
->cache
.num_houses
);
338 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
, STR_TOWN_VIEW_POPULATION_HOUSES
);
340 SetDParam(0, this->town
->supplied
[CT_PASSENGERS
].old_act
);
341 SetDParam(1, this->town
->supplied
[CT_PASSENGERS
].old_max
);
342 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_PASSENGERS_LAST_MONTH_MAX
);
344 SetDParam(0, this->town
->supplied
[CT_MAIL
].old_act
);
345 SetDParam(1, this->town
->supplied
[CT_MAIL
].old_max
);
346 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_MAIL_LAST_MONTH_MAX
);
349 for (int i
= TE_BEGIN
; i
< TE_END
; i
++) {
350 if (this->town
->goal
[i
] == 0) continue;
351 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& (TileHeight(this->town
->xy
) < LowestSnowLine() || this->town
->cache
.population
<= 90)) continue;
352 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
&& (GetTropicZone(this->town
->xy
) != TROPICZONE_DESERT
|| this->town
->cache
.population
<= 60)) continue;
355 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH
);
359 bool rtl
= _current_text_dir
== TD_RTL
;
360 uint cargo_text_left
= r
.left
+ WD_FRAMERECT_LEFT
+ (rtl
? 0 : 20);
361 uint cargo_text_right
= r
.right
- WD_FRAMERECT_RIGHT
- (rtl
? 20 : 0);
363 const CargoSpec
*cargo
= FindFirstCargoWithTownEffect((TownEffect
)i
);
364 assert(cargo
!= NULL
);
368 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
|| this->town
->goal
[i
] == TOWN_GROWTH_WINTER
) {
369 /* For 'original' gameplay, don't show the amount required (you need 1 or more ..) */
370 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL
;
371 if (this->town
->received
[i
].old_act
== 0) {
372 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL
;
374 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& TileHeight(this->town
->xy
) < GetSnowLine()) {
375 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER
;
379 SetDParam(0, cargo
->name
);
381 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED
;
382 if (this->town
->received
[i
].old_act
< this->town
->goal
[i
]) {
383 string
= STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED
;
386 SetDParam(0, cargo
->Index());
387 SetDParam(1, this->town
->received
[i
].old_act
);
388 SetDParam(2, cargo
->Index());
389 SetDParam(3, this->town
->goal
[i
]);
391 DrawString(cargo_text_left
, cargo_text_right
, y
+= FONT_HEIGHT_NORMAL
, string
);
394 if (HasBit(this->town
->flags
, TOWN_IS_GROWING
)) {
395 SetDParam(0, ((this->town
->growth_rate
& (~TOWN_GROW_RATE_CUSTOM
)) * TOWN_GROWTH_TICKS
+ DAY_TICKS
) / DAY_TICKS
);
396 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, this->town
->fund_buildings_months
== 0 ? STR_TOWN_VIEW_TOWN_GROWS_EVERY
: STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED
);
398 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_TOWN_GROW_STOPPED
);
401 /* only show the town noise, if the noise option is activated. */
402 if (_settings_game
.economy
.station_noise_level
) {
403 SetDParam(0, this->town
->noise_reached
);
404 SetDParam(1, this->town
->MaxTownNoise());
405 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_LEFT
, y
+= FONT_HEIGHT_NORMAL
, STR_TOWN_VIEW_NOISE_IN_TOWN
);
408 if (this->town
->text
!= NULL
) {
409 SetDParamStr(0, this->town
->text
);
410 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
+= FONT_HEIGHT_NORMAL
, UINT16_MAX
, STR_JUST_RAW_STRING
, TC_BLACK
);
414 virtual void OnClick(Point pt
, int widget
, int click_count
)
417 case WID_TV_CENTER_VIEW
: // scroll to location
419 ShowExtraViewPortWindow(this->town
->xy
);
421 ScrollMainWindowToTile(this->town
->xy
);
425 case WID_TV_SHOW_AUTHORITY
: // town authority
426 ShowTownAuthorityWindow(this->window_number
);
429 case WID_TV_CHANGE_NAME
: // rename
430 SetDParam(0, this->window_number
);
431 ShowQueryString(STR_TOWN_NAME
, STR_TOWN_VIEW_RENAME_TOWN_BUTTON
, MAX_LENGTH_TOWN_NAME_CHARS
, this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
434 case WID_TV_EXPAND
: { // expand town - only available on Scenario editor
435 /* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
436 static bool _warn_town_no_roads
= false;
438 if (!_settings_game
.economy
.allow_town_roads
&& !_warn_town_no_roads
) {
439 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS
, INVALID_STRING_ID
, WL_WARNING
);
440 _warn_town_no_roads
= true;
443 DoCommandP(0, this->window_number
, 0, CMD_EXPAND_TOWN
| CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN
));
447 case WID_TV_DELETE
: // delete town - only available on Scenario editor
448 DoCommandP(0, this->window_number
, 0, CMD_DELETE_TOWN
| CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE
));
453 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
457 size
->height
= GetDesiredInfoHeight(size
->width
);
463 * Gets the desired height for the information panel.
464 * @return the desired height in pixels.
466 uint
GetDesiredInfoHeight(int width
) const
468 uint aimed_height
= 3 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
471 for (int i
= TE_BEGIN
; i
< TE_END
; i
++) {
472 if (this->town
->goal
[i
] == 0) continue;
473 if (this->town
->goal
[i
] == TOWN_GROWTH_WINTER
&& (TileHeight(this->town
->xy
) < LowestSnowLine() || this->town
->cache
.population
<= 90)) continue;
474 if (this->town
->goal
[i
] == TOWN_GROWTH_DESERT
&& (GetTropicZone(this->town
->xy
) != TROPICZONE_DESERT
|| this->town
->cache
.population
<= 60)) continue;
477 aimed_height
+= FONT_HEIGHT_NORMAL
;
480 aimed_height
+= FONT_HEIGHT_NORMAL
;
482 aimed_height
+= FONT_HEIGHT_NORMAL
;
484 if (_settings_game
.economy
.station_noise_level
) aimed_height
+= FONT_HEIGHT_NORMAL
;
486 if (this->town
->text
!= NULL
) {
487 SetDParamStr(0, this->town
->text
);
488 aimed_height
+= GetStringHeight(STR_JUST_RAW_STRING
, width
- WD_FRAMERECT_LEFT
- WD_FRAMERECT_RIGHT
);
494 void ResizeWindowAsNeeded()
496 const NWidgetBase
*nwid_info
= this->GetWidget
<NWidgetBase
>(WID_TV_INFO
);
497 uint aimed_height
= GetDesiredInfoHeight(nwid_info
->current_x
);
498 if (aimed_height
> nwid_info
->current_y
|| (aimed_height
< nwid_info
->current_y
&& nwid_info
->current_y
> nwid_info
->smallest_y
)) {
503 virtual void OnResize()
505 if (this->viewport
!= NULL
) {
506 NWidgetViewport
*nvp
= this->GetWidget
<NWidgetViewport
>(WID_TV_VIEWPORT
);
507 nvp
->UpdateViewportCoordinates(this);
509 ScrollWindowToTile(this->town
->xy
, this, true); // Re-center viewport.
514 * Some data on this window has become invalid.
515 * @param data Information about the changed data.
516 * @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.
518 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
520 if (!gui_scope
) return;
521 /* Called when setting station noise or required cargoes have changed, in order to resize the window */
522 this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
523 this->ResizeWindowAsNeeded();
526 virtual void OnQueryTextFinished(char *str
)
528 if (str
== NULL
) return;
530 DoCommandP(0, this->window_number
, 0, CMD_RENAME_TOWN
| CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN
), NULL
, str
);
534 static const NWidgetPart _nested_town_game_view_widgets
[] = {
535 NWidget(NWID_HORIZONTAL
),
536 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
537 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TV_CAPTION
), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
538 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
539 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
540 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
542 NWidget(WWT_PANEL
, COLOUR_BROWN
),
543 NWidget(WWT_INSET
, COLOUR_BROWN
), SetPadding(2, 2, 2, 2),
544 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_TV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
547 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TV_INFO
), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
548 NWidget(NWID_HORIZONTAL
),
549 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
550 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_CENTER_VIEW
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION
, STR_TOWN_VIEW_CENTER_TOOLTIP
),
551 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_SHOW_AUTHORITY
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON
, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP
),
552 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_CHANGE_NAME
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_RENAME
, STR_TOWN_VIEW_RENAME_TOOLTIP
),
554 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
558 static WindowDesc
_town_game_view_desc(
559 WDP_AUTO
, "view_town", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL
,
560 WC_TOWN_VIEW
, WC_NONE
,
562 _nested_town_game_view_widgets
, lengthof(_nested_town_game_view_widgets
)
565 static const NWidgetPart _nested_town_editor_view_widgets
[] = {
566 NWidget(NWID_HORIZONTAL
),
567 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
568 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_TV_CAPTION
), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
569 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_CHANGE_NAME
), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME
, STR_TOWN_VIEW_RENAME_TOOLTIP
),
570 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
571 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
572 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
574 NWidget(WWT_PANEL
, COLOUR_BROWN
),
575 NWidget(WWT_INSET
, COLOUR_BROWN
), SetPadding(2, 2, 2, 2),
576 NWidget(NWID_VIEWPORT
, INVALID_COLOUR
, WID_TV_VIEWPORT
), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
579 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TV_INFO
), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
580 NWidget(NWID_HORIZONTAL
),
581 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
582 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_CENTER_VIEW
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION
, STR_TOWN_VIEW_CENTER_TOOLTIP
),
583 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_EXPAND
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_EXPAND_BUTTON
, STR_TOWN_VIEW_EXPAND_TOOLTIP
),
584 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_TV_DELETE
), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON
, STR_TOWN_VIEW_DELETE_TOOLTIP
),
586 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
590 static WindowDesc
_town_editor_view_desc(
591 WDP_AUTO
, "view_town_scen", 260, TownViewWindow::WID_TV_HEIGHT_NORMAL
,
592 WC_TOWN_VIEW
, WC_NONE
,
594 _nested_town_editor_view_widgets
, lengthof(_nested_town_editor_view_widgets
)
597 void ShowTownViewWindow(TownID town
)
599 if (_game_mode
== GM_EDITOR
) {
600 AllocateWindowDescFront
<TownViewWindow
>(&_town_editor_view_desc
, town
);
602 AllocateWindowDescFront
<TownViewWindow
>(&_town_game_view_desc
, town
);
606 static const NWidgetPart _nested_town_directory_widgets
[] = {
607 NWidget(NWID_HORIZONTAL
),
608 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
609 NWidget(WWT_CAPTION
, COLOUR_BROWN
), SetDataTip(STR_TOWN_DIRECTORY_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
610 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
611 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
612 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
614 NWidget(NWID_HORIZONTAL
),
615 NWidget(NWID_VERTICAL
),
616 NWidget(NWID_HORIZONTAL
),
617 NWidget(WWT_TEXTBTN
, COLOUR_BROWN
, WID_TD_SORT_ORDER
), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
618 NWidget(WWT_DROPDOWN
, COLOUR_BROWN
, WID_TD_SORT_CRITERIA
), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_SORT_CRITERIA
),
619 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetResize(1, 0), EndContainer(),
621 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_TD_LIST
), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP
),
622 SetFill(1, 0), SetResize(0, 10), SetScrollbar(WID_TD_SCROLLBAR
), EndContainer(),
623 NWidget(WWT_PANEL
, COLOUR_BROWN
),
624 NWidget(WWT_TEXT
, COLOUR_BROWN
, WID_TD_WORLD_POPULATION
), SetPadding(2, 0, 0, 2), SetMinimalSize(196, 12), SetFill(1, 0), SetDataTip(STR_TOWN_POPULATION
, STR_NULL
),
627 NWidget(NWID_VERTICAL
),
628 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_TD_SCROLLBAR
),
629 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
634 /** Town directory window class. */
635 struct TownDirectoryWindow
: public Window
{
637 /* Runtime saved values */
638 static Listing last_sorting
;
639 static const Town
*last_town
;
641 /* Constants for sorting towns */
642 static const StringID sorter_names
[];
643 static GUITownList::SortFunction
* const sorter_funcs
[];
649 void BuildSortTownList()
651 if (this->towns
.NeedRebuild()) {
656 *this->towns
.Append() = t
;
659 this->towns
.Compact();
660 this->towns
.RebuildDone();
661 this->vscroll
->SetCount(this->towns
.Length()); // Update scrollbar as well.
663 /* Always sort the towns. */
664 this->last_town
= NULL
;
666 this->SetWidgetDirty(WID_TD_LIST
); // Force repaint of the displayed towns.
669 /** Sort by town name */
670 static int CDECL
TownNameSorter(const Town
* const *a
, const Town
* const *b
)
672 static char buf_cache
[64];
677 SetDParam(0, ta
->index
);
678 GetString(buf
, STR_TOWN_NAME
, lastof(buf
));
680 /* If 'b' is the same town as in the last round, use the cached value
681 * We do this to speed stuff up ('b' is called with the same value a lot of
682 * times after each other) */
683 if (tb
!= last_town
) {
685 SetDParam(0, tb
->index
);
686 GetString(buf_cache
, STR_TOWN_NAME
, lastof(buf_cache
));
689 return strnatcmp(buf
, buf_cache
); // Sort by name (natural sorting).
692 /** Sort by population (default descending, as big towns are of the most interest). */
693 static int CDECL
TownPopulationSorter(const Town
* const *a
, const Town
* const *b
)
695 uint32 a_population
= (*a
)->cache
.population
;
696 uint32 b_population
= (*b
)->cache
.population
;
697 if (a_population
== b_population
) return TownDirectoryWindow::TownNameSorter(a
, b
);
698 return (a_population
< b_population
) ? -1 : 1;
701 /** Sort by town rating */
702 static int CDECL
TownRatingSorter(const Town
* const *a
, const Town
* const *b
)
704 int before
= TownDirectoryWindow::last_sorting
.order
? 1 : -1; // Value to get 'a' before 'b'.
706 /* Towns without rating are always after towns with rating. */
707 if (HasBit((*a
)->have_ratings
, _local_company
)) {
708 if (HasBit((*b
)->have_ratings
, _local_company
)) {
709 int16 a_rating
= (*a
)->ratings
[_local_company
];
710 int16 b_rating
= (*b
)->ratings
[_local_company
];
711 if (a_rating
== b_rating
) return TownDirectoryWindow::TownNameSorter(a
, b
);
712 return (a_rating
< b_rating
) ? -1 : 1;
716 if (HasBit((*b
)->have_ratings
, _local_company
)) return -before
;
717 return -before
* TownDirectoryWindow::TownNameSorter(a
, b
); // Sort unrated towns always on ascending town name.
721 TownDirectoryWindow(WindowDesc
*desc
) : Window(desc
)
723 this->CreateNestedTree();
725 this->vscroll
= this->GetScrollbar(WID_TD_SCROLLBAR
);
727 this->towns
.SetListing(this->last_sorting
);
728 this->towns
.SetSortFuncs(TownDirectoryWindow::sorter_funcs
);
729 this->towns
.ForceRebuild();
730 this->BuildSortTownList();
732 this->FinishInitNested(0);
735 virtual void SetStringParameters(int widget
) const
738 case WID_TD_WORLD_POPULATION
:
739 SetDParam(0, GetWorldPopulation());
742 case WID_TD_SORT_CRITERIA
:
743 SetDParam(0, TownDirectoryWindow::sorter_names
[this->towns
.SortType()]);
748 virtual void DrawWidget(const Rect
&r
, int widget
) const
751 case WID_TD_SORT_ORDER
:
752 this->DrawSortButtonState(widget
, this->towns
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
757 int y
= r
.top
+ WD_FRAMERECT_TOP
;
758 if (this->towns
.Length() == 0) { // No towns available.
759 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
, y
, STR_TOWN_DIRECTORY_NONE
);
763 /* At least one town available. */
764 bool rtl
= _current_text_dir
== TD_RTL
;
765 Dimension icon_size
= GetSpriteSize(SPR_TOWN_RATING_GOOD
);
766 int text_left
= r
.left
+ WD_FRAMERECT_LEFT
+ (rtl
? 0 : icon_size
.width
+ 2);
767 int text_right
= r
.right
- WD_FRAMERECT_RIGHT
- (rtl
? icon_size
.width
+ 2 : 0);
768 int icon_x
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
- icon_size
.width
: r
.left
+ WD_FRAMERECT_LEFT
;
770 for (uint i
= this->vscroll
->GetPosition(); i
< this->towns
.Length(); i
++) {
771 const Town
*t
= this->towns
[i
];
772 assert(t
->xy
!= INVALID_TILE
);
774 /* Draw rating icon. */
775 if (_game_mode
== GM_EDITOR
|| !HasBit(t
->have_ratings
, _local_company
)) {
776 DrawSprite(SPR_TOWN_RATING_NA
, PAL_NONE
, icon_x
, y
+ (this->resize
.step_height
- icon_size
.height
) / 2);
778 SpriteID icon
= SPR_TOWN_RATING_APALLING
;
779 if (t
->ratings
[_local_company
] > RATING_VERYPOOR
) icon
= SPR_TOWN_RATING_MEDIOCRE
;
780 if (t
->ratings
[_local_company
] > RATING_GOOD
) icon
= SPR_TOWN_RATING_GOOD
;
781 DrawSprite(icon
, PAL_NONE
, icon_x
, y
+ (this->resize
.step_height
- icon_size
.height
) / 2);
784 SetDParam(0, t
->index
);
785 SetDParam(1, t
->cache
.population
);
786 DrawString(text_left
, text_right
, y
+ (this->resize
.step_height
- FONT_HEIGHT_NORMAL
) / 2, STR_TOWN_DIRECTORY_TOWN
);
788 y
+= this->resize
.step_height
;
789 if (++n
== this->vscroll
->GetCapacity()) break; // max number of towns in 1 window
796 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
799 case WID_TD_SORT_ORDER
: {
800 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
801 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
802 d
.height
+= padding
.height
;
803 *size
= maxdim(*size
, d
);
806 case WID_TD_SORT_CRITERIA
: {
807 Dimension d
= {0, 0};
808 for (uint i
= 0; TownDirectoryWindow::sorter_names
[i
] != INVALID_STRING_ID
; i
++) {
809 d
= maxdim(d
, GetStringBoundingBox(TownDirectoryWindow::sorter_names
[i
]));
811 d
.width
+= padding
.width
;
812 d
.height
+= padding
.height
;
813 *size
= maxdim(*size
, d
);
817 Dimension d
= GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE
);
818 for (uint i
= 0; i
< this->towns
.Length(); i
++) {
819 const Town
*t
= this->towns
[i
];
823 SetDParam(0, t
->index
);
824 SetDParamMaxDigits(1, 8);
825 d
= maxdim(d
, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN
));
827 Dimension icon_size
= GetSpriteSize(SPR_TOWN_RATING_GOOD
);
828 d
.width
+= icon_size
.width
+ 2;
829 d
.height
= max(d
.height
, icon_size
.height
);
830 resize
->height
= d
.height
;
832 d
.width
+= padding
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
833 d
.height
+= padding
.height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
834 *size
= maxdim(*size
, d
);
837 case WID_TD_WORLD_POPULATION
: {
838 SetDParamMaxDigits(0, 10);
839 Dimension d
= GetStringBoundingBox(STR_TOWN_POPULATION
);
840 d
.width
+= padding
.width
;
841 d
.height
+= padding
.height
;
842 *size
= maxdim(*size
, d
);
848 virtual void OnClick(Point pt
, int widget
, int click_count
)
851 case WID_TD_SORT_ORDER
: // Click on sort order button
852 if (this->towns
.SortType() != 2) { // A different sort than by rating.
853 this->towns
.ToggleSortOrder();
854 this->last_sorting
= this->towns
.GetListing(); // Store new sorting order.
856 /* Some parts are always sorted ascending on name. */
857 this->last_sorting
.order
= !this->last_sorting
.order
;
858 this->towns
.SetListing(this->last_sorting
);
859 this->towns
.ForceResort();
865 case WID_TD_SORT_CRITERIA
: // Click on sort criteria dropdown
866 ShowDropDownMenu(this, TownDirectoryWindow::sorter_names
, this->towns
.SortType(), WID_TD_SORT_CRITERIA
, 0, 0);
869 case WID_TD_LIST
: { // Click on Town Matrix
870 uint id_v
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_TD_LIST
, WD_FRAMERECT_TOP
);
871 if (id_v
>= this->towns
.Length()) return; // click out of town bounds
873 const Town
*t
= this->towns
[id_v
];
876 ShowExtraViewPortWindow(t
->xy
);
878 ScrollMainWindowToTile(t
->xy
);
885 virtual void OnDropdownSelect(int widget
, int index
)
887 if (widget
!= WID_TD_SORT_CRITERIA
) return;
889 if (this->towns
.SortType() != index
) {
890 this->towns
.SetSortType(index
);
891 this->last_sorting
= this->towns
.GetListing(); // Store new sorting order.
892 this->BuildSortTownList();
896 virtual void OnPaint()
898 if (this->towns
.NeedRebuild()) this->BuildSortTownList();
902 virtual void OnHundredthTick()
904 this->BuildSortTownList();
908 virtual void OnResize()
910 this->vscroll
->SetCapacityFromWidget(this, WID_TD_LIST
);
914 * Some data on this window has become invalid.
915 * @param data Information about the changed data.
916 * @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.
918 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
921 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
922 this->towns
.ForceRebuild();
924 this->towns
.ForceResort();
929 Listing
TownDirectoryWindow::last_sorting
= {false, 0};
930 const Town
*TownDirectoryWindow::last_town
= NULL
;
932 /** Names of the sorting functions. */
933 const StringID
TownDirectoryWindow::sorter_names
[] = {
935 STR_SORT_BY_POPULATION
,
940 /** Available town directory sorting functions. */
941 GUITownList::SortFunction
* const TownDirectoryWindow::sorter_funcs
[] = {
943 &TownPopulationSorter
,
947 static WindowDesc
_town_directory_desc(
948 WDP_AUTO
, "list_towns", 208, 202,
949 WC_TOWN_DIRECTORY
, WC_NONE
,
951 _nested_town_directory_widgets
, lengthof(_nested_town_directory_widgets
)
954 void ShowTownDirectory()
956 if (BringWindowToFrontById(WC_TOWN_DIRECTORY
, 0)) return;
957 new TownDirectoryWindow(&_town_directory_desc
);
960 void CcFoundTown(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
962 if (result
.Failed()) return;
964 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_1F_SPLAT_OTHER
, tile
);
965 if (!_settings_client
.gui
.persistent_buildingtools
) ResetObjectToPlace();
968 void CcFoundRandomTown(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
970 if (result
.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id
)->xy
);
973 static const NWidgetPart _nested_found_town_widgets
[] = {
974 NWidget(NWID_HORIZONTAL
),
975 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
976 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_FOUND_TOWN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
977 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
978 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
980 /* Construct new town(s) buttons. */
981 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
982 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
983 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_NEW_TOWN
), SetMinimalSize(156, 12), SetFill(1, 0),
984 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON
, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP
), SetPadding(0, 2, 1, 2),
985 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_RANDOM_TOWN
), SetMinimalSize(156, 12), SetFill(1, 0),
986 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON
, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP
), SetPadding(0, 2, 1, 2),
987 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_MANY_RANDOM_TOWNS
), SetMinimalSize(156, 12), SetFill(1, 0),
988 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS
, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP
), SetPadding(0, 2, 0, 2),
989 /* Town name selection. */
990 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE
, STR_NULL
),
991 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_TF_TOWN_NAME_EDITBOX
), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
992 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE
, STR_FOUND_TOWN_NAME_EDITOR_HELP
),
993 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_TF_TOWN_NAME_RANDOM
), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
994 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON
, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP
),
995 /* Town size selection. */
996 NWidget(NWID_HORIZONTAL
), SetPIP(2, 0, 2),
997 NWidget(NWID_SPACER
), SetFill(1, 0),
998 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE
, STR_NULL
),
999 NWidget(NWID_SPACER
), SetFill(1, 0),
1001 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1002 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_SMALL
), SetMinimalSize(78, 12), SetFill(1, 0),
1003 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1004 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_MEDIUM
), SetMinimalSize(78, 12), SetFill(1, 0),
1005 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1007 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1008 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1009 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_LARGE
), SetMinimalSize(78, 12), SetFill(1, 0),
1010 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1011 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_SIZE_RANDOM
), SetMinimalSize(78, 12), SetFill(1, 0),
1012 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM
, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP
),
1014 NWidget(NWID_SPACER
), SetMinimalSize(0, 3),
1015 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_CITY
), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
1016 SetDataTip(STR_FOUND_TOWN_CITY
, STR_FOUND_TOWN_CITY_TOOLTIP
), SetFill(1, 0),
1017 /* Town roads selection. */
1018 NWidget(NWID_HORIZONTAL
), SetPIP(2, 0, 2),
1019 NWidget(NWID_SPACER
), SetFill(1, 0),
1020 NWidget(WWT_LABEL
, COLOUR_DARK_GREEN
), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT
, STR_NULL
),
1021 NWidget(NWID_SPACER
), SetFill(1, 0),
1023 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1024 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_ORIGINAL
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_ORIGINAL
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1025 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_BETTER
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_BETTER_ROADS
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1027 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1028 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(2, 0, 2),
1029 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_GRID2
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_2X2_GRID
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1030 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_GRID3
), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_3X3_GRID
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
),
1032 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
1033 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_TF_LAYOUT_RANDOM
), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
1034 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM
, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT
), SetFill(1, 0),
1035 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
1039 /** Found a town window class. */
1040 struct FoundTownWindow
: Window
{
1042 TownSize town_size
; ///< Selected town size
1043 TownLayout town_layout
; ///< Selected town layout
1044 bool city
; ///< Are we building a city?
1045 QueryString townname_editbox
; ///< Townname editbox
1046 bool townnamevalid
; ///< Is generated town name valid?
1047 uint32 townnameparts
; ///< Generated town name
1048 TownNameParams params
; ///< Town name parameters
1051 FoundTownWindow(WindowDesc
*desc
, WindowNumber window_number
) :
1053 town_size(TSZ_MEDIUM
),
1054 town_layout(_settings_game
.economy
.town_layout
),
1055 townname_editbox(MAX_LENGTH_TOWN_NAME_CHARS
* MAX_CHAR_LENGTH
, MAX_LENGTH_TOWN_NAME_CHARS
),
1056 params(_settings_game
.game_creation
.town_name
)
1058 this->InitNested(window_number
);
1059 this->querystrings
[WID_TF_TOWN_NAME_EDITBOX
] = &this->townname_editbox
;
1060 this->RandomTownName();
1061 this->UpdateButtons(true);
1064 void RandomTownName()
1066 this->townnamevalid
= GenerateTownName(&this->townnameparts
);
1068 if (!this->townnamevalid
) {
1069 this->townname_editbox
.text
.DeleteAll();
1071 GetTownName(this->townname_editbox
.text
.buf
, &this->params
, this->townnameparts
, &this->townname_editbox
.text
.buf
[this->townname_editbox
.text
.max_bytes
- 1]);
1072 this->townname_editbox
.text
.UpdateSize();
1074 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX
);
1076 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX
);
1079 void UpdateButtons(bool check_availability
)
1081 if (check_availability
&& _game_mode
!= GM_EDITOR
) {
1082 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN
, WID_TF_MANY_RANDOM_TOWNS
, WID_TF_SIZE_LARGE
, WIDGET_LIST_END
);
1083 this->SetWidgetsDisabledState(_settings_game
.economy
.found_town
!= TF_CUSTOM_LAYOUT
,
1084 WID_TF_LAYOUT_ORIGINAL
, WID_TF_LAYOUT_BETTER
, WID_TF_LAYOUT_GRID2
, WID_TF_LAYOUT_GRID3
, WID_TF_LAYOUT_RANDOM
, WIDGET_LIST_END
);
1085 if (_settings_game
.economy
.found_town
!= TF_CUSTOM_LAYOUT
) town_layout
= _settings_game
.economy
.town_layout
;
1088 for (int i
= WID_TF_SIZE_SMALL
; i
<= WID_TF_SIZE_RANDOM
; i
++) {
1089 this->SetWidgetLoweredState(i
, i
== WID_TF_SIZE_SMALL
+ this->town_size
);
1092 this->SetWidgetLoweredState(WID_TF_CITY
, this->city
);
1094 for (int i
= WID_TF_LAYOUT_ORIGINAL
; i
<= WID_TF_LAYOUT_RANDOM
; i
++) {
1095 this->SetWidgetLoweredState(i
, i
== WID_TF_LAYOUT_ORIGINAL
+ this->town_layout
);
1101 void ExecuteFoundTownCommand(TileIndex tile
, bool random
, StringID errstr
, CommandCallback cc
)
1103 const char *name
= NULL
;
1105 if (!this->townnamevalid
) {
1106 name
= this->townname_editbox
.text
.buf
;
1108 /* If user changed the name, send it */
1109 char buf
[MAX_LENGTH_TOWN_NAME_CHARS
* MAX_CHAR_LENGTH
];
1110 GetTownName(buf
, &this->params
, this->townnameparts
, lastof(buf
));
1111 if (strcmp(buf
, this->townname_editbox
.text
.buf
) != 0) name
= this->townname_editbox
.text
.buf
;
1114 bool success
= DoCommandP(tile
, this->town_size
| this->city
<< 2 | this->town_layout
<< 3 | random
<< 6,
1115 townnameparts
, CMD_FOUND_TOWN
| CMD_MSG(errstr
), cc
, name
);
1117 /* Rerandomise name, if success and no cost-estimation. */
1118 if (success
&& !_shift_pressed
) this->RandomTownName();
1121 virtual void OnClick(Point pt
, int widget
, int click_count
)
1124 case WID_TF_NEW_TOWN
:
1125 HandlePlacePushButton(this, WID_TF_NEW_TOWN
, SPR_CURSOR_TOWN
, HT_RECT
);
1128 case WID_TF_RANDOM_TOWN
:
1129 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN
, CcFoundRandomTown
);
1132 case WID_TF_TOWN_NAME_RANDOM
:
1133 this->RandomTownName();
1134 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX
);
1137 case WID_TF_MANY_RANDOM_TOWNS
:
1138 _generating_world
= true;
1139 UpdateNearestTownForRoadTiles(true);
1140 if (!GenerateTowns(this->town_layout
)) {
1141 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN
, STR_ERROR_NO_SPACE_FOR_TOWN
, WL_INFO
);
1143 UpdateNearestTownForRoadTiles(false);
1144 _generating_world
= false;
1147 case WID_TF_SIZE_SMALL
: case WID_TF_SIZE_MEDIUM
: case WID_TF_SIZE_LARGE
: case WID_TF_SIZE_RANDOM
:
1148 this->town_size
= (TownSize
)(widget
- WID_TF_SIZE_SMALL
);
1149 this->UpdateButtons(false);
1154 this->SetWidgetLoweredState(WID_TF_CITY
, this->city
);
1158 case WID_TF_LAYOUT_ORIGINAL
: case WID_TF_LAYOUT_BETTER
: case WID_TF_LAYOUT_GRID2
:
1159 case WID_TF_LAYOUT_GRID3
: case WID_TF_LAYOUT_RANDOM
:
1160 this->town_layout
= (TownLayout
)(widget
- WID_TF_LAYOUT_ORIGINAL
);
1161 this->UpdateButtons(false);
1166 virtual void OnPlaceObject(Point pt
, TileIndex tile
)
1168 this->ExecuteFoundTownCommand(tile
, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE
, CcFoundTown
);
1171 virtual void OnPlaceObjectAbort()
1173 this->RaiseButtons();
1174 this->UpdateButtons(false);
1178 * Some data on this window has become invalid.
1179 * @param data Information about the changed data.
1180 * @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.
1182 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
1184 if (!gui_scope
) return;
1185 this->UpdateButtons(true);
1189 static WindowDesc
_found_town_desc(
1190 WDP_AUTO
, "build_town", 160, 162,
1191 WC_FOUND_TOWN
, WC_NONE
,
1193 _nested_found_town_widgets
, lengthof(_nested_found_town_widgets
)
1196 void ShowFoundTownWindow()
1198 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
1199 AllocateWindowDescFront
<FoundTownWindow
>(&_found_town_desc
, 0);