4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file tree_gui.cpp GUIs for building trees. */
13 #include "window_gui.h"
15 #include "tilehighlight_func.h"
16 #include "company_func.h"
17 #include "company_base.h"
18 #include "command_func.h"
19 #include "sound_func.h"
20 #include "map/ground.h"
22 #include "widgets/tree_widget.h"
24 #include "table/sprites.h"
25 #include "table/strings.h"
26 #include "table/tree_land.h"
28 void PlaceTreesRandomly();
30 /** Tree Sprites with their palettes */
31 const PalSpriteID tree_sprites
[] = {
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
}
46 * The build trees window.
48 class BuildTreesWindow
: public Window
50 uint16 base
; ///< Base tree number used for drawing the window.
51 uint16 count
; ///< Number of different trees available.
52 TreeType tree_to_plant
; ///< Tree number to plant, \c TREE_INVALID for a random tree.
55 BuildTreesWindow (const WindowDesc
*desc
, WindowNumber window_number
) :
56 Window (desc
), base (0), count (0), tree_to_plant (TREE_INVALID
)
58 this->InitNested(window_number
);
63 * Calculate the maximum size of all tree sprites
64 * @return Dimension of the largest tree sprite
66 Dimension
GetMaxTreeSpriteSize()
68 Dimension size
, this_size
;
70 /* Avoid to use it uninitialized */
71 size
.width
= 32; // default width - 2
72 size
.height
= 39; // default height - 7
76 for (int i
= this->base
; i
< this->base
+ this->count
; i
++) {
77 if (i
>= (int)lengthof(tree_sprites
)) return size
;
78 this_size
= GetSpriteSize(tree_sprites
[i
].sprite
, &offset
);
79 size
.width
= max
<int>(size
.width
, 2 * max
<int>(this_size
.width
, -offset
.x
));
80 size
.height
= max
<int>(size
.height
, max
<int>(this_size
.height
, -offset
.y
));
86 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
88 if (widget
>= WID_BT_TYPE_11
&& widget
<= WID_BT_TYPE_34
) {
89 Dimension d
= GetMaxTreeSpriteSize();
90 /* Allow some pixels extra width and height */
91 size
->width
= d
.width
+ WD_FRAMERECT_LEFT
+ WD_FRAMERECT_RIGHT
;
92 size
->height
= d
.height
+ WD_FRAMERECT_RIGHT
+ WD_FRAMERECT_BOTTOM
+ 7; // we need some more space
96 if (widget
!= WID_BT_MANY_RANDOM
) return;
98 if (_game_mode
!= GM_EDITOR
) {
104 void DrawWidget (BlitArea
*dpi
, const Rect
&r
, int widget
) const OVERRIDE
106 if (widget
< WID_BT_TYPE_11
|| widget
> WID_BT_TYPE_34
|| widget
- WID_BT_TYPE_11
>= this->count
) return;
108 int i
= this->base
+ widget
- WID_BT_TYPE_11
;
109 /* Trees "grow" in the centre on the bottom line of the buttons */
110 DrawSprite (dpi
, tree_sprites
[i
].sprite
, tree_sprites
[i
].pal
, (r
.left
+ r
.right
) / 2 + WD_FRAMERECT_LEFT
, r
.bottom
- 7);
113 virtual void OnClick(Point pt
, int widget
, int click_count
)
116 case WID_BT_TYPE_11
: case WID_BT_TYPE_12
: case WID_BT_TYPE_13
: case WID_BT_TYPE_14
:
117 case WID_BT_TYPE_21
: case WID_BT_TYPE_22
: case WID_BT_TYPE_23
: case WID_BT_TYPE_24
:
118 case WID_BT_TYPE_31
: case WID_BT_TYPE_32
: case WID_BT_TYPE_33
: case WID_BT_TYPE_34
:
119 if (widget
- WID_BT_TYPE_11
>= this->count
) break;
121 if (HandlePlacePushButton (this, widget
, SPR_CURSOR_TREE
, POINTER_TILE
)) {
122 this->tree_to_plant
= (TreeType
)(this->base
+ widget
- WID_BT_TYPE_11
);
126 case WID_BT_TYPE_RANDOM
: // tree of random type.
127 if (HandlePlacePushButton (this, WID_BT_TYPE_RANDOM
, SPR_CURSOR_TREE
, POINTER_TILE
)) {
128 this->tree_to_plant
= TREE_INVALID
;
132 case WID_BT_MANY_RANDOM
: // place trees randomly over the landscape
133 if (_settings_client
.sound
.confirm
) SndPlayFx(SND_15_BEEP
);
134 PlaceTreesRandomly();
135 MarkWholeScreenDirty();
140 virtual void OnPlaceObject(Point pt
, TileIndex tile
)
142 VpStartPlaceSizing (tile
, VPM_X_AND_Y
);
145 void OnPlaceMouseUp (int userdata
, Point pt
, TileIndex start_tile
, TileIndex end_tile
) OVERRIDE
148 DoCommandP(end_tile
, this->tree_to_plant
, start_tile
, CMD_PLANT_TREE
);
153 * Initialize the window data
155 virtual void OnInit()
157 this->base
= _tree_base_by_landscape
[_settings_game
.game_creation
.landscape
];
158 this->count
= _tree_count_by_landscape
[_settings_game
.game_creation
.landscape
];
161 virtual void OnPlaceObjectAbort()
163 this->RaiseButtons();
167 static const NWidgetPart _nested_build_trees_widgets
[] = {
168 NWidget(NWID_HORIZONTAL
),
169 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
170 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_PLANT_TREE_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
171 NWidget(WWT_SHADEBOX
, COLOUR_DARK_GREEN
),
172 NWidget(WWT_STICKYBOX
, COLOUR_DARK_GREEN
),
174 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
175 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
176 NWidget(NWID_HORIZONTAL
),
177 NWidget(NWID_SPACER
), SetMinimalSize(2, 0),
178 NWidget(NWID_VERTICAL
),
179 NWidget(NWID_HORIZONTAL
),
180 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_11
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
182 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
183 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_12
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
185 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
186 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_13
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
188 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
189 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_14
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
192 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
193 NWidget(NWID_HORIZONTAL
),
194 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_21
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
196 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
197 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_22
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
199 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
200 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_23
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
202 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
203 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_24
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
206 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
207 NWidget(NWID_HORIZONTAL
),
208 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_31
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
210 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
211 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_32
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
213 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
214 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_33
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
216 NWidget(NWID_SPACER
), SetMinimalSize(1, 0),
217 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BT_TYPE_34
), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP
),
220 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
221 NWidget(WWT_TEXTBTN
, COLOUR_GREY
, WID_BT_TYPE_RANDOM
), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE
, STR_TREES_RANDOM_TYPE_TOOLTIP
),
222 NWidget(NWID_SPACER
), SetMinimalSize(0, 1),
223 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_BT_MANY_RANDOM
), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON
, STR_TREES_RANDOM_TREES_TOOLTIP
),
224 NWidget(NWID_SPACER
), SetMinimalSize(0, 2),
226 NWidget(NWID_SPACER
), SetMinimalSize(2, 0),
231 static WindowDesc::Prefs
_build_trees_prefs ("build_tree");
233 static const WindowDesc
_build_trees_desc(
235 WC_BUILD_TREES
, WC_NONE
,
237 _nested_build_trees_widgets
, lengthof(_nested_build_trees_widgets
),
241 void ShowBuildTreesToolbar()
243 if (_game_mode
!= GM_EDITOR
&& !Company::IsValidID(_local_company
)) return;
244 AllocateWindowDescFront
<BuildTreesWindow
>(&_build_trees_desc
, 0);