Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / terraform_gui.cpp
blob3958a4457c8097eaba2681efc1c0f40d6dd38dab
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 "house.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 "terraform_cmd.h"
36 #include "zoom_func.h"
37 #include "rail_cmd.h"
38 #include "landscape_cmd.h"
39 #include "terraform_cmd.h"
40 #include "object_cmd.h"
42 #include "widgets/terraform_widget.h"
44 #include "table/strings.h"
46 #include "safeguards.h"
48 void CcTerraform(Commands, const CommandCost &result, Money, TileIndex tile)
50 if (result.Succeeded()) {
51 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
52 } else {
53 SetRedErrorSquare(tile);
58 /** Scenario editor command that generates desert areas */
59 static void GenerateDesertArea(TileIndex end, TileIndex start)
61 if (_game_mode != GM_EDITOR) return;
63 Backup<bool> old_generating_world(_generating_world, true);
65 TileArea ta(start, end);
66 for (TileIndex tile : ta) {
67 SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
68 Command<CMD_LANDSCAPE_CLEAR>::Post(tile);
69 MarkTileDirtyByTile(tile);
71 old_generating_world.Restore();
72 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
75 /** Scenario editor command that generates rocky areas */
76 static void GenerateRockyArea(TileIndex end, TileIndex start)
78 if (_game_mode != GM_EDITOR) return;
80 bool success = false;
81 TileArea ta(start, end);
83 for (TileIndex tile : ta) {
84 switch (GetTileType(tile)) {
85 case MP_TREES:
86 if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
87 [[fallthrough]];
89 case MP_CLEAR:
90 MakeClear(tile, CLEAR_ROCKS, 3);
91 break;
93 default:
94 continue;
96 MarkTileDirtyByTile(tile);
97 success = true;
100 if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, end);
104 * A central place to handle all X_AND_Y dragged GUI functions.
105 * @param proc Procedure related to the dragging
106 * @param start_tile Begin of the dragging
107 * @param end_tile End of the dragging
108 * @return Returns true if the action was found and handled, and false otherwise. This
109 * allows for additional implements that are more local. For example X_Y drag
110 * of convertrail which belongs in rail_gui.cpp and not terraform_gui.cpp
112 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
114 if (!_settings_game.construction.freeform_edges) {
115 /* When end_tile is MP_VOID, the error tile will not be visible to the
116 * user. This happens when terraforming at the southern border. */
117 if (TileX(end_tile) == Map::MaxX()) end_tile += TileDiffXY(-1, 0);
118 if (TileY(end_tile) == Map::MaxY()) end_tile += TileDiffXY(0, -1);
121 switch (proc) {
122 case DDSP_DEMOLISH_AREA:
123 Command<CMD_CLEAR_AREA>::Post(STR_ERROR_CAN_T_CLEAR_THIS_AREA, CcPlaySound_EXPLOSION, end_tile, start_tile, _ctrl_pressed);
124 break;
125 case DDSP_RAISE_AND_LEVEL_AREA:
126 Command<CMD_LEVEL_LAND>::Post(STR_ERROR_CAN_T_RAISE_LAND_HERE, CcTerraform, end_tile, start_tile, _ctrl_pressed, LM_RAISE);
127 break;
128 case DDSP_LOWER_AND_LEVEL_AREA:
129 Command<CMD_LEVEL_LAND>::Post(STR_ERROR_CAN_T_LOWER_LAND_HERE, CcTerraform, end_tile, start_tile, _ctrl_pressed, LM_LOWER);
130 break;
131 case DDSP_LEVEL_AREA:
132 Command<CMD_LEVEL_LAND>::Post(STR_ERROR_CAN_T_LEVEL_LAND_HERE, CcTerraform, end_tile, start_tile, _ctrl_pressed, LM_LEVEL);
133 break;
134 case DDSP_CREATE_ROCKS:
135 GenerateRockyArea(end_tile, start_tile);
136 break;
137 case DDSP_CREATE_DESERT:
138 GenerateDesertArea(end_tile, start_tile);
139 break;
140 default:
141 return false;
144 return true;
148 * Start a drag for demolishing an area.
149 * @param tile Position of one corner.
151 void PlaceProc_DemolishArea(TileIndex tile)
153 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
156 /** Terra form toolbar managing class. */
157 struct TerraformToolbarWindow : Window {
158 int last_user_action; ///< Last started user action.
160 TerraformToolbarWindow(WindowDesc &desc, WindowNumber window_number) : Window(desc)
162 /* This is needed as we like to have the tree available on OnInit. */
163 this->CreateNestedTree();
164 this->FinishInitNested(window_number);
165 this->last_user_action = INVALID_WID_TT;
168 ~TerraformToolbarWindow()
172 void OnInit() override
174 /* Don't show the place object button when there are no objects to place. */
175 NWidgetStacked *show_object = this->GetWidget<NWidgetStacked>(WID_TT_SHOW_PLACE_OBJECT);
176 show_object->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE);
179 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
181 if (widget < WID_TT_BUTTONS_START) return;
183 switch (widget) {
184 case WID_TT_LOWER_LAND: // Lower land button
185 HandlePlacePushButton(this, WID_TT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT | HT_DIAGONAL);
186 this->last_user_action = widget;
187 break;
189 case WID_TT_RAISE_LAND: // Raise land button
190 HandlePlacePushButton(this, WID_TT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT | HT_DIAGONAL);
191 this->last_user_action = widget;
192 break;
194 case WID_TT_LEVEL_LAND: // Level land button
195 HandlePlacePushButton(this, WID_TT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
196 this->last_user_action = widget;
197 break;
199 case WID_TT_DEMOLISH: // Demolish aka dynamite button
200 HandlePlacePushButton(this, WID_TT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
201 this->last_user_action = widget;
202 break;
204 case WID_TT_BUY_LAND: // Buy land button
205 HandlePlacePushButton(this, WID_TT_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT | HT_DIAGONAL);
206 this->last_user_action = widget;
207 break;
209 case WID_TT_PLANT_TREES: // Plant trees button
210 ShowBuildTreesToolbar();
211 break;
213 case WID_TT_PLACE_SIGN: // Place sign button
214 HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
215 this->last_user_action = widget;
216 break;
218 case WID_TT_PLACE_OBJECT: // Place object button
219 ShowBuildObjectPicker();
220 break;
222 default: NOT_REACHED();
226 void OnPlaceObject([[maybe_unused]] Point pt, TileIndex tile) override
228 switch (this->last_user_action) {
229 case WID_TT_LOWER_LAND: // Lower land button
230 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
231 break;
233 case WID_TT_RAISE_LAND: // Raise land button
234 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
235 break;
237 case WID_TT_LEVEL_LAND: // Level land button
238 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
239 break;
241 case WID_TT_DEMOLISH: // Demolish aka dynamite button
242 PlaceProc_DemolishArea(tile);
243 break;
245 case WID_TT_BUY_LAND: // Buy land button
246 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_BUILD_OBJECT);
247 break;
249 case WID_TT_PLACE_SIGN: // Place sign button
250 PlaceProc_Sign(tile);
251 break;
253 default: NOT_REACHED();
257 void OnPlaceDrag(ViewportPlaceMethod select_method, [[maybe_unused]] ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt) override
259 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
262 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
264 Point pt = GetToolbarAlignedWindowPosition(sm_width);
265 pt.y += sm_height;
266 return pt;
269 void OnPlaceMouseUp([[maybe_unused]] ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt, TileIndex start_tile, TileIndex end_tile) override
271 if (pt.x != -1) {
272 switch (select_proc) {
273 default: NOT_REACHED();
274 case DDSP_DEMOLISH_AREA:
275 case DDSP_RAISE_AND_LEVEL_AREA:
276 case DDSP_LOWER_AND_LEVEL_AREA:
277 case DDSP_LEVEL_AREA:
278 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
279 break;
280 case DDSP_BUILD_OBJECT:
281 if (!_settings_game.construction.freeform_edges) {
282 /* When end_tile is MP_VOID, the error tile will not be visible to the
283 * user. This happens when terraforming at the southern border. */
284 if (TileX(end_tile) == Map::MaxX()) end_tile += TileDiffXY(-1, 0);
285 if (TileY(end_tile) == Map::MaxY()) end_tile += TileDiffXY(0, -1);
287 Command<CMD_BUILD_OBJECT_AREA>::Post(STR_ERROR_CAN_T_PURCHASE_THIS_LAND, CcPlaySound_CONSTRUCTION_RAIL,
288 end_tile, start_tile, OBJECT_OWNED_LAND, 0, (_ctrl_pressed ? true : false));
289 break;
294 void OnPlaceObjectAbort() override
296 this->RaiseButtons();
300 * Handler for global hotkeys of the TerraformToolbarWindow.
301 * @param hotkey Hotkey
302 * @return ES_HANDLED if hotkey was accepted.
304 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
306 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
307 Window *w = ShowTerraformToolbar(nullptr);
308 if (w == nullptr) return ES_NOT_HANDLED;
309 return w->OnHotkey(hotkey);
312 static inline HotkeyList hotkeys{"terraform", {
313 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
314 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
315 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
316 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
317 Hotkey('U', "buyland", WID_TT_BUY_LAND),
318 Hotkey('I', "trees", WID_TT_PLANT_TREES),
319 Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
320 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
321 }, TerraformToolbarGlobalHotkeys};
324 static constexpr NWidgetPart _nested_terraform_widgets[] = {
325 NWidget(NWID_HORIZONTAL),
326 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
327 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
328 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
329 EndContainer(),
330 NWidget(NWID_HORIZONTAL),
331 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
332 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
333 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
334 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
335 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
336 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
338 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
340 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
341 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
342 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
343 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
344 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
345 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
346 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
347 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
348 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
349 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
350 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
351 EndContainer(),
352 EndContainer(),
355 static WindowDesc _terraform_desc(
356 WDP_MANUAL, "toolbar_landscape", 0, 0,
357 WC_SCEN_LAND_GEN, WC_NONE,
358 WDF_CONSTRUCTION,
359 _nested_terraform_widgets,
360 &TerraformToolbarWindow::hotkeys
364 * Show the toolbar for terraforming in the game.
365 * @param link The toolbar we might want to link to.
366 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
368 Window *ShowTerraformToolbar(Window *link)
370 if (!Company::IsValidID(_local_company)) return nullptr;
372 Window *w;
373 if (link == nullptr) {
374 w = AllocateWindowDescFront<TerraformToolbarWindow>(_terraform_desc, 0);
375 return w;
378 /* Delete the terraform toolbar to place it again. */
379 CloseWindowById(WC_SCEN_LAND_GEN, 0, true);
380 w = AllocateWindowDescFront<TerraformToolbarWindow>(_terraform_desc, 0);
381 /* Align the terraform toolbar under the main toolbar. */
382 w->top -= w->height;
383 w->SetDirty();
384 /* Put the linked toolbar to the left / right of it. */
385 link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
386 link->top = w->top;
387 link->SetDirty();
389 return w;
392 static uint8_t _terraform_size = 1;
395 * Raise/Lower a bigger chunk of land at the same time in the editor. When
396 * raising get the lowest point, when lowering the highest point, and set all
397 * tiles in the selection to that height.
398 * @todo : Incorporate into game itself to allow for ingame raising/lowering of
399 * larger chunks at the same time OR remove altogether, as we have 'level land' ?
400 * @param tile The top-left tile where the terraforming will start
401 * @param mode true for raising, false for lowering land
403 static void CommonRaiseLowerBigLand(TileIndex tile, bool mode)
405 if (_terraform_size == 1) {
406 StringID msg =
407 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
409 Command<CMD_TERRAFORM_LAND>::Post(msg, CcTerraform, tile, SLOPE_N, mode);
410 } else {
411 assert(_terraform_size != 0);
412 TileArea ta(tile, _terraform_size, _terraform_size);
413 ta.ClampToMap();
415 if (ta.w == 0 || ta.h == 0) return;
417 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_CONSTRUCTION_OTHER, tile);
419 uint h;
420 if (mode != 0) {
421 /* Raise land */
422 h = MAX_TILE_HEIGHT;
423 for (TileIndex tile2 : ta) {
424 h = std::min(h, TileHeight(tile2));
426 } else {
427 /* Lower land */
428 h = 0;
429 for (TileIndex tile2 : ta) {
430 h = std::max(h, TileHeight(tile2));
434 for (TileIndex tile2 : ta) {
435 if (TileHeight(tile2) == h) {
436 Command<CMD_TERRAFORM_LAND>::Post(tile2, SLOPE_N, mode);
442 static const int8_t _multi_terraform_coords[][2] = {
443 { 0, -2},
444 { 4, 0}, { -4, 0}, { 0, 2},
445 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
446 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
447 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
448 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
449 {-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},
450 {-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},
453 static constexpr NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
454 NWidget(NWID_HORIZONTAL),
455 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
456 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
457 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
458 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
459 EndContainer(),
460 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
461 NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
462 NWidget(NWID_SPACER), SetFill(1, 0),
463 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
464 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
465 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
466 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
467 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
468 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
469 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
470 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
471 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
472 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
473 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
474 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
475 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
476 EndContainer(),
477 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
478 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
479 NWidget(NWID_SPACER), SetFill(1, 0),
480 EndContainer(),
481 NWidget(NWID_HORIZONTAL),
482 NWidget(NWID_SPACER), SetFill(1, 0),
483 NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
484 NWidget(NWID_SPACER), SetFill(1, 0),
485 NWidget(NWID_VERTICAL),
486 NWidget(NWID_SPACER), SetFill(0, 1),
487 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
488 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
489 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
490 NWidget(NWID_SPACER), SetFill(0, 1),
491 EndContainer(),
492 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
493 EndContainer(),
494 NWidget(NWID_SPACER), SetMinimalSize(0, 6),
495 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
496 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
497 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
498 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
499 EndContainer(),
503 * Callback function for the scenario editor 'reset landscape' confirmation window
504 * @param confirmed boolean value, true when yes was clicked, false otherwise
506 static void ResetLandscapeConfirmationCallback(Window *, bool confirmed)
508 if (confirmed) {
509 /* Set generating_world to true to get instant-green grass after removing
510 * company property. */
511 Backup<bool> old_generating_world(_generating_world, true);
513 /* Delete all companies */
514 for (Company *c : Company::Iterate()) {
515 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
516 delete c;
519 old_generating_world.Restore();
521 /* Delete all station signs */
522 for (BaseStation *st : BaseStation::Iterate()) {
523 /* There can be buoys, remove them */
524 if (IsBuoyTile(st->xy)) Command<CMD_LANDSCAPE_CLEAR>::Do(DC_EXEC | DC_BANKRUPT, st->xy);
525 if (!st->IsInUse()) delete st;
528 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
529 EngineOverrideManager::ResetToCurrentNewGRFConfig();
531 MarkWholeScreenDirty();
535 /** Landscape generation window handler in the scenario editor. */
536 struct ScenarioEditorLandscapeGenerationWindow : Window {
537 int last_user_action; ///< Last started user action.
539 ScenarioEditorLandscapeGenerationWindow(WindowDesc &desc, WindowNumber window_number) : Window(desc)
541 this->CreateNestedTree();
542 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
543 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
544 this->FinishInitNested(window_number);
545 this->last_user_action = INVALID_WID_ETT;
548 void OnPaint() override
550 this->DrawWidgets();
552 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
553 SetTileSelectSize(_terraform_size, _terraform_size);
557 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
559 if (widget != WID_ETT_DOTS) return;
561 size.width = std::max<uint>(size.width, ScaleGUITrad(59));
562 size.height = std::max<uint>(size.height, ScaleGUITrad(31));
565 void DrawWidget(const Rect &r, WidgetID widget) const override
567 if (widget != WID_ETT_DOTS) return;
569 int center_x = RoundDivSU(r.left + r.right, 2);
570 int center_y = RoundDivSU(r.top + r.bottom, 2);
572 int n = _terraform_size * _terraform_size;
573 const int8_t *coords = &_multi_terraform_coords[0][0];
575 assert(n != 0);
576 do {
577 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
578 coords += 2;
579 } while (--n);
582 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
584 if (widget < WID_ETT_BUTTONS_START) return;
586 switch (widget) {
587 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
588 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
589 this->last_user_action = widget;
590 break;
592 case WID_ETT_LOWER_LAND: // Lower land button
593 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
594 this->last_user_action = widget;
595 break;
597 case WID_ETT_RAISE_LAND: // Raise land button
598 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
599 this->last_user_action = widget;
600 break;
602 case WID_ETT_LEVEL_LAND: // Level land button
603 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
604 this->last_user_action = widget;
605 break;
607 case WID_ETT_PLACE_ROCKS: // Place rocks button
608 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
609 this->last_user_action = widget;
610 break;
612 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
613 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
614 this->last_user_action = widget;
615 break;
617 case WID_ETT_PLACE_OBJECT: // Place transmitter button
618 ShowBuildObjectPicker();
619 break;
621 case WID_ETT_INCREASE_SIZE:
622 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
623 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
624 this->HandleButtonClick(widget);
625 size += _terraform_size;
627 if (!IsInsideMM(size, 1, 8 + 1)) return;
628 _terraform_size = size;
630 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
631 this->SetDirty();
632 break;
635 case WID_ETT_NEW_SCENARIO: // gen random land
636 this->HandleButtonClick(widget);
637 ShowCreateScenario();
638 break;
640 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
641 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback);
642 break;
644 default: NOT_REACHED();
648 void OnTimeout() override
650 for (const auto &pair : this->widget_lookup) {
651 if (pair.first < WID_ETT_START || (pair.first >= WID_ETT_BUTTONS_START && pair.first < WID_ETT_BUTTONS_END)) continue; // skip the buttons
652 this->RaiseWidgetWhenLowered(pair.first);
656 void OnPlaceObject([[maybe_unused]] Point pt, TileIndex tile) override
658 switch (this->last_user_action) {
659 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
660 PlaceProc_DemolishArea(tile);
661 break;
663 case WID_ETT_LOWER_LAND: // Lower land button
664 CommonRaiseLowerBigLand(tile, false);
665 break;
667 case WID_ETT_RAISE_LAND: // Raise land button
668 CommonRaiseLowerBigLand(tile, true);
669 break;
671 case WID_ETT_LEVEL_LAND: // Level land button
672 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
673 break;
675 case WID_ETT_PLACE_ROCKS: // Place rocks button
676 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
677 break;
679 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
680 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
681 break;
683 default: NOT_REACHED();
687 void OnPlaceDrag(ViewportPlaceMethod select_method, [[maybe_unused]] ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt) override
689 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
692 void OnPlaceMouseUp([[maybe_unused]] ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt, TileIndex start_tile, TileIndex end_tile) override
694 if (pt.x != -1) {
695 switch (select_proc) {
696 default: NOT_REACHED();
697 case DDSP_CREATE_ROCKS:
698 case DDSP_CREATE_DESERT:
699 case DDSP_RAISE_AND_LEVEL_AREA:
700 case DDSP_LOWER_AND_LEVEL_AREA:
701 case DDSP_LEVEL_AREA:
702 case DDSP_DEMOLISH_AREA:
703 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
704 break;
709 void OnPlaceObjectAbort() override
711 this->RaiseButtons();
712 this->SetDirty();
716 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
717 * @param hotkey Hotkey
718 * @return ES_HANDLED if hotkey was accepted.
720 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
722 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
723 Window *w = ShowEditorTerraformToolbar();
724 if (w == nullptr) return ES_NOT_HANDLED;
725 return w->OnHotkey(hotkey);
728 static inline HotkeyList hotkeys{"terraform_editor", {
729 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
730 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
731 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
732 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
733 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
734 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
735 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
736 }, TerraformToolbarEditorGlobalHotkeys};
739 static WindowDesc _scen_edit_land_gen_desc(
740 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
741 WC_SCEN_LAND_GEN, WC_NONE,
742 WDF_CONSTRUCTION,
743 _nested_scen_edit_land_gen_widgets,
744 &ScenarioEditorLandscapeGenerationWindow::hotkeys
748 * Show the toolbar for terraforming in the scenario editor.
749 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
751 Window *ShowEditorTerraformToolbar()
753 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(_scen_edit_land_gen_desc, 0);