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 terraform_gui.cpp GUI related to terraforming the map. */
13 #include "map/ground.h"
14 #include "company_func.h"
15 #include "company_base.h"
17 #include "window_gui.h"
18 #include "window_func.h"
19 #include "viewport_func.h"
20 #include "command_func.h"
21 #include "signs_func.h"
22 #include "sound_func.h"
23 #include "base_station_base.h"
24 #include "textbuf_gui.h"
26 #include "landscape_type.h"
27 #include "tilehighlight_func.h"
28 #include "strings_func.h"
29 #include "newgrf_object.h"
32 #include "engine_base.h"
33 #include "terraform_gui.h"
35 #include "widgets/terraform_widget.h"
37 #include "table/strings.h"
39 void CcTerraform(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
41 if (result
.Succeeded()) {
42 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_1F_SPLAT
, tile
);
44 extern TileIndex _terraform_err_tile
;
45 SetRedErrorSquare(_terraform_err_tile
);
50 /** Scenario editor command that generates desert areas */
51 static void GenerateDesertArea(TileIndex end
, TileIndex start
)
53 if (_game_mode
!= GM_EDITOR
) return;
55 _generating_world
= true;
57 TileArea
ta(start
, end
);
58 TILE_AREA_LOOP(tile
, ta
) {
59 SetTropicZone(tile
, (_ctrl_pressed
) ? TROPICZONE_NORMAL
: TROPICZONE_DESERT
);
60 DoCommandP(tile
, 0, 0, CMD_LANDSCAPE_CLEAR
);
61 MarkTileDirtyByTile(tile
);
63 _generating_world
= false;
64 InvalidateWindowClassesData(WC_TOWN_VIEW
, 0);
67 /** Scenario editor command that generates rocky areas */
68 static void GenerateRockyArea(TileIndex end
, TileIndex start
)
70 if (_game_mode
!= GM_EDITOR
) return;
73 TileArea
ta(start
, end
);
75 TILE_AREA_LOOP(tile
, ta
) {
76 if (!IsGroundTile(tile
)) continue;
77 if (IsTreeTile(tile
) && GetClearGround(tile
) == GROUND_SHORE
) continue;
78 MakeClear(tile
, GROUND_ROCKS
, 3);
79 MarkTileDirtyByTile(tile
);
83 if (success
&& _settings_client
.sound
.confirm
) SndPlayTileFx(SND_1F_SPLAT
, end
);
87 * A central place to handle all X_AND_Y dragged GUI functions.
88 * @param proc Procedure related to the dragging
89 * @param start_tile Begin of the dragging
90 * @param end_tile End of the dragging
91 * @return Returns true if the action was found and handled, and false otherwise. This
92 * allows for additional implements that are more local. For example X_Y drag
93 * of convertrail which belongs in rail_gui.cpp and not terraform_gui.cpp
95 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc
, TileIndex start_tile
, TileIndex end_tile
)
97 if (!_settings_game
.construction
.freeform_edges
) {
98 /* When end_tile is void, the error tile will not be visible to the
99 * user. This happens when terraforming at the southern border. */
100 if (TileX(end_tile
) == MapMaxX()) end_tile
+= TileDiffXY(-1, 0);
101 if (TileY(end_tile
) == MapMaxY()) end_tile
+= TileDiffXY(0, -1);
105 case DDSP_DEMOLISH_AREA
:
106 DoCommandP(end_tile
, start_tile
, _ctrl_pressed
? 1 : 0, CMD_CLEAR_AREA
| CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA
), CcPlaySound10
);
108 case DDSP_RAISE_AND_LEVEL_AREA
:
109 DoCommandP(end_tile
, start_tile
, LM_RAISE
<< 1 | (_ctrl_pressed
? 1 : 0), CMD_LEVEL_LAND
| CMD_MSG(STR_ERROR_CAN_T_RAISE_LAND_HERE
), CcTerraform
);
111 case DDSP_LOWER_AND_LEVEL_AREA
:
112 DoCommandP(end_tile
, start_tile
, LM_LOWER
<< 1 | (_ctrl_pressed
? 1 : 0), CMD_LEVEL_LAND
| CMD_MSG(STR_ERROR_CAN_T_LOWER_LAND_HERE
), CcTerraform
);
114 case DDSP_LEVEL_AREA
:
115 DoCommandP(end_tile
, start_tile
, LM_LEVEL
<< 1 | (_ctrl_pressed
? 1 : 0), CMD_LEVEL_LAND
| CMD_MSG(STR_ERROR_CAN_T_LEVEL_LAND_HERE
), CcTerraform
);
117 case DDSP_CREATE_ROCKS
:
118 GenerateRockyArea(end_tile
, start_tile
);
120 case DDSP_CREATE_DESERT
:
121 GenerateDesertArea(end_tile
, start_tile
);
131 * Start a drag for demolishing an area.
132 * @param tile Position of one corner.
134 void PlaceProc_DemolishArea(TileIndex tile
)
136 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_DEMOLISH_AREA
);
139 /** Terra form toolbar managing class. */
140 struct TerraformToolbarWindow
: Window
{
141 int last_user_action
; ///< Last started user action.
143 TerraformToolbarWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
145 /* This is needed as we like to have the tree available on OnInit. */
146 this->CreateNestedTree();
147 this->FinishInitNested(window_number
);
148 this->last_user_action
= WIDGET_LIST_END
;
151 ~TerraformToolbarWindow()
155 virtual void OnInit()
157 /* Don't show the place object button when there are no objects to place. */
158 NWidgetStacked
*show_object
= this->GetWidget
<NWidgetStacked
>(WID_TT_SHOW_PLACE_OBJECT
);
159 show_object
->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE
);
162 virtual void OnClick(Point pt
, int widget
, int click_count
)
164 if (widget
< WID_TT_BUTTONS_START
) return;
167 case WID_TT_LOWER_LAND
: // Lower land button
168 HandlePlacePushButton(this, WID_TT_LOWER_LAND
, ANIMCURSOR_LOWERLAND
, HT_POINT
| HT_DIAGONAL
);
169 this->last_user_action
= widget
;
172 case WID_TT_RAISE_LAND
: // Raise land button
173 HandlePlacePushButton(this, WID_TT_RAISE_LAND
, ANIMCURSOR_RAISELAND
, HT_POINT
| HT_DIAGONAL
);
174 this->last_user_action
= widget
;
177 case WID_TT_LEVEL_LAND
: // Level land button
178 HandlePlacePushButton(this, WID_TT_LEVEL_LAND
, SPR_CURSOR_LEVEL_LAND
, HT_POINT
| HT_DIAGONAL
);
179 this->last_user_action
= widget
;
182 case WID_TT_DEMOLISH
: // Demolish aka dynamite button
183 HandlePlacePushButton(this, WID_TT_DEMOLISH
, ANIMCURSOR_DEMOLISH
, HT_RECT
| HT_DIAGONAL
);
184 this->last_user_action
= widget
;
187 case WID_TT_BUY_LAND
: // Buy land button
188 HandlePlacePushButton(this, WID_TT_BUY_LAND
, SPR_CURSOR_BUY_LAND
, HT_RECT
);
189 this->last_user_action
= widget
;
192 case WID_TT_PLANT_TREES
: // Plant trees button
193 ShowBuildTreesToolbar();
196 case WID_TT_PLACE_SIGN
: // Place sign button
197 HandlePlacePushButton(this, WID_TT_PLACE_SIGN
, SPR_CURSOR_SIGN
, HT_RECT
);
198 this->last_user_action
= widget
;
201 case WID_TT_PLACE_OBJECT
: // Place object button
202 /* Don't show the place object button when there are no objects to place. */
203 if (ObjectClass::GetUIClassCount() == 0) return;
204 if (HandlePlacePushButton(this, WID_TT_PLACE_OBJECT
, SPR_CURSOR_TRANSMITTER
, HT_RECT
)) {
205 ShowBuildObjectPicker(this);
206 this->last_user_action
= widget
;
210 default: NOT_REACHED();
214 virtual void OnPlaceObject(Point pt
, TileIndex tile
)
216 switch (this->last_user_action
) {
217 case WID_TT_LOWER_LAND
: // Lower land button
218 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_LOWER_AND_LEVEL_AREA
);
221 case WID_TT_RAISE_LAND
: // Raise land button
222 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_RAISE_AND_LEVEL_AREA
);
225 case WID_TT_LEVEL_LAND
: // Level land button
226 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_LEVEL_AREA
);
229 case WID_TT_DEMOLISH
: // Demolish aka dynamite button
230 PlaceProc_DemolishArea(tile
);
233 case WID_TT_BUY_LAND
: // Buy land button
234 DoCommandP(tile
, OBJECT_OWNED_LAND
, 0, CMD_BUILD_OBJECT
| CMD_MSG(STR_ERROR_CAN_T_PURCHASE_THIS_LAND
), CcPlaySound1E
);
237 case WID_TT_PLACE_SIGN
: // Place sign button
238 PlaceProc_Sign(tile
);
241 case WID_TT_PLACE_OBJECT
: // Place object button
242 PlaceProc_Object(tile
);
245 default: NOT_REACHED();
249 virtual void OnPlaceDrag(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
)
251 VpSelectTilesWithMethod(pt
.x
, pt
.y
, select_method
);
254 virtual Point
OnInitialPosition(int16 sm_width
, int16 sm_height
, int window_number
)
256 Point pt
= GetToolbarAlignedWindowPosition(sm_width
);
261 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
, TileIndex start_tile
, TileIndex end_tile
)
264 switch (select_proc
) {
265 default: NOT_REACHED();
266 case DDSP_DEMOLISH_AREA
:
267 case DDSP_RAISE_AND_LEVEL_AREA
:
268 case DDSP_LOWER_AND_LEVEL_AREA
:
269 case DDSP_LEVEL_AREA
:
270 GUIPlaceProcDragXY(select_proc
, start_tile
, end_tile
);
276 virtual void OnPlaceObjectAbort()
278 DeleteWindowById(WC_BUILD_OBJECT
, 0);
279 this->RaiseButtons();
282 static HotkeyList hotkeys
;
286 * Handler for global hotkeys of the TerraformToolbarWindow.
287 * @param hotkey Hotkey
288 * @return ES_HANDLED if hotkey was accepted.
290 static EventState
TerraformToolbarGlobalHotkeys(int hotkey
)
292 if (_game_mode
!= GM_NORMAL
) return ES_NOT_HANDLED
;
293 Window
*w
= ShowTerraformToolbar(NULL
);
294 if (w
== NULL
) return ES_NOT_HANDLED
;
295 return w
->OnHotkey(hotkey
);
298 static Hotkey terraform_hotkeys
[] = {
299 Hotkey('Q' | WKC_GLOBAL_HOTKEY
, "lower", WID_TT_LOWER_LAND
),
300 Hotkey('W' | WKC_GLOBAL_HOTKEY
, "raise", WID_TT_RAISE_LAND
),
301 Hotkey('E' | WKC_GLOBAL_HOTKEY
, "level", WID_TT_LEVEL_LAND
),
302 Hotkey('D' | WKC_GLOBAL_HOTKEY
, "dynamite", WID_TT_DEMOLISH
),
303 Hotkey('U', "buyland", WID_TT_BUY_LAND
),
304 Hotkey('I', "trees", WID_TT_PLANT_TREES
),
305 Hotkey('O', "placesign", WID_TT_PLACE_SIGN
),
306 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT
),
309 HotkeyList
TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys
, TerraformToolbarGlobalHotkeys
);
311 static const NWidgetPart _nested_terraform_widgets
[] = {
312 NWidget(NWID_HORIZONTAL
),
313 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
314 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_LANDSCAPING_TOOLBAR
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
315 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
317 NWidget(NWID_HORIZONTAL
),
318 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_TT_LOWER_LAND
), SetMinimalSize(22, 22),
319 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN
, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND
),
320 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_TT_RAISE_LAND
), SetMinimalSize(22, 22),
321 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP
, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND
),
322 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_TT_LEVEL_LAND
), SetMinimalSize(22, 22),
323 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND
, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP
),
325 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetMinimalSize(4, 22), EndContainer(),
327 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_TT_DEMOLISH
), SetMinimalSize(22, 22),
328 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE
, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC
),
329 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_TT_BUY_LAND
), SetMinimalSize(22, 22),
330 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND
, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND
),
331 NWidget(WWT_PUSHIMGBTN
, COLOUR_DARK_GREEN
, WID_TT_PLANT_TREES
), SetMinimalSize(22, 22),
332 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES
, STR_SCENEDIT_TOOLBAR_PLANT_TREES
),
333 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_TT_PLACE_SIGN
), SetMinimalSize(22, 22),
334 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN
, STR_SCENEDIT_TOOLBAR_PLACE_SIGN
),
335 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_TT_SHOW_PLACE_OBJECT
),
336 NWidget(WWT_IMGBTN
, COLOUR_DARK_GREEN
, WID_TT_PLACE_OBJECT
), SetMinimalSize(22, 22),
337 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER
, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT
),
342 static WindowDesc
_terraform_desc(
343 WDP_MANUAL
, "toolbar_landscape", 0, 0,
344 WC_SCEN_LAND_GEN
, WC_NONE
,
346 _nested_terraform_widgets
, lengthof(_nested_terraform_widgets
),
347 &TerraformToolbarWindow::hotkeys
351 * Show the toolbar for terraforming in the game.
352 * @param link The toolbar we might want to link to.
353 * @return The allocated toolbar.
355 Window
*ShowTerraformToolbar(Window
*link
)
357 if (!Company::IsValidID(_local_company
)) return NULL
;
361 w
= AllocateWindowDescFront
<TerraformToolbarWindow
>(&_terraform_desc
, 0);
365 /* Delete the terraform toolbar to place it again. */
366 DeleteWindowById(WC_SCEN_LAND_GEN
, 0, true);
367 w
= AllocateWindowDescFront
<TerraformToolbarWindow
>(&_terraform_desc
, 0);
368 /* Align the terraform toolbar under the main toolbar. */
371 /* Put the linked toolbar to the left / right of it. */
372 link
->left
= w
->left
+ (_current_text_dir
== TD_RTL
? w
->width
: -link
->width
);
379 static byte _terraform_size
= 1;
382 * Raise/Lower a bigger chunk of land at the same time in the editor. When
383 * raising get the lowest point, when lowering the highest point, and set all
384 * tiles in the selection to that height.
385 * @todo : Incorporate into game itself to allow for ingame raising/lowering of
386 * larger chunks at the same time OR remove altogether, as we have 'level land' ?
387 * @param tile The top-left tile where the terraforming will start
388 * @param mode 1 for raising, 0 for lowering land
390 static void CommonRaiseLowerBigLand(TileIndex tile
, int mode
)
392 if (_terraform_size
== 1) {
394 mode
? STR_ERROR_CAN_T_RAISE_LAND_HERE
: STR_ERROR_CAN_T_LOWER_LAND_HERE
;
396 DoCommandP(tile
, SLOPE_N
, (uint32
)mode
, CMD_TERRAFORM_LAND
| CMD_MSG(msg
), CcTerraform
);
398 assert(_terraform_size
!= 0);
399 TileArea
ta(tile
, _terraform_size
, _terraform_size
);
402 if (ta
.w
== 0 || ta
.h
== 0) return;
404 if (_settings_client
.sound
.confirm
) SndPlayTileFx(SND_1F_SPLAT
, tile
);
410 TILE_AREA_LOOP(tile2
, ta
) {
411 h
= min(h
, TileHeight(tile2
));
416 TILE_AREA_LOOP(tile2
, ta
) {
417 h
= max(h
, TileHeight(tile2
));
421 TILE_AREA_LOOP(tile2
, ta
) {
422 if (TileHeight(tile2
) == h
) {
423 DoCommandP(tile2
, SLOPE_N
, (uint32
)mode
, CMD_TERRAFORM_LAND
);
429 static const int8 _multi_terraform_coords
[][2] = {
431 { 4, 0}, { -4, 0}, { 0, 2},
432 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
433 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
434 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
435 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
436 {-24, 2}, {-20, 4}, {-16, 6}, {-12, 8}, { -8, 10}, { -4, 12}, { 0, 14}, { 4, 12}, { 8, 10}, { 12, 8}, { 16, 6}, { 20, 4}, { 24, 2},
437 {-28, 0}, {-24, -2}, {-20, -4}, {-16, -6}, {-12, -8}, { -8,-10}, { -4,-12}, { 0,-14}, { 4,-12}, { 8,-10}, { 12, -8}, { 16, -6}, { 20, -4}, { 24, -2}, { 28, 0},
440 static const NWidgetPart _nested_scen_edit_land_gen_widgets
[] = {
441 NWidget(NWID_HORIZONTAL
),
442 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
443 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
444 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
445 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
447 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
448 NWidget(NWID_HORIZONTAL
), SetPadding(2, 2, 7, 2),
449 NWidget(NWID_SPACER
), SetFill(1, 0),
450 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_DEMOLISH
), SetMinimalSize(22, 22),
451 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE
, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC
),
452 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_LOWER_LAND
), SetMinimalSize(22, 22),
453 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN
, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND
),
454 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_RAISE_LAND
), SetMinimalSize(22, 22),
455 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP
, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND
),
456 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_LEVEL_LAND
), SetMinimalSize(22, 22),
457 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND
, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP
),
458 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_PLACE_ROCKS
), SetMinimalSize(22, 22),
459 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS
, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE
),
460 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_ETT_SHOW_PLACE_DESERT
),
461 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_PLACE_DESERT
), SetMinimalSize(22, 22),
462 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT
, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA
),
464 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_PLACE_OBJECT
), SetMinimalSize(23, 22),
465 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER
, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT
),
466 NWidget(NWID_SPACER
), SetFill(1, 0),
468 NWidget(NWID_HORIZONTAL
),
469 NWidget(NWID_SPACER
), SetFill(1, 0),
470 NWidget(WWT_EMPTY
, COLOUR_DARK_GREEN
, WID_ETT_DOTS
), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY
, STR_NULL
),
471 NWidget(NWID_SPACER
), SetFill(1, 0),
472 NWidget(NWID_VERTICAL
),
473 NWidget(NWID_SPACER
), SetFill(0, 1),
474 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_INCREASE_SIZE
), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP
, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA
),
475 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
476 NWidget(WWT_IMGBTN
, COLOUR_GREY
, WID_ETT_DECREASE_SIZE
), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN
, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA
),
477 NWidget(NWID_SPACER
), SetFill(0, 1),
479 NWidget(NWID_SPACER
), SetMinimalSize(2, 0),
481 NWidget(NWID_SPACER
), SetMinimalSize(0, 6),
482 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_ETT_NEW_SCENARIO
), SetMinimalSize(160, 12),
483 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD
, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND
), SetPadding(0, 2, 0, 2),
484 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_ETT_RESET_LANDSCAPE
), SetMinimalSize(160, 12),
485 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE
, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP
), SetPadding(1, 2, 2, 2),
490 * Callback function for the scenario editor 'reset landscape' confirmation window
491 * @param w Window unused
492 * @param confirmed boolean value, true when yes was clicked, false otherwise
494 static void ResetLandscapeConfirmationCallback(Window
*w
, bool confirmed
)
497 /* Set generating_world to true to get instant-green grass after removing
498 * company property. */
499 _generating_world
= true;
501 /* Delete all companies */
503 FOR_ALL_COMPANIES(c
) {
504 ChangeOwnershipOfCompanyItems(c
->index
, INVALID_OWNER
);
508 _generating_world
= false;
510 /* Delete all station signs */
512 FOR_ALL_BASE_STATIONS(st
) {
513 /* There can be buoys, remove them */
514 if (IsBuoyTile(st
->xy
)) DoCommand(st
->xy
, 0, 0, DC_EXEC
| DC_BANKRUPT
, CMD_LANDSCAPE_CLEAR
);
515 if (!st
->IsInUse()) delete st
;
518 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
519 EngineOverrideManager::ResetToCurrentNewGRFConfig();
521 MarkWholeScreenDirty();
525 /** Landscape generation window handler in the scenario editor. */
526 struct ScenarioEditorLandscapeGenerationWindow
: Window
{
527 int last_user_action
; ///< Last started user action.
529 ScenarioEditorLandscapeGenerationWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
531 this->CreateNestedTree();
532 NWidgetStacked
*show_desert
= this->GetWidget
<NWidgetStacked
>(WID_ETT_SHOW_PLACE_DESERT
);
533 show_desert
->SetDisplayedPlane(_settings_game
.game_creation
.landscape
== LT_TROPIC
? 0 : SZSP_NONE
);
534 this->FinishInitNested(window_number
);
535 this->last_user_action
= WIDGET_LIST_END
;
538 virtual void OnPaint()
542 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND
) || this->IsWidgetLowered(WID_ETT_RAISE_LAND
)) { // change area-size if raise/lower corner is selected
543 SetTileSelectSize(_terraform_size
, _terraform_size
);
547 virtual void DrawWidget(const Rect
&r
, int widget
) const
549 if (widget
!= WID_ETT_DOTS
) return;
551 int center_x
= RoundDivSU(r
.left
+ r
.right
, 2);
552 int center_y
= RoundDivSU(r
.top
+ r
.bottom
, 2);
554 int n
= _terraform_size
* _terraform_size
;
555 const int8
*coords
= &_multi_terraform_coords
[0][0];
559 DrawSprite(SPR_WHITE_POINT
, PAL_NONE
, center_x
+ coords
[0], center_y
+ coords
[1]);
564 virtual void OnClick(Point pt
, int widget
, int click_count
)
566 if (widget
< WID_ETT_BUTTONS_START
) return;
569 case WID_ETT_DEMOLISH
: // Demolish aka dynamite button
570 HandlePlacePushButton(this, WID_ETT_DEMOLISH
, ANIMCURSOR_DEMOLISH
, HT_RECT
| HT_DIAGONAL
);
571 this->last_user_action
= widget
;
574 case WID_ETT_LOWER_LAND
: // Lower land button
575 HandlePlacePushButton(this, WID_ETT_LOWER_LAND
, ANIMCURSOR_LOWERLAND
, HT_POINT
);
576 this->last_user_action
= widget
;
579 case WID_ETT_RAISE_LAND
: // Raise land button
580 HandlePlacePushButton(this, WID_ETT_RAISE_LAND
, ANIMCURSOR_RAISELAND
, HT_POINT
);
581 this->last_user_action
= widget
;
584 case WID_ETT_LEVEL_LAND
: // Level land button
585 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND
, SPR_CURSOR_LEVEL_LAND
, HT_POINT
| HT_DIAGONAL
);
586 this->last_user_action
= widget
;
589 case WID_ETT_PLACE_ROCKS
: // Place rocks button
590 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS
, SPR_CURSOR_ROCKY_AREA
, HT_RECT
);
591 this->last_user_action
= widget
;
594 case WID_ETT_PLACE_DESERT
: // Place desert button (in tropical climate)
595 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT
, SPR_CURSOR_DESERT
, HT_RECT
);
596 this->last_user_action
= widget
;
599 case WID_ETT_PLACE_OBJECT
: // Place transmitter button
600 if (HandlePlacePushButton(this, WID_ETT_PLACE_OBJECT
, SPR_CURSOR_TRANSMITTER
, HT_RECT
)) {
601 ShowBuildObjectPicker(this);
602 this->last_user_action
= widget
;
606 case WID_ETT_INCREASE_SIZE
:
607 case WID_ETT_DECREASE_SIZE
: { // Increase/Decrease terraform size
608 int size
= (widget
== WID_ETT_INCREASE_SIZE
) ? 1 : -1;
609 this->HandleButtonClick(widget
);
610 size
+= _terraform_size
;
612 if (!IsInsideMM(size
, 1, 8 + 1)) return;
613 _terraform_size
= size
;
615 if (_settings_client
.sound
.click_beep
) SndPlayFx(SND_15_BEEP
);
620 case WID_ETT_NEW_SCENARIO
: // gen random land
621 this->HandleButtonClick(widget
);
622 ShowCreateScenario();
625 case WID_ETT_RESET_LANDSCAPE
: // Reset landscape
626 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION
, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT
, NULL
, ResetLandscapeConfirmationCallback
);
629 default: NOT_REACHED();
633 virtual void OnTimeout()
635 for (uint i
= WID_ETT_START
; i
< this->nested_array_size
; i
++) {
636 if (i
== WID_ETT_BUTTONS_START
) i
= WID_ETT_BUTTONS_END
; // skip the buttons
637 if (this->IsWidgetLowered(i
)) {
638 this->RaiseWidget(i
);
639 this->SetWidgetDirty(i
);
644 virtual void OnPlaceObject(Point pt
, TileIndex tile
)
646 switch (this->last_user_action
) {
647 case WID_ETT_DEMOLISH
: // Demolish aka dynamite button
648 PlaceProc_DemolishArea(tile
);
651 case WID_ETT_LOWER_LAND
: // Lower land button
652 CommonRaiseLowerBigLand(tile
, 0);
655 case WID_ETT_RAISE_LAND
: // Raise land button
656 CommonRaiseLowerBigLand(tile
, 1);
659 case WID_ETT_LEVEL_LAND
: // Level land button
660 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_LEVEL_AREA
);
663 case WID_ETT_PLACE_ROCKS
: // Place rocks button
664 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_CREATE_ROCKS
);
667 case WID_ETT_PLACE_DESERT
: // Place desert button (in tropical climate)
668 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_CREATE_DESERT
);
671 case WID_ETT_PLACE_OBJECT
: // Place transmitter button
672 PlaceProc_Object(tile
);
675 default: NOT_REACHED();
679 virtual void OnPlaceDrag(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
)
681 VpSelectTilesWithMethod(pt
.x
, pt
.y
, select_method
);
684 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
, TileIndex start_tile
, TileIndex end_tile
)
687 switch (select_proc
) {
688 default: NOT_REACHED();
689 case DDSP_CREATE_ROCKS
:
690 case DDSP_CREATE_DESERT
:
691 case DDSP_RAISE_AND_LEVEL_AREA
:
692 case DDSP_LOWER_AND_LEVEL_AREA
:
693 case DDSP_LEVEL_AREA
:
694 case DDSP_DEMOLISH_AREA
:
695 GUIPlaceProcDragXY(select_proc
, start_tile
, end_tile
);
701 virtual void OnPlaceObjectAbort()
703 this->RaiseButtons();
705 DeleteWindowById(WC_BUILD_OBJECT
, 0);
708 static HotkeyList hotkeys
;
712 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
713 * @param hotkey Hotkey
714 * @return ES_HANDLED if hotkey was accepted.
716 static EventState
TerraformToolbarEditorGlobalHotkeys(int hotkey
)
718 if (_game_mode
!= GM_EDITOR
) return ES_NOT_HANDLED
;
719 Window
*w
= ShowEditorTerraformToolbar();
720 if (w
== NULL
) return ES_NOT_HANDLED
;
721 return w
->OnHotkey(hotkey
);
724 static Hotkey terraform_editor_hotkeys
[] = {
725 Hotkey('D' | WKC_GLOBAL_HOTKEY
, "dynamite", WID_ETT_DEMOLISH
),
726 Hotkey('Q' | WKC_GLOBAL_HOTKEY
, "lower", WID_ETT_LOWER_LAND
),
727 Hotkey('W' | WKC_GLOBAL_HOTKEY
, "raise", WID_ETT_RAISE_LAND
),
728 Hotkey('E' | WKC_GLOBAL_HOTKEY
, "level", WID_ETT_LEVEL_LAND
),
729 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS
),
730 Hotkey('T', "desert", WID_ETT_PLACE_DESERT
),
731 Hotkey('O', "object", WID_ETT_PLACE_OBJECT
),
735 HotkeyList
ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys
, TerraformToolbarEditorGlobalHotkeys
);
737 static WindowDesc
_scen_edit_land_gen_desc(
738 WDP_AUTO
, "toolbar_landscape_scen", 0, 0,
739 WC_SCEN_LAND_GEN
, WC_NONE
,
741 _nested_scen_edit_land_gen_widgets
, lengthof(_nested_scen_edit_land_gen_widgets
),
742 &ScenarioEditorLandscapeGenerationWindow::hotkeys
746 * Show the toolbar for terraforming in the scenario editor.
747 * @return The allocated toolbar.
749 Window
*ShowEditorTerraformToolbar()
751 return AllocateWindowDescFront
<ScenarioEditorLandscapeGenerationWindow
>(&_scen_edit_land_gen_desc
, 0);