Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / terraform_gui.cpp
blob9f727ce7298d6154f0e59d95a4cd86978b80ab93
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 "clear_map.h"
12 #include "company_func.h"
13 #include "company_base.h"
14 #include "gui.h"
15 #include "window_gui.h"
16 #include "window_func.h"
17 #include "viewport_func.h"
18 #include "command_func.h"
19 #include "signs_func.h"
20 #include "sound_func.h"
21 #include "base_station_base.h"
22 #include "textbuf_gui.h"
23 #include "genworld.h"
24 #include "tree_map.h"
25 #include "landscape_type.h"
26 #include "tilehighlight_func.h"
27 #include "strings_func.h"
28 #include "newgrf_object.h"
29 #include "object.h"
30 #include "hotkeys.h"
31 #include "engine_base.h"
32 #include "terraform_gui.h"
33 #include "zoom_func.h"
35 #include "widgets/terraform_widget.h"
37 #include "table/strings.h"
39 #include "safeguards.h"
41 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2, uint32 cmd)
43 if (result.Succeeded()) {
44 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
45 } else {
46 extern TileIndex _terraform_err_tile;
47 SetRedErrorSquare(_terraform_err_tile);
52 /** Scenario editor command that generates desert areas */
53 static void GenerateDesertArea(TileIndex end, TileIndex start)
55 if (_game_mode != GM_EDITOR) return;
57 _generating_world = true;
59 TileArea ta(start, end);
60 TILE_AREA_LOOP(tile, ta) {
61 SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
62 DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
63 MarkTileDirtyByTile(tile);
65 _generating_world = false;
66 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
69 /** Scenario editor command that generates rocky areas */
70 static void GenerateRockyArea(TileIndex end, TileIndex start)
72 if (_game_mode != GM_EDITOR) return;
74 bool success = false;
75 TileArea ta(start, end);
77 TILE_AREA_LOOP(tile, ta) {
78 switch (GetTileType(tile)) {
79 case MP_TREES:
80 if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
81 FALLTHROUGH;
83 case MP_CLEAR:
84 MakeClear(tile, CLEAR_ROCKS, 3);
85 break;
87 default:
88 continue;
90 MarkTileDirtyByTile(tile);
91 success = true;
94 if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, end);
97 /**
98 * A central place to handle all X_AND_Y dragged GUI functions.
99 * @param proc Procedure related to the dragging
100 * @param start_tile Begin of the dragging
101 * @param end_tile End of the dragging
102 * @return Returns true if the action was found and handled, and false otherwise. This
103 * allows for additional implements that are more local. For example X_Y drag
104 * of convertrail which belongs in rail_gui.cpp and not terraform_gui.cpp
106 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
108 if (!_settings_game.construction.freeform_edges) {
109 /* When end_tile is MP_VOID, the error tile will not be visible to the
110 * user. This happens when terraforming at the southern border. */
111 if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
112 if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
115 switch (proc) {
116 case DDSP_DEMOLISH_AREA:
117 DoCommandP(end_tile, start_tile, _ctrl_pressed ? 1 : 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound_EXPLOSION);
118 break;
119 case DDSP_RAISE_AND_LEVEL_AREA:
120 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);
121 break;
122 case DDSP_LOWER_AND_LEVEL_AREA:
123 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);
124 break;
125 case DDSP_LEVEL_AREA:
126 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);
127 break;
128 case DDSP_CREATE_ROCKS:
129 GenerateRockyArea(end_tile, start_tile);
130 break;
131 case DDSP_CREATE_DESERT:
132 GenerateDesertArea(end_tile, start_tile);
133 break;
134 default:
135 return false;
138 return true;
142 * Start a drag for demolishing an area.
143 * @param tile Position of one corner.
145 void PlaceProc_DemolishArea(TileIndex tile)
147 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
150 /** Terra form toolbar managing class. */
151 struct TerraformToolbarWindow : Window {
152 int last_user_action; ///< Last started user action.
154 TerraformToolbarWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
156 /* This is needed as we like to have the tree available on OnInit. */
157 this->CreateNestedTree();
158 this->FinishInitNested(window_number);
159 this->last_user_action = WIDGET_LIST_END;
162 ~TerraformToolbarWindow()
166 void OnInit() override
168 /* Don't show the place object button when there are no objects to place. */
169 NWidgetStacked *show_object = this->GetWidget<NWidgetStacked>(WID_TT_SHOW_PLACE_OBJECT);
170 show_object->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE);
173 void OnClick(Point pt, int widget, int click_count) override
175 if (widget < WID_TT_BUTTONS_START) return;
177 switch (widget) {
178 case WID_TT_LOWER_LAND: // Lower land button
179 HandlePlacePushButton(this, WID_TT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT | HT_DIAGONAL);
180 this->last_user_action = widget;
181 break;
183 case WID_TT_RAISE_LAND: // Raise land button
184 HandlePlacePushButton(this, WID_TT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT | HT_DIAGONAL);
185 this->last_user_action = widget;
186 break;
188 case WID_TT_LEVEL_LAND: // Level land button
189 HandlePlacePushButton(this, WID_TT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
190 this->last_user_action = widget;
191 break;
193 case WID_TT_DEMOLISH: // Demolish aka dynamite button
194 HandlePlacePushButton(this, WID_TT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
195 this->last_user_action = widget;
196 break;
198 case WID_TT_BUY_LAND: // Buy land button
199 HandlePlacePushButton(this, WID_TT_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT);
200 this->last_user_action = widget;
201 break;
203 case WID_TT_PLANT_TREES: // Plant trees button
204 ShowBuildTreesToolbar();
205 break;
207 case WID_TT_PLACE_SIGN: // Place sign button
208 HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
209 this->last_user_action = widget;
210 break;
212 case WID_TT_PLACE_OBJECT: // Place object button
213 ShowBuildObjectPicker();
214 break;
216 default: NOT_REACHED();
220 void OnPlaceObject(Point pt, TileIndex tile) override
222 switch (this->last_user_action) {
223 case WID_TT_LOWER_LAND: // Lower land button
224 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
225 break;
227 case WID_TT_RAISE_LAND: // Raise land button
228 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
229 break;
231 case WID_TT_LEVEL_LAND: // Level land button
232 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
233 break;
235 case WID_TT_DEMOLISH: // Demolish aka dynamite button
236 PlaceProc_DemolishArea(tile);
237 break;
239 case WID_TT_BUY_LAND: // Buy land button
240 DoCommandP(tile, OBJECT_OWNED_LAND, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_PURCHASE_THIS_LAND), CcPlaySound_SPLAT_RAIL);
241 break;
243 case WID_TT_PLACE_SIGN: // Place sign button
244 PlaceProc_Sign(tile);
245 break;
247 default: NOT_REACHED();
251 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
253 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
256 Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
258 Point pt = GetToolbarAlignedWindowPosition(sm_width);
259 pt.y += sm_height;
260 return pt;
263 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
265 if (pt.x != -1) {
266 switch (select_proc) {
267 default: NOT_REACHED();
268 case DDSP_DEMOLISH_AREA:
269 case DDSP_RAISE_AND_LEVEL_AREA:
270 case DDSP_LOWER_AND_LEVEL_AREA:
271 case DDSP_LEVEL_AREA:
272 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
273 break;
278 void OnPlaceObjectAbort() override
280 this->RaiseButtons();
283 static HotkeyList hotkeys;
287 * Handler for global hotkeys of the TerraformToolbarWindow.
288 * @param hotkey Hotkey
289 * @return ES_HANDLED if hotkey was accepted.
291 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
293 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
294 Window *w = ShowTerraformToolbar(nullptr);
295 if (w == nullptr) return ES_NOT_HANDLED;
296 return w->OnHotkey(hotkey);
299 static Hotkey terraform_hotkeys[] = {
300 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
301 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
302 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
303 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
304 Hotkey('U', "buyland", WID_TT_BUY_LAND),
305 Hotkey('I', "trees", WID_TT_PLANT_TREES),
306 Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
307 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
308 HOTKEY_LIST_END
310 HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
312 static const NWidgetPart _nested_terraform_widgets[] = {
313 NWidget(NWID_HORIZONTAL),
314 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
315 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
316 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
317 EndContainer(),
318 NWidget(NWID_HORIZONTAL),
319 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
320 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
321 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
322 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
323 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
324 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
326 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
328 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
329 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
330 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
331 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
332 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
333 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
334 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
335 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
336 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
337 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
338 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
339 EndContainer(),
340 EndContainer(),
343 static WindowDesc _terraform_desc(
344 WDP_MANUAL, "toolbar_landscape", 0, 0,
345 WC_SCEN_LAND_GEN, WC_NONE,
346 WDF_CONSTRUCTION,
347 _nested_terraform_widgets, lengthof(_nested_terraform_widgets),
348 &TerraformToolbarWindow::hotkeys
352 * Show the toolbar for terraforming in the game.
353 * @param link The toolbar we might want to link to.
354 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
356 Window *ShowTerraformToolbar(Window *link)
358 if (!Company::IsValidID(_local_company)) return nullptr;
360 Window *w;
361 if (link == nullptr) {
362 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
363 return w;
366 /* Delete the terraform toolbar to place it again. */
367 DeleteWindowById(WC_SCEN_LAND_GEN, 0, true);
368 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
369 /* Align the terraform toolbar under the main toolbar. */
370 w->top -= w->height;
371 w->SetDirty();
372 /* Put the linked toolbar to the left / right of it. */
373 link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
374 link->top = w->top;
375 link->SetDirty();
377 return w;
380 static byte _terraform_size = 1;
383 * Raise/Lower a bigger chunk of land at the same time in the editor. When
384 * raising get the lowest point, when lowering the highest point, and set all
385 * tiles in the selection to that height.
386 * @todo : Incorporate into game itself to allow for ingame raising/lowering of
387 * larger chunks at the same time OR remove altogether, as we have 'level land' ?
388 * @param tile The top-left tile where the terraforming will start
389 * @param mode 1 for raising, 0 for lowering land
391 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
393 if (_terraform_size == 1) {
394 StringID msg =
395 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
397 DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
398 } else {
399 assert(_terraform_size != 0);
400 TileArea ta(tile, _terraform_size, _terraform_size);
401 ta.ClampToMap();
403 if (ta.w == 0 || ta.h == 0) return;
405 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
407 uint h;
408 if (mode != 0) {
409 /* Raise land */
410 h = MAX_TILE_HEIGHT;
411 TILE_AREA_LOOP(tile2, ta) {
412 h = min(h, TileHeight(tile2));
414 } else {
415 /* Lower land */
416 h = 0;
417 TILE_AREA_LOOP(tile2, ta) {
418 h = max(h, TileHeight(tile2));
422 TILE_AREA_LOOP(tile2, ta) {
423 if (TileHeight(tile2) == h) {
424 DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
430 static const int8 _multi_terraform_coords[][2] = {
431 { 0, -2},
432 { 4, 0}, { -4, 0}, { 0, 2},
433 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
434 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
435 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
436 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
437 {-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},
438 {-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},
441 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
442 NWidget(NWID_HORIZONTAL),
443 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
444 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
445 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
446 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
447 EndContainer(),
448 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
449 NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
450 NWidget(NWID_SPACER), SetFill(1, 0),
451 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
452 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
453 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
454 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
455 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
456 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
457 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
458 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
459 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
460 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
461 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
462 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
463 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
464 EndContainer(),
465 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
466 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
467 NWidget(NWID_SPACER), SetFill(1, 0),
468 EndContainer(),
469 NWidget(NWID_HORIZONTAL),
470 NWidget(NWID_SPACER), SetFill(1, 0),
471 NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
472 NWidget(NWID_SPACER), SetFill(1, 0),
473 NWidget(NWID_VERTICAL),
474 NWidget(NWID_SPACER), SetFill(0, 1),
475 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
476 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
477 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
478 NWidget(NWID_SPACER), SetFill(0, 1),
479 EndContainer(),
480 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
481 EndContainer(),
482 NWidget(NWID_SPACER), SetMinimalSize(0, 6),
483 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
484 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
485 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
486 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
487 EndContainer(),
491 * Callback function for the scenario editor 'reset landscape' confirmation window
492 * @param w Window unused
493 * @param confirmed boolean value, true when yes was clicked, false otherwise
495 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
497 if (confirmed) {
498 /* Set generating_world to true to get instant-green grass after removing
499 * company property. */
500 _generating_world = true;
502 /* Delete all companies */
503 for (Company *c : Company::Iterate()) {
504 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
505 delete c;
508 _generating_world = false;
510 /* Delete all station signs */
511 for (BaseStation *st : BaseStation::Iterate()) {
512 /* There can be buoys, remove them */
513 if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
514 if (!st->IsInUse()) delete st;
517 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
518 EngineOverrideManager::ResetToCurrentNewGRFConfig();
520 MarkWholeScreenDirty();
524 /** Landscape generation window handler in the scenario editor. */
525 struct ScenarioEditorLandscapeGenerationWindow : Window {
526 int last_user_action; ///< Last started user action.
528 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
530 this->CreateNestedTree();
531 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
532 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
533 this->FinishInitNested(window_number);
534 this->last_user_action = WIDGET_LIST_END;
537 void OnPaint() override
539 this->DrawWidgets();
541 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
542 SetTileSelectSize(_terraform_size, _terraform_size);
546 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
548 if (widget != WID_ETT_DOTS) return;
550 size->width = max<uint>(size->width, ScaleGUITrad(59));
551 size->height = max<uint>(size->height, ScaleGUITrad(31));
554 void DrawWidget(const Rect &r, int widget) const override
556 if (widget != WID_ETT_DOTS) return;
558 int center_x = RoundDivSU(r.left + r.right, 2);
559 int center_y = RoundDivSU(r.top + r.bottom, 2);
561 int n = _terraform_size * _terraform_size;
562 const int8 *coords = &_multi_terraform_coords[0][0];
564 assert(n != 0);
565 do {
566 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
567 coords += 2;
568 } while (--n);
571 void OnClick(Point pt, int widget, int click_count) override
573 if (widget < WID_ETT_BUTTONS_START) return;
575 switch (widget) {
576 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
577 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
578 this->last_user_action = widget;
579 break;
581 case WID_ETT_LOWER_LAND: // Lower land button
582 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
583 this->last_user_action = widget;
584 break;
586 case WID_ETT_RAISE_LAND: // Raise land button
587 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
588 this->last_user_action = widget;
589 break;
591 case WID_ETT_LEVEL_LAND: // Level land button
592 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
593 this->last_user_action = widget;
594 break;
596 case WID_ETT_PLACE_ROCKS: // Place rocks button
597 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
598 this->last_user_action = widget;
599 break;
601 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
602 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
603 this->last_user_action = widget;
604 break;
606 case WID_ETT_PLACE_OBJECT: // Place transmitter button
607 ShowBuildObjectPicker();
608 break;
610 case WID_ETT_INCREASE_SIZE:
611 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
612 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
613 this->HandleButtonClick(widget);
614 size += _terraform_size;
616 if (!IsInsideMM(size, 1, 8 + 1)) return;
617 _terraform_size = size;
619 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
620 this->SetDirty();
621 break;
624 case WID_ETT_NEW_SCENARIO: // gen random land
625 this->HandleButtonClick(widget);
626 ShowCreateScenario();
627 break;
629 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
630 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, nullptr, ResetLandscapeConfirmationCallback);
631 break;
633 default: NOT_REACHED();
637 void OnTimeout() override
639 for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
640 if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
641 if (this->IsWidgetLowered(i)) {
642 this->RaiseWidget(i);
643 this->SetWidgetDirty(i);
648 void OnPlaceObject(Point pt, TileIndex tile) override
650 switch (this->last_user_action) {
651 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
652 PlaceProc_DemolishArea(tile);
653 break;
655 case WID_ETT_LOWER_LAND: // Lower land button
656 CommonRaiseLowerBigLand(tile, 0);
657 break;
659 case WID_ETT_RAISE_LAND: // Raise land button
660 CommonRaiseLowerBigLand(tile, 1);
661 break;
663 case WID_ETT_LEVEL_LAND: // Level land button
664 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
665 break;
667 case WID_ETT_PLACE_ROCKS: // Place rocks button
668 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
669 break;
671 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
672 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
673 break;
675 default: NOT_REACHED();
679 void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
681 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
684 void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
686 if (pt.x != -1) {
687 switch (select_proc) {
688 default: NOT_REACHED();
689 case DDSP_CREATE_ROCKS:
690 case DDSP_CREATE_DESERT:
691 case DDSP_RAISE_AND_LEVEL_AREA:
692 case DDSP_LOWER_AND_LEVEL_AREA:
693 case DDSP_LEVEL_AREA:
694 case DDSP_DEMOLISH_AREA:
695 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
696 break;
701 void OnPlaceObjectAbort() override
703 this->RaiseButtons();
704 this->SetDirty();
707 static HotkeyList hotkeys;
711 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
712 * @param hotkey Hotkey
713 * @return ES_HANDLED if hotkey was accepted.
715 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
717 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
718 Window *w = ShowEditorTerraformToolbar();
719 if (w == nullptr) return ES_NOT_HANDLED;
720 return w->OnHotkey(hotkey);
723 static Hotkey terraform_editor_hotkeys[] = {
724 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
725 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
726 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
727 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
728 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
729 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
730 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
731 HOTKEY_LIST_END
734 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
736 static WindowDesc _scen_edit_land_gen_desc(
737 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
738 WC_SCEN_LAND_GEN, WC_NONE,
739 WDF_CONSTRUCTION,
740 _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
741 &ScenarioEditorLandscapeGenerationWindow::hotkeys
745 * Show the toolbar for terraforming in the scenario editor.
746 * @return The allocated toolbar if the window was newly opened, else \c nullptr.
748 Window *ShowEditorTerraformToolbar()
750 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);