# tree_gui.cpp

Go to the documentation of this file.

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