Feature: Purchase land multiple tiles at a time
[openttd-github.git] / src / terraform_gui.cpp
blobc76ab8ce8df36e21f3563e513505368318fd9ce6
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file terraform_gui.cpp GUI related to terraforming the map. */
10 #include "stdafx.h"
11 #include "core/backup_type.hpp"
12 #include "clear_map.h"
13 #include "company_func.h"
14 #include "company_base.h"
15 #include "gui.h"
16 #include "window_gui.h"
17 #include "window_func.h"
18 #include "viewport_func.h"
19 #include "command_func.h"
20 #include "signs_func.h"
21 #include "sound_func.h"
22 #include "base_station_base.h"
23 #include "textbuf_gui.h"
24 #include "genworld.h"
25 #include "tree_map.h"
26 #include "landscape_type.h"
27 #include "tilehighlight_func.h"
28 #include "strings_func.h"
29 #include "newgrf_object.h"
30 #include "object.h"
31 #include "hotkeys.h"
32 #include "engine_base.h"
33 #include "terraform_gui.h"
34 #include "terraform_cmd.h"
35 #include "zoom_func.h"
36 #include "rail_cmd.h"
37 #include "landscape_cmd.h"
38 #include "terraform_cmd.h"
39 #include "object_cmd.h"
41 #include "widgets/terraform_widget.h"
43 #include "table/strings.h"
45 #include "safeguards.h"
47 void CcTerraform(Commands cmd, const CommandCost &result, Money, TileIndex tile)
49 if (result.Succeeded()) {
50 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
51 } else {
52 SetRedErrorSquare(tile);
57 /** Scenario editor command that generates desert areas */
58 static void GenerateDesertArea(TileIndex end, TileIndex start)
60 if (_game_mode != GM_EDITOR) return;
62 Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
64 TileArea ta(start, end);
65 for (TileIndex tile : ta) {
66 SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
67 Command<CMD_LANDSCAPE_CLEAR>::Post(tile);
68 MarkTileDirtyByTile(tile);
70 old_generating_world.Restore();
71 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
74 /** Scenario editor command that generates rocky areas */
75 static void GenerateRockyArea(TileIndex end, TileIndex start)
77 if (_game_mode != GM_EDITOR) return;
79 bool success = false;
80 TileArea ta(start, end);
82 for (TileIndex tile : ta) {
83 switch (GetTileType(tile)) {
84 case MP_TREES:
85 if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
86 FALLTHROUGH;
88 case MP_CLEAR:
89 MakeClear(tile, CLEAR_ROCKS, 3);
90 break;
92 default:
93 continue;
95 MarkTileDirtyByTile(tile);
96 success = true;
99 if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, end);
103 * A central place to handle all X_AND_Y dragged GUI functions.
104 * @param proc Procedure related to the dragging
105 * @param start_tile Begin of the dragging
106 * @param end_tile End of the dragging
107 * @return Returns true if the action was found and handled, and false otherwise. This
108 * allows for additional implements that are more local. For example X_Y drag
109 * of convertrail which belongs in rail_gui.cpp and not terraform_gui.cpp
111 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
113 if (!_settings_game.construction.freeform_edges) {
114 /* When end_tile is MP_VOID, the error tile will not be visible to the
115 * user. This happens when terraforming at the southern border. */
116 if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
117 if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
120 switch (proc) {
121 case DDSP_DEMOLISH_AREA:
122 Command<CMD_CLEAR_AREA>::Post(STR_ERROR_CAN_T_CLEAR_THIS_AREA, CcPlaySound_EXPLOSION, end_tile, start_tile, _ctrl_pressed);
123 break;
124 case DDSP_RAISE_AND_LEVEL_AREA:
125 Command<CMD_LEVEL_LAND>::Post(STR_ERROR_CAN_T_RAISE_LAND_HERE, CcTerraform, end_tile, start_tile, _ctrl_pressed, LM_RAISE);
126 break;
127 case DDSP_LOWER_AND_LEVEL_AREA:
128 Command<CMD_LEVEL_LAND>::Post(STR_ERROR_CAN_T_LOWER_LAND_HERE, CcTerraform, end_tile, start_tile, _ctrl_pressed, LM_LOWER);
129 break;
130 case DDSP_LEVEL_AREA:
131 Command<CMD_LEVEL_LAND>::Post(STR_ERROR_CAN_T_LEVEL_LAND_HERE, CcTerraform, end_tile, start_tile, _ctrl_pressed, LM_LEVEL);
132 break;
133 case DDSP_CREATE_ROCKS:
134 GenerateRockyArea(end_tile, start_tile);
135 break;
136 case DDSP_CREATE_DESERT:
137 GenerateDesertArea(end_tile, start_tile);
138 break;
139 default:
140 return false;
143 return true;
147 * Start a drag for demolishing an area.
148 * @param tile Position of one corner.
150 void PlaceProc_DemolishArea(TileIndex tile)
152 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
155 /** Terra form toolbar managing class. */
156 struct TerraformToolbarWindow : Window {
157 int last_user_action; ///< Last started user action.
159 TerraformToolbarWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
161 /* This is needed as we like to have the tree available on OnInit. */
162 this->CreateNestedTree();
163 this->FinishInitNested(window_number);
164 this->last_user_action = WIDGET_LIST_END;
167 ~TerraformToolbarWindow()
171 void OnInit() override
173 /* Don't show the place object button when there are no objects to place. */
174 NWidgetStacked *show_object = this->GetWidget<NWidgetStacked>(WID_TT_SHOW_PLACE_OBJECT);
175 show_object->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE);
178 void OnClick(Point pt, int widget, int click_count) override
180 if (widget < WID_TT_BUTTONS_START) return;
182 switch (widget) {
183 case WID_TT_LOWER_LAND: // Lower land button
184 HandlePlacePushButton(this, WID_TT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT | HT_DIAGONAL);
185 this->last_user_action = widget;
186 break;
188 case WID_TT_RAISE_LAND: // Raise land button
189 HandlePlacePushButton(this, WID_TT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT | HT_DIAGONAL);
190 this->last_user_action = widget;
191 break;
193 case WID_TT_LEVEL_LAND: // Level land button
194 HandlePlacePushButton(this, WID_TT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
195 this->last_user_action = widget;
196 break;
198 case WID_TT_DEMOLISH: // Demolish aka dynamite button
199 HandlePlacePushButton(this, WID_TT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
200 this->last_user_action = widget;
201 break;
203 case WID_TT_BUY_LAND: // Buy land button
204 HandlePlacePushButton(this, WID_TT_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT | HT_DIAGONAL);
205 this->last_user_action = widget;
206 break;
208 case WID_TT_PLANT_TREES: // Plant trees button
209 ShowBuildTreesToolbar();
210 break;
212 case WID_TT_PLACE_SIGN: // Place sign button
213 HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
214 this->last_user_action = widget;
215 break;
217 case WID_TT_PLACE_OBJECT: // Place object button
218 ShowBuildObjectPicker();
219 break;
221 default: NOT_REACHED();
225 void OnPlaceObject(Point pt, TileIndex tile) override
227 switch (this->last_user_action) {
228 case WID_TT_LOWER_LAND: // Lower land button
229 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
230 break;
232 case WID_TT_RAISE_LAND: // Raise land button
233 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
234 break;
236 case WID_TT_LEVEL_LAND: // Level land button
237 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
238 break;
240 case WID_TT_DEMOLISH: // Demolish aka dynamite button
241 PlaceProc_DemolishArea(tile);
242 break;
244 case WID_TT_BUY_LAND: // Buy land button
245 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_BUILD_OBJECT);
246 break;
248 case WID_TT_PLACE_SIGN: // Place sign button
249 PlaceProc_Sign(tile);
250 break;
252 default: NOT_REACHED();
256 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
258 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
261 Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
263 Point pt = GetToolbarAlignedWindowPosition(sm_width);
264 pt.y += sm_height;
265 return pt;
268 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
270 if (pt.x != -1) {
271 switch (select_proc) {
272 default: NOT_REACHED();
273 case DDSP_DEMOLISH_AREA:
274 case DDSP_RAISE_AND_LEVEL_AREA:
275 case DDSP_LOWER_AND_LEVEL_AREA:
276 case DDSP_LEVEL_AREA:
277 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
278 break;
279 case DDSP_BUILD_OBJECT:
280 if (!_settings_game.construction.freeform_edges) {
281 /* When end_tile is MP_VOID, the error tile will not be visible to the
282 * user. This happens when terraforming at the southern border. */
283 if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
284 if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
286 Command<CMD_BUILD_OBJECT_AREA>::Post(STR_ERROR_CAN_T_PURCHASE_THIS_LAND, CcPlaySound_CONSTRUCTION_RAIL,
287 end_tile, start_tile, OBJECT_OWNED_LAND, 0, (_ctrl_pressed ? true : false));
288 break;
293 void OnPlaceObjectAbort() override
295 this->RaiseButtons();
298 static HotkeyList hotkeys;
302 * Handler for global hotkeys of the TerraformToolbarWindow.
303 * @param hotkey Hotkey
304 * @return ES_HANDLED if hotkey was accepted.
306 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
308 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
309 Window *w = ShowTerraformToolbar(nullptr);
310 if (w == nullptr) return ES_NOT_HANDLED;
311 return w->OnHotkey(hotkey);
314 static Hotkey terraform_hotkeys[] = {
315 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
316 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
317 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
318 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
319 Hotkey('U', "buyland", WID_TT_BUY_LAND),
320 Hotkey('I', "trees", WID_TT_PLANT_TREES),
321 Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
322 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
323 HOTKEY_LIST_END
325 HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
327 static const NWidgetPart _nested_terraform_widgets[] = {
328 NWidget(NWID_HORIZONTAL),
329 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
330 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
331 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
332 EndContainer(),
333 NWidget(NWID_HORIZONTAL),
334 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
335 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
336 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
337 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
338 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
339 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
341 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
343 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
344 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
345 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
346 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
347 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
348 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
349 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
350 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
351 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
352 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
353 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
354 EndContainer(),
355 EndContainer(),
358 static WindowDesc _terraform_desc(
359 WDP_MANUAL, "toolbar_landscape", 0, 0,
360 WC_SCEN_LAND_GEN, WC_NONE,
361 WDF_CONSTRUCTION,
362 _nested_terraform_widgets, lengthof(_nested_terraform_widgets),
363 &TerraformToolbarWindow::hotkeys
367 * Show the toolbar for terraforming in the game.
368 * @param link The toolbar we might want to link to.
369 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
371 Window *ShowTerraformToolbar(Window *link)
373 if (!Company::IsValidID(_local_company)) return nullptr;
375 Window *w;
376 if (link == nullptr) {
377 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
378 return w;
381 /* Delete the terraform toolbar to place it again. */
382 CloseWindowById(WC_SCEN_LAND_GEN, 0, true);
383 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
384 /* Align the terraform toolbar under the main toolbar. */
385 w->top -= w->height;
386 w->SetDirty();
387 /* Put the linked toolbar to the left / right of it. */
388 link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
389 link->top = w->top;
390 link->SetDirty();
392 return w;
395 static byte _terraform_size = 1;
398 * Raise/Lower a bigger chunk of land at the same time in the editor. When
399 * raising get the lowest point, when lowering the highest point, and set all
400 * tiles in the selection to that height.
401 * @todo : Incorporate into game itself to allow for ingame raising/lowering of
402 * larger chunks at the same time OR remove altogether, as we have 'level land' ?
403 * @param tile The top-left tile where the terraforming will start
404 * @param mode true for raising, false for lowering land
406 static void CommonRaiseLowerBigLand(TileIndex tile, bool mode)
408 if (_terraform_size == 1) {
409 StringID msg =
410 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
412 Command<CMD_TERRAFORM_LAND>::Post(msg, CcTerraform, tile, SLOPE_N, mode);
413 } else {
414 assert(_terraform_size != 0);
415 TileArea ta(tile, _terraform_size, _terraform_size);
416 ta.ClampToMap();
418 if (ta.w == 0 || ta.h == 0) return;
420 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
422 uint h;
423 if (mode != 0) {
424 /* Raise land */
425 h = MAX_TILE_HEIGHT;
426 for (TileIndex tile2 : ta) {
427 h = std::min(h, TileHeight(tile2));
429 } else {
430 /* Lower land */
431 h = 0;
432 for (TileIndex tile2 : ta) {
433 h = std::max(h, TileHeight(tile2));
437 for (TileIndex tile2 : ta) {
438 if (TileHeight(tile2) == h) {
439 Command<CMD_TERRAFORM_LAND>::Post(tile2, SLOPE_N, mode);
445 static const int8 _multi_terraform_coords[][2] = {
446 { 0, -2},
447 { 4, 0}, { -4, 0}, { 0, 2},
448 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
449 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
450 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
451 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
452 {-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},
453 {-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},
456 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
457 NWidget(NWID_HORIZONTAL),
458 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
459 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
460 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
461 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
462 EndContainer(),
463 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
464 NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
465 NWidget(NWID_SPACER), SetFill(1, 0),
466 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
467 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
468 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
469 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
470 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
471 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
472 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
473 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
474 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
475 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
476 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
477 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
478 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
479 EndContainer(),
480 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
481 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
482 NWidget(NWID_SPACER), SetFill(1, 0),
483 EndContainer(),
484 NWidget(NWID_HORIZONTAL),
485 NWidget(NWID_SPACER), SetFill(1, 0),
486 NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
487 NWidget(NWID_SPACER), SetFill(1, 0),
488 NWidget(NWID_VERTICAL),
489 NWidget(NWID_SPACER), SetFill(0, 1),
490 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
491 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
492 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
493 NWidget(NWID_SPACER), SetFill(0, 1),
494 EndContainer(),
495 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
496 EndContainer(),
497 NWidget(NWID_SPACER), SetMinimalSize(0, 6),
498 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
499 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
500 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
501 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
502 EndContainer(),
506 * Callback function for the scenario editor 'reset landscape' confirmation window
507 * @param w Window unused
508 * @param confirmed boolean value, true when yes was clicked, false otherwise
510 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
512 if (confirmed) {
513 /* Set generating_world to true to get instant-green grass after removing
514 * company property. */
515 Backup<bool> old_generating_world(_generating_world, true, FILE_LINE);
517 /* Delete all companies */
518 for (Company *c : Company::Iterate()) {
519 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
520 delete c;
523 old_generating_world.Restore();
525 /* Delete all station signs */
526 for (BaseStation *st : BaseStation::Iterate()) {
527 /* There can be buoys, remove them */
528 if (IsBuoyTile(st->xy)) Command<CMD_LANDSCAPE_CLEAR>::Do(DC_EXEC | DC_BANKRUPT, st->xy);
529 if (!st->IsInUse()) delete st;
532 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
533 EngineOverrideManager::ResetToCurrentNewGRFConfig();
535 MarkWholeScreenDirty();
539 /** Landscape generation window handler in the scenario editor. */
540 struct ScenarioEditorLandscapeGenerationWindow : Window {
541 int last_user_action; ///< Last started user action.
543 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
545 this->CreateNestedTree();
546 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
547 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
548 this->FinishInitNested(window_number);
549 this->last_user_action = WIDGET_LIST_END;
552 void OnPaint() override
554 this->DrawWidgets();
556 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
557 SetTileSelectSize(_terraform_size, _terraform_size);
561 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
563 if (widget != WID_ETT_DOTS) return;
565 size->width = std::max<uint>(size->width, ScaleGUITrad(59));
566 size->height = std::max<uint>(size->height, ScaleGUITrad(31));
569 void DrawWidget(const Rect &r, int widget) const override
571 if (widget != WID_ETT_DOTS) return;
573 int center_x = RoundDivSU(r.left + r.right, 2);
574 int center_y = RoundDivSU(r.top + r.bottom, 2);
576 int n = _terraform_size * _terraform_size;
577 const int8 *coords = &_multi_terraform_coords[0][0];
579 assert(n != 0);
580 do {
581 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
582 coords += 2;
583 } while (--n);
586 void OnClick(Point pt, int widget, int click_count) override
588 if (widget < WID_ETT_BUTTONS_START) return;
590 switch (widget) {
591 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
592 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
593 this->last_user_action = widget;
594 break;
596 case WID_ETT_LOWER_LAND: // Lower land button
597 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
598 this->last_user_action = widget;
599 break;
601 case WID_ETT_RAISE_LAND: // Raise land button
602 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
603 this->last_user_action = widget;
604 break;
606 case WID_ETT_LEVEL_LAND: // Level land button
607 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
608 this->last_user_action = widget;
609 break;
611 case WID_ETT_PLACE_ROCKS: // Place rocks button
612 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
613 this->last_user_action = widget;
614 break;
616 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
617 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
618 this->last_user_action = widget;
619 break;
621 case WID_ETT_PLACE_OBJECT: // Place transmitter button
622 ShowBuildObjectPicker();
623 break;
625 case WID_ETT_INCREASE_SIZE:
626 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
627 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
628 this->HandleButtonClick(widget);
629 size += _terraform_size;
631 if (!IsInsideMM(size, 1, 8 + 1)) return;
632 _terraform_size = size;
634 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
635 this->SetDirty();
636 break;
639 case WID_ETT_NEW_SCENARIO: // gen random land
640 this->HandleButtonClick(widget);
641 ShowCreateScenario();
642 break;
644 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
645 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback);
646 break;
648 default: NOT_REACHED();
652 void OnTimeout() override
654 for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
655 if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
656 if (this->IsWidgetLowered(i)) {
657 this->RaiseWidget(i);
658 this->SetWidgetDirty(i);
663 void OnPlaceObject(Point pt, TileIndex tile) override
665 switch (this->last_user_action) {
666 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
667 PlaceProc_DemolishArea(tile);
668 break;
670 case WID_ETT_LOWER_LAND: // Lower land button
671 CommonRaiseLowerBigLand(tile, false);
672 break;
674 case WID_ETT_RAISE_LAND: // Raise land button
675 CommonRaiseLowerBigLand(tile, true);
676 break;
678 case WID_ETT_LEVEL_LAND: // Level land button
679 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
680 break;
682 case WID_ETT_PLACE_ROCKS: // Place rocks button
683 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
684 break;
686 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
687 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
688 break;
690 default: NOT_REACHED();
694 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
696 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
699 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
701 if (pt.x != -1) {
702 switch (select_proc) {
703 default: NOT_REACHED();
704 case DDSP_CREATE_ROCKS:
705 case DDSP_CREATE_DESERT:
706 case DDSP_RAISE_AND_LEVEL_AREA:
707 case DDSP_LOWER_AND_LEVEL_AREA:
708 case DDSP_LEVEL_AREA:
709 case DDSP_DEMOLISH_AREA:
710 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
711 break;
716 void OnPlaceObjectAbort() override
718 this->RaiseButtons();
719 this->SetDirty();
722 static HotkeyList hotkeys;
726 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
727 * @param hotkey Hotkey
728 * @return ES_HANDLED if hotkey was accepted.
730 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
732 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
733 Window *w = ShowEditorTerraformToolbar();
734 if (w == nullptr) return ES_NOT_HANDLED;
735 return w->OnHotkey(hotkey);
738 static Hotkey terraform_editor_hotkeys[] = {
739 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
740 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
741 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
742 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
743 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
744 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
745 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
746 HOTKEY_LIST_END
749 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
751 static WindowDesc _scen_edit_land_gen_desc(
752 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
753 WC_SCEN_LAND_GEN, WC_NONE,
754 WDF_CONSTRUCTION,
755 _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
756 &ScenarioEditorLandscapeGenerationWindow::hotkeys
760 * Show the toolbar for terraforming in the scenario editor.
761 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
763 Window *ShowEditorTerraformToolbar()
765 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);