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 27397 2015-09-13 12:04:47Z alberth $ */
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 ShowBuildObjectPicker();
214 break;
215
216 default: NOT_REACHED();
217 }
218 }
219
220 virtual void OnPlaceObject(Point pt, TileIndex tile)
221 {
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;
226
227 case WID_TT_RAISE_LAND: // Raise land button
228 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
229 break;
230
231 case WID_TT_LEVEL_LAND: // Level land button
232 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
233 break;
234
235 case WID_TT_DEMOLISH: // Demolish aka dynamite button
236 PlaceProc_DemolishArea(tile);
237 break;
238
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), CcPlaySound1E);
241 break;
242
243 case WID_TT_PLACE_SIGN: // Place sign button
244 PlaceProc_Sign(tile);
245 break;
246
247 default: NOT_REACHED();
248 }
249 }
250
251 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
252 {
253 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
254 }
255
256 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
257 {
258 Point pt = GetToolbarAlignedWindowPosition(sm_width);
259 pt.y += sm_height;
260 return pt;
261 }
262
263 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
264 {
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;
274 }
275 }
276 }
277
278 virtual void OnPlaceObjectAbort()
279 {
280 this->RaiseButtons();
281 }
282
283 static HotkeyList hotkeys;
284 };
285
291 static EventState TerraformToolbarGlobalHotkeys(int hotkey)
292 {
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);
297 }
298
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
309 };
310 HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
311
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),
325
326 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
327
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(),
341 };
342
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
349 );
350
356 Window *ShowTerraformToolbar(Window *link)
357 {
358 if (!Company::IsValidID(_local_company)) return NULL;
359
360 Window *w;
361 if (link == NULL) {
362 w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
363 return w;
364 }
365
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();
376
377 return w;
378 }
379
380 static byte _terraform_size = 1;
381
391 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
392 {
393 if (_terraform_size == 1) {
394 StringID msg =
395 mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
396
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();
402
403 if (ta.w == 0 || ta.h == 0) return;
404
405 if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT_OTHER, tile);
406
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));
413 }
414 } else {
415 /* Lower land */
416 h = 0;
417 TILE_AREA_LOOP(tile2, ta) {
418 h = max(h, TileHeight(tile2));
419 }
420 }
421
422 TILE_AREA_LOOP(tile2, ta) {
423 if (TileHeight(tile2) == h) {
424 DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
425 }
426 }
427 }
428 }
429
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},
439 };
440
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(),
488 };
489
495 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
496 {
497 if (confirmed) {
498 /* Set generating_world to true to get instant-green grass after removing
499 * company property. */
500 _generating_world = true;
501
502 /* Delete all companies */
503 Company *c;
504 FOR_ALL_COMPANIES(c) {
505 ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
506 delete c;
507 }
508
509 _generating_world = false;
510
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;
517 }
518
519 /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
520 EngineOverrideManager::ResetToCurrentNewGRFConfig();
521
522 MarkWholeScreenDirty();
523 }
524 }
525
527 struct ScenarioEditorLandscapeGenerationWindow : Window {
528 int last_user_action;
529
530 ScenarioEditorLandscapeGenerationWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
531 {
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;
537 }
538
539 virtual void OnPaint()
540 {
541 this->DrawWidgets();
542
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);
545 }
546 }
547
548 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
549 {
550 if (widget != WID_ETT_DOTS) return;
551
552 size->width = max<uint>(size->width, ScaleGUITrad(59));
553 size->height = max<uint>(size->height, ScaleGUITrad(31));
554 }
555
556 virtual void DrawWidget(const Rect &r, int widget) const
557 {
558 if (widget != WID_ETT_DOTS) return;
559
560 int center_x = RoundDivSU(r.left + r.right, 2);
561 int center_y = RoundDivSU(r.top + r.bottom, 2);
562
563 int n = _terraform_size * _terraform_size;
564 const int8 *coords = &_multi_terraform_coords[0][0];
565
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);
571 }
572
573 virtual void OnClick(Point pt, int widget, int click_count)
574 {
575 if (widget < WID_ETT_BUTTONS_START) return;
576
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;
582
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;
587
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;
592
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;
597
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;
602
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;
607
608 case WID_ETT_PLACE_OBJECT: // Place transmitter button
609 ShowBuildObjectPicker();
610 break;
611
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;
617
618 if (!IsInsideMM(size, 1, 8 + 1)) return;
619 _terraform_size = size;
620
621 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
622 this->SetDirty();
623 break;
624 }
625
626 case WID_ETT_NEW_SCENARIO: // gen random land
627 this->HandleButtonClick(widget);
628 ShowCreateScenario();
629 break;
630
631 case WID_ETT_RESET_LANDSCAPE: // Reset landscape
632 ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, NULL, ResetLandscapeConfirmationCallback);
633 break;
634
635 default: NOT_REACHED();
636 }
637 }
638
639 virtual void OnTimeout()
640 {
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);
646 }
647 }
648 }
649
650 virtual void OnPlaceObject(Point pt, TileIndex tile)
651 {
652 switch (this->last_user_action) {
653 case WID_ETT_DEMOLISH: // Demolish aka dynamite button
654 PlaceProc_DemolishArea(tile);
655 break;
656
657 case WID_ETT_LOWER_LAND: // Lower land button
658 CommonRaiseLowerBigLand(tile, 0);
659 break;
660
661 case WID_ETT_RAISE_LAND: // Raise land button
662 CommonRaiseLowerBigLand(tile, 1);
663 break;
664
665 case WID_ETT_LEVEL_LAND: // Level land button
666 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
667 break;
668
669 case WID_ETT_PLACE_ROCKS: // Place rocks button
670 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
671 break;
672
673 case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
674 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
675 break;
676
677 default: NOT_REACHED();
678 }
679 }
680
681 virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
682 {
683 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
684 }
685
686 virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
687 {
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;
699 }
700 }
701 }
702
703 virtual void OnPlaceObjectAbort()
704 {
705 this->RaiseButtons();
706 this->SetDirty();
707 }
708
709 static HotkeyList hotkeys;
710 };
711
717 static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
718 {
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);
723 }
724
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
734 };
735
736 HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
737
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
744 );
745
750 Window *ShowEditorTerraformToolbar()
751 {
752 return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);
753 }