# terraform_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: terraform_gui.cpp 24846 2012-12-23 21:09:09Z frosch $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "clear_map.h"
    00014 #include "company_func.h"
    00015 #include "company_base.h"
    00016 #include "gui.h"
    00017 #include "window_gui.h"
    00018 #include "window_func.h"
    00019 #include "viewport_func.h"
    00020 #include "command_func.h"
    00021 #include "signs_func.h"
    00022 #include "sound_func.h"
    00023 #include "base_station_base.h"
    00024 #include "textbuf_gui.h"
    00025 #include "genworld.h"
    00026 #include "tree_map.h"
    00027 #include "landscape_type.h"
    00028 #include "tilehighlight_func.h"
    00029 #include "strings_func.h"
    00030 #include "newgrf_object.h"
    00031 #include "object.h"
    00032 #include "hotkeys.h"
    00033 #include "engine_base.h"
    00034
    00035 #include "widgets/terraform_widget.h"
    00036
    00037 #include "table/strings.h"
    00038
    00039 void CcTerraform(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00040 {
    00041   if (result.Succeeded()) {
    00042     if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT, tile);
    00043   } else {
    00044     extern TileIndex _terraform_err_tile;
    00045     SetRedErrorSquare(_terraform_err_tile);
    00046   }
    00047 }
    00048
    00049
    00051 static void GenerateDesertArea(TileIndex end, TileIndex start)
    00052 {
    00053   if (_game_mode != GM_EDITOR) return;
    00054
    00055   _generating_world = true;
    00056
    00057   TileArea ta(start, end);
    00058   TILE_AREA_LOOP(tile, ta) {
    00059     SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
    00060     DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
    00061     MarkTileDirtyByTile(tile);
    00062   }
    00063   _generating_world = false;
    00064   InvalidateWindowClassesData(WC_TOWN_VIEW, 0);
    00065 }
    00066
    00068 static void GenerateRockyArea(TileIndex end, TileIndex start)
    00069 {
    00070   if (_game_mode != GM_EDITOR) return;
    00071
    00072   bool success = false;
    00073   TileArea ta(start, end);
    00074
    00075   TILE_AREA_LOOP(tile, ta) {
    00076     switch (GetTileType(tile)) {
    00077       case MP_TREES:
    00078         if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
    00079         /* FALL THROUGH */
    00080       case MP_CLEAR:
    00081         MakeClear(tile, CLEAR_ROCKS, 3);
    00082         break;
    00083
    00084       default: continue;
    00085     }
    00086     MarkTileDirtyByTile(tile);
    00087     success = true;
    00088   }
    00089
    00090   if (success && _settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT, end);
    00091 }
    00092
    00102 bool GUIPlaceProcDragXY(ViewportDragDropSelectionProcess proc, TileIndex start_tile, TileIndex end_tile)
    00103 {
    00104   if (!_settings_game.construction.freeform_edges) {
    00105     /* When end_tile is MP_VOID, the error tile will not be visible to the
    00106      * user. This happens when terraforming at the southern border. */
    00107     if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
    00108     if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
    00109   }
    00110
    00111   switch (proc) {
    00112     case DDSP_DEMOLISH_AREA:
    00113       DoCommandP(end_tile, start_tile, _ctrl_pressed ? 1 : 0, CMD_CLEAR_AREA | CMD_MSG(STR_ERROR_CAN_T_CLEAR_THIS_AREA), CcPlaySound10);
    00114       break;
    00115     case DDSP_RAISE_AND_LEVEL_AREA:
    00116       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);
    00117       break;
    00118     case DDSP_LOWER_AND_LEVEL_AREA:
    00119       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);
    00120       break;
    00121     case DDSP_LEVEL_AREA:
    00122       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);
    00123       break;
    00124     case DDSP_CREATE_ROCKS:
    00125       GenerateRockyArea(end_tile, start_tile);
    00126       break;
    00127     case DDSP_CREATE_DESERT:
    00128       GenerateDesertArea(end_tile, start_tile);
    00129       break;
    00130     default:
    00131       return false;
    00132   }
    00133
    00134   return true;
    00135 }
    00136
    00141 void PlaceProc_DemolishArea(TileIndex tile)
    00142 {
    00143   VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_DEMOLISH_AREA);
    00144 }
    00145
    00147 struct TerraformToolbarWindow : Window {
    00148   int last_user_action;
    00149
    00150   TerraformToolbarWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00151   {
    00152     /* This is needed as we like to have the tree available on OnInit. */
    00153     this->CreateNestedTree(desc);
    00154     this->FinishInitNested(desc, window_number);
    00155     this->last_user_action = WIDGET_LIST_END;
    00156   }
    00157
    00158   ~TerraformToolbarWindow()
    00159   {
    00160   }
    00161
    00162   virtual void OnInit()
    00163   {
    00164     /* Don't show the place object button when there are no objects to place. */
    00165     NWidgetStacked *show_object = this->GetWidget<NWidgetStacked>(WID_TT_SHOW_PLACE_OBJECT);
    00166     show_object->SetDisplayedPlane(ObjectClass::GetUIClassCount() != 0 ? 0 : SZSP_NONE);
    00167   }
    00168
    00169   virtual void OnClick(Point pt, int widget, int click_count)
    00170   {
    00171     if (widget < WID_TT_BUTTONS_START) return;
    00172
    00173     switch (widget) {
    00174       case WID_TT_LOWER_LAND: // Lower land button
    00175         HandlePlacePushButton(this, WID_TT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT | HT_DIAGONAL);
    00176         this->last_user_action = widget;
    00177         break;
    00178
    00179       case WID_TT_RAISE_LAND: // Raise land button
    00180         HandlePlacePushButton(this, WID_TT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT | HT_DIAGONAL);
    00181         this->last_user_action = widget;
    00182         break;
    00183
    00184       case WID_TT_LEVEL_LAND: // Level land button
    00185         HandlePlacePushButton(this, WID_TT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
    00186         this->last_user_action = widget;
    00187         break;
    00188
    00189       case WID_TT_DEMOLISH: // Demolish aka dynamite button
    00190         HandlePlacePushButton(this, WID_TT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
    00191         this->last_user_action = widget;
    00192         break;
    00193
    00194       case WID_TT_BUY_LAND: // Buy land button
    00195         HandlePlacePushButton(this, WID_TT_BUY_LAND, SPR_CURSOR_BUY_LAND, HT_RECT);
    00196         this->last_user_action = widget;
    00197         break;
    00198
    00199       case WID_TT_PLANT_TREES: // Plant trees button
    00200         ShowBuildTreesToolbar();
    00201         break;
    00202
    00203       case WID_TT_PLACE_SIGN: // Place sign button
    00204         HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
    00205         this->last_user_action = widget;
    00206         break;
    00207
    00208       case WID_TT_PLACE_OBJECT: // Place object button
    00209         /* Don't show the place object button when there are no objects to place. */
    00210         if (ObjectClass::GetUIClassCount() == 0) return;
    00211         if (HandlePlacePushButton(this, WID_TT_PLACE_OBJECT, SPR_CURSOR_TRANSMITTER, HT_RECT)) {
    00212           ShowBuildObjectPicker(this);
    00213           this->last_user_action = widget;
    00214         }
    00215         break;
    00216
    00217       default: NOT_REACHED();
    00218     }
    00219   }
    00220
    00221   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00222   {
    00223     int num = CheckHotkeyMatch(terraform_hotkeys, keycode, this);
    00224     if (num == -1) return ES_NOT_HANDLED;
    00225     this->OnClick(Point(), num, 1);
    00226     return ES_HANDLED;
    00227   }
    00228
    00229   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00230   {
    00231     switch (this->last_user_action) {
    00232       case WID_TT_LOWER_LAND: // Lower land button
    00233         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
    00234         break;
    00235
    00236       case WID_TT_RAISE_LAND: // Raise land button
    00237         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
    00238         break;
    00239
    00240       case WID_TT_LEVEL_LAND: // Level land button
    00241         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
    00242         break;
    00243
    00244       case WID_TT_DEMOLISH: // Demolish aka dynamite button
    00245         PlaceProc_DemolishArea(tile);
    00246         break;
    00247
    00248       case WID_TT_BUY_LAND: // Buy land button
    00249         DoCommandP(tile, OBJECT_OWNED_LAND, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_PURCHASE_THIS_LAND), CcPlaySound1E);
    00250         break;
    00251
    00252       case WID_TT_PLACE_SIGN: // Place sign button
    00253         PlaceProc_Sign(tile);
    00254         break;
    00255
    00256       case WID_TT_PLACE_OBJECT: // Place object button
    00257         PlaceProc_Object(tile);
    00258         break;
    00259
    00260       default: NOT_REACHED();
    00261     }
    00262   }
    00263
    00264   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
    00265   {
    00266     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
    00267   }
    00268
    00269   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00270   {
    00271     Point pt = GetToolbarAlignedWindowPosition(sm_width);
    00272     pt.y += sm_height;
    00273     return pt;
    00274   }
    00275
    00276   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
    00277   {
    00278     if (pt.x != -1) {
    00279       switch (select_proc) {
    00280         default: NOT_REACHED();
    00281         case DDSP_DEMOLISH_AREA:
    00282         case DDSP_RAISE_AND_LEVEL_AREA:
    00283         case DDSP_LOWER_AND_LEVEL_AREA:
    00284         case DDSP_LEVEL_AREA:
    00285           GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
    00286           break;
    00287       }
    00288     }
    00289   }
    00290
    00291   virtual void OnPlaceObjectAbort()
    00292   {
    00293     DeleteWindowById(WC_BUILD_OBJECT, 0);
    00294     this->RaiseButtons();
    00295   }
    00296
    00297   static Hotkey<TerraformToolbarWindow> terraform_hotkeys[];
    00298 };
    00299
    00300 Hotkey<TerraformToolbarWindow> TerraformToolbarWindow::terraform_hotkeys[] = {
    00301   Hotkey<TerraformToolbarWindow>('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
    00302   Hotkey<TerraformToolbarWindow>('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
    00303   Hotkey<TerraformToolbarWindow>('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
    00304   Hotkey<TerraformToolbarWindow>('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
    00305   Hotkey<TerraformToolbarWindow>('U', "buyland", WID_TT_BUY_LAND),
    00306   Hotkey<TerraformToolbarWindow>('I', "trees", WID_TT_PLANT_TREES),
    00307   Hotkey<TerraformToolbarWindow>('O', "placesign", WID_TT_PLACE_SIGN),
    00308   Hotkey<TerraformToolbarWindow>('P', "placeobject", WID_TT_PLACE_OBJECT),
    00309   HOTKEY_LIST_END(TerraformToolbarWindow)
    00310 };
    00311 Hotkey<TerraformToolbarWindow> *_terraform_hotkeys = TerraformToolbarWindow::terraform_hotkeys;
    00312
    00313 static const NWidgetPart _nested_terraform_widgets[] = {
    00314   NWidget(NWID_HORIZONTAL),
    00315     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00316     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00317     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00318   EndContainer(),
    00319   NWidget(NWID_HORIZONTAL),
    00320     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
    00321                 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
    00322     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
    00323                 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
    00324     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
    00325                 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
    00326
    00327     NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
    00328
    00329     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
    00330                 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
    00331     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
    00332                 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
    00333     NWidget(WWT_PUSHIMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
    00334                 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
    00335     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
    00336                 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
    00337     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
    00338       NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
    00339                 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
    00340     EndContainer(),
    00341   EndContainer(),
    00342 };
    00343
    00344 static const WindowDesc _terraform_desc(
    00345   WDP_MANUAL, 0, 0,
    00346   WC_SCEN_LAND_GEN, WC_NONE,
    00347   WDF_CONSTRUCTION,
    00348   _nested_terraform_widgets, lengthof(_nested_terraform_widgets)
    00349 );
    00350
    00356 Window *ShowTerraformToolbar(Window *link)
    00357 {
    00358   if (!Company::IsValidID(_local_company)) return NULL;
    00359
    00360   Window *w;
    00361   if (link == NULL) {
    00362     w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
    00363     return w;
    00364   }
    00365
    00366   /* Delete the terraform toolbar to place it again. */
    00367   DeleteWindowById(WC_SCEN_LAND_GEN, 0, true);
    00368   w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
    00369   /* Align the terraform toolbar under the main toolbar. */
    00370   w->top -= w->height;
    00371   w->SetDirty();
    00372   /* Put the linked toolbar to the left / right of it. */
    00373   link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
    00374   link->top  = w->top;
    00375   link->SetDirty();
    00376
    00377   return w;
    00378 }
    00379
    00380 EventState TerraformToolbarGlobalHotkeys(uint16 key, uint16 keycode)
    00381 {
    00382   int num = CheckHotkeyMatch<TerraformToolbarWindow>(_terraform_hotkeys, keycode, NULL, true);
    00383   if (num == -1) return ES_NOT_HANDLED;
    00384   Window *w = ShowTerraformToolbar(NULL);
    00385   if (w == NULL) return ES_NOT_HANDLED;
    00386   return w->OnKeyPress(key, keycode);
    00387 }
    00388
    00389 static byte _terraform_size = 1;
    00390
    00400 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
    00401 {
    00402   if (_terraform_size == 1) {
    00403     StringID msg =
    00404       mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
    00405
    00406     DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
    00407   } else {
    00408     assert(_terraform_size != 0);
    00409     TileArea ta(tile, _terraform_size, _terraform_size);
    00410     ta.ClampToMap();
    00411
    00412     if (ta.w == 0 || ta.h == 0) return;
    00413
    00414     if (_settings_client.sound.confirm) SndPlayTileFx(SND_1F_SPLAT, tile);
    00415
    00416     uint h;
    00417     if (mode != 0) {
    00418       /* Raise land */
    00419       h = MAX_TILE_HEIGHT;
    00420       TILE_AREA_LOOP(tile2, ta) {
    00421         h = min(h, TileHeight(tile2));
    00422       }
    00423     } else {
    00424       /* Lower land */
    00425       h = 0;
    00426       TILE_AREA_LOOP(tile2, ta) {
    00427         h = max(h, TileHeight(tile2));
    00428       }
    00429     }
    00430
    00431     TILE_AREA_LOOP(tile2, ta) {
    00432       if (TileHeight(tile2) == h) {
    00433         DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
    00434       }
    00435     }
    00436   }
    00437 }
    00438
    00439 static const int8 _multi_terraform_coords[][2] = {
    00440   {  0, -2},
    00441   {  4,  0}, { -4,  0}, {  0,  2},
    00442   { -8,  2}, { -4,  4}, {  0,  6}, {  4,  4}, {  8,  2},
    00443   {-12,  0}, { -8, -2}, { -4, -4}, {  0, -6}, {  4, -4}, {  8, -2}, { 12,  0},
    00444   {-16,  2}, {-12,  4}, { -8,  6}, { -4,  8}, {  0, 10}, {  4,  8}, {  8,  6}, { 12,  4}, { 16,  2},
    00445   {-20,  0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, {  0,-10}, {  4, -8}, {  8, -6}, { 12, -4}, { 16, -2}, { 20,  0},
    00446   {-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},
    00447   {-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},
    00448 };
    00449
    00450 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
    00451   NWidget(NWID_HORIZONTAL),
    00452     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00453     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00454     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00455     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00456   EndContainer(),
    00457   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00458     NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
    00459       NWidget(NWID_SPACER), SetFill(1, 0),
    00460       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
    00461                     SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
    00462       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
    00463                     SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
    00464       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
    00465                     SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
    00466       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
    00467                     SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
    00468       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
    00469                     SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
    00470       NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
    00471         NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
    00472                       SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
    00473       EndContainer(),
    00474       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
    00475                     SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
    00476       NWidget(NWID_SPACER), SetFill(1, 0),
    00477     EndContainer(),
    00478     NWidget(NWID_HORIZONTAL),
    00479       NWidget(NWID_SPACER), SetFill(1, 0),
    00480       NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
    00481       NWidget(NWID_SPACER), SetFill(1, 0),
    00482       NWidget(NWID_VERTICAL),
    00483         NWidget(NWID_SPACER), SetFill(0, 1),
    00484         NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
    00485         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00486         NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
    00487         NWidget(NWID_SPACER), SetFill(0, 1),
    00488       EndContainer(),
    00489       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00490     EndContainer(),
    00491     NWidget(NWID_SPACER), SetMinimalSize(0, 6),
    00492     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
    00493                 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
    00494     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
    00495                 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
    00496   EndContainer(),
    00497 };
    00498
    00504 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
    00505 {
    00506   if (confirmed) {
    00507     /* Set generating_world to true to get instant-green grass after removing
    00508      * company property. */
    00509     _generating_world = true;
    00510
    00511     /* Delete all companies */
    00512     Company *c;
    00513     FOR_ALL_COMPANIES(c) {
    00514       ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
    00515       delete c;
    00516     }
    00517
    00518     _generating_world = false;
    00519
    00520     /* Delete all station signs */
    00521     BaseStation *st;
    00522     FOR_ALL_BASE_STATIONS(st) {
    00523       /* There can be buoys, remove them */
    00524       if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
    00525       if (!st->IsInUse()) delete st;
    00526     }
    00527
    00528     /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
    00529     EngineOverrideManager::ResetToCurrentNewGRFConfig();
    00530
    00531     MarkWholeScreenDirty();
    00532   }
    00533 }
    00534
    00536 struct ScenarioEditorLandscapeGenerationWindow : Window {
    00537   int last_user_action;
    00538
    00539   ScenarioEditorLandscapeGenerationWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00540   {
    00541     this->CreateNestedTree(desc);
    00542     NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
    00543     show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
    00544     this->FinishInitNested(desc, window_number);
    00545     this->last_user_action = WIDGET_LIST_END;
    00546   }
    00547
    00548   virtual void OnPaint()
    00549   {
    00550     this->DrawWidgets();
    00551
    00552     if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
    00553       SetTileSelectSize(_terraform_size, _terraform_size);
    00554     }
    00555   }
    00556
    00557   virtual void DrawWidget(const Rect &r, int widget) const
    00558   {
    00559     if (widget != WID_ETT_DOTS) return;
    00560
    00561     int center_x = RoundDivSU(r.left + r.right, 2);
    00562     int center_y = RoundDivSU(r.top + r.bottom, 2);
    00563
    00564     int n = _terraform_size * _terraform_size;
    00565     const int8 *coords = &_multi_terraform_coords[0][0];
    00566
    00567     assert(n != 0);
    00568     do {
    00569       DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + coords[0], center_y + coords[1]);
    00570       coords += 2;
    00571     } while (--n);
    00572   }
    00573
    00574   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00575   {
    00576     int num = CheckHotkeyMatch(terraform_editor_hotkeys, keycode, this);
    00577     if (num == -1) return ES_NOT_HANDLED;
    00578     this->OnClick(Point(), num, 1);
    00579     return ES_HANDLED;
    00580   }
    00581
    00582   virtual void OnClick(Point pt, int widget, int click_count)
    00583   {
    00584     if (widget < WID_ETT_BUTTONS_START) return;
    00585
    00586     switch (widget) {
    00587       case WID_ETT_DEMOLISH: // Demolish aka dynamite button
    00588         HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
    00589         this->last_user_action = widget;
    00590         break;
    00591
    00592       case WID_ETT_LOWER_LAND: // Lower land button
    00593         HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
    00594         this->last_user_action = widget;
    00595         break;
    00596
    00597       case WID_ETT_RAISE_LAND: // Raise land button
    00598         HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
    00599         this->last_user_action = widget;
    00600         break;
    00601
    00602       case WID_ETT_LEVEL_LAND: // Level land button
    00603         HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
    00604         this->last_user_action = widget;
    00605         break;
    00606
    00607       case WID_ETT_PLACE_ROCKS: // Place rocks button
    00608         HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
    00609         this->last_user_action = widget;
    00610         break;
    00611
    00612       case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
    00613         HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
    00614         this->last_user_action = widget;
    00615         break;
    00616
    00617       case WID_ETT_PLACE_OBJECT: // Place transmitter button
    00618         if (HandlePlacePushButton(this, WID_ETT_PLACE_OBJECT, SPR_CURSOR_TRANSMITTER, HT_RECT)) {
    00619           ShowBuildObjectPicker(this);
    00620           this->last_user_action = widget;
    00621         }
    00622         break;
    00623
    00624       case WID_ETT_INCREASE_SIZE:
    00625       case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
    00626         int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
    00627         this->HandleButtonClick(widget);
    00628         size += _terraform_size;
    00629
    00630         if (!IsInsideMM(size, 1, 8 + 1)) return;
    00631         _terraform_size = size;
    00632
    00633         if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
    00634         this->SetDirty();
    00635         break;
    00636       }
    00637
    00638       case WID_ETT_NEW_SCENARIO: // gen random land
    00639         this->HandleButtonClick(widget);
    00640         ShowCreateScenario();
    00641         break;
    00642
    00643       case WID_ETT_RESET_LANDSCAPE: // Reset landscape
    00644         ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, NULL, ResetLandscapeConfirmationCallback);
    00645         break;
    00646
    00647       default: NOT_REACHED();
    00648     }
    00649   }
    00650
    00651   virtual void OnTimeout()
    00652   {
    00653     for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
    00654       if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
    00655       if (this->IsWidgetLowered(i)) {
    00656         this->RaiseWidget(i);
    00657         this->SetWidgetDirty(i);
    00658       }
    00659     }
    00660   }
    00661
    00662   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00663   {
    00664     switch (this->last_user_action) {
    00665       case WID_ETT_DEMOLISH: // Demolish aka dynamite button
    00666         PlaceProc_DemolishArea(tile);
    00667         break;
    00668
    00669       case WID_ETT_LOWER_LAND: // Lower land button
    00670         CommonRaiseLowerBigLand(tile, 0);
    00671         break;
    00672
    00673       case WID_ETT_RAISE_LAND: // Raise land button
    00674         CommonRaiseLowerBigLand(tile, 1);
    00675         break;
    00676
    00677       case WID_ETT_LEVEL_LAND: // Level land button
    00678         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
    00679         break;
    00680
    00681       case WID_ETT_PLACE_ROCKS: // Place rocks button
    00682         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
    00683         break;
    00684
    00685       case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
    00686         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
    00687         break;
    00688
    00689       case WID_ETT_PLACE_OBJECT: // Place transmitter button
    00690         PlaceProc_Object(tile);
    00691         break;
    00692
    00693       default: NOT_REACHED();
    00694     }
    00695   }
    00696
    00697   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
    00698   {
    00699     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
    00700   }
    00701
    00702   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
    00703   {
    00704     if (pt.x != -1) {
    00705       switch (select_proc) {
    00706         default: NOT_REACHED();
    00707         case DDSP_CREATE_ROCKS:
    00708         case DDSP_CREATE_DESERT:
    00709         case DDSP_RAISE_AND_LEVEL_AREA:
    00710         case DDSP_LOWER_AND_LEVEL_AREA:
    00711         case DDSP_LEVEL_AREA:
    00712         case DDSP_DEMOLISH_AREA:
    00713           GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
    00714           break;
    00715       }
    00716     }
    00717   }
    00718
    00719   virtual void OnPlaceObjectAbort()
    00720   {
    00721     this->RaiseButtons();
    00722     this->SetDirty();
    00723     DeleteWindowById(WC_BUILD_OBJECT, 0);
    00724   }
    00725
    00726   static Hotkey<ScenarioEditorLandscapeGenerationWindow> terraform_editor_hotkeys[];
    00727 };
    00728
    00729 Hotkey<ScenarioEditorLandscapeGenerationWindow> ScenarioEditorLandscapeGenerationWindow::terraform_editor_hotkeys[] = {
    00730   Hotkey<ScenarioEditorLandscapeGenerationWindow>('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
    00731   Hotkey<ScenarioEditorLandscapeGenerationWindow>('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
    00732   Hotkey<ScenarioEditorLandscapeGenerationWindow>('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
    00733   Hotkey<ScenarioEditorLandscapeGenerationWindow>('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
    00734   Hotkey<ScenarioEditorLandscapeGenerationWindow>('R', "rocky", WID_ETT_PLACE_ROCKS),
    00735   Hotkey<ScenarioEditorLandscapeGenerationWindow>('T', "desert", WID_ETT_PLACE_DESERT),
    00736   Hotkey<ScenarioEditorLandscapeGenerationWindow>('O', "object", WID_ETT_PLACE_OBJECT),
    00737   HOTKEY_LIST_END(ScenarioEditorLandscapeGenerationWindow)
    00738 };
    00739
    00740 Hotkey<ScenarioEditorLandscapeGenerationWindow> *_terraform_editor_hotkeys = ScenarioEditorLandscapeGenerationWindow::terraform_editor_hotkeys;
    00741
    00742 static const WindowDesc _scen_edit_land_gen_desc(
    00743   WDP_AUTO, 0, 0,
    00744   WC_SCEN_LAND_GEN, WC_NONE,
    00745   WDF_CONSTRUCTION,
    00746   _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets)
    00747 );
    00748
    00753 Window *ShowEditorTerraformToolbar()
    00754 {
    00755   return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);
    00756 }
    00757
    00758 EventState TerraformToolbarEditorGlobalHotkeys(uint16 key, uint16 keycode)
    00759 {
    00760   int num = CheckHotkeyMatch<ScenarioEditorLandscapeGenerationWindow>(_terraform_editor_hotkeys, keycode, NULL, true);
    00761   if (num == -1) return ES_NOT_HANDLED;
    00762   Window *w = ShowEditorTerraformToolbar();
    00763   if (w == NULL) return ES_NOT_HANDLED;
    00764   return w->OnKeyPress(key, keycode);
    00765 }