(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / terraform_gui.cpp
blob6274de8cddbb8b01cd5d19ae2a14529063b4f18e
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file terraform_gui.cpp GUI related to terraforming the map. */
12 #include "stdafx.h"
13 #include "clear_map.h"
14 #include "company_func.h"
15 #include "company_base.h"
16 #include "gui.h"
17 #include "window_gui.h"
18 #include "window_func.h"
19 #include "viewport_func.h"
20 #include "command_func.h"
21 #include "signs_func.h"
22 #include "sound_func.h"
23 #include "base_station_base.h"
24 #include "textbuf_gui.h"
25 #include "genworld.h"
26 #include "tree_map.h"
27 #include "landscape_type.h"
28 #include "tilehighlight_func.h"
29 #include "strings_func.h"
30 #include "newgrf_object.h"
31 #include "object.h"
32 #include "hotkeys.h"
33 #include "engine_base.h"
34 #include "terraform_gui.h"
35 #include "zoom_func.h"
37 #include "widgets/terraform_widget.h"
39 #include "table/strings.h"
41 #include "safeguards.h"
43 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
45 if (result.Succeeded()) {
46 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
47 } else {
48 extern TileIndex _terraform_err_tile;
49 SetRedErrorSquare(_terraform_err_tile);
54 /** Scenario editor command that generates desert areas */
55 static void GenerateDesertArea(TileIndex end, TileIndex start)
57 if (_game_mode != GM_EDITOR) return;
59 _generating_world = true;
61 TileArea ta(start, end);
62 TILE_AREA_LOOP(tile, ta) {
63 SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
64 DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
65 MarkTileDirtyByTile(tile);
67 _generating_world = false;
68 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
71 /** Scenario editor command that generates rocky areas */
72 static void GenerateRockyArea(TileIndex end, TileIndex start)
74 if (_game_mode != GM_EDITOR) return;
76 bool success = false;
77 TileArea ta(start, end);
79 TILE_AREA_LOOP(tile, ta) {
80 switch (GetTileType(tile)) {
81 case MP_TREES:
82 if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
83 /* FALL THROUGH */
84 case MP_CLEAR:
85 MakeClear(tile, CLEAR_ROCKS, 3);
86 break;
88 default: 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 virtual void OnInit()
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 virtual void OnClick(Point pt, int widget, int click_count)
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 virtual void OnPlaceObject(Point pt, TileIndex tile)
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 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
253 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
256 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
258 Point pt = GetToolbarAlignedWindowPosition(sm_width);
259 pt.y += sm_height;
260 return pt;
263 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
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 virtual void OnPlaceObjectAbort()
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(NULL);
295 if (w == NULL) 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 NULL.
356 Window *ShowTerraformToolbar(Window *link)
358 if (!Company::IsValidID(_local_company)) return NULL;
360 Window *w;
361 if (link == NULL) {
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 Company *c;
504 FOR_ALL_COMPANIES(c) {
505 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
506 delete c;
509 _generating_world = false;
511 /* Delete all station signs */
512 BaseStation *st;
513 FOR_ALL_BASE_STATIONS(st) {
514 /* There can be buoys, remove them */
515 if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
516 if (!st->IsInUse()) delete st;
519 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
520 EngineOverrideManager::ResetToCurrentNewGRFConfig();
522 MarkWholeScreenDirty();
526 /** Landscape generation window handler in the scenario editor. */
527 struct ScenarioEditorLandscapeGenerationWindow : Window {
528 int last_user_action; ///< Last started user action.
530 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
532 this->CreateNestedTree();
533 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
534 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
535 this->FinishInitNested(window_number);
536 this->last_user_action = WIDGET_LIST_END;
539 virtual void OnPaint()
541 this->DrawWidgets();
543 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
544 SetTileSelectSize(_terraform_size, _terraform_size);
548 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
550 if (widget != WID_ETT_DOTS) return;
552 size->width = max<uint>(size->width, ScaleGUITrad(59));
553 size->height = max<uint>(size->height, ScaleGUITrad(31));
556 virtual void DrawWidget(const Rect &r, int widget) const
558 if (widget != WID_ETT_DOTS) return;
560 int center_x = RoundDivSU(r.left + r.right, 2);
561 int center_y = RoundDivSU(r.top + r.bottom, 2);
563 int n = _terraform_size * _terraform_size;
564 const int8 *coords = &_multi_terraform_coords[0][0];
566 assert(n != 0);
567 do {
568 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
569 coords += 2;
570 } while (--n);
573 virtual void OnClick(Point pt, int widget, int click_count)
575 if (widget < WID_ETT_BUTTONS_START) return;
577 switch (widget) {
578 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
579 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
580 this->last_user_action = widget;
581 break;
583 case WID_ETT_LOWER_LAND: // Lower land button
584 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
585 this->last_user_action = widget;
586 break;
588 case WID_ETT_RAISE_LAND: // Raise land button
589 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
590 this->last_user_action = widget;
591 break;
593 case WID_ETT_LEVEL_LAND: // Level land button
594 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
595 this->last_user_action = widget;
596 break;
598 case WID_ETT_PLACE_ROCKS: // Place rocks button
599 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
600 this->last_user_action = widget;
601 break;
603 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
604 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
605 this->last_user_action = widget;
606 break;
608 case WID_ETT_PLACE_OBJECT: // Place transmitter button
609 ShowBuildObjectPicker();
610 break;
612 case WID_ETT_INCREASE_SIZE:
613 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
614 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
615 this->HandleButtonClick(widget);
616 size += _terraform_size;
618 if (!IsInsideMM(size, 1, 8 + 1)) return;
619 _terraform_size = size;
621 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
622 this->SetDirty();
623 break;
626 case WID_ETT_NEW_SCENARIO: // gen random land
627 this->HandleButtonClick(widget);
628 ShowCreateScenario();
629 break;
631 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
632 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, NULL, ResetLandscapeConfirmationCallback);
633 break;
635 default: NOT_REACHED();
639 virtual void OnTimeout()
641 for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
642 if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
643 if (this->IsWidgetLowered(i)) {
644 this->RaiseWidget(i);
645 this->SetWidgetDirty(i);
650 virtual void OnPlaceObject(Point pt, TileIndex tile)
652 switch (this->last_user_action) {
653 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
654 PlaceProc_DemolishArea(tile);
655 break;
657 case WID_ETT_LOWER_LAND: // Lower land button
658 CommonRaiseLowerBigLand(tile, 0);
659 break;
661 case WID_ETT_RAISE_LAND: // Raise land button
662 CommonRaiseLowerBigLand(tile, 1);
663 break;
665 case WID_ETT_LEVEL_LAND: // Level land button
666 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
667 break;
669 case WID_ETT_PLACE_ROCKS: // Place rocks button
670 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
671 break;
673 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
674 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
675 break;
677 default: NOT_REACHED();
681 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
683 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
686 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
688 if (pt.x != -1) {
689 switch (select_proc) {
690 default: NOT_REACHED();
691 case DDSP_CREATE_ROCKS:
692 case DDSP_CREATE_DESERT:
693 case DDSP_RAISE_AND_LEVEL_AREA:
694 case DDSP_LOWER_AND_LEVEL_AREA:
695 case DDSP_LEVEL_AREA:
696 case DDSP_DEMOLISH_AREA:
697 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
698 break;
703 virtual void OnPlaceObjectAbort()
705 this->RaiseButtons();
706 this->SetDirty();
709 static HotkeyList hotkeys;
713 * Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
714 * @param hotkey Hotkey
715 * @return ES_HANDLED if hotkey was accepted.
717 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
719 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
720 Window *w = ShowEditorTerraformToolbar();
721 if (w == NULL) return ES_NOT_HANDLED;
722 return w->OnHotkey(hotkey);
725 static Hotkey terraform_editor_hotkeys[] = {
726 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
727 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
728 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
729 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
730 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
731 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
732 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
733 HOTKEY_LIST_END
736 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
738 static WindowDesc _scen_edit_land_gen_desc(
739 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
740 WC_SCEN_LAND_GEN, WC_NONE,
741 WDF_CONSTRUCTION,
742 _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
743 &ScenarioEditorLandscapeGenerationWindow::hotkeys
747 * Show the toolbar for terraforming in the scenario editor.
748 * @return The allocated toolbar if the window was newly opened, else \c NULL.
750 Window *ShowEditorTerraformToolbar()
752 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);