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"
24 #include "widgets/tree_widget.h"
26 #include "table/sprites.h"
27 #include "table/strings.h"
28 #include "table/tree_land.h"
30 #include "safeguards.h"
32 void PlaceTreesRandomly();
33 uint
PlaceTreeGroupAroundTile(TileIndex tile
, TreeType treetype
, uint radius
, uint count
, bool set_zone
);
35 /** Tree Sprites with their palettes */
36 const PalSpriteID tree_sprites
[] = {
37 { 1621, PAL_NONE
}, { 1635, PAL_NONE
}, { 1656, PAL_NONE
}, { 1579, PAL_NONE
},
38 { 1607, PAL_NONE
}, { 1593, PAL_NONE
}, { 1614, PAL_NONE
}, { 1586, PAL_NONE
},
39 { 1663, PAL_NONE
}, { 1677, PAL_NONE
}, { 1691, PAL_NONE
}, { 1705, PAL_NONE
},
40 { 1711, PAL_NONE
}, { 1746, PAL_NONE
}, { 1753, PAL_NONE
}, { 1732, PAL_NONE
},
41 { 1739, PAL_NONE
}, { 1718, PAL_NONE
}, { 1725, PAL_NONE
}, { 1760, PAL_NONE
},
42 { 1838, PAL_NONE
}, { 1844, PAL_NONE
}, { 1866, PAL_NONE
}, { 1871, PAL_NONE
},
43 { 1899, PAL_NONE
}, { 1935, PAL_NONE
}, { 1928, PAL_NONE
}, { 1915, PAL_NONE
},
44 { 1887, PAL_NONE
}, { 1908, PAL_NONE
}, { 1824, PAL_NONE
}, { 1943, PAL_NONE
},
45 { 1950, PAL_NONE
}, { 1957, PALETTE_TO_GREEN
}, { 1964, PALETTE_TO_RED
}, { 1971, PAL_NONE
},
46 { 1978, PAL_NONE
}, { 1985, PALETTE_TO_RED
, }, { 1992, PALETTE_TO_PALE_GREEN
}, { 1999, PALETTE_TO_YELLOW
}, { 2006, PALETTE_TO_RED
}
50 * Calculate the maximum size of all tree sprites
51 * @return Dimension of the largest tree sprite
53 static Dimension
GetMaxTreeSpriteSize()
55 const uint16 base
= _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
];
56 const uint16 count
= _tree_count_by_landscape
[_settings_game
.game_creation
.landscape
];
58 Dimension size
, this_size
;
60 /* Avoid to use it uninitialized */
61 size
.width
= 32; // default width - WD_FRAMERECT_LEFT
62 size
.height
= 39; // default height - BUTTON_BOTTOM_OFFSET
66 for (int i
= base
; i
< base
+ count
; i
++) {
67 if (i
>= (int)lengthof(tree_sprites
)) return size
;
68 this_size
= GetSpriteSize(tree_sprites
[i
].sprite
, &offset
);
69 size
.width
= std::max
<int>(size
.width
, 2 * std::max
<int>(this_size
.width
, -offset
.x
));
70 size
.height
= std::max
<int>(size
.height
, std::max
<int>(this_size
.height
, -offset
.y
));
78 * The build trees window.
80 class BuildTreesWindow
: public Window
82 /** Visual Y offset of tree root from the bottom of the tree type buttons */
83 static const int BUTTON_BOTTOM_OFFSET
= 7;
91 int tree_to_plant
; ///< Tree number to plant, \c TREE_INVALID for a random tree.
92 PlantingMode mode
; ///< Current mode for planting
95 * Update the GUI and enable/disable planting to reflect selected options.
101 const int current_tree
= this->tree_to_plant
;
103 if (this->tree_to_plant
>= 0) {
104 /* Activate placement */
105 if (_settings_client
.sound
.confirm
) SndPlayFx(SND_15_BEEP
);
106 SetObjectToPlace(SPR_CURSOR_TREE
, PAL_NONE
, HT_RECT
, this->window_class
, this->window_number
);
107 this->tree_to_plant
= current_tree
; // SetObjectToPlace may call ResetObjectToPlace which may reset tree_to_plant to -1
109 /* Deactivate placement */
110 ResetObjectToPlace();
113 if (this->tree_to_plant
== TREE_INVALID
) {
114 this->LowerWidget(WID_BT_TYPE_RANDOM
);
115 } else if (this->tree_to_plant
>= 0) {
116 this->LowerWidget(WID_BT_TYPE_BUTTON_FIRST
+ this->tree_to_plant
);
119 switch (this->mode
) {
120 case PM_NORMAL
: this->LowerWidget(WID_BT_MODE_NORMAL
); break;
121 case PM_FOREST_SM
: this->LowerWidget(WID_BT_MODE_FOREST_SM
); break;
122 case PM_FOREST_LG
: this->LowerWidget(WID_BT_MODE_FOREST_LG
); break;
123 default: NOT_REACHED();
129 void DoPlantForest(TileIndex tile
)
131 TreeType treetype
= (TreeType
)this->tree_to_plant
;
132 if (this->tree_to_plant
== TREE_INVALID
) {
133 treetype
= (TreeType
)(InteractiveRandomRange(_tree_count_by_landscape
[_settings_game
.game_creation
.landscape
]) + _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
]);
135 const uint radius
= this->mode
== PM_FOREST_LG
? 12 : 5;
136 const uint count
= this->mode
== PM_FOREST_LG
? 12 : 5;
137 // Create tropic zones only when the tree type is selected by the user and not picked randomly.
138 PlaceTreeGroupAroundTile(tile
, treetype
, radius
, count
, this->tree_to_plant
!= TREE_INVALID
);
142 BuildTreesWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
), tree_to_plant(-1), mode(PM_NORMAL
)
144 this->InitNested(window_number
);
145 ResetObjectToPlace();
147 this->LowerWidget(WID_BT_MODE_NORMAL
);
149 /* Show scenario editor tools in editor */
150 auto *se_tools
= this->GetWidget
<NWidgetStacked
>(WID_BT_SE_PANE
);
151 if (_game_mode
!= GM_EDITOR
) {
152 se_tools
->SetDisplayedPlane(SZSP_HORIZONTAL
);
157 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
159 if (widget
>= WID_BT_TYPE_BUTTON_FIRST
) {
160 /* Ensure tree type buttons are sized after the largest tree type */
161 Dimension d
= GetMaxTreeSpriteSize();
162 size
->width
= d
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
163 size
->height
= d
.height
+ WD_FRAMERECT_RIGHT
+ WD_FRAMERECT_BOTTOM
+ ScaleGUITrad(BUTTON_BOTTOM_OFFSET
); // we need some more space
167 void DrawWidget(const Rect
&r
, int widget
) const override
169 if (widget
>= WID_BT_TYPE_BUTTON_FIRST
) {
170 const int index
= widget
- WID_BT_TYPE_BUTTON_FIRST
;
171 /* Trees "grow" in the centre on the bottom line of the buttons */
172 DrawSprite(tree_sprites
[index
].sprite
, tree_sprites
[index
].pal
, (r
.left
+ r
.right
) / 2 + WD_FRAMERECT_LEFT
, r
.bottom
- ScaleGUITrad(BUTTON_BOTTOM_OFFSET
));
176 void OnClick(Point pt
, int widget
, int click_count
) override
179 case WID_BT_TYPE_RANDOM
: // tree of random type.
180 this->tree_to_plant
= this->tree_to_plant
== TREE_INVALID
? -1 : TREE_INVALID
;
184 case WID_BT_MANY_RANDOM
: // place trees randomly over the landscape
185 if (_settings_client
.sound
.confirm
) SndPlayFx(SND_15_BEEP
);
186 PlaceTreesRandomly();
187 MarkWholeScreenDirty();
190 case WID_BT_MODE_NORMAL
:
191 this->mode
= PM_NORMAL
;
195 case WID_BT_MODE_FOREST_SM
:
196 assert(_game_mode
== GM_EDITOR
);
197 this->mode
= PM_FOREST_SM
;
201 case WID_BT_MODE_FOREST_LG
:
202 assert(_game_mode
== GM_EDITOR
);
203 this->mode
= PM_FOREST_LG
;
208 if (widget
>= WID_BT_TYPE_BUTTON_FIRST
) {
209 const int index
= widget
- WID_BT_TYPE_BUTTON_FIRST
;
210 this->tree_to_plant
= this->tree_to_plant
== index
? -1 : index
;
217 void OnPlaceObject(Point pt
, TileIndex tile
) override
219 if (_game_mode
!= GM_EDITOR
&& this->mode
== PM_NORMAL
) {
220 VpStartPlaceSizing(tile
, VPM_X_AND_Y
, DDSP_PLANT_TREES
);
222 VpStartDragging(DDSP_PLANT_TREES
);
226 void OnPlaceDrag(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
) override
228 if (_game_mode
!= GM_EDITOR
&& this->mode
== PM_NORMAL
) {
229 VpSelectTilesWithMethod(pt
.x
, pt
.y
, select_method
);
231 TileIndex tile
= TileVirtXY(pt
.x
, pt
.y
);
233 if (this->mode
== PM_NORMAL
) {
234 Command
<CMD_PLANT_TREE
>::Post(tile
, tile
, this->tree_to_plant
);
236 this->DoPlantForest(tile
);
241 void OnPlaceMouseUp(ViewportPlaceMethod select_method
, ViewportDragDropSelectionProcess select_proc
, Point pt
, TileIndex start_tile
, TileIndex end_tile
) override
243 if (_game_mode
!= GM_EDITOR
&& this->mode
== PM_NORMAL
&& pt
.x
!= -1 && select_proc
== DDSP_PLANT_TREES
) {
244 Command
<CMD_PLANT_TREE
>::Post(STR_ERROR_CAN_T_PLANT_TREE_HERE
, end_tile
, start_tile
, this->tree_to_plant
);
248 void OnPlaceObjectAbort() override
250 this->tree_to_plant
= -1;
256 * Make widgets for the current available tree types.
257 * This does not use a NWID_MATRIX or WWT_MATRIX control as those are more difficult to
258 * get producing the correct result than dynamically building the widgets is.
259 * @see NWidgetFunctionType
261 static NWidgetBase
*MakeTreeTypeButtons(int *biggest_index
)
263 const byte type_base
= _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
];
264 const byte type_count
= _tree_count_by_landscape
[_settings_game
.game_creation
.landscape
];
266 /* Toyland has 9 tree types, which look better in 3x3 than 4x3 */
267 const int num_columns
= type_count
== 9 ? 3 : 4;
268 const int num_rows
= CeilDiv(type_count
, num_columns
);
269 byte cur_type
= type_base
;
271 NWidgetVertical
*vstack
= new NWidgetVertical(NC_EQUALSIZE
);
272 vstack
->SetPIP(0, 1, 0);
274 for (int row
= 0; row
< num_rows
; row
++) {
275 NWidgetHorizontal
*hstack
= new NWidgetHorizontal(NC_EQUALSIZE
);
276 hstack
->SetPIP(0, 1, 0);
278 for (int col
= 0; col
< num_columns
; col
++) {
279 if (cur_type
> type_base
+ type_count
) break;
280 NWidgetBackground
*button
= new NWidgetBackground(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_BUTTON_FIRST
+ cur_type
);
281 button
->SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
);
283 *biggest_index
= WID_BT_TYPE_BUTTON_FIRST
+ cur_type
;
291 static const NWidgetPart _nested_build_trees_widgets
[] = {
292 NWidget(NWID_HORIZONTAL
),
293 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
294 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_PLANT_TREE_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
295 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
296 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
298 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
299 NWidget(NWID_VERTICAL
), SetPadding(2),
300 NWidgetFunction(MakeTreeTypeButtons
),
301 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
302 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BT_TYPE_RANDOM
), SetDataTip(STR_TREES_RANDOM_TYPE
, STR_TREES_RANDOM_TYPE_TOOLTIP
),
303 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_BT_SE_PANE
),
304 NWidget(NWID_VERTICAL
),
305 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
306 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
307 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BT_MODE_NORMAL
), SetFill(1, 0), SetDataTip(STR_TREES_MODE_NORMAL_BUTTON
, STR_TREES_MODE_NORMAL_TOOLTIP
),
308 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
),
309 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
),
311 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
312 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_BT_MANY_RANDOM
), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON
, STR_TREES_RANDOM_TREES_TOOLTIP
),
319 static WindowDesc
_build_trees_desc(
320 WDP_AUTO
, "build_tree", 0, 0,
321 WC_BUILD_TREES
, WC_NONE
,
323 _nested_build_trees_widgets
, lengthof(_nested_build_trees_widgets
)
326 void ShowBuildTreesToolbar()
328 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
329 AllocateWindowDescFront
<BuildTreesWindow
>(&_build_trees_desc
, 0);