2008-05-08 13:29:35 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-08 13:29:35 +00:00
|
|
|
/** @file tree_gui.cpp GUIs for building trees. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "window_gui.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "tilehighlight_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
|
|
|
#include "company_base.h"
|
2008-05-08 13:29:35 +00:00
|
|
|
#include "command_func.h"
|
|
|
|
#include "sound_func.h"
|
2010-04-14 19:57:19 +00:00
|
|
|
#include "tree_map.h"
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/tree_widget.h"
|
|
|
|
|
2008-05-08 13:29:35 +00:00
|
|
|
#include "table/sprites.h"
|
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/tree_land.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2008-05-12 21:46:08 +00:00
|
|
|
void PlaceTreesRandomly();
|
|
|
|
|
2011-08-31 22:52:26 +00:00
|
|
|
/** Tree Sprites with their palettes */
|
|
|
|
const PalSpriteID tree_sprites[] = {
|
|
|
|
{ 1621, PAL_NONE }, { 1587, PAL_NONE }, { 1656, PAL_NONE }, { 1579, PAL_NONE },
|
|
|
|
{ 1607, PAL_NONE }, { 1593, PAL_NONE }, { 1614, PAL_NONE }, { 1586, PAL_NONE },
|
|
|
|
{ 1663, PAL_NONE }, { 1677, PAL_NONE }, { 1691, PAL_NONE }, { 1705, PAL_NONE },
|
|
|
|
{ 1711, PAL_NONE }, { 1746, PAL_NONE }, { 1753, PAL_NONE }, { 1732, PAL_NONE },
|
|
|
|
{ 1739, PAL_NONE }, { 1718, PAL_NONE }, { 1725, PAL_NONE }, { 1760, PAL_NONE },
|
|
|
|
{ 1838, PAL_NONE }, { 1844, PAL_NONE }, { 1866, PAL_NONE }, { 1871, PAL_NONE },
|
|
|
|
{ 1899, PAL_NONE }, { 1935, PAL_NONE }, { 1928, PAL_NONE }, { 1915, PAL_NONE },
|
|
|
|
{ 1887, PAL_NONE }, { 1908, PAL_NONE }, { 1824, PAL_NONE }, { 1943, PAL_NONE },
|
|
|
|
{ 1950, PAL_NONE }, { 1957, PALETTE_TO_GREEN }, { 1964, PALETTE_TO_RED }, { 1971, PAL_NONE },
|
|
|
|
{ 1978, PAL_NONE }, { 1985, PALETTE_TO_RED, }, { 1992, PALETTE_TO_PALE_GREEN }, { 1999, PALETTE_TO_YELLOW }, { 2006, PALETTE_TO_RED }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-25 22:07:25 +00:00
|
|
|
/**
|
|
|
|
* The build trees window.
|
|
|
|
*/
|
2008-05-12 21:46:08 +00:00
|
|
|
class BuildTreesWindow : public Window
|
|
|
|
{
|
2009-09-20 21:09:27 +00:00
|
|
|
uint16 base; ///< Base tree number used for drawing the window.
|
|
|
|
uint16 count; ///< Number of different trees available.
|
2010-04-14 19:57:19 +00:00
|
|
|
TreeType tree_to_plant; ///< Tree number to plant, \c TREE_INVALID for a random tree.
|
2008-05-12 21:46:08 +00:00
|
|
|
|
|
|
|
public:
|
2013-05-26 19:23:42 +00:00
|
|
|
BuildTreesWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
|
2008-05-12 21:46:08 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->InitNested(window_number);
|
2009-09-20 21:09:27 +00:00
|
|
|
ResetObjectToPlace();
|
|
|
|
}
|
|
|
|
|
2011-08-31 22:52:26 +00:00
|
|
|
/**
|
|
|
|
* Calculate the maximum size of all tree sprites
|
|
|
|
* @return Dimension of the largest tree sprite
|
|
|
|
*/
|
|
|
|
Dimension GetMaxTreeSpriteSize()
|
|
|
|
{
|
|
|
|
Dimension size, this_size;
|
|
|
|
Point offset;
|
|
|
|
/* Avoid to use it uninitialized */
|
|
|
|
size.width = 32; // default width - 2
|
|
|
|
size.height = 39; // default height - 7
|
|
|
|
offset.x = 0;
|
|
|
|
offset.y = 0;
|
|
|
|
|
2011-09-08 17:54:49 +00:00
|
|
|
for (int i = this->base; i < this->base + this->count; i++) {
|
|
|
|
if (i >= (int)lengthof(tree_sprites)) return size;
|
2011-08-31 22:52:26 +00:00
|
|
|
this_size = GetSpriteSize(tree_sprites[i].sprite, &offset);
|
|
|
|
size.width = max<int>(size.width, 2 * max<int>(this_size.width, -offset.x));
|
|
|
|
size.height = max<int>(size.height, max<int>(this_size.height, -offset.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2009-11-22 18:28:14 +00:00
|
|
|
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
|
2009-09-20 21:09:27 +00:00
|
|
|
{
|
2011-12-16 16:57:15 +00:00
|
|
|
if (widget >= WID_BT_TYPE_11 && widget <= WID_BT_TYPE_34) {
|
2011-08-31 22:52:26 +00:00
|
|
|
Dimension d = GetMaxTreeSpriteSize();
|
|
|
|
/* Allow some pixels extra width and height */
|
|
|
|
size->width = d.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
|
|
|
|
size->height = d.height + WD_FRAMERECT_RIGHT + WD_FRAMERECT_BOTTOM + 7; // we need some more space
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-16 16:57:15 +00:00
|
|
|
if (widget != WID_BT_MANY_RANDOM) return;
|
2009-09-20 21:09:27 +00:00
|
|
|
|
2008-05-12 21:46:08 +00:00
|
|
|
if (_game_mode != GM_EDITOR) {
|
2009-09-20 21:09:27 +00:00
|
|
|
size->width = 0;
|
|
|
|
size->height = 0;
|
2008-05-12 21:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2009-09-20 21:09:27 +00:00
|
|
|
virtual void DrawWidget(const Rect &r, int widget) const
|
2008-05-12 21:46:08 +00:00
|
|
|
{
|
2011-12-16 16:57:15 +00:00
|
|
|
if (widget < WID_BT_TYPE_11 || widget > WID_BT_TYPE_34 || widget - WID_BT_TYPE_11 >= this->count) return;
|
2008-05-12 21:46:08 +00:00
|
|
|
|
2011-12-16 16:57:15 +00:00
|
|
|
int i = this->base + widget - WID_BT_TYPE_11;
|
2011-08-31 22:52:26 +00:00
|
|
|
/* Trees "grow" in the centre on the bottom line of the buttons */
|
|
|
|
DrawSprite(tree_sprites[i].sprite, tree_sprites[i].pal, (r.left + r.right) / 2 + WD_FRAMERECT_LEFT, r.bottom - 7);
|
2008-05-12 21:46:08 +00:00
|
|
|
}
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2010-01-30 18:34:48 +00:00
|
|
|
virtual void OnClick(Point pt, int widget, int click_count)
|
2008-05-12 21:46:08 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:57:15 +00:00
|
|
|
case WID_BT_TYPE_11: case WID_BT_TYPE_12: case WID_BT_TYPE_13: case WID_BT_TYPE_14:
|
|
|
|
case WID_BT_TYPE_21: case WID_BT_TYPE_22: case WID_BT_TYPE_23: case WID_BT_TYPE_24:
|
|
|
|
case WID_BT_TYPE_31: case WID_BT_TYPE_32: case WID_BT_TYPE_33: case WID_BT_TYPE_34:
|
|
|
|
if (widget - WID_BT_TYPE_11 >= this->count) break;
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2010-12-24 15:08:19 +00:00
|
|
|
if (HandlePlacePushButton(this, widget, SPR_CURSOR_TREE, HT_RECT)) {
|
2011-12-16 16:57:15 +00:00
|
|
|
this->tree_to_plant = (TreeType)(this->base + widget - WID_BT_TYPE_11);
|
2008-05-12 21:46:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2011-12-16 16:57:15 +00:00
|
|
|
case WID_BT_TYPE_RANDOM: // tree of random type.
|
|
|
|
if (HandlePlacePushButton(this, WID_BT_TYPE_RANDOM, SPR_CURSOR_TREE, HT_RECT)) {
|
2010-04-14 19:57:19 +00:00
|
|
|
this->tree_to_plant = TREE_INVALID;
|
2008-05-12 21:46:08 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2011-12-16 16:57:15 +00:00
|
|
|
case WID_BT_MANY_RANDOM: // place trees randomly over the landscape
|
2012-12-23 21:09:09 +00:00
|
|
|
if (_settings_client.sound.confirm) SndPlayFx(SND_15_BEEP);
|
2008-05-12 21:46:08 +00:00
|
|
|
PlaceTreesRandomly();
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2008-05-12 21:46:08 +00:00
|
|
|
virtual void OnPlaceObject(Point pt, TileIndex tile)
|
|
|
|
{
|
2012-04-17 19:43:57 +00:00
|
|
|
VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_PLANT_TREES);
|
2008-05-12 21:46:08 +00:00
|
|
|
}
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2008-05-18 14:04:51 +00:00
|
|
|
virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
|
2008-05-12 21:46:08 +00:00
|
|
|
{
|
|
|
|
VpSelectTilesWithMethod(pt.x, pt.y, select_method);
|
|
|
|
}
|
2008-05-08 13:29:35 +00:00
|
|
|
|
2008-05-18 14:04:51 +00:00
|
|
|
virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
|
2008-05-12 21:46:08 +00:00
|
|
|
{
|
|
|
|
if (pt.x != -1 && select_proc == DDSP_PLANT_TREES) {
|
2008-12-28 14:37:19 +00:00
|
|
|
DoCommandP(end_tile, this->tree_to_plant, start_tile,
|
2009-04-21 23:40:56 +00:00
|
|
|
CMD_PLANT_TREE | CMD_MSG(STR_ERROR_CAN_T_PLANT_TREE_HERE));
|
2008-05-12 21:46:08 +00:00
|
|
|
}
|
2008-05-08 13:29:35 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 21:31:29 +00:00
|
|
|
/**
|
2011-08-31 22:52:26 +00:00
|
|
|
* Initialize the window data
|
2011-03-13 21:31:29 +00:00
|
|
|
*/
|
2011-08-31 22:52:26 +00:00
|
|
|
virtual void OnInit()
|
2009-09-20 21:09:27 +00:00
|
|
|
{
|
|
|
|
this->base = _tree_base_by_landscape[_settings_game.game_creation.landscape];
|
|
|
|
this->count = _tree_count_by_landscape[_settings_game.game_creation.landscape];
|
|
|
|
}
|
|
|
|
|
2008-05-12 21:46:08 +00:00
|
|
|
virtual void OnPlaceObjectAbort()
|
|
|
|
{
|
|
|
|
this->RaiseButtons();
|
|
|
|
}
|
2008-05-08 13:29:35 +00:00
|
|
|
};
|
|
|
|
|
2009-03-25 22:07:25 +00:00
|
|
|
static const NWidgetPart _nested_build_trees_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_PLANT_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2009-12-21 16:24:29 +00:00
|
|
|
NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
|
2009-12-09 17:10:57 +00:00
|
|
|
NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
2009-11-24 21:13:36 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
|
2009-03-25 22:07:25 +00:00
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 2),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(2, 0),
|
|
|
|
NWidget(NWID_VERTICAL),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_11), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_12), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_13), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_14), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 1),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_21), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_22), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_23), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_24), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 1),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_31), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_32), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_33), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(1, 0),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BT_TYPE_34), SetMinimalSize(34, 46), SetDataTip(0x0, STR_PLANT_TREE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 1),
|
2011-12-16 16:57:15 +00:00
|
|
|
NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_BT_TYPE_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TYPE, STR_TREES_RANDOM_TYPE_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 1),
|
2012-11-11 16:07:46 +00:00
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_BT_MANY_RANDOM), SetMinimalSize(139, 12), SetDataTip(STR_TREES_RANDOM_TREES_BUTTON, STR_TREES_RANDOM_TREES_TOOLTIP),
|
2009-03-25 22:07:25 +00:00
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 2),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(2, 0),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _build_trees_desc(
|
2013-05-26 19:25:01 +00:00
|
|
|
WDP_AUTO, "build_tree", 0, 0,
|
2008-05-08 13:29:35 +00:00
|
|
|
WC_BUILD_TREES, WC_NONE,
|
2012-11-11 16:10:43 +00:00
|
|
|
WDF_CONSTRUCTION,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_build_trees_widgets, lengthof(_nested_build_trees_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2008-05-08 13:29:35 +00:00
|
|
|
|
|
|
|
void ShowBuildTreesToolbar()
|
|
|
|
{
|
2009-05-17 01:00:56 +00:00
|
|
|
if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
|
2008-05-12 21:46:08 +00:00
|
|
|
AllocateWindowDescFront<BuildTreesWindow>(&_build_trees_desc, 0);
|
2008-05-08 13:29:35 +00:00
|
|
|
}
|