tree_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "window_gui.h"
00014 #include "gfx_func.h"
00015 #include "tilehighlight_func.h"
00016 #include "company_func.h"
00017 #include "company_base.h"
00018 #include "command_func.h"
00019 #include "sound_func.h"
00020 #include "tree_map.h"
00021 
00022 #include "widgets/tree_widget.h"
00023 
00024 #include "table/sprites.h"
00025 #include "table/strings.h"
00026 #include "table/tree_land.h"
00027 
00028 void PlaceTreesRandomly();
00029 
00031 const PalSpriteID tree_sprites[] = {
00032   { 1621, PAL_NONE }, { 1587, PAL_NONE }, { 1656, PAL_NONE }, { 1579, PAL_NONE },
00033   { 1607, PAL_NONE }, { 1593, PAL_NONE }, { 1614, PAL_NONE }, { 1586, PAL_NONE },
00034   { 1663, PAL_NONE }, { 1677, PAL_NONE }, { 1691, PAL_NONE }, { 1705, PAL_NONE },
00035   { 1711, PAL_NONE }, { 1746, PAL_NONE }, { 1753, PAL_NONE }, { 1732, PAL_NONE },
00036   { 1739, PAL_NONE }, { 1718, PAL_NONE }, { 1725, PAL_NONE }, { 1760, PAL_NONE },
00037   { 1838, PAL_NONE }, { 1844, PAL_NONE }, { 1866, PAL_NONE }, { 1871, PAL_NONE },
00038   { 1899, PAL_NONE }, { 1935, PAL_NONE }, { 1928, PAL_NONE }, { 1915, PAL_NONE },
00039   { 1887, PAL_NONE }, { 1908, PAL_NONE }, { 1824, PAL_NONE }, { 1943, PAL_NONE },
00040   { 1950, PAL_NONE }, { 1957, PALETTE_TO_GREEN }, { 1964, PALETTE_TO_RED },        { 1971, PAL_NONE },
00041   { 1978, PAL_NONE }, { 1985, PALETTE_TO_RED, },  { 1992, PALETTE_TO_PALE_GREEN }, { 1999, PALETTE_TO_YELLOW }, { 2006, PALETTE_TO_RED }
00042 };
00043 
00044 
00048 class BuildTreesWindow : public Window
00049 {
00050   uint16 base;        
00051   uint16 count;       
00052   TreeType tree_to_plant; 
00053 
00054 public:
00055   BuildTreesWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00056   {
00057     this->InitNested(desc, window_number);
00058     ResetObjectToPlace();
00059   }
00060 
00065   Dimension GetMaxTreeSpriteSize()
00066   {
00067     Dimension size, this_size;
00068     Point offset;
00069     /* Avoid to use it uninitialized */
00070     size.width  = 32; // default width - 2
00071     size.height = 39; // default height - 7
00072     offset.x = 0;
00073     offset.y = 0;
00074 
00075     for (int i = this->base; i < this->base + this->count; i++) {
00076       if (i >= (int)lengthof(tree_sprites)) return size;
00077       this_size = GetSpriteSize(tree_sprites[i].sprite, &offset);
00078       size.width = max<int>(size.width, 2 * max<int>(this_size.width, -offset.x));
00079       size.height = max<int>(size.height, max<int>(this_size.height, -offset.y));
00080     }
00081 
00082     return size;
00083   }
00084 
00085   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00086   {
00087     if (widget >= WID_BT_TYPE_11 && widget <= WID_BT_TYPE_34) {
00088       Dimension d = GetMaxTreeSpriteSize();
00089       /* Allow some pixels extra width and height */
00090       size->width = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00091       size->height = d.height + WD_FRAMERECT_RIGHT + WD_FRAMERECT_BOTTOM + 7; // we need some more space
00092       return;
00093     }
00094 
00095     if (widget != WID_BT_MANY_RANDOM) return;
00096 
00097     if (_game_mode != GM_EDITOR) {
00098       size->width = 0;
00099       size->height = 0;
00100     }
00101   }
00102 
00103   virtual void OnPaint()
00104   {
00105     this->DrawWidgets();
00106   }
00107 
00108   virtual void DrawWidget(const Rect &r, int widget) const
00109   {
00110     if (widget < WID_BT_TYPE_11 || widget > WID_BT_TYPE_34 || widget - WID_BT_TYPE_11 >= this->count) return;
00111 
00112     int i = this->base + widget - WID_BT_TYPE_11;
00113     /* Trees "grow" in the centre on the bottom line of the buttons */
00114     DrawSprite(tree_sprites[i].sprite, tree_sprites[i].pal, (r.left + r.right) / 2 + WD_FRAMERECT_LEFT, r.bottom - 7);
00115   }
00116 
00117   virtual void OnClick(Point pt, int widget, int click_count)
00118   {
00119     switch (widget) {
00120       case WID_BT_TYPE_11: case WID_BT_TYPE_12: case WID_BT_TYPE_13: case WID_BT_TYPE_14:
00121       case WID_BT_TYPE_21: case WID_BT_TYPE_22: case WID_BT_TYPE_23: case WID_BT_TYPE_24:
00122       case WID_BT_TYPE_31: case WID_BT_TYPE_32: case WID_BT_TYPE_33: case WID_BT_TYPE_34:
00123         if (widget - WID_BT_TYPE_11 >= this->count) break;
00124 
00125         if (HandlePlacePushButton(this, widget, SPR_CURSOR_TREE, HT_RECT)) {
00126           this->tree_to_plant = (TreeType)(this->base + widget - WID_BT_TYPE_11);
00127         }
00128         break;
00129 
00130       case WID_BT_TYPE_RANDOM: // tree of random type.
00131         if (HandlePlacePushButton(this, WID_BT_TYPE_RANDOM, SPR_CURSOR_TREE, HT_RECT)) {
00132           this->tree_to_plant = TREE_INVALID;
00133         }
00134         break;
00135 
00136       case WID_BT_MANY_RANDOM: // place trees randomly over the landscape
00137         this->LowerWidget(WID_BT_MANY_RANDOM);
00138         this->SetTimeout();
00139         SndPlayFx(SND_15_BEEP);
00140         PlaceTreesRandomly();
00141         MarkWholeScreenDirty();
00142         break;
00143     }
00144   }
00145 
00146   virtual void OnPlaceObject(Point pt, TileIndex tile)
00147   {
00148     VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_PLANT_TREES);
00149   }
00150 
00151   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
00152   {
00153     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
00154   }
00155 
00156   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
00157   {
00158     if (pt.x != -1 && select_proc == DDSP_PLANT_TREES) {
00159       DoCommandP(end_tile, this->tree_to_plant, start_tile,
00160         CMD_PLANT_TREE | CMD_MSG(STR_ERROR_CAN_T_PLANT_TREE_HERE));
00161     }
00162   }
00163 
00167   virtual void OnInit()
00168   {
00169     this->base  = _tree_base_by_landscape[_settings_game.game_creation.landscape];
00170     this->count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
00171   }
00172 
00173   virtual void OnTimeout()
00174   {
00175     this->RaiseWidget(WID_BT_MANY_RANDOM);
00176     this->SetWidgetDirty(WID_BT_MANY_RANDOM);
00177   }
00178 
00179   virtual void OnPlaceObjectAbort()
00180   {
00181     this->RaiseButtons();
00182   }
00183 };
00184 
00185 static const NWidgetPart _nested_build_trees_widgets[] = {
00186   NWidget(NWID_HORIZONTAL),
00187     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00188     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00189     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00190     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00191   EndContainer(),
00192   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00193     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00194     NWidget(NWID_HORIZONTAL),
00195       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00196       NWidget(NWID_VERTICAL),
00197         NWidget(NWID_HORIZONTAL),
00198           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_11), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00199           EndContainer(),
00200           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00201           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_12), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00202           EndContainer(),
00203           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00204           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_13), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00205           EndContainer(),
00206           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00207           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_14), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00208           EndContainer(),
00209         EndContainer(),
00210         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00211         NWidget(NWID_HORIZONTAL),
00212           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_21), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00213           EndContainer(),
00214           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00215           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_22), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00216           EndContainer(),
00217           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00218           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_23), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00219           EndContainer(),
00220           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00221           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_24), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00222           EndContainer(),
00223         EndContainer(),
00224         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00225         NWidget(NWID_HORIZONTAL),
00226           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_31), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00227           EndContainer(),
00228           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00229           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_32), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00230           EndContainer(),
00231           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00232           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_33), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00233           EndContainer(),
00234           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00235           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_34), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00236           EndContainer(),
00237         EndContainer(),
00238         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00239         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_TYPE_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
00240         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00241         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_MANY_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
00242         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00243       EndContainer(),
00244       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00245     EndContainer(),
00246   EndContainer(),
00247 };
00248 
00249 static const WindowDesc _build_trees_desc(
00250   WDP_AUTO, 0, 0,
00251   WC_BUILD_TREES, WC_NONE,
00252   WDF_CONSTRUCTION,
00253   _nested_build_trees_widgets, lengthof(_nested_build_trees_widgets)
00254 );
00255 
00256 void ShowBuildTreesToolbar()
00257 {
00258   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
00259   AllocateWindowDescFront<BuildTreesWindow>(&_build_trees_desc, 0);
00260 }