# tree_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: tree_gui.cpp 18588 2009-12-21 16:24:29Z alberth $ */
    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 "openttd.h"
    00014 #include "window_gui.h"
    00015 #include "gfx_func.h"
    00016 #include "tilehighlight_func.h"
    00017 #include "company_func.h"
    00018 #include "company_base.h"
    00019 #include "command_func.h"
    00020 #include "sound_func.h"
    00021
    00022 #include "table/sprites.h"
    00023 #include "table/strings.h"
    00024 #include "table/tree_land.h"
    00025
    00026 void PlaceTreesRandomly();
    00027
    00029 enum BuildTreesWidgets {
    00030   BTW_TYPE_11,
    00031   BTW_TYPE_12,
    00032   BTW_TYPE_13,
    00033   BTW_TYPE_14,
    00034   BTW_TYPE_21,
    00035   BTW_TYPE_22,
    00036   BTW_TYPE_23,
    00037   BTW_TYPE_24,
    00038   BTW_TYPE_31,
    00039   BTW_TYPE_32,
    00040   BTW_TYPE_33,
    00041   BTW_TYPE_34,
    00042   BTW_TYPE_RANDOM,
    00043   BTW_MANY_RANDOM,
    00044 };
    00045
    00049 class BuildTreesWindow : public Window
    00050 {
    00051   uint16 base;
    00052   uint16 count;
    00053   uint tree_to_plant;
    00054
    00055 public:
    00056   BuildTreesWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00057   {
    00058     this->InitNested(desc, window_number);
    00059     ResetObjectToPlace();
    00060   }
    00061
    00062   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00063   {
    00064     if (widget != BTW_MANY_RANDOM) return;
    00065
    00066     if (_game_mode != GM_EDITOR) {
    00067       size->width = 0;
    00068       size->height = 0;
    00069     }
    00070   }
    00071
    00072   virtual void OnPaint()
    00073   {
    00074     this->OnInvalidateData(0);
    00075     this->DrawWidgets();
    00076   }
    00077
    00078   virtual void DrawWidget(const Rect &r, int widget) const
    00079   {
    00080     static const PalSpriteID tree_sprites[] = {
    00081       { 0x655, PAL_NONE }, { 0x663, PAL_NONE }, { 0x678, PAL_NONE }, { 0x62B, PAL_NONE },
    00082       { 0x647, PAL_NONE }, { 0x639, PAL_NONE }, { 0x64E, PAL_NONE }, { 0x632, PAL_NONE },
    00083       { 0x67F, PAL_NONE }, { 0x68D, PAL_NONE }, { 0x69B, PAL_NONE }, { 0x6A9, PAL_NONE },
    00084       { 0x6AF, PAL_NONE }, { 0x6D2, PAL_NONE }, { 0x6D9, PAL_NONE }, { 0x6C4, PAL_NONE },
    00085       { 0x6CB, PAL_NONE }, { 0x6B6, PAL_NONE }, { 0x6BD, PAL_NONE }, { 0x6E0, PAL_NONE },
    00086       { 0x72E, PAL_NONE }, { 0x734, PAL_NONE }, { 0x74A, PAL_NONE }, { 0x74F, PAL_NONE },
    00087       { 0x76B, PAL_NONE }, { 0x78F, PAL_NONE }, { 0x788, PAL_NONE }, { 0x77B, PAL_NONE },
    00088       { 0x75F, PAL_NONE }, { 0x774, PAL_NONE }, { 0x720, PAL_NONE }, { 0x797, PAL_NONE },
    00089       { 0x79E, PAL_NONE }, { 0x7A5, PALETTE_TO_GREEN }, { 0x7AC, PALETTE_TO_RED }, { 0x7B3, PAL_NONE },
    00090       { 0x7BA, PAL_NONE }, { 0x7C1, PALETTE_TO_RED, }, { 0x7C8, PALETTE_TO_PALE_GREEN }, { 0x7CF, PALETTE_TO_YELLOW }, { 0x7D6, PALETTE_TO_RED }
    00091     };
    00092
    00093     if (widget < BTW_TYPE_11 || widget > BTW_TYPE_34 || widget - BTW_TYPE_11 >= this->count) return;
    00094
    00095     int i = this->base + widget - BTW_TYPE_11;
    00096     DrawSprite(tree_sprites[i].sprite, tree_sprites[i].pal, (r.left + r.right) / 2, r.bottom - 7);
    00097   }
    00098
    00099   virtual void OnClick(Point pt, int widget)
    00100   {
    00101     switch (widget) {
    00102       case BTW_TYPE_11: case BTW_TYPE_12: case BTW_TYPE_13: case BTW_TYPE_14:
    00103       case BTW_TYPE_21: case BTW_TYPE_22: case BTW_TYPE_23: case BTW_TYPE_24:
    00104       case BTW_TYPE_31: case BTW_TYPE_32: case BTW_TYPE_33: case BTW_TYPE_34:
    00105         if (widget - BTW_TYPE_11 >= this->count) break;
    00106
    00107         if (HandlePlacePushButton(this, widget, SPR_CURSOR_TREE, HT_RECT, NULL)) {
    00108           this->tree_to_plant = this->base + widget - BTW_TYPE_11;
    00109         }
    00110         break;
    00111
    00112       case BTW_TYPE_RANDOM: // tree of random type.
    00113         if (HandlePlacePushButton(this, BTW_TYPE_RANDOM, SPR_CURSOR_TREE, HT_RECT, NULL)) {
    00114           this->tree_to_plant = UINT_MAX;
    00115         }
    00116         break;
    00117
    00118       case BTW_MANY_RANDOM: // place trees randomly over the landscape
    00119         this->LowerWidget(BTW_MANY_RANDOM);
    00120         this->flags4 |= WF_TIMEOUT_BEGIN;
    00121         SndPlayFx(SND_15_BEEP);
    00122         PlaceTreesRandomly();
    00123         MarkWholeScreenDirty();
    00124         break;
    00125     }
    00126   }
    00127
    00128   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00129   {
    00130     VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_PLANT_TREES);
    00131     VpSetPlaceSizingLimit(20);
    00132   }
    00133
    00134   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
    00135   {
    00136     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
    00137   }
    00138
    00139   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
    00140   {
    00141     if (pt.x != -1 && select_proc == DDSP_PLANT_TREES) {
    00142       DoCommandP(end_tile, this->tree_to_plant, start_tile,
    00143         CMD_PLANT_TREE | CMD_MSG(STR_ERROR_CAN_T_PLANT_TREE_HERE));
    00144     }
    00145   }
    00146
    00147   virtual void OnInvalidateData(int data = 0)
    00148   {
    00149     this->base  = _tree_base_by_landscape[_settings_game.game_creation.landscape];
    00150     this->count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
    00151   }
    00152
    00153   virtual void OnTimeout()
    00154   {
    00155     this->RaiseWidget(BTW_MANY_RANDOM);
    00156     this->SetWidgetDirty(BTW_MANY_RANDOM);
    00157   }
    00158
    00159   virtual void OnPlaceObjectAbort()
    00160   {
    00161     this->RaiseButtons();
    00162   }
    00163 };
    00164
    00165 static const NWidgetPart _nested_build_trees_widgets[] = {
    00166   NWidget(NWID_HORIZONTAL),
    00167     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00168     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00169     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00170     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00171   EndContainer(),
    00172   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00173     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00174     NWidget(NWID_HORIZONTAL),
    00175       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00176       NWidget(NWID_VERTICAL),
    00177         NWidget(NWID_HORIZONTAL),
    00178           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_11), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00179           EndContainer(),
    00180           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00181           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_12), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00182           EndContainer(),
    00183           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00184           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_13), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00185           EndContainer(),
    00186           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00187           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_14), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00188           EndContainer(),
    00189         EndContainer(),
    00190         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00191         NWidget(NWID_HORIZONTAL),
    00192           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_21), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00193           EndContainer(),
    00194           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00195           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_22), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00196           EndContainer(),
    00197           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00198           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_23), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00199           EndContainer(),
    00200           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00201           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_24), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00202           EndContainer(),
    00203         EndContainer(),
    00204         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00205         NWidget(NWID_HORIZONTAL),
    00206           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_31), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00207           EndContainer(),
    00208           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00209           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_32), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00210           EndContainer(),
    00211           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00212           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_33), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00213           EndContainer(),
    00214           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00215           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_34), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00216           EndContainer(),
    00217         EndContainer(),
    00218         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00219         NWidget(WWT_TEXTBTN, COLOUR_GREY, BTW_TYPE_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
    00220         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00221         NWidget(WWT_TEXTBTN, COLOUR_GREY, BTW_MANY_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
    00222         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00223       EndContainer(),
    00224       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00225     EndContainer(),
    00226   EndContainer(),
    00227 };
    00228
    00229 static const WindowDesc _build_trees_desc(
    00230   WDP_AUTO, 0, 0,
    00231   WC_BUILD_TREES, WC_NONE,
    00232   WDF_CONSTRUCTION,
    00233   _nested_build_trees_widgets, lengthof(_nested_build_trees_widgets)
    00234 );
    00235
    00236 void ShowBuildTreesToolbar()
    00237 {
    00238   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
    00239   AllocateWindowDescFront<BuildTreesWindow>(&_build_trees_desc, 0);
    00240 }