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         SndPlayFx(SND_15_BEEP);
00138         PlaceTreesRandomly();
00139         MarkWholeScreenDirty();
00140         break;
00141     }
00142   }
00143 
00144   virtual void OnPlaceObject(Point pt, TileIndex tile)
00145   {
00146     VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_PLANT_TREES);
00147   }
00148 
00149   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
00150   {
00151     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
00152   }
00153 
00154   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
00155   {
00156     if (pt.x != -1 && select_proc == DDSP_PLANT_TREES) {
00157       DoCommandP(end_tile, this->tree_to_plant, start_tile,
00158         CMD_PLANT_TREE | CMD_MSG(STR_ERROR_CAN_T_PLANT_TREE_HERE));
00159     }
00160   }
00161 
00165   virtual void OnInit()
00166   {
00167     this->base  = _tree_base_by_landscape[_settings_game.game_creation.landscape];
00168     this->count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
00169   }
00170 
00171   virtual void OnPlaceObjectAbort()
00172   {
00173     this->RaiseButtons();
00174   }
00175 };
00176 
00177 static const NWidgetPart _nested_build_trees_widgets[] = {
00178   NWidget(NWID_HORIZONTAL),
00179     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00180     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00181     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00182     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00183   EndContainer(),
00184   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00185     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00186     NWidget(NWID_HORIZONTAL),
00187       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00188       NWidget(NWID_VERTICAL),
00189         NWidget(NWID_HORIZONTAL),
00190           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_11), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00191           EndContainer(),
00192           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00193           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_12), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00194           EndContainer(),
00195           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00196           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_13), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00197           EndContainer(),
00198           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00199           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_14), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00200           EndContainer(),
00201         EndContainer(),
00202         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00203         NWidget(NWID_HORIZONTAL),
00204           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_21), 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_22), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00208           EndContainer(),
00209           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00210           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_23), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00211           EndContainer(),
00212           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00213           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_24), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00214           EndContainer(),
00215         EndContainer(),
00216         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00217         NWidget(NWID_HORIZONTAL),
00218           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_31), 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_32), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00222           EndContainer(),
00223           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00224           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_33), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00225           EndContainer(),
00226           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00227           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_34), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00228           EndContainer(),
00229         EndContainer(),
00230         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00231         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_TYPE_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
00232         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00233         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BT_MANY_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
00234         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00235       EndContainer(),
00236       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00237     EndContainer(),
00238   EndContainer(),
00239 };
00240 
00241 static const WindowDesc _build_trees_desc(
00242   WDP_AUTO, 0, 0,
00243   WC_BUILD_TREES, WC_NONE,
00244   WDF_CONSTRUCTION,
00245   _nested_build_trees_widgets, lengthof(_nested_build_trees_widgets)
00246 );
00247 
00248 void ShowBuildTreesToolbar()
00249 {
00250   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
00251   AllocateWindowDescFront<BuildTreesWindow>(&_build_trees_desc, 0);
00252 }