Update readme.md
[openttd-joker.git] / src / terraform_gui.cpp
blobd2864ef097cb5820ff101194a68185f55997714c
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"
42 #include "cheat_type.h"
44 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
46 if (result.Succeeded()) {
47 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
48 } else {
49 extern TileIndex _terraform_err_tile;
50 SetRedErrorSquare(_terraform_err_tile);
55 /** Scenario editor command that generates desert areas */
56 static void GenerateDesertArea(TileIndex end, TileIndex start)
58 if (_game_mode != GM_EDITOR) return;
60 _generating_world = true;
62 TileArea ta(start, end);
63 TILE_AREA_LOOP(tile, ta) {
64 SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
65 DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
66 MarkTileDirtyByTile(tile);
68 _generating_world = false;
69 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
72 /** Scenario editor command that generates rocky areas */
73 static void GenerateRockyArea(TileIndex end, TileIndex start)
75 if (_game_mode != GM_EDITOR) return;
77 bool success = false;
78 TileArea ta(start, end);
80 TILE_AREA_LOOP(tile, ta) {
81 switch (GetTileType(tile)) {
82 case MP_TREES:
83 if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
84 FALLTHROUGH;
86 case MP_CLEAR:
87 MakeClear(tile, CLEAR_ROCKS, 3);
88 break;
90 default:
91 continue;
93 MarkTileDirtyByTile(tile);
94 success = true;
97 if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, end);
100 static bool IsIndustryOrRailStationInArea(TileIndex start_tile, TileIndex end_tile, bool diagonal)
102 std::unique_ptr<TileIterator> tile_iterator;
104 if (diagonal) {
105 tile_iterator = std::make_unique<DiagonalTileIterator>(end_tile, start_tile);
106 } else {
107 tile_iterator = std::make_unique<OrthogonalTileIterator>(end_tile, start_tile);
110 bool destroying_industry_or_station = false;
112 for (; *tile_iterator != INVALID_TILE; ++(*tile_iterator)) {
113 if ((_cheats.magic_bulldozer.value && IsTileType(*tile_iterator, MP_INDUSTRY)) ||
114 IsRailStationTile(*tile_iterator)) {
115 destroying_industry_or_station = true;
116 break;
120 return destroying_industry_or_station;
124 * A central place to handle all X_AND_Y dragged GUI functions.
125 * @param proc Procedure related to the dragging
126 * @param start_tile Begin of the dragging
127 * @param end_tile End of the dragging
128 * @return Returns true if the action was found and handled, and false otherwise. This
129 * allows for additional implements that are more local. For example X_Y drag
130 * of convertrail which belongs in rail_gui.cpp and not terraform_gui.cpp
132 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
134 if (!_settings_game.construction.freeform_edges) {
135 /* When end_tile is MP_VOID, the error tile will not be visible to the
136 * user. This happens when terraforming at the southern border. */
137 if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
138 if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
141 switch (proc) {
142 case DDSP_DEMOLISH_AREA: {
143 const bool should_query_first = IsIndustryOrRailStationInArea(start_tile, end_tile, _ctrl_pressed);
145 const auto cmd_clear_area = [=](Window*, bool confirmed) {
146 if (confirmed) {
147 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 1 : 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound_EXPLOSION);
151 if (should_query_first) {
152 ShowQuery(STR_QUERY_CLEAR_AREA_CAPTION, STR_CLEAR_AREA_CONFIRMATION_TEXT, nullptr, cmd_clear_area);
153 } else {
154 cmd_clear_area(nullptr, true);
156 break;
158 case DDSP_RAISE_AND_LEVEL_AREA:
159 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);
160 break;
161 case DDSP_LOWER_AND_LEVEL_AREA:
162 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);
163 break;
164 case DDSP_LEVEL_AREA:
165 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);
166 break;
167 case DDSP_CREATE_ROCKS:
168 GenerateRockyArea(end_tile, start_tile);
169 break;
170 case DDSP_CREATE_DESERT:
171 GenerateDesertArea(end_tile, start_tile);
172 break;
173 default:
174 return false;
177 return true;
181 * Start a drag for demolishing an area.
182 * @param tile Position of one corner.
184 void PlaceProc_DemolishArea(TileIndex tile)
186 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
189 static void PlaceProc_Measure(TileIndex tile)
191 VpStartPlaceSizing(tile, VPM_A_B_LINE, DDSP_MEASURE);
194 /** Terra form toolbar managing class. */
195 struct TerraformToolbarWindow : Window {
196 int last_user_action; ///< Last started user action.
198 TerraformToolbarWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
200 /* This is needed as we like to have the tree available on OnInit. */
201 this->CreateNestedTree();
202 this->FinishInitNested(window_number);
203 this->last_user_action = WIDGET_LIST_END;
206 ~TerraformToolbarWindow()
210 virtual void OnInit()
212 /* Don't show the place object button when there are no objects to place. */
213 NWidgetStacked *show_object = this->GetWidget<NWidgetStacked>(WID_TT_SHOW_PLACE_OBJECT);
214 show_object->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE);
217 virtual void OnClick(Point pt, int widget, int click_count)
219 if (widget < WID_TT_BUTTONS_START) return;
221 switch (widget) {
222 case WID_TT_LOWER_LAND: // Lower land button
223 HandlePlacePushButton(this, WID_TT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT | HT_DIAGONAL);
224 this->last_user_action = widget;
225 break;
227 case WID_TT_RAISE_LAND: // Raise land button
228 HandlePlacePushButton(this, WID_TT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT | HT_DIAGONAL);
229 this->last_user_action = widget;
230 break;
232 case WID_TT_LEVEL_LAND: // Level land button
233 HandlePlacePushButton(this, WID_TT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
234 this->last_user_action = widget;
235 break;
237 case WID_TT_DEMOLISH: // Demolish aka dynamite button
238 HandlePlacePushButton(this, WID_TT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
239 this->last_user_action = widget;
240 break;
242 case WID_TT_BUY_LAND: // Buy land button
243 HandlePlacePushButton(this, WID_TT_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT);
244 this->last_user_action = widget;
245 break;
247 case WID_TT_PLANT_TREES: // Plant trees button
248 ShowBuildTreesToolbar();
249 break;
251 case WID_TT_MEASUREMENT_TOOL:
252 HandlePlacePushButton(this, WID_TT_MEASUREMENT_TOOL, SPR_CURSOR_QUERY, HT_RECT);
253 this->last_user_action = widget;
254 break;
256 case WID_TT_PLACE_SIGN: // Place sign button
257 HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
258 this->last_user_action = widget;
259 break;
261 case WID_TT_PLACE_OBJECT: // Place object button
262 ShowBuildObjectPicker();
263 break;
265 default: NOT_REACHED();
269 virtual void OnPlaceObject(Point pt, TileIndex tile)
271 switch (this->last_user_action) {
272 case WID_TT_LOWER_LAND: // Lower land button
273 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
274 break;
276 case WID_TT_RAISE_LAND: // Raise land button
277 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
278 break;
280 case WID_TT_LEVEL_LAND: // Level land button
281 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
282 break;
284 case WID_TT_DEMOLISH: // Demolish aka dynamite button
285 PlaceProc_DemolishArea(tile);
286 break;
288 case WID_TT_BUY_LAND: // Buy land button
289 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_PLACE_OBJECT);
290 break;
292 case WID_TT_MEASUREMENT_TOOL:
293 PlaceProc_Measure(tile);
294 break;
296 case WID_TT_PLACE_SIGN: // Place sign button
297 PlaceProc_Sign(tile);
298 break;
300 default: NOT_REACHED();
304 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
306 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
309 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
311 Point pt = GetToolbarAlignedWindowPosition(sm_width);
312 pt.y += sm_height;
313 return pt;
316 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
318 if (pt.x != -1) {
319 switch (select_proc) {
320 default: NOT_REACHED();
321 case DDSP_DEMOLISH_AREA:
322 case DDSP_RAISE_AND_LEVEL_AREA:
323 case DDSP_LOWER_AND_LEVEL_AREA:
324 case DDSP_LEVEL_AREA:
325 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
326 break;
327 case DDSP_MEASURE:
328 //nothing to do, just draw a tooltip
329 break;
330 case DDSP_PLACE_OBJECT:
332 uint32 p2;
334 SB(p2, 2, 30, start_tile);
336 if (pt.x != -1 && select_proc == DDSP_PLACE_OBJECT) {
337 DoCommandP(end_tile, OBJECT_OWNED_LAND, p2, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT), CcTerraform);
340 break;
345 virtual void OnPlaceObjectAbort()
347 this->RaiseButtons();
348 this->SetDirty();
351 static HotkeyList hotkeys;
355 * Handler for global hotkeys of the TerraformToolbarWindow.
356 * @param hotkey Hotkey
357 * @return ES_HANDLED if hotkey was accepted.
359 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
361 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
362 Window *w = ShowTerraformToolbar(nullptr);
363 if (w == nullptr) return ES_NOT_HANDLED;
364 return w->OnHotkey(hotkey);
367 static Hotkey terraform_hotkeys[] = {
368 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
369 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
370 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
371 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
372 Hotkey('U', "buyland", WID_TT_BUY_LAND),
373 Hotkey('I', "trees", WID_TT_PLANT_TREES),
374 Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
375 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
376 HOTKEY_LIST_END
378 HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
380 static const NWidgetPart _nested_terraform_widgets[] = {
381 NWidget(NWID_HORIZONTAL),
382 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
383 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
384 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
385 EndContainer(),
386 NWidget(NWID_HORIZONTAL),
387 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
388 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
389 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
390 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
391 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
392 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
394 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
396 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
397 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
398 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
399 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
400 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
401 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
402 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_MEASUREMENT_TOOL), SetMinimalSize(22,22),
403 SetFill(0, 1), SetDataTip(SPR_IMG_QUERY, STR_LANDSCAPING_TOOLTIP_RULER_TOOL),
404 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
405 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
406 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
407 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
408 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
409 EndContainer(),
410 EndContainer(),
413 static WindowDesc _terraform_desc(
414 WDP_MANUAL, "toolbar_landscape", 0, 0,
415 WC_SCEN_LAND_GEN, WC_NONE,
416 WDF_CONSTRUCTION,
417 _nested_terraform_widgets, lengthof(_nested_terraform_widgets),
418 &TerraformToolbarWindow::hotkeys
422 * Show the toolbar for terraforming in the game.
423 * @param link The toolbar we might want to link to.
424 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
426 Window *ShowTerraformToolbar(Window *link)
428 if (!Company::IsValidID(_local_company)) return nullptr;
430 Window *w;
431 if (link == nullptr) {
432 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
433 return w;
436 /* Delete the terraform toolbar to place it again. */
437 DeleteWindowById(WC_SCEN_LAND_GEN, 0, true);
438 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
439 /* Align the terraform toolbar under the main toolbar. */
440 w->top -= w->height;
441 w->SetDirty();
442 /* Put the linked toolbar to the left / right of it. */
443 link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
444 link->top = w->top;
445 link->SetDirty();
447 return w;
450 static byte _terraform_size = 1;
453 * Raise/Lower a bigger chunk of land at the same time in the editor. When
454 * raising get the lowest point, when lowering the highest point, and set all
455 * tiles in the selection to that height.
456 * @todo : Incorporate into game itself to allow for ingame raising/lowering of
457 * larger chunks at the same time OR remove altogether, as we have 'level land' ?
458 * @param tile The top-left tile where the terraforming will start
459 * @param mode 1 for raising, 0 for lowering land
461 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
463 if (_terraform_size == 1) {
464 StringID msg =
465 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
467 DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
468 } else {
469 assert(_terraform_size != 0);
470 TileArea ta(tile, _terraform_size, _terraform_size);
471 ta.ClampToMap();
473 if (ta.w == 0 || ta.h == 0) return;
475 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
477 uint h;
478 if (mode != 0) {
479 /* Raise land */
480 h = MAX_TILE_HEIGHT;
481 TILE_AREA_LOOP(tile2, ta) {
482 h = min(h, TileHeight(tile2));
484 } else {
485 /* Lower land */
486 h = 0;
487 TILE_AREA_LOOP(tile2, ta) {
488 h = max(h, TileHeight(tile2));
492 TILE_AREA_LOOP(tile2, ta) {
493 if (TileHeight(tile2) == h) {
494 DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
500 static const int8 _multi_terraform_coords[][2] = {
501 { 0, -2},
502 { 4, 0}, { -4, 0}, { 0, 2},
503 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
504 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
505 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
506 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
507 {-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},
508 {-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},
511 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
512 NWidget(NWID_HORIZONTAL),
513 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
514 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
515 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
516 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
517 EndContainer(),
518 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
519 NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
520 NWidget(NWID_SPACER), SetFill(1, 0),
521 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
522 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
523 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
524 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
525 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
526 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
527 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
528 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
529 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
530 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
531 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
532 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
533 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
534 EndContainer(),
535 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
536 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
537 NWidget(NWID_SPACER), SetFill(1, 0),
538 EndContainer(),
539 NWidget(NWID_HORIZONTAL),
540 NWidget(NWID_SPACER), SetFill(1, 0),
541 NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
542 NWidget(NWID_SPACER), SetFill(1, 0),
543 NWidget(NWID_VERTICAL),
544 NWidget(NWID_SPACER), SetFill(0, 1),
545 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
546 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
547 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
548 NWidget(NWID_SPACER), SetFill(0, 1),
549 EndContainer(),
550 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
551 EndContainer(),
552 NWidget(NWID_SPACER), SetMinimalSize(0, 6),
553 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
554 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
555 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
556 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
557 EndContainer(),
561 * Callback function for the scenario editor 'reset landscape' confirmation window
562 * @param w Window unused
563 * @param confirmed boolean value, true when yes was clicked, false otherwise
565 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
567 if (confirmed) {
568 /* Set generating_world to true to get instant-green grass after removing
569 * company property. */
570 _generating_world = true;
572 /* Delete all companies */
573 Company *c;
574 FOR_ALL_COMPANIES(c) {
575 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
576 delete c;
579 _generating_world = false;
581 /* Delete all station signs */
582 BaseStation *st;
583 FOR_ALL_BASE_STATIONS(st) {
584 /* There can be buoys, remove them */
585 if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
586 if (!st->IsInUse()) delete st;
589 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
590 EngineOverrideManager::ResetToCurrentNewGRFConfig();
592 MarkWholeScreenDirty();
596 /** Landscape generation window handler in the scenario editor. */
597 struct ScenarioEditorLandscapeGenerationWindow : Window {
598 int last_user_action; ///< Last started user action.
600 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
602 this->CreateNestedTree();
603 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
604 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
605 this->FinishInitNested(window_number);
606 this->last_user_action = WIDGET_LIST_END;
609 virtual void OnPaint()
611 this->DrawWidgets();
613 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
614 SetTileSelectSize(_terraform_size, _terraform_size);
618 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
620 if (widget != WID_ETT_DOTS) return;
622 size->width = max<uint>(size->width, ScaleGUITrad(59));
623 size->height = max<uint>(size->height, ScaleGUITrad(31));
626 virtual void DrawWidget(const Rect &r, int widget) const
628 if (widget != WID_ETT_DOTS) return;
630 int center_x = RoundDivSU(r.left + r.right, 2);
631 int center_y = RoundDivSU(r.top + r.bottom, 2);
633 int n = _terraform_size * _terraform_size;
634 const int8 *coords = &_multi_terraform_coords[0][0];
636 assert(n != 0);
637 do {
638 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
639 coords += 2;
640 } while (--n);
643 virtual void OnClick(Point pt, int widget, int click_count)
645 if (widget < WID_ETT_BUTTONS_START) return;
647 switch (widget) {
648 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
649 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
650 this->last_user_action = widget;
651 break;
653 case WID_ETT_LOWER_LAND: // Lower land button
654 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
655 this->last_user_action = widget;
656 break;
658 case WID_ETT_RAISE_LAND: // Raise land button
659 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
660 this->last_user_action = widget;
661 break;
663 case WID_ETT_LEVEL_LAND: // Level land button
664 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
665 this->last_user_action = widget;
666 break;
668 case WID_ETT_PLACE_ROCKS: // Place rocks button
669 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
670 this->last_user_action = widget;
671 break;
673 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
674 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
675 this->last_user_action = widget;
676 break;
678 case WID_ETT_PLACE_OBJECT: // Place transmitter button
679 ShowBuildObjectPicker();
680 break;
682 case WID_ETT_INCREASE_SIZE:
683 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
684 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
685 this->HandleButtonClick(widget);
686 size += _terraform_size;
688 if (!IsInsideMM(size, 1, 8 + 1)) return;
689 _terraform_size = size;
691 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
692 this->SetDirty();
693 break;
696 case WID_ETT_NEW_SCENARIO: // gen random land
697 this->HandleButtonClick(widget);
698 ShowCreateScenario();
699 break;
701 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
702 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback);
703 break;
705 default: NOT_REACHED();
709 virtual void OnTimeout()
711 for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
712 if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
713 if (this->IsWidgetLowered(i)) {
714 this->RaiseWidget(i);
715 this->SetWidgetDirty(i);
720 virtual void OnPlaceObject(Point pt, TileIndex tile)
722 switch (this->last_user_action) {
723 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
724 PlaceProc_DemolishArea(tile);
725 break;
727 case WID_ETT_LOWER_LAND: // Lower land button
728 CommonRaiseLowerBigLand(tile, 0);
729 break;
731 case WID_ETT_RAISE_LAND: // Raise land button
732 CommonRaiseLowerBigLand(tile, 1);
733 break;
735 case WID_ETT_LEVEL_LAND: // Level land button
736 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
737 break;
739 case WID_ETT_PLACE_ROCKS: // Place rocks button
740 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
741 break;
743 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
744 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
745 break;
747 default: NOT_REACHED();
751 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
753 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
756 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
758 if (pt.x != -1) {
759 switch (select_proc) {
760 default: NOT_REACHED();
761 case DDSP_CREATE_ROCKS:
762 case DDSP_CREATE_DESERT:
763 case DDSP_RAISE_AND_LEVEL_AREA:
764 case DDSP_LOWER_AND_LEVEL_AREA:
765 case DDSP_LEVEL_AREA:
766 case DDSP_DEMOLISH_AREA:
767 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
768 break;
773 virtual void OnPlaceObjectAbort()
775 this->RaiseButtons();
776 this->SetDirty();
779 static HotkeyList hotkeys;
783 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
784 * @param hotkey Hotkey
785 * @return ES_HANDLED if hotkey was accepted.
787 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
789 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
790 Window *w = ShowEditorTerraformToolbar();
791 if (w == nullptr) return ES_NOT_HANDLED;
792 return w->OnHotkey(hotkey);
795 static Hotkey terraform_editor_hotkeys[] = {
796 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
797 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
798 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
799 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
800 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
801 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
802 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
803 HOTKEY_LIST_END
806 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
808 static WindowDesc _scen_edit_land_gen_desc(
809 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
810 WC_SCEN_LAND_GEN, WC_NONE,
811 WDF_CONSTRUCTION,
812 _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
813 &ScenarioEditorLandscapeGenerationWindow::hotkeys
817 * Show the toolbar for terraforming in the scenario editor.
818 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
820 Window *ShowEditorTerraformToolbar()
822 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);