OpenTTD: src/terraform_gui.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

terraform_gui.cpp
Go to the documentation of this file.
1 /* $Id: terraform_gui.cpp 27134 2015-02-01 20:54:24Z frosch $ */
2
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 */
9
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"
36
37 #include "widgets/terraform_widget.h"
38
39 #include "table/strings.h"
40
41 #include "safeguards.h"
42
43 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
44 {
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);
50 }
51 }
52
53
55 static void GenerateDesertArea(TileIndex end, TileIndex start)
56 {
57 if (_game_mode != GM_EDITOR) return;
58
59 _generating_world = true;
60
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);
66 }
67 _generating_world = false;
68 InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
69 }
70
72 static void GenerateRockyArea(TileIndex end, TileIndex start)
73 {
74 if (_game_mode != GM_EDITOR) return;
75
76 bool success = false;
77 TileArea ta(start, end);
78
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;
87
88 default: continue;
89 }
90 MarkTileDirtyByTile(tile);
91 success = true;
92 }
93
94 if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, end);
95 }
96
106 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
107 {
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);
113 }
114
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), CcPlaySound10);
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;
136 }
137
138 return true;
139 }
140
145 void PlaceProc_DemolishArea(TileIndex tile)
146 {
147 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
148 }
149
151 struct TerraformToolbarWindow : Window {
152 int last_user_action;
153
154 TerraformToolbarWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
155 {
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;
160 }
161
162 ~TerraformToolbarWindow()
163 {
164 }
165
166 virtual void OnInit()
167 {
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);
171 }
172
173 virtual void OnClick(Point pt, int widget, int click_count)
174 {
175 if (widget < WID_TT_BUTTONS_START) return;
176
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;
182
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;
187
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;
192
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;
197
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;
202
203 case WID_TT_PLANT_TREES: // Plant trees button
204 ShowBuildTreesToolbar();
205 break;
206
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;
211
212 case WID_TT_PLACE_OBJECT: // Place object button
213 /* Don't show the place object button when there are no objects to place. */
214 if (ObjectClass::GetUIClassCount() == 0) return;
215 if (HandlePlacePushButton(this, WID_TT_PLACE_OBJECT, SPR_CURSOR_TRANSMITTER, HT_RECT)) {
216 ShowBuildObjectPicker(this);
217 this->last_user_action = widget;
218 }
219 break;
220
221 default: NOT_REACHED();
222 }
223 }
224
225 virtual void OnPlaceObject(Point pt, TileIndex tile)
226 {
227 switch (this->last_user_action) {
228 case WID_TT_LOWER_LAND: // Lower land button
229 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
230 break;
231
232 case WID_TT_RAISE_LAND: // Raise land button
233 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
234 break;
235
236 case WID_TT_LEVEL_LAND: // Level land button
237 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
238 break;
239
240 case WID_TT_DEMOLISH: // Demolish aka dynamite button
241 PlaceProc_DemolishArea(tile);
242 break;
243
244 case WID_TT_BUY_LAND: // Buy land button
245 DoCommandP(tile, OBJECT_OWNED_LAND, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_PURCHASE_THIS_LAND), CcPlaySound1E);
246 break;
247
248 case WID_TT_PLACE_SIGN: // Place sign button
249 PlaceProc_Sign(tile);
250 break;
251
252 case WID_TT_PLACE_OBJECT: // Place object button
253 PlaceProc_Object(tile);
254 break;
255
256 default: NOT_REACHED();
257 }
258 }
259
260 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
261 {
262 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
263 }
264
265 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
266 {
267 Point pt = GetToolbarAlignedWindowPosition(sm_width);
268 pt.y += sm_height;
269 return pt;
270 }
271
272 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
273 {
274 if (pt.x != -1) {
275 switch (select_proc) {
276 default: NOT_REACHED();
277 case DDSP_DEMOLISH_AREA:
278 case DDSP_RAISE_AND_LEVEL_AREA:
279 case DDSP_LOWER_AND_LEVEL_AREA:
280 case DDSP_LEVEL_AREA:
281 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
282 break;
283 }
284 }
285 }
286
287 virtual void OnPlaceObjectAbort()
288 {
289 DeleteWindowById(WC_BUILD_OBJECT, 0);
290 this->RaiseButtons();
291 }
292
293 static HotkeyList hotkeys;
294 };
295
301 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
302 {
303 if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
304 Window *w = ShowTerraformToolbar(NULL);
305 if (w == NULL) return ES_NOT_HANDLED;
306 return w->OnHotkey(hotkey);
307 }
308
309 static Hotkey terraform_hotkeys[] = {
310 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
311 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
312 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
313 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
314 Hotkey('U', "buyland", WID_TT_BUY_LAND),
315 Hotkey('I', "trees", WID_TT_PLANT_TREES),
316 Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
317 Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
318 HOTKEY_LIST_END
319 };
320 HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
321
322 static const NWidgetPart _nested_terraform_widgets[] = {
323 NWidget(NWID_HORIZONTAL),
324 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
325 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
326 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
327 EndContainer(),
328 NWidget(NWID_HORIZONTAL),
329 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
330 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
331 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
332 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
333 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
334 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
335
336 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
337
338 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
339 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
340 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
341 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
342 NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
343 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
344 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
345 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
346 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
347 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
348 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
349 EndContainer(),
350 EndContainer(),
351 };
352
353 static WindowDesc _terraform_desc(
354 WDP_MANUAL, "toolbar_landscape", 0, 0,
355 WC_SCEN_LAND_GEN, WC_NONE,
356 WDF_CONSTRUCTION,
357 _nested_terraform_widgets, lengthof(_nested_terraform_widgets),
358 &TerraformToolbarWindow::hotkeys
359 );
360
366 Window *ShowTerraformToolbar(Window *link)
367 {
368 if (!Company::IsValidID(_local_company)) return NULL;
369
370 Window *w;
371 if (link == NULL) {
372 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
373 return w;
374 }
375
376 /* Delete the terraform toolbar to place it again. */
377 DeleteWindowById(WC_SCEN_LAND_GEN, 0, true);
378 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
379 /* Align the terraform toolbar under the main toolbar. */
380 w->top -= w->height;
381 w->SetDirty();
382 /* Put the linked toolbar to the left / right of it. */
383 link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
384 link->top = w->top;
385 link->SetDirty();
386
387 return w;
388 }
389
390 static byte _terraform_size = 1;
391
401 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
402 {
403 if (_terraform_size == 1) {
404 StringID msg =
405 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
406
407 DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
408 } else {
409 assert(_terraform_size != 0);
410 TileArea ta(tile, _terraform_size, _terraform_size);
411 ta.ClampToMap();
412
413 if (ta.w == 0 || ta.h == 0) return;
414
415 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
416
417 uint h;
418 if (mode != 0) {
419 /* Raise land */
420 h = MAX_TILE_HEIGHT;
421 TILE_AREA_LOOP(tile2, ta) {
422 h = min(h, TileHeight(tile2));
423 }
424 } else {
425 /* Lower land */
426 h = 0;
427 TILE_AREA_LOOP(tile2, ta) {
428 h = max(h, TileHeight(tile2));
429 }
430 }
431
432 TILE_AREA_LOOP(tile2, ta) {
433 if (TileHeight(tile2) == h) {
434 DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
435 }
436 }
437 }
438 }
439
440 static const int8 _multi_terraform_coords[][2] = {
441 { 0, -2},
442 { 4, 0}, { -4, 0}, { 0, 2},
443 { -8, 2}, { -4, 4}, { 0, 6}, { 4, 4}, { 8, 2},
444 {-12, 0}, { -8, -2}, { -4, -4}, { 0, -6}, { 4, -4}, { 8, -2}, { 12, 0},
445 {-16, 2}, {-12, 4}, { -8, 6}, { -4, 8}, { 0, 10}, { 4, 8}, { 8, 6}, { 12, 4}, { 16, 2},
446 {-20, 0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, { 0,-10}, { 4, -8}, { 8, -6}, { 12, -4}, { 16, -2}, { 20, 0},
447 {-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},
448 {-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},
449 };
450
451 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
452 NWidget(NWID_HORIZONTAL),
453 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
454 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
455 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
456 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
457 EndContainer(),
458 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
459 NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
460 NWidget(NWID_SPACER), SetFill(1, 0),
461 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
462 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
463 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
464 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
465 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
466 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
467 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
468 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
469 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
470 SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
471 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
472 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
473 SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
474 EndContainer(),
475 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
476 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
477 NWidget(NWID_SPACER), SetFill(1, 0),
478 EndContainer(),
479 NWidget(NWID_HORIZONTAL),
480 NWidget(NWID_SPACER), SetFill(1, 0),
481 NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
482 NWidget(NWID_SPACER), SetFill(1, 0),
483 NWidget(NWID_VERTICAL),
484 NWidget(NWID_SPACER), SetFill(0, 1),
485 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
486 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
487 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
488 NWidget(NWID_SPACER), SetFill(0, 1),
489 EndContainer(),
490 NWidget(NWID_SPACER), SetMinimalSize(2, 0),
491 EndContainer(),
492 NWidget(NWID_SPACER), SetMinimalSize(0, 6),
493 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
494 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
495 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
496 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
497 EndContainer(),
498 };
499
505 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
506 {
507 if (confirmed) {
508 /* Set generating_world to true to get instant-green grass after removing
509 * company property. */
510 _generating_world = true;
511
512 /* Delete all companies */
513 Company *c;
514 FOR_ALL_COMPANIES(c) {
515 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
516 delete c;
517 }
518
519 _generating_world = false;
520
521 /* Delete all station signs */
522 BaseStation *st;
523 FOR_ALL_BASE_STATIONS(st) {
524 /* There can be buoys, remove them */
525 if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
526 if (!st->IsInUse()) delete st;
527 }
528
529 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
530 EngineOverrideManager::ResetToCurrentNewGRFConfig();
531
532 MarkWholeScreenDirty();
533 }
534 }
535
537 struct ScenarioEditorLandscapeGenerationWindow : Window {
538 int last_user_action;
539
540 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
541 {
542 this->CreateNestedTree();
543 NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
544 show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
545 this->FinishInitNested(window_number);
546 this->last_user_action = WIDGET_LIST_END;
547 }
548
549 virtual void OnPaint()
550 {
551 this->DrawWidgets();
552
553 if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
554 SetTileSelectSize(_terraform_size, _terraform_size);
555 }
556 }
557
558 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
559 {
560 if (widget != WID_ETT_DOTS) return;
561
562 size->width = max<uint>(size->width, ScaleGUITrad(59));
563 size->height = max<uint>(size->height, ScaleGUITrad(31));
564 }
565
566 virtual void DrawWidget(const Rect &r, int widget) const
567 {
568 if (widget != WID_ETT_DOTS) return;
569
570 int center_x = RoundDivSU(r.left + r.right, 2);
571 int center_y = RoundDivSU(r.top + r.bottom, 2);
572
573 int n = _terraform_size * _terraform_size;
574 const int8 *coords = &_multi_terraform_coords[0][0];
575
576 assert(n != 0);
577 do {
578 DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + ScaleGUITrad(coords[0]), center_y + ScaleGUITrad(coords[1]));
579 coords += 2;
580 } while (--n);
581 }
582
583 virtual void OnClick(Point pt, int widget, int click_count)
584 {
585 if (widget < WID_ETT_BUTTONS_START) return;
586
587 switch (widget) {
588 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
589 HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
590 this->last_user_action = widget;
591 break;
592
593 case WID_ETT_LOWER_LAND: // Lower land button
594 HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
595 this->last_user_action = widget;
596 break;
597
598 case WID_ETT_RAISE_LAND: // Raise land button
599 HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
600 this->last_user_action = widget;
601 break;
602
603 case WID_ETT_LEVEL_LAND: // Level land button
604 HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
605 this->last_user_action = widget;
606 break;
607
608 case WID_ETT_PLACE_ROCKS: // Place rocks button
609 HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
610 this->last_user_action = widget;
611 break;
612
613 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
614 HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
615 this->last_user_action = widget;
616 break;
617
618 case WID_ETT_PLACE_OBJECT: // Place transmitter button
619 if (HandlePlacePushButton(this, WID_ETT_PLACE_OBJECT, SPR_CURSOR_TRANSMITTER, HT_RECT)) {
620 ShowBuildObjectPicker(this);
621 this->last_user_action = widget;
622 }
623 break;
624
625 case WID_ETT_INCREASE_SIZE:
626 case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
627 int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
628 this->HandleButtonClick(widget);
629 size += _terraform_size;
630
631 if (!IsInsideMM(size, 1, 8 + 1)) return;
632 _terraform_size = size;
633
634 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
635 this->SetDirty();
636 break;
637 }
638
639 case WID_ETT_NEW_SCENARIO: // gen random land
640 this->HandleButtonClick(widget);
641 ShowCreateScenario();
642 break;
643
644 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
645 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, NULL, ResetLandscapeConfirmationCallback);
646 break;
647
648 default: NOT_REACHED();
649 }
650 }
651
652 virtual void OnTimeout()
653 {
654 for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
655 if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
656 if (this->IsWidgetLowered(i)) {
657 this->RaiseWidget(i);
658 this->SetWidgetDirty(i);
659 }
660 }
661 }
662
663 virtual void OnPlaceObject(Point pt, TileIndex tile)
664 {
665 switch (this->last_user_action) {
666 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
667 PlaceProc_DemolishArea(tile);
668 break;
669
670 case WID_ETT_LOWER_LAND: // Lower land button
671 CommonRaiseLowerBigLand(tile, 0);
672 break;
673
674 case WID_ETT_RAISE_LAND: // Raise land button
675 CommonRaiseLowerBigLand(tile, 1);
676 break;
677
678 case WID_ETT_LEVEL_LAND: // Level land button
679 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
680 break;
681
682 case WID_ETT_PLACE_ROCKS: // Place rocks button
683 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
684 break;
685
686 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
687 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
688 break;
689
690 case WID_ETT_PLACE_OBJECT: // Place transmitter button
691 PlaceProc_Object(tile);
692 break;
693
694 default: NOT_REACHED();
695 }
696 }
697
698 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
699 {
700 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
701 }
702
703 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
704 {
705 if (pt.x != -1) {
706 switch (select_proc) {
707 default: NOT_REACHED();
708 case DDSP_CREATE_ROCKS:
709 case DDSP_CREATE_DESERT:
710 case DDSP_RAISE_AND_LEVEL_AREA:
711 case DDSP_LOWER_AND_LEVEL_AREA:
712 case DDSP_LEVEL_AREA:
713 case DDSP_DEMOLISH_AREA:
714 GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
715 break;
716 }
717 }
718 }
719
720 virtual void OnPlaceObjectAbort()
721 {
722 this->RaiseButtons();
723 this->SetDirty();
724 DeleteWindowById(WC_BUILD_OBJECT, 0);
725 }
726
727 static HotkeyList hotkeys;
728 };
729
735 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
736 {
737 if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
738 Window *w = ShowEditorTerraformToolbar();
739 if (w == NULL) return ES_NOT_HANDLED;
740 return w->OnHotkey(hotkey);
741 }
742
743 static Hotkey terraform_editor_hotkeys[] = {
744 Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
745 Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
746 Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
747 Hotkey('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
748 Hotkey('R', "rocky", WID_ETT_PLACE_ROCKS),
749 Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
750 Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
751 HOTKEY_LIST_END
752 };
753
754 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
755
756 static WindowDesc _scen_edit_land_gen_desc(
757 WDP_AUTO, "toolbar_landscape_scen", 0, 0,
758 WC_SCEN_LAND_GEN, WC_NONE,
759 WDF_CONSTRUCTION,
760 _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets),
761 &ScenarioEditorLandscapeGenerationWindow::hotkeys
762 );
763
768 Window *ShowEditorTerraformToolbar()
769 {
770 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);
771 }