Make it possible to stop road vehicles from slowing down in curves so "diagonal"...
[openttd-joker.git] / src / terraform_gui.cpp
blobd037cfa7a66f29d0d098f6ef78a2cf07425ad8be
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file terraform_gui.cpp GUI related to terraforming the map. */
12 #include "stdafx.h"
13 #include "clear_map.h"
14 #include "company_func.h"
15 #include "company_base.h"
16 #include "gui.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"
25 #include "genworld.h"
26 #include "tree_map.h"
27 #include "landscape_type.h"
28 #include "tilehighlight_func.h"
29 #include "strings_func.h"
30 #include "newgrf_object.h"
31 #include "object.h"
32 #include "hotkeys.h"
33 #include "engine_base.h"
34 #include "terraform_gui.h"
35 #include "zoom_func.h"
37 #include "widgets/terraform_widget.h"
39 #include "table/strings.h"
41 #include "safeguards.h"
43 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
45 if (result.Succeeded()) {
46 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
47 } else {
48 extern TileIndex _terraform_err_tile;
49 SetRedErrorSquare(_terraform_err_tile);
54 /** Scenario editor command that generates desert areas */
55 static void GenerateDesertArea(TileIndex end, TileIndex start)
57 if (_game_mode != GM_EDITOR) return;
59 _generating_world = true;
61 TileArea ta(start, end);
62 TILE_AREA_LOOP(tile, ta) {
63 SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
64 DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
65 MarkTileDirtyByTile(tile);
67 _generating_world = false;
68 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
71 /** Scenario editor command that generates rocky areas */
72 static void GenerateRockyArea(TileIndex end, TileIndex start)
74 if (_game_mode != GM_EDITOR) return;
76 bool success = false;
77 TileArea ta(start, end);
79 TILE_AREA_LOOP(tile, ta) {
80 switch (GetTileType(tile)) {
81 case MP_TREES:
82 if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
83 FALLTHROUGH;
85 case MP_CLEAR:
86 MakeClear(tile, CLEAR_ROCKS, 3);
87 break;
89 default:
90 continue;
92 MarkTileDirtyByTile(tile);
93 success = true;
96 if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, end);
99 /**
100 * A central place to handle all X_AND_Y dragged GUI functions.
101 * @param proc Procedure related to the dragging
102 * @param start_tile Begin of the dragging
103 * @param end_tile End of the dragging
104 * @return Returns true if the action was found and handled, and false otherwise. This
105 * allows for additional implements that are more local. For example X_Y drag
106 * of convertrail which belongs in rail_gui.cpp and not terraform_gui.cpp
108 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
110 if (!_settings_game.construction.freeform_edges) {
111 /* When end_tile is MP_VOID, the error tile will not be visible to the
112 * user. This happens when terraforming at the southern border. */
113 if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
114 if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
117 switch (proc) {
118 case DDSP_DEMOLISH_AREA:
119 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 1 : 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound_EXPLOSION);
120 break;
121 case DDSP_RAISE_AND_LEVEL_AREA:
122 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);
123 break;
124 case DDSP_LOWER_AND_LEVEL_AREA:
125 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);
126 break;
127 case DDSP_LEVEL_AREA:
128 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);
129 break;
130 case DDSP_CREATE_ROCKS:
131 GenerateRockyArea(end_tile, start_tile);
132 break;
133 case DDSP_CREATE_DESERT:
134 GenerateDesertArea(end_tile, start_tile);
135 break;
136 default:
137 return false;
140 return true;
144 * Start a drag for demolishing an area.
145 * @param tile Position of one corner.
147 void PlaceProc_DemolishArea(TileIndex tile)
149 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
152 static void PlaceProc_Measure(TileIndex tile)
154 VpStartPlaceSizing(tile, VPM_A_B_LINE, DDSP_MEASURE);
157 /** Terra form toolbar managing class. */
158 struct TerraformToolbarWindow : Window {
159 int last_user_action; ///< Last started user action.
161 TerraformToolbarWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
163 /* This is needed as we like to have the tree available on OnInit. */
164 this->CreateNestedTree();
165 this->FinishInitNested(window_number);
166 this->last_user_action = WIDGET_LIST_END;
169 ~TerraformToolbarWindow()
173 virtual void OnInit()
175 /* Don't show the place object button when there are no objects to place. */
176 NWidgetStacked *show_object = this->GetWidget<NWidgetStacked>(WID_TT_SHOW_PLACE_OBJECT);
177 show_object->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE);
180 virtual void OnClick(Point pt, int widget, int click_count)
182 if (widget < WID_TT_BUTTONS_START) return;
184 switch (widget) {
185 case WID_TT_LOWER_LAND: // Lower land button
186 HandlePlacePushButton(this, WID_TT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT | HT_DIAGONAL);
187 this->last_user_action = widget;
188 break;
190 case WID_TT_RAISE_LAND: // Raise land button
191 HandlePlacePushButton(this, WID_TT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT | HT_DIAGONAL);
192 this->last_user_action = widget;
193 break;
195 case WID_TT_LEVEL_LAND: // Level land button
196 HandlePlacePushButton(this, WID_TT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
197 this->last_user_action = widget;
198 break;
200 case WID_TT_DEMOLISH: // Demolish aka dynamite button
201 HandlePlacePushButton(this, WID_TT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
202 this->last_user_action = widget;
203 break;
205 case WID_TT_BUY_LAND: // Buy land button
206 HandlePlacePushButton(this, WID_TT_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT);
207 this->last_user_action = widget;
208 break;
210 case WID_TT_PLANT_TREES: // Plant trees button
211 ShowBuildTreesToolbar();
212 break;
214 case WID_TT_MEASUREMENT_TOOL:
215 HandlePlacePushButton(this, WID_TT_MEASUREMENT_TOOL, SPR_CURSOR_QUERY, HT_RECT);
216 this->last_user_action = widget;
217 break;
219 case WID_TT_PLACE_SIGN: // Place sign button
220 HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
221 this->last_user_action = widget;
222 break;
224 case WID_TT_PLACE_OBJECT: // Place object button
225 ShowBuildObjectPicker();
226 break;
228 default: NOT_REACHED();
232 virtual void OnPlaceObject(Point pt, TileIndex tile)
234 switch (this->last_user_action) {
235 case WID_TT_LOWER_LAND: // Lower land button
236 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
237 break;
239 case WID_TT_RAISE_LAND: // Raise land button
240 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
241 break;
243 case WID_TT_LEVEL_LAND: // Level land button
244 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
245 break;
247 case WID_TT_DEMOLISH: // Demolish aka dynamite button
248 PlaceProc_DemolishArea(tile);
249 break;
251 case WID_TT_BUY_LAND: // Buy land button
252 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_PLACE_OBJECT);
253 break;
255 case WID_TT_MEASUREMENT_TOOL:
256 PlaceProc_Measure(tile);
257 break;
259 case WID_TT_PLACE_SIGN: // Place sign button
260 PlaceProc_Sign(tile);
261 break;
263 default: NOT_REACHED();
267 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
269 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
272 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
274 Point pt = GetToolbarAlignedWindowPosition(sm_width);
275 pt.y += sm_height;
276 return pt;
279 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
281 if (pt.x != -1) {
282 switch (select_proc) {
283 default: NOT_REACHED();
284 case DDSP_DEMOLISH_AREA:
285 case DDSP_RAISE_AND_LEVEL_AREA:
286 case DDSP_LOWER_AND_LEVEL_AREA:
287 case DDSP_LEVEL_AREA:
288 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
289 break;
290 case DDSP_MEASURE:
291 //nothing to do, just draw a tooltip
292 break;
293 case DDSP_PLACE_OBJECT:
295 uint32 p2;
297 SB(p2, 2, 30, start_tile);
299 if (pt.x != -1 && select_proc == DDSP_PLACE_OBJECT) {
300 DoCommandP(end_tile, OBJECT_OWNED_LAND, p2, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT), CcTerraform);
303 break;
308 virtual void OnPlaceObjectAbort()
310 this->RaiseButtons();
311 this->SetDirty();
314 static HotkeyList hotkeys;
318 * Handler for global hotkeys of the TerraformToolbarWindow.
319 * @param hotkey Hotkey
320 * @return ES_HANDLED if hotkey was accepted.
322 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
324 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
325 Window *w = ShowTerraformToolbar(nullptr);
326 if (w == nullptr) return ES_NOT_HANDLED;
327 return w->OnHotkey(hotkey);
330 static Hotkey terraform_hotkeys[] = {
331 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
332 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
333 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
334 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
335 Hotkey('U', "buyland", WID_TT_BUY_LAND),
336 Hotkey('I', "trees", WID_TT_PLANT_TREES),
337 Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
338 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
339 HOTKEY_LIST_END
341 HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
343 static const NWidgetPart _nested_terraform_widgets[] = {
344 NWidget(NWID_HORIZONTAL),
345 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
346 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
347 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
348 EndContainer(),
349 NWidget(NWID_HORIZONTAL),
350 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
351 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
352 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
353 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
354 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
355 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
357 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
359 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
360 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
361 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
362 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
363 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
364 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
365 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_MEASUREMENT_TOOL), SetMinimalSize(22,22),
366 SetFill(0, 1), SetDataTip(SPR_IMG_QUERY, STR_LANDSCAPING_TOOLTIP_RULER_TOOL),
367 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
368 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
369 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
370 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
371 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
372 EndContainer(),
373 EndContainer(),
376 static WindowDesc _terraform_desc(
377 WDP_MANUAL, "toolbar_landscape", 0, 0,
378 WC_SCEN_LAND_GEN, WC_NONE,
379 WDF_CONSTRUCTION,
380 _nested_terraform_widgets, lengthof(_nested_terraform_widgets),
381 &TerraformToolbarWindow::hotkeys
385 * Show the toolbar for terraforming in the game.
386 * @param link The toolbar we might want to link to.
387 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
389 Window *ShowTerraformToolbar(Window *link)
391 if (!Company::IsValidID(_local_company)) return nullptr;
393 Window *w;
394 if (link == nullptr) {
395 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
396 return w;
399 /* Delete the terraform toolbar to place it again. */
400 DeleteWindowById(WC_SCEN_LAND_GEN, 0, true);
401 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
402 /* Align the terraform toolbar under the main toolbar. */
403 w->top -= w->height;
404 w->SetDirty();
405 /* Put the linked toolbar to the left / right of it. */
406 link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
407 link->top = w->top;
408 link->SetDirty();
410 return w;
413 static byte _terraform_size = 1;
416 * Raise/Lower a bigger chunk of land at the same time in the editor. When
417 * raising get the lowest point, when lowering the highest point, and set all
418 * tiles in the selection to that height.
419 * @todo : Incorporate into game itself to allow for ingame raising/lowering of
420 * larger chunks at the same time OR remove altogether, as we have 'level land' ?
421 * @param tile The top-left tile where the terraforming will start
422 * @param mode 1 for raising, 0 for lowering land
424 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
426 if (_terraform_size == 1) {
427 StringID msg =
428 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
430 DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
431 } else {
432 assert(_terraform_size != 0);
433 TileArea ta(tile, _terraform_size, _terraform_size);
434 ta.ClampToMap();
436 if (ta.w == 0 || ta.h == 0) return;
438 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
440 uint h;
441 if (mode != 0) {
442 /* Raise land */
443 h = MAX_TILE_HEIGHT;
444 TILE_AREA_LOOP(tile2, ta) {
445 h = min(h, TileHeight(tile2));
447 } else {
448 /* Lower land */
449 h = 0;
450 TILE_AREA_LOOP(tile2, ta) {
451 h = max(h, TileHeight(tile2));
455 TILE_AREA_LOOP(tile2, ta) {
456 if (TileHeight(tile2) == h) {
457 DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
463 static const int8 _multi_terraform_coords[][2] = {
464 { 0, -2},
465 { 4, 0}, { -4, 0}, { 0, 2},
466 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
467 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
468 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
469 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
470 {-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},
471 {-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},
474 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
475 NWidget(NWID_HORIZONTAL),
476 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
477 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
478 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
479 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
480 EndContainer(),
481 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
482 NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
483 NWidget(NWID_SPACER), SetFill(1, 0),
484 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
485 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
486 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
487 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
488 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
489 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
490 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
491 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
492 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
493 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
494 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
495 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
496 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
497 EndContainer(),
498 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
499 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
500 NWidget(NWID_SPACER), SetFill(1, 0),
501 EndContainer(),
502 NWidget(NWID_HORIZONTAL),
503 NWidget(NWID_SPACER), SetFill(1, 0),
504 NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
505 NWidget(NWID_SPACER), SetFill(1, 0),
506 NWidget(NWID_VERTICAL),
507 NWidget(NWID_SPACER), SetFill(0, 1),
508 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
509 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
510 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
511 NWidget(NWID_SPACER), SetFill(0, 1),
512 EndContainer(),
513 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
514 EndContainer(),
515 NWidget(NWID_SPACER), SetMinimalSize(0, 6),
516 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
517 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
518 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
519 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
520 EndContainer(),
524 * Callback function for the scenario editor 'reset landscape' confirmation window
525 * @param w Window unused
526 * @param confirmed boolean value, true when yes was clicked, false otherwise
528 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
530 if (confirmed) {
531 /* Set generating_world to true to get instant-green grass after removing
532 * company property. */
533 _generating_world = true;
535 /* Delete all companies */
536 Company *c;
537 FOR_ALL_COMPANIES(c) {
538 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
539 delete c;
542 _generating_world = false;
544 /* Delete all station signs */
545 BaseStation *st;
546 FOR_ALL_BASE_STATIONS(st) {
547 /* There can be buoys, remove them */
548 if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
549 if (!st->IsInUse()) delete st;
552 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
553 EngineOverrideManager::ResetToCurrentNewGRFConfig();
555 MarkWholeScreenDirty();
559 /** Landscape generation window handler in the scenario editor. */
560 struct ScenarioEditorLandscapeGenerationWindow : Window {
561 int last_user_action; ///< Last started user action.
563 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
565 this->CreateNestedTree();
566 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
567 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
568 this->FinishInitNested(window_number);
569 this->last_user_action = WIDGET_LIST_END;
572 virtual void OnPaint()
574 this->DrawWidgets();
576 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
577 SetTileSelectSize(_terraform_size, _terraform_size);
581 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
583 if (widget != WID_ETT_DOTS) return;
585 size->width = max<uint>(size->width, ScaleGUITrad(59));
586 size->height = max<uint>(size->height, ScaleGUITrad(31));
589 virtual void DrawWidget(const Rect &r, int widget) const
591 if (widget != WID_ETT_DOTS) return;
593 int center_x = RoundDivSU(r.left + r.right, 2);
594 int center_y = RoundDivSU(r.top + r.bottom, 2);
596 int n = _terraform_size * _terraform_size;
597 const int8 *coords = &_multi_terraform_coords[0][0];
599 assert(n != 0);
600 do {
601 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
602 coords += 2;
603 } while (--n);
606 virtual void OnClick(Point pt, int widget, int click_count)
608 if (widget < WID_ETT_BUTTONS_START) return;
610 switch (widget) {
611 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
612 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
613 this->last_user_action = widget;
614 break;
616 case WID_ETT_LOWER_LAND: // Lower land button
617 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
618 this->last_user_action = widget;
619 break;
621 case WID_ETT_RAISE_LAND: // Raise land button
622 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
623 this->last_user_action = widget;
624 break;
626 case WID_ETT_LEVEL_LAND: // Level land button
627 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
628 this->last_user_action = widget;
629 break;
631 case WID_ETT_PLACE_ROCKS: // Place rocks button
632 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
633 this->last_user_action = widget;
634 break;
636 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
637 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
638 this->last_user_action = widget;
639 break;
641 case WID_ETT_PLACE_OBJECT: // Place transmitter button
642 ShowBuildObjectPicker();
643 break;
645 case WID_ETT_INCREASE_SIZE:
646 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
647 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
648 this->HandleButtonClick(widget);
649 size += _terraform_size;
651 if (!IsInsideMM(size, 1, 8 + 1)) return;
652 _terraform_size = size;
654 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
655 this->SetDirty();
656 break;
659 case WID_ETT_NEW_SCENARIO: // gen random land
660 this->HandleButtonClick(widget);
661 ShowCreateScenario();
662 break;
664 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
665 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback);
666 break;
668 default: NOT_REACHED();
672 virtual void OnTimeout()
674 for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
675 if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
676 if (this->IsWidgetLowered(i)) {
677 this->RaiseWidget(i);
678 this->SetWidgetDirty(i);
683 virtual void OnPlaceObject(Point pt, TileIndex tile)
685 switch (this->last_user_action) {
686 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
687 PlaceProc_DemolishArea(tile);
688 break;
690 case WID_ETT_LOWER_LAND: // Lower land button
691 CommonRaiseLowerBigLand(tile, 0);
692 break;
694 case WID_ETT_RAISE_LAND: // Raise land button
695 CommonRaiseLowerBigLand(tile, 1);
696 break;
698 case WID_ETT_LEVEL_LAND: // Level land button
699 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
700 break;
702 case WID_ETT_PLACE_ROCKS: // Place rocks button
703 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
704 break;
706 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
707 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
708 break;
710 default: NOT_REACHED();
714 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
716 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
719 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
721 if (pt.x != -1) {
722 switch (select_proc) {
723 default: NOT_REACHED();
724 case DDSP_CREATE_ROCKS:
725 case DDSP_CREATE_DESERT:
726 case DDSP_RAISE_AND_LEVEL_AREA:
727 case DDSP_LOWER_AND_LEVEL_AREA:
728 case DDSP_LEVEL_AREA:
729 case DDSP_DEMOLISH_AREA:
730 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
731 break;
736 virtual void OnPlaceObjectAbort()
738 this->RaiseButtons();
739 this->SetDirty();
742 static HotkeyList hotkeys;
746 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
747 * @param hotkey Hotkey
748 * @return ES_HANDLED if hotkey was accepted.
750 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
752 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
753 Window *w = ShowEditorTerraformToolbar();
754 if (w == nullptr) return ES_NOT_HANDLED;
755 return w->OnHotkey(hotkey);
758 static Hotkey terraform_editor_hotkeys[] = {
759 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
760 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
761 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
762 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
763 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
764 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
765 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
766 HOTKEY_LIST_END
769 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
771 static WindowDesc _scen_edit_land_gen_desc(
772 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
773 WC_SCEN_LAND_GEN, WC_NONE,
774 WDF_CONSTRUCTION,
775 _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
776 &ScenarioEditorLandscapeGenerationWindow::hotkeys
780 * Show the toolbar for terraforming in the scenario editor.
781 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
783 Window *ShowEditorTerraformToolbar()
785 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);