Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / tree_gui.cpp
blob4f683ee9ea68888a95e74d91168018f1bf896187
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 */
8 /** @file tree_gui.cpp GUIs for building trees. */
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 "core/random_func.hpp"
18 #include "sound_func.h"
19 #include "strings_func.h"
20 #include "zoom_func.h"
21 #include "tree_map.h"
22 #include "tree_cmd.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 }
49 /**
50 * Calculate the maximum size of all tree sprites
51 * @return Dimension of the largest tree sprite
53 static Dimension GetMaxTreeSpriteSize()
55 const uint16_t base = _tree_base_by_landscape[_settings_game.game_creation.landscape];
56 const uint16_t count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
58 Dimension size, this_size;
59 Point offset;
60 /* Avoid to use it uninitialized */
61 size.width = ScaleGUITrad(32); // default width - WD_FRAMERECT_LEFT
62 size.height = ScaleGUITrad(39); // default height - BUTTON_BOTTOM_OFFSET
63 offset.x = 0;
64 offset.y = 0;
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));
73 return size;
77 /**
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;
85 enum PlantingMode {
86 PM_NORMAL,
87 PM_FOREST_SM,
88 PM_FOREST_LG,
91 int tree_to_plant; ///< Tree number to plant, \c TREE_INVALID for a random tree.
92 PlantingMode mode; ///< Current mode for planting
94 /**
95 * Update the GUI and enable/disable planting to reflect selected options.
97 void UpdateMode()
99 this->RaiseButtons();
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 | HT_DIAGONAL, 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
108 } else {
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();
126 this->SetDirty();
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);
141 public:
142 BuildTreesWindow(WindowDesc &desc, WindowNumber window_number) : Window(desc), tree_to_plant(-1), mode(PM_NORMAL)
144 this->CreateNestedTree();
145 ResetObjectToPlace();
147 this->LowerWidget(WID_BT_MODE_NORMAL);
148 /* Show scenario editor tools in editor */
149 if (_game_mode != GM_EDITOR) {
150 this->GetWidget<NWidgetStacked>(WID_BT_SE_PANE)->SetDisplayedPlane(SZSP_HORIZONTAL);
152 this->FinishInitNested(window_number);
155 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
157 if (widget >= WID_BT_TYPE_BUTTON_FIRST) {
158 /* Ensure tree type buttons are sized after the largest tree type */
159 Dimension d = GetMaxTreeSpriteSize();
160 size.width = d.width + padding.width;
161 size.height = d.height + padding.height + ScaleGUITrad(BUTTON_BOTTOM_OFFSET); // we need some more space
165 void DrawWidget(const Rect &r, WidgetID widget) const override
167 if (widget >= WID_BT_TYPE_BUTTON_FIRST) {
168 const int index = widget - WID_BT_TYPE_BUTTON_FIRST;
169 /* Trees "grow" in the centre on the bottom line of the buttons */
170 DrawSprite(tree_sprites[index].sprite, tree_sprites[index].pal, CenterBounds(r.left, r.right, 0), r.bottom - ScaleGUITrad(BUTTON_BOTTOM_OFFSET));
174 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
176 switch (widget) {
177 case WID_BT_TYPE_RANDOM: // tree of random type.
178 this->tree_to_plant = this->tree_to_plant == TREE_INVALID ? -1 : TREE_INVALID;
179 this->UpdateMode();
180 break;
182 case WID_BT_MANY_RANDOM: // place trees randomly over the landscape
183 if (_settings_client.sound.confirm) SndPlayFx(SND_15_BEEP);
184 PlaceTreesRandomly();
185 MarkWholeScreenDirty();
186 break;
188 case WID_BT_MODE_NORMAL:
189 this->mode = PM_NORMAL;
190 this->UpdateMode();
191 break;
193 case WID_BT_MODE_FOREST_SM:
194 assert(_game_mode == GM_EDITOR);
195 this->mode = PM_FOREST_SM;
196 this->UpdateMode();
197 break;
199 case WID_BT_MODE_FOREST_LG:
200 assert(_game_mode == GM_EDITOR);
201 this->mode = PM_FOREST_LG;
202 this->UpdateMode();
203 break;
205 default:
206 if (widget >= WID_BT_TYPE_BUTTON_FIRST) {
207 const int index = widget - WID_BT_TYPE_BUTTON_FIRST;
208 this->tree_to_plant = this->tree_to_plant == index ? -1 : index;
209 this->UpdateMode();
211 break;
215 void OnPlaceObject([[maybe_unused]] Point pt, TileIndex tile) override
217 if (_game_mode != GM_EDITOR && this->mode == PM_NORMAL) {
218 VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_PLANT_TREES);
219 } else {
220 VpStartDragging(DDSP_PLANT_TREES);
224 void OnPlaceDrag(ViewportPlaceMethod select_method, [[maybe_unused]] ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt) override
226 if (_game_mode != GM_EDITOR && this->mode == PM_NORMAL) {
227 VpSelectTilesWithMethod(pt.x, pt.y, select_method);
228 } else {
229 TileIndex tile = TileVirtXY(pt.x, pt.y);
231 if (this->mode == PM_NORMAL) {
232 Command<CMD_PLANT_TREE>::Post(tile, tile, this->tree_to_plant, false);
233 } else {
234 this->DoPlantForest(tile);
239 void OnPlaceMouseUp([[maybe_unused]] ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, [[maybe_unused]] Point pt, TileIndex start_tile, TileIndex end_tile) override
241 if (_game_mode != GM_EDITOR && this->mode == PM_NORMAL && pt.x != -1 && select_proc == DDSP_PLANT_TREES) {
242 Command<CMD_PLANT_TREE>::Post(STR_ERROR_CAN_T_PLANT_TREE_HERE, end_tile, start_tile, this->tree_to_plant, _ctrl_pressed);
246 void OnPlaceObjectAbort() override
248 this->tree_to_plant = -1;
249 this->UpdateMode();
254 * Make widgets for the current available tree types.
255 * This does not use a NWID_MATRIX or WWT_MATRIX control as those are more difficult to
256 * get producing the correct result than dynamically building the widgets is.
257 * @see NWidgetFunctionType
259 static std::unique_ptr<NWidgetBase> MakeTreeTypeButtons()
261 const uint8_t type_base = _tree_base_by_landscape[_settings_game.game_creation.landscape];
262 const uint8_t type_count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
264 /* Toyland has 9 tree types, which look better in 3x3 than 4x3 */
265 const int num_columns = type_count == 9 ? 3 : 4;
266 const int num_rows = CeilDiv(type_count, num_columns);
267 uint8_t cur_type = type_base;
269 auto vstack = std::make_unique<NWidgetVertical>(NC_EQUALSIZE);
270 vstack->SetPIP(0, 1, 0);
272 for (int row = 0; row < num_rows; row++) {
273 auto hstack = std::make_unique<NWidgetHorizontal>(NC_EQUALSIZE);
274 hstack->SetPIP(0, 1, 0);
275 for (int col = 0; col < num_columns; col++) {
276 if (cur_type > type_base + type_count) break;
277 auto button = std::make_unique<NWidgetBackground>(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_BUTTON_FIRST + cur_type);
278 button->SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP);
279 hstack->Add(std::move(button));
280 cur_type++;
282 vstack->Add(std::move(hstack));
285 return vstack;
288 static constexpr NWidgetPart _nested_build_trees_widgets[] = {
289 NWidget(NWID_HORIZONTAL),
290 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
291 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
292 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
293 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
294 EndContainer(),
295 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
296 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0), SetPadding(2),
297 NWidgetFunction(MakeTreeTypeButtons),
298 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_TYPE_RANDOM), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
299 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_BT_SE_PANE),
300 NWidget(NWID_VERTICAL), SetPIP(0, 1, 0),
301 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
302 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_MODE_NORMAL), SetFill(1, 0), SetDataTip(STR_TREES_MODE_NORMAL_BUTTON, STR_TREES_MODE_NORMAL_TOOLTIP),
303 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),
304 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),
305 EndContainer(),
306 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BT_MANY_RANDOM), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
307 EndContainer(),
308 EndContainer(),
309 EndContainer(),
310 EndContainer(),
313 static WindowDesc _build_trees_desc(
314 WDP_AUTO, "build_tree", 0, 0,
315 WC_BUILD_TREES, WC_NONE,
316 WDF_CONSTRUCTION,
317 _nested_build_trees_widgets
320 void ShowBuildTreesToolbar()
322 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
323 AllocateWindowDescFront<BuildTreesWindow>(_build_trees_desc, 0);