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/>.
8 /** @file tree_gui.cpp GUIs for building trees. */
11 #include "window_gui.h"
13 #include "tilehighlight_func.h"
14 #include "company_func.h"
15 #include "company_base.h"
16 #include "command_func.h"
17 #include "core/random_func.hpp"
18 #include "sound_func.h"
19 #include "strings_func.h"
20 #include "zoom_func.h"
23 #include "widgets/tree_widget.h"
25 #include "table/sprites.h"
26 #include "table/strings.h"
27 #include "table/tree_land.h"
29 #include "safeguards.h"
31 void PlaceTreesRandomly();
32 uint
PlaceTreeGroupAroundTile(TileIndex tile
, TreeType treetype
, uint radius
, uint count
, bool set_zone
);
34 /** Tree Sprites with their palettes */
35 const PalSpriteID tree_sprites
[] = {
36 { 1621, PAL_NONE
}, { 1635, PAL_NONE
}, { 1656, PAL_NONE
}, { 1579, PAL_NONE
},
37 { 1607, PAL_NONE
}, { 1593, PAL_NONE
}, { 1614, PAL_NONE
}, { 1586, PAL_NONE
},
38 { 1663, PAL_NONE
}, { 1677, PAL_NONE
}, { 1691, PAL_NONE
}, { 1705, PAL_NONE
},
39 { 1711, PAL_NONE
}, { 1746, PAL_NONE
}, { 1753, PAL_NONE
}, { 1732, PAL_NONE
},
40 { 1739, PAL_NONE
}, { 1718, PAL_NONE
}, { 1725, PAL_NONE
}, { 1760, PAL_NONE
},
41 { 1838, PAL_NONE
}, { 1844, PAL_NONE
}, { 1866, PAL_NONE
}, { 1871, PAL_NONE
},
42 { 1899, PAL_NONE
}, { 1935, PAL_NONE
}, { 1928, PAL_NONE
}, { 1915, PAL_NONE
},
43 { 1887, PAL_NONE
}, { 1908, PAL_NONE
}, { 1824, PAL_NONE
}, { 1943, PAL_NONE
},
44 { 1950, PAL_NONE
}, { 1957, PALETTE_TO_GREEN
}, { 1964, PALETTE_TO_RED
}, { 1971, PAL_NONE
},
45 { 1978, PAL_NONE
}, { 1985, PALETTE_TO_RED
, }, { 1992, PALETTE_TO_PALE_GREEN
}, { 1999, PALETTE_TO_YELLOW
}, { 2006, PALETTE_TO_RED
}
49 * Calculate the maximum size of all tree sprites
50 * @return Dimension of the largest tree sprite
52 static Dimension
GetMaxTreeSpriteSize()
54 const uint16 base
= _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
];
55 const uint16 count
= _tree_count_by_landscape
[_settings_game
.game_creation
.landscape
];
57 Dimension size
, this_size
;
59 /* Avoid to use it uninitialized */
60 size
.width
= 32; // default width - WD_FRAMERECT_LEFT
61 size
.height
= 39; // default height - BUTTON_BOTTOM_OFFSET
65 for (int i
= base
; i
< base
+ count
; i
++) {
66 if (i
>= (int)lengthof(tree_sprites
)) return size
;
67 this_size
= GetSpriteSize(tree_sprites
[i
].sprite
, &offset
);
68 size
.width
= std::max
<int>(size
.width
, 2 * std::max
<int>(this_size
.width
, -offset
.x
));
69 size
.height
= std::max
<int>(size
.height
, std::max
<int>(this_size
.height
, -offset
.y
));
77 * The build trees window.
79 class BuildTreesWindow
: public Window
81 /** Visual Y offset of tree root from the bottom of the tree type buttons */
82 static const int BUTTON_BOTTOM_OFFSET
= 7;
90 int tree_to_plant
; ///< Tree number to plant, \c TREE_INVALID for a random tree.
91 PlantingMode mode
; ///< Current mode for planting
94 * Update the GUI and enable/disable planting to reflect selected options.
100 const int current_tree
= this->tree_to_plant
;
102 if (this->tree_to_plant
>= 0) {
103 /* Activate placement */
104 if (_settings_client
.sound
.confirm
) SndPlayFx(SND_15_BEEP
);
105 SetObjectToPlace(SPR_CURSOR_TREE
, PAL_NONE
, HT_RECT
, this->window_class
, this->window_number
);
106 this->tree_to_plant
= current_tree
; // SetObjectToPlace may call ResetObjectToPlace which may reset tree_to_plant to -1
108 /* Deactivate placement */
109 ResetObjectToPlace();
112 if (this->tree_to_plant
== TREE_INVALID
) {
113 this->LowerWidget(WID_BT_TYPE_RANDOM
);
114 } else if (this->tree_to_plant
>= 0) {
115 this->LowerWidget(WID_BT_TYPE_BUTTON_FIRST
+ this->tree_to_plant
);
118 switch (this->mode
) {
119 case PM_NORMAL
: this->LowerWidget(WID_BT_MODE_NORMAL
); break;
120 case PM_FOREST_SM
: this->LowerWidget(WID_BT_MODE_FOREST_SM
); break;
121 case PM_FOREST_LG
: this->LowerWidget(WID_BT_MODE_FOREST_LG
); break;
122 default: NOT_REACHED();
128 void DoPlantForest(TileIndex tile
)
130 TreeType treetype
= (TreeType
)this->tree_to_plant
;
131 if (this->tree_to_plant
== TREE_INVALID
) {
132 treetype
= (TreeType
)(InteractiveRandomRange(_tree_count_by_landscape
[_settings_game
.game_creation
.landscape
]) + _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
]);
134 const uint radius
= this->mode
== PM_FOREST_LG
? 12 : 5;
135 const uint count
= this->mode
== PM_FOREST_LG
? 12 : 5;
136 // Create tropic zones only when the tree type is selected by the user and not picked randomly.
137 PlaceTreeGroupAroundTile(tile
, treetype
, radius
, count
, this->tree_to_plant
!= TREE_INVALID
);
141 BuildTreesWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
), tree_to_plant(-1), mode(PM_NORMAL
)
143 this->InitNested(window_number
);
144 ResetObjectToPlace();
146 this->LowerWidget(WID_BT_MODE_NORMAL
);
148 /* Show scenario editor tools in editor */
149 auto *se_tools
= this->GetWidget
<NWidgetStacked
>(WID_BT_SE_PANE
);
150 if (_game_mode
!= GM_EDITOR
) {
151 se_tools
->SetDisplayedPlane(SZSP_HORIZONTAL
);
156 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
158 if (widget
>= WID_BT_TYPE_BUTTON_FIRST
) {
159 /* Ensure tree type buttons are sized after the largest tree type */
160 Dimension d
= GetMaxTreeSpriteSize();
161 size
->width
= d
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
162 size
->height
= d
.height
+ WD_FRAMERECT_RIGHT
+ WD_FRAMERECT_BOTTOM
+ ScaleGUITrad(BUTTON_BOTTOM_OFFSET
); // we need some more space
166 void DrawWidget(const Rect
&r
, int widget
) const override
168 if (widget
>= WID_BT_TYPE_BUTTON_FIRST
) {
169 const int index
= widget
- WID_BT_TYPE_BUTTON_FIRST
;
170 /* Trees "grow" in the centre on the bottom line of the buttons */
171 DrawSprite(tree_sprites
[index
].sprite
, tree_sprites
[index
].pal
, (r
.left
+ r
.right
) / 2 + WD_FRAMERECT_LEFT
, r
.bottom
- ScaleGUITrad(BUTTON_BOTTOM_OFFSET
));
175 void OnClick(Point pt
, int widget
, int click_count
) override
178 case WID_BT_TYPE_RANDOM
: // tree of random type.
179 this->tree_to_plant
= this->tree_to_plant
== TREE_INVALID
? -1 : TREE_INVALID
;
183 case WID_BT_MANY_RANDOM
: // place trees randomly over the landscape
184 if (_settings_client
.sound
.confirm
) SndPlayFx(SND_15_BEEP
);
185 PlaceTreesRandomly();
186 MarkWholeScreenDirty();
189 case WID_BT_MODE_NORMAL
:
190 this->mode
= PM_NORMAL
;
194 case WID_BT_MODE_FOREST_SM
:
195 assert(_game_mode
== GM_EDITOR
);
196 this->mode
= PM_FOREST_SM
;
200 case WID_BT_MODE_FOREST_LG
:
201 assert(_game_mode
== GM_EDITOR
);
202 this->mode
= PM_FOREST_LG
;
207 if (widget
>= WID_BT_TYPE_BUTTON_FIRST
) {
208 const int index
= widget
- WID_BT_TYPE_BUTTON_FIRST
;
209 this->tree_to_plant
= this->tree_to_plant
== index
? -1 : index
;
216 void OnPlaceObject(Point pt
, TileIndex tile
) override
218 if (_game_mode
!= GM_EDITOR
&& this->mode
== PM_NORMAL
) {
219 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_PLANT_TREES
);
221 VpStartDragging(DDSP_PLANT_TREES
);
225 void OnPlaceDrag(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
) override
227 if (_game_mode
!= GM_EDITOR
&& this->mode
== PM_NORMAL
) {
228 VpSelectTilesWithMethod(pt
.x
, pt
.y
, select_method
);
230 TileIndex tile
= TileVirtXY(pt
.x
, pt
.y
);
232 if (this->mode
== PM_NORMAL
) {
233 DoCommandP(tile
, this->tree_to_plant
, tile
, CMD_PLANT_TREE
);
235 this->DoPlantForest(tile
);
240 void OnPlaceMouseUp(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
, TileIndex start_tile
, TileIndex end_tile
) override
242 if (_game_mode
!= GM_EDITOR
&& this->mode
== PM_NORMAL
&& pt
.x
!= -1 && select_proc
== DDSP_PLANT_TREES
) {
243 DoCommandP(end_tile
, this->tree_to_plant
, start_tile
, CMD_PLANT_TREE
| CMD_MSG(STR_ERROR_CAN_T_PLANT_TREE_HERE
));
247 void OnPlaceObjectAbort() override
249 this->tree_to_plant
= -1;
255 * Make widgets for the current available tree types.
256 * This does not use a NWID_MATRIX or WWT_MATRIX control as those are more difficult to
257 * get producing the correct result than dynamically building the widgets is.
258 * @see NWidgetFunctionType
260 static NWidgetBase
*MakeTreeTypeButtons(int *biggest_index
)
262 const byte type_base
= _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
];
263 const byte type_count
= _tree_count_by_landscape
[_settings_game
.game_creation
.landscape
];
265 /* Toyland has 9 tree types, which look better in 3x3 than 4x3 */
266 const int num_columns
= type_count
== 9 ? 3 : 4;
267 const int num_rows
= CeilDiv(type_count
, num_columns
);
268 byte cur_type
= type_base
;
270 NWidgetVertical
*vstack
= new NWidgetVertical(NC_EQUALSIZE
);
271 vstack
->SetPIP(0, 1, 0);
273 for (int row
= 0; row
< num_rows
; row
++) {
274 NWidgetHorizontal
*hstack
= new NWidgetHorizontal(NC_EQUALSIZE
);
275 hstack
->SetPIP(0, 1, 0);
277 for (int col
= 0; col
< num_columns
; col
++) {
278 if (cur_type
> type_base
+ type_count
) break;
279 NWidgetBackground
*button
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_BUTTON_FIRST
+ cur_type
);
280 button
->SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
);
282 *biggest_index
= WID_BT_TYPE_BUTTON_FIRST
+ cur_type
;
290 static const NWidgetPart _nested_build_trees_widgets
[] = {
291 NWidget(NWID_HORIZONTAL
),
292 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
293 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_PLANT_TREE_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
294 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
295 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
297 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
298 NWidget(NWID_VERTICAL
), SetPadding(2),
299 NWidgetFunction(MakeTreeTypeButtons
),
300 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
301 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BT_TYPE_RANDOM
), SetDataTip(STR_TREES_RANDOM_TYPE
, STR_TREES_RANDOM_TYPE_TOOLTIP
),
302 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BT_SE_PANE
),
303 NWidget(NWID_VERTICAL
),
304 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
305 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
306 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BT_MODE_NORMAL
), SetFill(1, 0), SetDataTip(STR_TREES_MODE_NORMAL_BUTTON
, STR_TREES_MODE_NORMAL_TOOLTIP
),
307 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BT_MODE_FOREST_SM
), SetFill(1, 0), SetDataTip(STR_TREES_MODE_FOREST_SM_BUTTON
, STR_TREES_MODE_FOREST_SM_TOOLTIP
),
308 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BT_MODE_FOREST_LG
), SetFill(1, 0), SetDataTip(STR_TREES_MODE_FOREST_LG_BUTTON
, STR_TREES_MODE_FOREST_LG_TOOLTIP
),
310 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
311 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_BT_MANY_RANDOM
), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON
, STR_TREES_RANDOM_TREES_TOOLTIP
),
318 static WindowDesc
_build_trees_desc(
319 WDP_AUTO
, "build_tree", 0, 0,
320 WC_BUILD_TREES
, WC_NONE
,
322 _nested_build_trees_widgets
, lengthof(_nested_build_trees_widgets
)
325 void ShowBuildTreesToolbar()
327 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
328 AllocateWindowDescFront
<BuildTreesWindow
>(&_build_trees_desc
, 0);