tree_gui.cpp

Go to the documentation of this file.
00001 /* $Id: tree_gui.cpp 23538 2011-12-16 16:57:15Z planetmaker $ */
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_LIMITED, DDSP_PLANT_TREES);
00149     VpSetPlaceSizingLimit(20);
00150   }
00151 
00152   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
00153   {
00154     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
00155   }
00156 
00157   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
00158   {
00159     if (pt.x != -1 && select_proc == DDSP_PLANT_TREES) {
00160       DoCommandP(end_tile, this->tree_to_plant, start_tile,
00161         CMD_PLANT_TREE | CMD_MSG(STR_ERROR_CAN_T_PLANT_TREE_HERE));
00162     }
00163   }
00164 
00168   virtual void OnInit()
00169   {
00170     this->base  = _tree_base_by_landscape[_settings_game.game_creation.landscape];
00171     this->count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
00172   }
00173 
00174   virtual void OnTimeout()
00175   {
00176     this->RaiseWidget(WID_BT_MANY_RANDOM);
00177     this->SetWidgetDirty(WID_BT_MANY_RANDOM);
00178   }
00179 
00180   virtual void OnPlaceObjectAbort()
00181   {
00182     this->RaiseButtons();
00183   }
00184 };
00185 
00186 static const NWidgetPart _nested_build_trees_widgets[] = {
00187   NWidget(NWID_HORIZONTAL),
00188     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00189     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00190     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00191     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00192   EndContainer(),
00193   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00194     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00195     NWidget(NWID_HORIZONTAL),
00196       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00197       NWidget(NWID_VERTICAL),
00198         NWidget(NWID_HORIZONTAL),
00199           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_11), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00200           EndContainer(),
00201           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00202           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_12), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00203           EndContainer(),
00204           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00205           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_13), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00206           EndContainer(),
00207           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00208           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_14), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00209           EndContainer(),
00210         EndContainer(),
00211         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00212         NWidget(NWID_HORIZONTAL),
00213           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_21), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00214           EndContainer(),
00215           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00216           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_22), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00217           EndContainer(),
00218           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00219           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_23), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00220           EndContainer(),
00221           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00222           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_24), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00223           EndContainer(),
00224         EndContainer(),
00225         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00226         NWidget(NWID_HORIZONTAL),
00227           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_31), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00228           EndContainer(),
00229           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00230           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_32), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00231           EndContainer(),
00232           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00233           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_33), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00234           EndContainer(),
00235           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
00236           NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_34), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
00237           EndContainer(),
00238         EndContainer(),
00239         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00240         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_TYPE_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
00241         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00242         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_MANY_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
00243         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00244       EndContainer(),
00245       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
00246     EndContainer(),
00247   EndContainer(),
00248 };
00249 
00250 static const WindowDesc _build_trees_desc(
00251   WDP_AUTO, 0, 0,
00252   WC_BUILD_TREES, WC_NONE,
00253   WDF_CONSTRUCTION,
00254   _nested_build_trees_widgets, lengthof(_nested_build_trees_widgets)
00255 );
00256 
00257 void ShowBuildTreesToolbar()
00258 {
00259   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
00260   AllocateWindowDescFront<BuildTreesWindow>(&_build_trees_desc, 0);
00261 }