# terraform_gui.cpp

Go to the documentation of this file.

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