terraform_gui.cpp

Go to the documentation of this file.
00001 /* $Id: terraform_gui.cpp 23547 2011-12-16 18:21:13Z truebrain $ */
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     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) 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::GetCount() != 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         /* This button is NOT a place-push-button, so don't treat it as such */
00201         this->HandleButtonClick(WID_TT_PLANT_TREES);
00202         ShowBuildTreesToolbar();
00203         break;
00204 
00205       case WID_TT_PLACE_SIGN: // Place sign button
00206         HandlePlacePushButton(this, WID_TT_PLACE_SIGN, SPR_CURSOR_SIGN, HT_RECT);
00207         this->last_user_action = widget;
00208         break;
00209 
00210       case WID_TT_PLACE_OBJECT: // Place object button
00211         /* Don't show the place object button when there are no objects to place. */
00212         if (ObjectClass::GetCount() == 0) return;
00213         if (HandlePlacePushButton(this, WID_TT_PLACE_OBJECT, SPR_CURSOR_TRANSMITTER, HT_RECT)) {
00214           ShowBuildObjectPicker(this);
00215           this->last_user_action = widget;
00216         }
00217         break;
00218 
00219       default: NOT_REACHED();
00220     }
00221   }
00222 
00223   virtual void OnTimeout()
00224   {
00225     this->RaiseWidget(WID_TT_PLANT_TREES);
00226     this->SetWidgetDirty(WID_TT_PLANT_TREES);
00227   }
00228 
00229   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00230   {
00231     int num = CheckHotkeyMatch(terraform_hotkeys, keycode, this);
00232     if (num == -1) return ES_NOT_HANDLED;
00233     this->OnClick(Point(), num, 1);
00234     return ES_HANDLED;
00235   }
00236 
00237   virtual void OnPlaceObject(Point pt, TileIndex tile)
00238   {
00239     switch (this->last_user_action) {
00240       case WID_TT_LOWER_LAND: // Lower land button
00241         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LOWER_AND_LEVEL_AREA);
00242         break;
00243 
00244       case WID_TT_RAISE_LAND: // Raise land button
00245         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_RAISE_AND_LEVEL_AREA);
00246         break;
00247 
00248       case WID_TT_LEVEL_LAND: // Level land button
00249         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
00250         break;
00251 
00252       case WID_TT_DEMOLISH: // Demolish aka dynamite button
00253         PlaceProc_DemolishArea(tile);
00254         break;
00255 
00256       case WID_TT_BUY_LAND: // Buy land button
00257         DoCommandP(tile, OBJECT_OWNED_LAND, 0, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_PURCHASE_THIS_LAND), CcPlaySound1E);
00258         break;
00259 
00260       case WID_TT_PLACE_SIGN: // Place sign button
00261         PlaceProc_Sign(tile);
00262         break;
00263 
00264       case WID_TT_PLACE_OBJECT: // Place object button
00265         PlaceProc_Object(tile);
00266         break;
00267 
00268       default: NOT_REACHED();
00269     }
00270   }
00271 
00272   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
00273   {
00274     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
00275   }
00276 
00277   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00278   {
00279     Point pt = GetToolbarAlignedWindowPosition(sm_width);
00280     pt.y += sm_height;
00281     return pt;
00282   }
00283 
00284   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
00285   {
00286     if (pt.x != -1) {
00287       switch (select_proc) {
00288         default: NOT_REACHED();
00289         case DDSP_DEMOLISH_AREA:
00290         case DDSP_RAISE_AND_LEVEL_AREA:
00291         case DDSP_LOWER_AND_LEVEL_AREA:
00292         case DDSP_LEVEL_AREA:
00293           GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
00294           break;
00295       }
00296     }
00297   }
00298 
00299   virtual void OnPlaceObjectAbort()
00300   {
00301     DeleteWindowById(WC_BUILD_OBJECT, 0);
00302     this->RaiseButtons();
00303   }
00304 
00305   static Hotkey<TerraformToolbarWindow> terraform_hotkeys[];
00306 };
00307 
00308 Hotkey<TerraformToolbarWindow> TerraformToolbarWindow::terraform_hotkeys[] = {
00309   Hotkey<TerraformToolbarWindow>('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
00310   Hotkey<TerraformToolbarWindow>('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
00311   Hotkey<TerraformToolbarWindow>('E' | WKC_GLOBAL_HOTKEY, "level", WID_TT_LEVEL_LAND),
00312   Hotkey<TerraformToolbarWindow>('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_TT_DEMOLISH),
00313   Hotkey<TerraformToolbarWindow>('U', "buyland", WID_TT_BUY_LAND),
00314   Hotkey<TerraformToolbarWindow>('I', "trees", WID_TT_PLANT_TREES),
00315   Hotkey<TerraformToolbarWindow>('O', "placesign", WID_TT_PLACE_SIGN),
00316   Hotkey<TerraformToolbarWindow>('P', "placeobject", WID_TT_PLACE_OBJECT),
00317   HOTKEY_LIST_END(TerraformToolbarWindow)
00318 };
00319 Hotkey<TerraformToolbarWindow> *_terraform_hotkeys = TerraformToolbarWindow::terraform_hotkeys;
00320 
00321 static const NWidgetPart _nested_terraform_widgets[] = {
00322   NWidget(NWID_HORIZONTAL),
00323     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00324     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_LANDSCAPING_TOOLBAR, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00325     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00326   EndContainer(),
00327   NWidget(NWID_HORIZONTAL),
00328     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOWER_LAND), SetMinimalSize(22, 22),
00329                 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
00330     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_RAISE_LAND), SetMinimalSize(22, 22),
00331                 SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
00332     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LEVEL_LAND), SetMinimalSize(22, 22),
00333                 SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
00334 
00335     NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), EndContainer(),
00336 
00337     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_DEMOLISH), SetMinimalSize(22, 22),
00338                 SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
00339     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUY_LAND), SetMinimalSize(22, 22),
00340                 SetFill(0, 1), SetDataTip(SPR_IMG_BUY_LAND, STR_LANDSCAPING_TOOLTIP_PURCHASE_LAND),
00341     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLANT_TREES), SetMinimalSize(22, 22),
00342                 SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_SCENEDIT_TOOLBAR_PLANT_TREES),
00343     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_SIGN), SetMinimalSize(22, 22),
00344                 SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_SCENEDIT_TOOLBAR_PLACE_SIGN),
00345     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_TT_SHOW_PLACE_OBJECT),
00346       NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_PLACE_OBJECT), SetMinimalSize(22, 22),
00347                 SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
00348     EndContainer(),
00349   EndContainer(),
00350 };
00351 
00352 static const WindowDesc _terraform_desc(
00353   WDP_MANUAL, 0, 0,
00354   WC_SCEN_LAND_GEN, WC_NONE,
00355   WDF_CONSTRUCTION,
00356   _nested_terraform_widgets, lengthof(_nested_terraform_widgets)
00357 );
00358 
00364 Window *ShowTerraformToolbar(Window *link)
00365 {
00366   if (!Company::IsValidID(_local_company)) return NULL;
00367 
00368   Window *w;
00369   if (link == NULL) {
00370     w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
00371     return w;
00372   }
00373 
00374   /* Delete the terraform toolbar to place it again. */
00375   DeleteWindowById(WC_SCEN_LAND_GEN, 0, true);
00376   w = AllocateWindowDescFront<TerraformToolbarWindow>(&_terraform_desc, 0);
00377   /* Align the terraform toolbar under the main toolbar. */
00378   w->top -= w->height;
00379   w->SetDirty();
00380   /* Put the linked toolbar to the left / right of it. */
00381   link->left = w->left + (_current_text_dir == TD_RTL ? w->width : -link->width);
00382   link->top  = w->top;
00383   link->SetDirty();
00384 
00385   return w;
00386 }
00387 
00388 EventState TerraformToolbarGlobalHotkeys(uint16 key, uint16 keycode)
00389 {
00390   int num = CheckHotkeyMatch<TerraformToolbarWindow>(_terraform_hotkeys, keycode, NULL, true);
00391   if (num == -1) return ES_NOT_HANDLED;
00392   Window *w = ShowTerraformToolbar(NULL);
00393   if (w == NULL) return ES_NOT_HANDLED;
00394   return w->OnKeyPress(key, keycode);
00395 }
00396 
00397 static byte _terraform_size = 1;
00398 
00408 static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
00409 {
00410   if (_terraform_size == 1) {
00411     StringID msg =
00412       mode ? STR_ERROR_CAN_T_RAISE_LAND_HERE : STR_ERROR_CAN_T_LOWER_LAND_HERE;
00413 
00414     DoCommandP(tile, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND | CMD_MSG(msg), CcTerraform);
00415   } else {
00416     assert(_terraform_size != 0);
00417     TileArea ta(tile, _terraform_size, _terraform_size);
00418     ta.ClampToMap();
00419 
00420     if (ta.w == 0 || ta.h == 0) return;
00421 
00422     SndPlayTileFx(SND_1F_SPLAT, tile);
00423 
00424     uint h;
00425     if (mode != 0) {
00426       /* Raise land */
00427       h = MAX_TILE_HEIGHT;
00428       TILE_AREA_LOOP(tile2, ta) {
00429         h = min(h, TileHeight(tile2));
00430       }
00431     } else {
00432       /* Lower land */
00433       h = 0;
00434       TILE_AREA_LOOP(tile2, ta) {
00435         h = max(h, TileHeight(tile2));
00436       }
00437     }
00438 
00439     TILE_AREA_LOOP(tile2, ta) {
00440       if (TileHeight(tile2) == h) {
00441         DoCommandP(tile2, SLOPE_N, (uint32)mode, CMD_TERRAFORM_LAND);
00442       }
00443     }
00444   }
00445 }
00446 
00447 static const int8 _multi_terraform_coords[][2] = {
00448   {  0, -2},
00449   {  4,  0}, { -4,  0}, {  0,  2},
00450   { -8,  2}, { -4,  4}, {  0,  6}, {  4,  4}, {  8,  2},
00451   {-12,  0}, { -8, -2}, { -4, -4}, {  0, -6}, {  4, -4}, {  8, -2}, { 12,  0},
00452   {-16,  2}, {-12,  4}, { -8,  6}, { -4,  8}, {  0, 10}, {  4,  8}, {  8,  6}, { 12,  4}, { 16,  2},
00453   {-20,  0}, {-16, -2}, {-12, -4}, { -8, -6}, { -4, -8}, {  0,-10}, {  4, -8}, {  8, -6}, { 12, -4}, { 16, -2}, { 20,  0},
00454   {-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},
00455   {-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},
00456 };
00457 
00458 static const NWidgetPart _nested_scen_edit_land_gen_widgets[] = {
00459   NWidget(NWID_HORIZONTAL),
00460     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00461     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TERRAFORM_TOOLBAR_LAND_GENERATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00462     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00463     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00464   EndContainer(),
00465   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00466     NWidget(NWID_HORIZONTAL), SetPadding(2, 2, 7, 2),
00467       NWidget(NWID_SPACER), SetFill(1, 0),
00468       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DEMOLISH), SetMinimalSize(22, 22),
00469                     SetFill(0, 1), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
00470       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LOWER_LAND), SetMinimalSize(22, 22),
00471                     SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_DOWN, STR_LANDSCAPING_TOOLTIP_LOWER_A_CORNER_OF_LAND),
00472       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_RAISE_LAND), SetMinimalSize(22, 22),
00473                     SetFill(0, 1), SetDataTip(SPR_IMG_TERRAFORM_UP, STR_LANDSCAPING_TOOLTIP_RAISE_A_CORNER_OF_LAND),
00474       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_LEVEL_LAND), SetMinimalSize(22, 22),
00475                     SetFill(0, 1), SetDataTip(SPR_IMG_LEVEL_LAND, STR_LANDSCAPING_LEVEL_LAND_TOOLTIP),
00476       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_ROCKS), SetMinimalSize(22, 22),
00477                     SetFill(0, 1), SetDataTip(SPR_IMG_ROCKS, STR_TERRAFORM_TOOLTIP_PLACE_ROCKY_AREAS_ON_LANDSCAPE),
00478       NWidget(NWID_SELECTION, INVALID_COLOUR, WID_ETT_SHOW_PLACE_DESERT),
00479         NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_DESERT), SetMinimalSize(22, 22),
00480                       SetFill(0, 1), SetDataTip(SPR_IMG_DESERT, STR_TERRAFORM_TOOLTIP_DEFINE_DESERT_AREA),
00481       EndContainer(),
00482       NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_PLACE_OBJECT), SetMinimalSize(23, 22),
00483                     SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_SCENEDIT_TOOLBAR_PLACE_OBJECT),
00484       NWidget(NWID_SPACER), SetFill(1, 0),
00485     EndContainer(),
00486     NWidget(NWID_HORIZONTAL),
00487       NWidget(NWID_SPACER), SetFill(1, 0),
00488       NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, WID_ETT_DOTS), SetMinimalSize(59, 31), SetDataTip(STR_EMPTY, STR_NULL),
00489       NWidget(NWID_SPACER), SetFill(1, 0),
00490       NWidget(NWID_VERTICAL),
00491         NWidget(NWID_SPACER), SetFill(0, 1),
00492         NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_INCREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_UP, STR_TERRAFORM_TOOLTIP_INCREASE_SIZE_OF_LAND_AREA),
00493         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00494         NWidget(WWT_IMGBTN, COLOUR_GREY, WID_ETT_DECREASE_SIZE), SetMinimalSize(12, 12), SetDataTip(SPR_ARROW_DOWN, STR_TERRAFORM_TOOLTIP_DECREASE_SIZE_OF_LAND_AREA),
00495         NWidget(NWID_SPACER), SetFill(0, 1),
00496       EndContainer(),
00497       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00498     EndContainer(),
00499     NWidget(NWID_SPACER), SetMinimalSize(0, 6),
00500     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_NEW_SCENARIO), SetMinimalSize(160, 12),
00501                 SetFill(1, 0), SetDataTip(STR_TERRAFORM_SE_NEW_WORLD, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetPadding(0, 2, 0, 2),
00502     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_ETT_RESET_LANDSCAPE), SetMinimalSize(160, 12),
00503                 SetFill(1, 0), SetDataTip(STR_TERRAFORM_RESET_LANDSCAPE, STR_TERRAFORM_RESET_LANDSCAPE_TOOLTIP), SetPadding(1, 2, 2, 2),
00504   EndContainer(),
00505 };
00506 
00512 static void ResetLandscapeConfirmationCallback(Window *w, bool confirmed)
00513 {
00514   if (confirmed) {
00515     /* Set generating_world to true to get instant-green grass after removing
00516      * company property. */
00517     _generating_world = true;
00518 
00519     /* Delete all companies */
00520     Company *c;
00521     FOR_ALL_COMPANIES(c) {
00522       ChangeOwnershipOfCompanyItems(c->index, INVALID_OWNER);
00523       delete c;
00524     }
00525 
00526     _generating_world = false;
00527 
00528     /* Delete all station signs */
00529     BaseStation *st;
00530     FOR_ALL_BASE_STATIONS(st) {
00531       /* There can be buoys, remove them */
00532       if (IsBuoyTile(st->xy)) DoCommand(st->xy, 0, 0, DC_EXEC | DC_BANKRUPT, CMD_LANDSCAPE_CLEAR);
00533       if (!st->IsInUse()) delete st;
00534     }
00535 
00536     /* Now that all vehicles are gone, we can reset the engine pool. Maybe it reduces some NewGRF changing-mess */
00537     EngineOverrideManager::ResetToCurrentNewGRFConfig();
00538 
00539     MarkWholeScreenDirty();
00540   }
00541 }
00542 
00544 struct ScenarioEditorLandscapeGenerationWindow : Window {
00545   int last_user_action; 
00546 
00547   ScenarioEditorLandscapeGenerationWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00548   {
00549     this->CreateNestedTree(desc);
00550     NWidgetStacked *show_desert = this->GetWidget<NWidgetStacked>(WID_ETT_SHOW_PLACE_DESERT);
00551     show_desert->SetDisplayedPlane(_settings_game.game_creation.landscape == LT_TROPIC ? 0 : SZSP_NONE);
00552     this->FinishInitNested(desc, window_number);
00553     this->last_user_action = WIDGET_LIST_END;
00554   }
00555 
00556   virtual void OnPaint()
00557   {
00558     this->DrawWidgets();
00559 
00560     if (this->IsWidgetLowered(WID_ETT_LOWER_LAND) || this->IsWidgetLowered(WID_ETT_RAISE_LAND)) { // change area-size if raise/lower corner is selected
00561       SetTileSelectSize(_terraform_size, _terraform_size);
00562     }
00563   }
00564 
00565   virtual void DrawWidget(const Rect &r, int widget) const
00566   {
00567     if (widget != WID_ETT_DOTS) return;
00568 
00569     int center_x = RoundDivSU(r.left + r.right, 2);
00570     int center_y = RoundDivSU(r.top + r.bottom, 2);
00571 
00572     int n = _terraform_size * _terraform_size;
00573     const int8 *coords = &_multi_terraform_coords[0][0];
00574 
00575     assert(n != 0);
00576     do {
00577       DrawSprite(SPR_WHITE_POINT, PAL_NONE, center_x + coords[0], center_y + coords[1]);
00578       coords += 2;
00579     } while (--n);
00580   }
00581 
00582   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00583   {
00584     int num = CheckHotkeyMatch(terraform_editor_hotkeys, keycode, this);
00585     if (num == -1) return ES_NOT_HANDLED;
00586     this->OnClick(Point(), num, 1);
00587     return ES_HANDLED;
00588   }
00589 
00590   virtual void OnClick(Point pt, int widget, int click_count)
00591   {
00592     if (widget < WID_ETT_BUTTONS_START) return;
00593 
00594     switch (widget) {
00595       case WID_ETT_DEMOLISH: // Demolish aka dynamite button
00596         HandlePlacePushButton(this, WID_ETT_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
00597         this->last_user_action = widget;
00598         break;
00599 
00600       case WID_ETT_LOWER_LAND: // Lower land button
00601         HandlePlacePushButton(this, WID_ETT_LOWER_LAND, ANIMCURSOR_LOWERLAND, HT_POINT);
00602         this->last_user_action = widget;
00603         break;
00604 
00605       case WID_ETT_RAISE_LAND: // Raise land button
00606         HandlePlacePushButton(this, WID_ETT_RAISE_LAND, ANIMCURSOR_RAISELAND, HT_POINT);
00607         this->last_user_action = widget;
00608         break;
00609 
00610       case WID_ETT_LEVEL_LAND: // Level land button
00611         HandlePlacePushButton(this, WID_ETT_LEVEL_LAND, SPR_CURSOR_LEVEL_LAND, HT_POINT | HT_DIAGONAL);
00612         this->last_user_action = widget;
00613         break;
00614 
00615       case WID_ETT_PLACE_ROCKS: // Place rocks button
00616         HandlePlacePushButton(this, WID_ETT_PLACE_ROCKS, SPR_CURSOR_ROCKY_AREA, HT_RECT);
00617         this->last_user_action = widget;
00618         break;
00619 
00620       case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
00621         HandlePlacePushButton(this, WID_ETT_PLACE_DESERT, SPR_CURSOR_DESERT, HT_RECT);
00622         this->last_user_action = widget;
00623         break;
00624 
00625       case WID_ETT_PLACE_OBJECT: // Place transmitter button
00626         if (HandlePlacePushButton(this, WID_ETT_PLACE_OBJECT, SPR_CURSOR_TRANSMITTER, HT_RECT)) {
00627           ShowBuildObjectPicker(this);
00628           this->last_user_action = widget;
00629         }
00630         break;
00631 
00632       case WID_ETT_INCREASE_SIZE:
00633       case WID_ETT_DECREASE_SIZE: { // Increase/Decrease terraform size
00634         int size = (widget == WID_ETT_INCREASE_SIZE) ? 1 : -1;
00635         this->HandleButtonClick(widget);
00636         size += _terraform_size;
00637 
00638         if (!IsInsideMM(size, 1, 8 + 1)) return;
00639         _terraform_size = size;
00640 
00641         SndPlayFx(SND_15_BEEP);
00642         this->SetDirty();
00643         break;
00644       }
00645 
00646       case WID_ETT_NEW_SCENARIO: // gen random land
00647         this->HandleButtonClick(widget);
00648         ShowCreateScenario();
00649         break;
00650 
00651       case WID_ETT_RESET_LANDSCAPE: // Reset landscape
00652         ShowQuery(STR_QUERY_RESET_LANDSCAPE_CAPTION, STR_RESET_LANDSCAPE_CONFIRMATION_TEXT, NULL, ResetLandscapeConfirmationCallback);
00653         break;
00654 
00655       default: NOT_REACHED();
00656     }
00657   }
00658 
00659   virtual void OnTimeout()
00660   {
00661     for (uint i = WID_ETT_START; i < this->nested_array_size; i++) {
00662       if (i == WID_ETT_BUTTONS_START) i = WID_ETT_BUTTONS_END; // skip the buttons
00663       if (this->IsWidgetLowered(i)) {
00664         this->RaiseWidget(i);
00665         this->SetWidgetDirty(i);
00666       }
00667     }
00668   }
00669 
00670   virtual void OnPlaceObject(Point pt, TileIndex tile)
00671   {
00672     switch (this->last_user_action) {
00673       case WID_ETT_DEMOLISH: // Demolish aka dynamite button
00674         PlaceProc_DemolishArea(tile);
00675         break;
00676 
00677       case WID_ETT_LOWER_LAND: // Lower land button
00678         CommonRaiseLowerBigLand(tile, 0);
00679         break;
00680 
00681       case WID_ETT_RAISE_LAND: // Raise land button
00682         CommonRaiseLowerBigLand(tile, 1);
00683         break;
00684 
00685       case WID_ETT_LEVEL_LAND: // Level land button
00686         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_LEVEL_AREA);
00687         break;
00688 
00689       case WID_ETT_PLACE_ROCKS: // Place rocks button
00690         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_ROCKS);
00691         break;
00692 
00693       case WID_ETT_PLACE_DESERT: // Place desert button (in tropical climate)
00694         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CREATE_DESERT);
00695         break;
00696 
00697       case WID_ETT_PLACE_OBJECT: // Place transmitter button
00698         PlaceProc_Object(tile);
00699         break;
00700 
00701       default: NOT_REACHED();
00702     }
00703   }
00704 
00705   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
00706   {
00707     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
00708   }
00709 
00710   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
00711   {
00712     if (pt.x != -1) {
00713       switch (select_proc) {
00714         default: NOT_REACHED();
00715         case DDSP_CREATE_ROCKS:
00716         case DDSP_CREATE_DESERT:
00717         case DDSP_RAISE_AND_LEVEL_AREA:
00718         case DDSP_LOWER_AND_LEVEL_AREA:
00719         case DDSP_LEVEL_AREA:
00720         case DDSP_DEMOLISH_AREA:
00721           GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
00722           break;
00723       }
00724     }
00725   }
00726 
00727   virtual void OnPlaceObjectAbort()
00728   {
00729     this->RaiseButtons();
00730     this->SetDirty();
00731     DeleteWindowById(WC_BUILD_OBJECT, 0);
00732   }
00733 
00734   static Hotkey<ScenarioEditorLandscapeGenerationWindow> terraform_editor_hotkeys[];
00735 };
00736 
00737 Hotkey<ScenarioEditorLandscapeGenerationWindow> ScenarioEditorLandscapeGenerationWindow::terraform_editor_hotkeys[] = {
00738   Hotkey<ScenarioEditorLandscapeGenerationWindow>('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
00739   Hotkey<ScenarioEditorLandscapeGenerationWindow>('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
00740   Hotkey<ScenarioEditorLandscapeGenerationWindow>('W' | WKC_GLOBAL_HOTKEY, "raise", WID_ETT_RAISE_LAND),
00741   Hotkey<ScenarioEditorLandscapeGenerationWindow>('E' | WKC_GLOBAL_HOTKEY, "level", WID_ETT_LEVEL_LAND),
00742   Hotkey<ScenarioEditorLandscapeGenerationWindow>('R', "rocky", WID_ETT_PLACE_ROCKS),
00743   Hotkey<ScenarioEditorLandscapeGenerationWindow>('T', "desert", WID_ETT_PLACE_DESERT),
00744   Hotkey<ScenarioEditorLandscapeGenerationWindow>('O', "object", WID_ETT_PLACE_OBJECT),
00745   HOTKEY_LIST_END(ScenarioEditorLandscapeGenerationWindow)
00746 };
00747 
00748 Hotkey<ScenarioEditorLandscapeGenerationWindow> *_terraform_editor_hotkeys = ScenarioEditorLandscapeGenerationWindow::terraform_editor_hotkeys;
00749 
00750 static const WindowDesc _scen_edit_land_gen_desc(
00751   WDP_AUTO, 0, 0,
00752   WC_SCEN_LAND_GEN, WC_NONE,
00753   WDF_CONSTRUCTION,
00754   _nested_scen_edit_land_gen_widgets, lengthof(_nested_scen_edit_land_gen_widgets)
00755 );
00756 
00761 Window *ShowEditorTerraformToolbar()
00762 {
00763   return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);
00764 }
00765 
00766 EventState TerraformToolbarEditorGlobalHotkeys(uint16 key, uint16 keycode)
00767 {
00768   int num = CheckHotkeyMatch<ScenarioEditorLandscapeGenerationWindow>(_terraform_editor_hotkeys, keycode, NULL, true);
00769   if (num == -1) return ES_NOT_HANDLED;
00770   Window *w = ShowEditorTerraformToolbar();
00771   if (w == NULL) return ES_NOT_HANDLED;
00772   return w->OnKeyPress(key, keycode);
00773 }