OpenTTD
tree_gui.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "window_gui.h"
12 #include "gfx_func.h"
13 #include "tilehighlight_func.h"
14 #include "company_func.h"
15 #include "company_base.h"
16 #include "command_func.h"
17 #include "sound_func.h"
18 #include "tree_map.h"
19 
20 #include "widgets/tree_widget.h"
21 
22 #include "table/sprites.h"
23 #include "table/strings.h"
24 #include "table/tree_land.h"
25 
26 #include "safeguards.h"
27 
28 void PlaceTreesRandomly();
29 
32  { 1621, PAL_NONE }, { 1587, PAL_NONE }, { 1656, PAL_NONE }, { 1579, PAL_NONE },
33  { 1607, PAL_NONE }, { 1593, PAL_NONE }, { 1614, PAL_NONE }, { 1586, PAL_NONE },
34  { 1663, PAL_NONE }, { 1677, PAL_NONE }, { 1691, PAL_NONE }, { 1705, PAL_NONE },
35  { 1711, PAL_NONE }, { 1746, PAL_NONE }, { 1753, PAL_NONE }, { 1732, PAL_NONE },
36  { 1739, PAL_NONE }, { 1718, PAL_NONE }, { 1725, PAL_NONE }, { 1760, PAL_NONE },
37  { 1838, PAL_NONE }, { 1844, PAL_NONE }, { 1866, PAL_NONE }, { 1871, PAL_NONE },
38  { 1899, PAL_NONE }, { 1935, PAL_NONE }, { 1928, PAL_NONE }, { 1915, PAL_NONE },
39  { 1887, PAL_NONE }, { 1908, PAL_NONE }, { 1824, PAL_NONE }, { 1943, PAL_NONE },
40  { 1950, PAL_NONE }, { 1957, PALETTE_TO_GREEN }, { 1964, PALETTE_TO_RED }, { 1971, PAL_NONE },
41  { 1978, PAL_NONE }, { 1985, PALETTE_TO_RED, }, { 1992, PALETTE_TO_PALE_GREEN }, { 1999, PALETTE_TO_YELLOW }, { 2006, PALETTE_TO_RED }
42 };
43 
44 
48 class BuildTreesWindow : public Window
49 {
50  uint16 base;
51  uint16 count;
53 
54 public:
56  {
57  this->InitNested(window_number);
59  }
60 
66  {
67  Dimension size, this_size;
68  Point offset;
69  /* Avoid to use it uninitialized */
70  size.width = 32; // default width - 2
71  size.height = 39; // default height - 7
72  offset.x = 0;
73  offset.y = 0;
74 
75  for (int i = this->base; i < this->base + this->count; i++) {
76  if (i >= (int)lengthof(tree_sprites)) return size;
77  this_size = GetSpriteSize(tree_sprites[i].sprite, &offset);
78  size.width = max<int>(size.width, 2 * max<int>(this_size.width, -offset.x));
79  size.height = max<int>(size.height, max<int>(this_size.height, -offset.y));
80  }
81 
82  return size;
83  }
84 
85  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
86  {
87  if (widget >= WID_BT_TYPE_11 && widget <= WID_BT_TYPE_34) {
89  /* Allow some pixels extra width and height */
90  size->width = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
91  size->height = d.height + WD_FRAMERECT_RIGHT + WD_FRAMERECT_BOTTOM + 7; // we need some more space
92  return;
93  }
94 
95  if (widget != WID_BT_MANY_RANDOM) return;
96 
97  if (_game_mode != GM_EDITOR) {
98  size->width = 0;
99  size->height = 0;
100  }
101  }
102 
103  void DrawWidget(const Rect &r, int widget) const override
104  {
105  if (widget < WID_BT_TYPE_11 || widget > WID_BT_TYPE_34 || widget - WID_BT_TYPE_11 >= this->count) return;
106 
107  int i = this->base + widget - WID_BT_TYPE_11;
108  /* Trees "grow" in the centre on the bottom line of the buttons */
109  DrawSprite(tree_sprites[i].sprite, tree_sprites[i].pal, (r.left + r.right) / 2 + WD_FRAMERECT_LEFT, r.bottom - 7);
110  }
111 
112  void OnClick(Point pt, int widget, int click_count) override
113  {
114  switch (widget) {
118  if (widget - WID_BT_TYPE_11 >= this->count) break;
119 
120  if (HandlePlacePushButton(this, widget, SPR_CURSOR_TREE, HT_RECT)) {
121  this->tree_to_plant = (TreeType)(this->base + widget - WID_BT_TYPE_11);
122  }
123  break;
124 
125  case WID_BT_TYPE_RANDOM: // tree of random type.
126  if (HandlePlacePushButton(this, WID_BT_TYPE_RANDOM, SPR_CURSOR_TREE, HT_RECT)) {
127  this->tree_to_plant = TREE_INVALID;
128  }
129  break;
130 
131  case WID_BT_MANY_RANDOM: // place trees randomly over the landscape
132  if (_settings_client.sound.confirm) SndPlayFx(SND_15_BEEP);
135  break;
136  }
137  }
138 
139  void OnPlaceObject(Point pt, TileIndex tile) override
140  {
142  }
143 
144  void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
145  {
146  VpSelectTilesWithMethod(pt.x, pt.y, select_method);
147  }
148 
149  void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
150  {
151  if (pt.x != -1 && select_proc == DDSP_PLANT_TREES) {
152  DoCommandP(end_tile, this->tree_to_plant, start_tile,
153  CMD_PLANT_TREE | CMD_MSG(STR_ERROR_CAN_T_PLANT_TREE_HERE));
154  }
155  }
156 
160  void OnInit() override
161  {
162  this->base = _tree_base_by_landscape[_settings_game.game_creation.landscape];
163  this->count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
164  }
165 
166  void OnPlaceObjectAbort() override
167  {
168  this->RaiseButtons();
169  }
170 };
171 
172 static const NWidgetPart _nested_build_trees_widgets[] = {
174  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
175  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
176  NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
177  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
178  EndContainer(),
179  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
185  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_11), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
186  EndContainer(),
188  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_12), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
189  EndContainer(),
191  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_13), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
192  EndContainer(),
194  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_14), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
195  EndContainer(),
196  EndContainer(),
199  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_21), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
200  EndContainer(),
202  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_22), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
203  EndContainer(),
205  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_23), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
206  EndContainer(),
208  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_24), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
209  EndContainer(),
210  EndContainer(),
213  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_31), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
214  EndContainer(),
216  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_32), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
217  EndContainer(),
219  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_33), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
220  EndContainer(),
222  NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_34), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
223  EndContainer(),
224  EndContainer(),
226  NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_TYPE_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
228  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BT_MANY_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
230  EndContainer(),
232  EndContainer(),
233  EndContainer(),
234 };
235 
236 static WindowDesc _build_trees_desc(
237  WDP_AUTO, "build_tree", 0, 0,
240  _nested_build_trees_widgets, lengthof(_nested_build_trees_widgets)
241 );
242 
243 void ShowBuildTreesToolbar()
244 {
245  if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
246  AllocateWindowDescFront<BuildTreesWindow>(&_build_trees_desc, 0);
247 }
ViewportDragDropSelectionProcess
Drag and drop selection process, or, what to do with an area of land when you&#39;ve selected it...
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:79
const PalSpriteID tree_sprites[]
Tree Sprites with their palettes.
Definition: tree_gui.cpp:31
Definition of stuff that is very close to a company, like the company struct itself.
ResizeInfo resize
Resize information.
Definition: window_gui.h:322
byte landscape
the landscape we&#39;re currently in
High level window description.
Definition: window_gui.h:166
Tree 3st column 2nd row.
Definition: tree_widget.h:24
Tree 2st column 2nd row.
Definition: tree_widget.h:20
plant a tree
Definition: command_type.h:212
Horizontal container.
Definition: widget_type.h:73
void OnPlaceObjectAbort() override
The user cancelled a tile highlight mode that has been set.
Definition: tree_gui.cpp:166
void PlaceTreesRandomly()
Place some trees randomly.
Definition: tree_cmd.cpp:244
Combination of a palette sprite and a &#39;real&#39; sprite.
Definition: gfx_type.h:22
Close box (at top-left of a window)
Definition: widget_type.h:67
void OnInit() override
Initialize the window data.
Definition: tree_gui.cpp:160
Map accessors for tree tiles.
Button to build many random trees.
Definition: tree_widget.h:28
Tree 2st column 3rd row.
Definition: tree_widget.h:21
Functions, definitions and such used only by the GUI.
An invalid tree.
Definition: tree_map.h:32
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:908
Data structure for an opened window.
Definition: window_gui.h:276
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1857
Types related to the tree widgets.
Invisible widget that takes some space.
Definition: widget_type.h:77
void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
The user has dragged over the map when the tile highlight mode has been set.
Definition: tree_gui.cpp:149
Plant trees.
This window is used for construction; close it whenever changing company.
Definition: window_gui.h:208
uint16 count
Number of different trees available.
Definition: tree_gui.cpp:51
SoundSettings sound
sound effect settings
Sprites to use and how to display them for tree tiles.
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: tree_gui.cpp:85
uint16 base
Base tree number used for drawing the window.
Definition: tree_gui.cpp:50
ViewportPlaceMethod
Viewport place method (type of highlighted area and placed objects)
Definition: viewport_type.h:96
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: tree_gui.cpp:112
void RaiseButtons(bool autoraise=false)
Raise the buttons of the window.
Definition: window.cpp:573
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1012
Functions related to the gfx engine.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:78
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:945
Tree 2st column 1st row.
Definition: tree_widget.h:19
Definition of base types and functions in a cross-platform compatible way.
Tree 1st column 2nd row.
Definition: tree_widget.h:16
A number of safeguards to prevent using unsafe methods.
Tree 3st column 1st row.
Definition: tree_widget.h:23
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:102
rectangle (stations, depots, ...)
Simple depressed panel.
Definition: widget_type.h:48
TreeType tree_to_plant
Tree number to plant, TREE_INVALID for a random tree.
Definition: tree_gui.cpp:52
Button to build random type of tree.
Definition: tree_widget.h:27
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1112
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:63
bool DoCommandP(const CommandContainer *container, bool my_cmd)
Shortcut for the long DoCommandP when having a container with the data.
Definition: command.cpp:532
void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
highlighting tiles while only going over them with the mouse
Definition: viewport.cpp:2605
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: tree_gui.cpp:103
Functions related to sound.
void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method)
Selects tiles while dragging.
Definition: viewport.cpp:3078
Build trees toolbar; Window numbers:
Definition: window_type.h:79
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
Functions related to companies.
area of land in X and Y directions
Tree 1st column 3rd row.
Definition: tree_widget.h:17
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
bool HandlePlacePushButton(Window *w, int widget, CursorID cursor, HighLightStyle mode)
This code is shared for the majority of the pushbuttons.
Definition: main_gui.cpp:98
Vertical container.
Definition: widget_type.h:75
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:997
The build trees window.
Definition: tree_gui.cpp:48
Dimension GetMaxTreeSpriteSize()
Calculate the maximum size of all tree sprites.
Definition: tree_gui.cpp:65
Tree 3st column 4th row.
Definition: tree_widget.h:26
void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
The user is dragging over the map when the tile highlight mode has been set.
Definition: tree_gui.cpp:144
bool confirm
Play sound effect on successful constructions or other actions.
Functions related to commands.
Coordinates of a point in 2D.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:770
TreeType
List of tree types along all landscape types.
Definition: tree_map.h:25
void OnPlaceObject(Point pt, TileIndex tile) override
The user clicked some place on the map when a tile highlight mode has been set.
Definition: tree_gui.cpp:139
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:280
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:61
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
#define CMD_MSG(x)
Used to combine a StringID with the command.
Definition: command_type.h:368
int32 WindowNumber
Number to differentiate different windows of the same class.
Definition: window_type.h:705
GameCreationSettings game_creation
settings used during the creation of a game (map)
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3353
Specification of a rectangle with absolute coordinates of all edges.
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:312
Functions related to tile highlights.
Tree 1st column 1st row.
Definition: tree_widget.h:15
Find a place automatically.
Definition: window_gui.h:154
Tree 1st column 4th row.
Definition: tree_widget.h:18
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:44
Tree 3st column 3rd row.
Definition: tree_widget.h:25
Dimensions (a width and height) of a rectangle in 2D.
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:60
This file contains all sprite-related enums and defines.
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition: widget_type.h:62
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:835
Tree 2st column 4th row.
Definition: tree_widget.h:22
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1462
(Toggle) Button with text
Definition: widget_type.h:53