# 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 "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   TreeType 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, int click_count)
    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)) {
    00108           this->tree_to_plant = (TreeType)(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)) {
    00114           this->tree_to_plant = TREE_INVALID;
    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
    00152   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00153   {
    00154     if (!gui_scope) return;
    00155     this->base  = _tree_base_by_landscape[_settings_game.game_creation.landscape];
    00156     this->count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
    00157   }
    00158
    00159   virtual void OnTimeout()
    00160   {
    00161     this->RaiseWidget(BTW_MANY_RANDOM);
    00162     this->SetWidgetDirty(BTW_MANY_RANDOM);
    00163   }
    00164
    00165   virtual void OnPlaceObjectAbort()
    00166   {
    00167     this->RaiseButtons();
    00168   }
    00169 };
    00170
    00171 static const NWidgetPart _nested_build_trees_widgets[] = {
    00172   NWidget(NWID_HORIZONTAL),
    00173     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00174     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00175     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
    00176     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00177   EndContainer(),
    00178   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00179     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00180     NWidget(NWID_HORIZONTAL),
    00181       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00182       NWidget(NWID_VERTICAL),
    00183         NWidget(NWID_HORIZONTAL),
    00184           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_11), 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_12), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00188           EndContainer(),
    00189           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00190           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_13), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00191           EndContainer(),
    00192           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00193           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_14), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00194           EndContainer(),
    00195         EndContainer(),
    00196         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00197         NWidget(NWID_HORIZONTAL),
    00198           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_21), 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_22), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00202           EndContainer(),
    00203           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00204           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_23), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00205           EndContainer(),
    00206           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00207           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_24), 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, BTW_TYPE_31), 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_32), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00216           EndContainer(),
    00217           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00218           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_33), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00219           EndContainer(),
    00220           NWidget(NWID_SPACER), SetMinimalSize(1, 0),
    00221           NWidget(WWT_PANEL, COLOUR_GREY, BTW_TYPE_34), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
    00222           EndContainer(),
    00223         EndContainer(),
    00224         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00225         NWidget(WWT_TEXTBTN, COLOUR_GREY, BTW_TYPE_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
    00226         NWidget(NWID_SPACER), SetMinimalSize(0, 1),
    00227         NWidget(WWT_TEXTBTN, COLOUR_GREY, BTW_MANY_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
    00228         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    00229       EndContainer(),
    00230       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    00231     EndContainer(),
    00232   EndContainer(),
    00233 };
    00234
    00235 static const WindowDesc _build_trees_desc(
    00236   WDP_AUTO, 0, 0,
    00237   WC_BUILD_TREES, WC_NONE,
    00238   WDF_CONSTRUCTION,
    00239   _nested_build_trees_widgets, lengthof(_nested_build_trees_widgets)
    00240 );
    00241
    00242 void ShowBuildTreesToolbar()
    00243 {
    00244   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
    00245   AllocateWindowDescFront<BuildTreesWindow>(&_build_trees_desc, 0);
    00246 }