2010-08-28 19:43:41 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file object_gui.cpp The GUI for objects. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "command_func.h"
|
|
|
|
#include "newgrf.h"
|
|
|
|
#include "newgrf_object.h"
|
|
|
|
#include "newgrf_text.h"
|
|
|
|
#include "strings_func.h"
|
|
|
|
#include "viewport_func.h"
|
|
|
|
#include "window_gui.h"
|
2013-05-24 19:13:59 +00:00
|
|
|
#include "window_func.h"
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/object_widget.h"
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
|
|
|
static ObjectClassID _selected_object_class; ///< the currently visible object class
|
|
|
|
static int _selected_object_index; ///< the index of the selected object in the current class or -1
|
2010-12-10 21:34:36 +00:00
|
|
|
static uint8 _selected_object_view; ///< the view of the selected object
|
2013-05-24 19:13:59 +00:00
|
|
|
static uint _matrix_col_count; ///< Number of columns of the object type matrix
|
|
|
|
static uint _list_row_count; ///< Number of rows of the object class list
|
2010-08-28 19:43:41 +00:00
|
|
|
|
|
|
|
/** The window used for building objects. */
|
|
|
|
class BuildObjectWindow : public PickerWindowBase {
|
|
|
|
static const int OBJECT_MARGIN = 4; ///< The margin (in pixels) around an object.
|
|
|
|
int line_height; ///< The height of a single line.
|
|
|
|
int info_height; ///< The height of the info box.
|
2011-05-14 18:35:40 +00:00
|
|
|
Scrollbar *vscroll; ///< The scrollbar.
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2013-05-24 19:08:35 +00:00
|
|
|
/** Scroll #WID_BO_CLASS_LIST so that the selected object class is visible. */
|
|
|
|
void EnsureSelectedObjectClassIsVisible()
|
|
|
|
{
|
|
|
|
uint pos = 0;
|
|
|
|
for (int i = 0; i < _selected_object_class; i++) {
|
|
|
|
if (ObjectClass::Get((ObjectClassID) i)->GetUISpecCount() == 0) continue;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
this->vscroll->ScrollTowards(pos);
|
|
|
|
}
|
|
|
|
|
2013-05-24 19:11:30 +00:00
|
|
|
/**
|
|
|
|
* Tests whether the previously selected object can be selected.
|
|
|
|
* @return \c true if the selected object is available, \c false otherwise.
|
|
|
|
*/
|
|
|
|
bool CanRestoreSelectedObject()
|
|
|
|
{
|
|
|
|
if (_selected_object_index == -1) return false;
|
|
|
|
|
|
|
|
ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
if ((int)sel_objclass->GetSpecCount() <= _selected_object_index) return false;
|
|
|
|
|
|
|
|
return sel_objclass->GetSpec(_selected_object_index)->IsAvailable();
|
|
|
|
}
|
|
|
|
|
2013-05-24 19:13:59 +00:00
|
|
|
/**
|
|
|
|
* Calculate the number of columns of the #WID_BO_SELECT_MATRIX widget.
|
|
|
|
* @return Number of columns in the matrix.
|
|
|
|
*/
|
|
|
|
uint GetMatrixColumnCount()
|
|
|
|
{
|
|
|
|
const NWidgetBase *matrix = this->GetWidget<NWidgetBase>(WID_BO_SELECT_MATRIX);
|
|
|
|
return 1 + (matrix->current_x - matrix->smallest_x) / matrix->resize_x;
|
|
|
|
}
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
public:
|
2013-05-26 19:23:42 +00:00
|
|
|
BuildObjectWindow(WindowDesc *desc, Window *w) : PickerWindowBase(desc, w), info_height(1)
|
2010-08-28 19:43:41 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->CreateNestedTree();
|
2011-12-16 18:06:16 +00:00
|
|
|
this->vscroll = this->GetScrollbar(WID_BO_SCROLLBAR);
|
2013-06-30 14:34:23 +00:00
|
|
|
this->FinishInitNested(0);
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
this->vscroll->SetPosition(0);
|
2012-04-22 16:28:37 +00:00
|
|
|
this->vscroll->SetCount(ObjectClass::GetUIClassCount());
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2013-05-24 19:11:30 +00:00
|
|
|
if (this->CanRestoreSelectedObject()) {
|
|
|
|
this->SelectOtherObject(_selected_object_index);
|
|
|
|
} else {
|
|
|
|
this->SelectFirstAvailableObject(true);
|
|
|
|
}
|
2012-04-24 17:26:08 +00:00
|
|
|
assert(ObjectClass::Get(_selected_object_class)->GetUISpecCount() > 0); // object GUI should be disables elsewise
|
2013-05-24 19:08:35 +00:00
|
|
|
this->EnsureSelectedObjectClassIsVisible();
|
2011-12-16 18:06:16 +00:00
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetCount(4);
|
2011-01-13 21:55:41 +00:00
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX);
|
|
|
|
matrix->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL));
|
2012-04-22 16:28:37 +00:00
|
|
|
matrix->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
|
2013-05-24 19:13:59 +00:00
|
|
|
|
|
|
|
/* If needed restore the window previous size with the stored values.*/
|
|
|
|
uint default_num_cols = this->GetMatrixColumnCount();
|
|
|
|
uint default_num_rows = this->vscroll->GetCapacity();
|
|
|
|
int delta_x = (_matrix_col_count > default_num_cols) ? (_matrix_col_count - default_num_cols) * this->resize.step_width : 0;
|
|
|
|
int delta_y = (_list_row_count > default_num_rows) ? (_list_row_count - default_num_rows) * this->resize.step_height : 0;
|
|
|
|
if (delta_x > 0 || delta_y > 0) {
|
|
|
|
ResizeWindow(this, delta_x, delta_y, false);
|
|
|
|
/* The window may be linked to the toolbars, thus positioned at the left-bottom of the toolbars.
|
|
|
|
* If the resized window is wider than the toolbars, its position need te be adjusted to ensure all
|
|
|
|
* matrix columns are visible. */
|
|
|
|
this->FindWindowPlacementAndResize(this->width, this->height);
|
|
|
|
}
|
|
|
|
|
2013-06-30 14:37:01 +00:00
|
|
|
if (_selected_object_index != -1) matrix->SetClicked(ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index));
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~BuildObjectWindow()
|
|
|
|
{
|
2013-05-24 19:13:59 +00:00
|
|
|
/* Store the number of columns of the object type matrix and the number of rows of the object class list
|
|
|
|
* to restore them on the next window invocation. */
|
|
|
|
_matrix_col_count = this->GetMatrixColumnCount();
|
|
|
|
_list_row_count = this->vscroll->GetCapacity();
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetStringParameters(int widget) const
|
|
|
|
{
|
|
|
|
switch (widget) {
|
2012-04-24 20:01:34 +00:00
|
|
|
case WID_BO_OBJECT_NAME: {
|
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
|
|
|
SetDParam(0, spec != NULL ? spec->name : STR_EMPTY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_OBJECT_SIZE: {
|
2012-04-22 16:28:14 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2010-08-28 19:43:41 +00:00
|
|
|
int size = spec == NULL ? 0 : spec->size;
|
2010-12-10 21:34:36 +00:00
|
|
|
SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
|
|
|
|
SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
|
2010-08-28 19:43:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
|
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_CLASS_LIST: {
|
2012-04-22 16:28:19 +00:00
|
|
|
for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
|
2012-04-22 16:28:37 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
|
|
|
|
if (objclass->GetUISpecCount() == 0) continue;
|
|
|
|
size->width = max(size->width, GetStringBoundingBox(objclass->name).width);
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
2011-01-13 21:59:39 +00:00
|
|
|
size->width += padding.width;
|
2010-08-28 19:43:41 +00:00
|
|
|
this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
|
2011-01-13 22:02:13 +00:00
|
|
|
resize->height = this->line_height;
|
2013-06-30 14:33:15 +00:00
|
|
|
size->height = 5 * this->line_height;
|
2010-08-28 19:43:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-07-07 11:59:43 +00:00
|
|
|
case WID_BO_OBJECT_NAME:
|
|
|
|
case WID_BO_OBJECT_SIZE:
|
|
|
|
/* We do not want the window to resize when selecting objects; better clip texts */
|
|
|
|
size->width = 0;
|
|
|
|
break;
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_OBJECT_MATRIX: {
|
2010-12-10 21:34:36 +00:00
|
|
|
/* Get the right amount of buttons based on the current spec. */
|
2012-04-22 16:28:14 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2010-12-10 21:34:36 +00:00
|
|
|
if (spec != NULL) {
|
|
|
|
if (spec->views >= 2) size->width += resize->width;
|
|
|
|
if (spec->views >= 4) size->height += resize->height;
|
|
|
|
}
|
2011-01-13 21:55:41 +00:00
|
|
|
resize->width = 0;
|
|
|
|
resize->height = 0;
|
2010-12-10 21:34:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_OBJECT_SPRITE: {
|
2013-01-08 22:46:42 +00:00
|
|
|
bool two_wide = false; // Whether there will be two widgets next to each other in the matrix or not.
|
2010-12-10 21:34:36 +00:00
|
|
|
int height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
|
|
|
|
|
|
|
|
/* Get the height and view information. */
|
2010-08-28 19:43:41 +00:00
|
|
|
for (int i = 0; i < NUM_OBJECTS; i++) {
|
2010-12-10 21:34:36 +00:00
|
|
|
const ObjectSpec *spec = ObjectSpec::Get(i);
|
2012-04-22 16:28:37 +00:00
|
|
|
if (!spec->IsEverAvailable()) continue;
|
2010-12-10 21:34:36 +00:00
|
|
|
two_wide |= spec->views >= 2;
|
|
|
|
height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the pixel heights. */
|
|
|
|
for (size_t i = 0; i < lengthof(height); i++) {
|
|
|
|
height[i] *= TILE_HEIGHT;
|
|
|
|
height[i] += TILE_PIXELS + 2 * OBJECT_MARGIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now determine the size of the minimum widgets. When there are two columns, then
|
|
|
|
* we want these columns to be slightly less wide. When there are two rows, then
|
|
|
|
* determine the size of the widgets based on the maximum size for a single row
|
|
|
|
* of widgets, or just the twice the widget height of the two row ones. */
|
|
|
|
size->height = max(height[0], height[1] * 2 + 2);
|
|
|
|
if (two_wide) {
|
|
|
|
size->width = (3 * TILE_PIXELS + 2 * OBJECT_MARGIN) * 2 + 2;
|
|
|
|
} else {
|
|
|
|
size->width = 4 * TILE_PIXELS + 2 * OBJECT_MARGIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the right size for the single widget based on the current spec. */
|
2012-04-22 16:28:14 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2010-12-10 21:34:36 +00:00
|
|
|
if (spec != NULL) {
|
|
|
|
if (spec->views >= 2) size->width = size->width / 2 - 1;
|
|
|
|
if (spec->views >= 4) size->height = size->height / 2 - 1;
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_INFO:
|
2010-08-28 19:43:41 +00:00
|
|
|
size->height = this->info_height;
|
|
|
|
break;
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_SELECT_MATRIX:
|
2011-01-13 21:55:41 +00:00
|
|
|
fill->height = 1;
|
|
|
|
resize->height = 1;
|
|
|
|
break;
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DrawWidget(const Rect &r, int widget) const
|
|
|
|
{
|
2010-12-10 21:34:36 +00:00
|
|
|
switch (GB(widget, 0, 16)) {
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_CLASS_LIST: {
|
2010-08-28 19:43:41 +00:00
|
|
|
int y = r.top;
|
2012-04-22 16:28:37 +00:00
|
|
|
uint pos = 0;
|
|
|
|
for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
|
|
|
|
ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
|
|
|
|
if (objclass->GetUISpecCount() == 0) continue;
|
|
|
|
if (!this->vscroll->IsVisible(pos++)) continue;
|
|
|
|
SetDParam(0, objclass->name);
|
2011-01-13 21:59:39 +00:00
|
|
|
DrawString(r.left + WD_MATRIX_LEFT, r.right + WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, STR_JUST_STRING,
|
|
|
|
((int)i == _selected_object_class) ? TC_WHITE : TC_BLACK);
|
2010-08-28 19:43:41 +00:00
|
|
|
y += this->line_height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_OBJECT_SPRITE: {
|
2012-04-22 16:28:14 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2010-08-28 19:43:41 +00:00
|
|
|
if (spec == NULL) break;
|
|
|
|
|
2012-05-28 13:44:55 +00:00
|
|
|
/* Height of the selection matrix.
|
|
|
|
* Depending on the number of views, the matrix has a 1x1, 1x2, 2x1 or 2x2 layout. To make the previews
|
|
|
|
* look nice in all layouts, we use the 4x4 layout (smallest previews) as starting point. For the bigger
|
2013-01-08 22:46:42 +00:00
|
|
|
* previews in the layouts with less views we add space homogeneously on all sides, so the 4x4 preview-rectangle
|
2012-05-28 13:44:55 +00:00
|
|
|
* is centered in the 2x1, 1x2 resp. 1x1 buttons. */
|
|
|
|
uint matrix_height = this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->current_y;
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
DrawPixelInfo tmp_dpi;
|
|
|
|
/* Set up a clipping area for the preview. */
|
|
|
|
if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
|
|
|
|
DrawPixelInfo *old_dpi = _cur_dpi;
|
|
|
|
_cur_dpi = &tmp_dpi;
|
2010-08-28 19:45:56 +00:00
|
|
|
if (spec->grf_prop.grffile == NULL) {
|
|
|
|
extern const DrawTileSprites _objects[];
|
|
|
|
const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
|
2012-05-28 13:44:55 +00:00
|
|
|
DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
|
2010-08-28 19:45:56 +00:00
|
|
|
} else {
|
2012-05-28 13:44:55 +00:00
|
|
|
DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
|
2010-08-28 19:45:56 +00:00
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
_cur_dpi = old_dpi;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_SELECT_IMAGE: {
|
2012-04-22 16:28:37 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
|
|
|
|
if (obj_index < 0) break;
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(obj_index);
|
2011-01-13 21:55:41 +00:00
|
|
|
if (spec == NULL) break;
|
|
|
|
|
|
|
|
if (!spec->IsAvailable()) {
|
2011-05-06 21:13:29 +00:00
|
|
|
GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
|
2011-01-13 21:55:41 +00:00
|
|
|
}
|
|
|
|
DrawPixelInfo tmp_dpi;
|
|
|
|
/* Set up a clipping area for the preview. */
|
|
|
|
if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
|
|
|
|
DrawPixelInfo *old_dpi = _cur_dpi;
|
|
|
|
_cur_dpi = &tmp_dpi;
|
|
|
|
if (spec->grf_prop.grffile == NULL) {
|
|
|
|
extern const DrawTileSprites _objects[];
|
|
|
|
const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
|
|
|
|
DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
|
|
|
|
} else {
|
|
|
|
DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec,
|
|
|
|
min(_selected_object_view, spec->views - 1));
|
|
|
|
}
|
|
|
|
_cur_dpi = old_dpi;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_INFO: {
|
2012-04-22 16:28:14 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2010-08-28 19:43:41 +00:00
|
|
|
if (spec == NULL) break;
|
|
|
|
|
|
|
|
/* Get the extra message for the GUI */
|
|
|
|
if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
|
2011-03-10 19:32:24 +00:00
|
|
|
uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
|
2011-11-08 17:24:43 +00:00
|
|
|
if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
|
|
|
|
if (callback_res > 0x400) {
|
|
|
|
ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_FUND_MORE_TEXT, callback_res);
|
|
|
|
} else {
|
|
|
|
StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
|
|
|
|
if (message != STR_NULL && message != STR_UNDEFINED) {
|
|
|
|
StartTextRefStackUsage(6);
|
|
|
|
/* Use all the available space left from where we stand up to the
|
|
|
|
* end of the window. We ALSO enlarge the window if needed, so we
|
|
|
|
* can 'go' wild with the bottom of the window. */
|
|
|
|
int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
|
|
|
|
StopTextRefStackUsage();
|
|
|
|
if (y > this->info_height) {
|
|
|
|
BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
|
|
|
|
bow->info_height = y + 2;
|
|
|
|
bow->ReInit();
|
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-13 21:55:41 +00:00
|
|
|
/**
|
|
|
|
* Select the specified object in #_selected_object_class class.
|
|
|
|
* @param object_index Object index to select, \c -1 means select nothing.
|
|
|
|
*/
|
2010-08-28 19:43:41 +00:00
|
|
|
void SelectOtherObject(int object_index)
|
|
|
|
{
|
|
|
|
_selected_object_index = object_index;
|
2010-12-10 21:34:36 +00:00
|
|
|
if (_selected_object_index != -1) {
|
2012-04-22 16:28:14 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2010-12-10 21:34:36 +00:00
|
|
|
_selected_object_view = min(_selected_object_view, spec->views - 1);
|
|
|
|
this->ReInit();
|
|
|
|
} else {
|
|
|
|
_selected_object_view = 0;
|
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
|
2012-04-22 16:28:37 +00:00
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index != -1 ? ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index) : -1);
|
2010-08-28 19:43:41 +00:00
|
|
|
this->UpdateSelectSize();
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateSelectSize()
|
|
|
|
{
|
|
|
|
if (_selected_object_index == -1) {
|
|
|
|
SetTileSelectSize(1, 1);
|
|
|
|
} else {
|
2012-04-22 16:28:14 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2010-12-10 21:34:36 +00:00
|
|
|
int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
|
|
|
|
int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
|
2010-08-28 19:43:41 +00:00
|
|
|
SetTileSelectSize(w, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-13 22:02:13 +00:00
|
|
|
virtual void OnResize()
|
|
|
|
{
|
2011-12-16 18:06:16 +00:00
|
|
|
this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
|
2011-01-13 22:02:13 +00:00
|
|
|
}
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
virtual void OnClick(Point pt, int widget, int click_count)
|
|
|
|
{
|
2010-12-10 21:34:36 +00:00
|
|
|
switch (GB(widget, 0, 16)) {
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_CLASS_LIST: {
|
2010-08-28 19:43:41 +00:00
|
|
|
int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
|
2012-04-22 16:28:37 +00:00
|
|
|
if (num_clicked >= (int)ObjectClass::GetUIClassCount()) break;
|
2011-01-13 21:59:39 +00:00
|
|
|
|
2012-04-22 16:28:37 +00:00
|
|
|
_selected_object_class = ObjectClass::GetUIClass(num_clicked);
|
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
|
2011-01-13 21:59:39 +00:00
|
|
|
this->SelectFirstAvailableObject(false);
|
2010-08-28 19:43:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-12-10 21:34:36 +00:00
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_SELECT_IMAGE: {
|
2012-04-22 16:28:37 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
int num_clicked = objclass->GetIndexFromUI(GB(widget, 16, 16));
|
|
|
|
if (num_clicked >= 0 && objclass->GetSpec(num_clicked)->IsAvailable()) this->SelectOtherObject(num_clicked);
|
2011-01-13 21:55:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_OBJECT_SPRITE:
|
2010-12-10 21:34:36 +00:00
|
|
|
if (_selected_object_index != -1) {
|
|
|
|
_selected_object_view = GB(widget, 16, 16);
|
2011-12-16 18:06:16 +00:00
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
|
2010-12-10 21:34:36 +00:00
|
|
|
this->UpdateSelectSize();
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
break;
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select the first available object.
|
|
|
|
* @param change_class If true, change the class if no object in the current
|
|
|
|
* class is available.
|
|
|
|
*/
|
|
|
|
void SelectFirstAvailableObject(bool change_class)
|
|
|
|
{
|
|
|
|
/* First try to select an object in the selected class. */
|
2012-04-22 16:28:14 +00:00
|
|
|
ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
|
|
|
|
const ObjectSpec *spec = sel_objclass->GetSpec(i);
|
2010-08-28 19:43:41 +00:00
|
|
|
if (spec->IsAvailable()) {
|
2012-04-22 16:27:41 +00:00
|
|
|
this->SelectOtherObject(i);
|
2010-08-28 19:43:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (change_class) {
|
|
|
|
/* If that fails, select the first available object
|
|
|
|
* from a random class. */
|
|
|
|
for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
|
2012-04-22 16:28:14 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(j);
|
|
|
|
for (uint i = 0; i < objclass->GetSpecCount(); i++) {
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(i);
|
2010-08-28 19:43:41 +00:00
|
|
|
if (spec->IsAvailable()) {
|
|
|
|
_selected_object_class = j;
|
|
|
|
this->SelectOtherObject(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 17:26:08 +00:00
|
|
|
/* If all objects are unavailable, select nothing... */
|
|
|
|
if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
|
|
|
|
/* ... but make sure that the class is not empty. */
|
|
|
|
for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
|
|
|
|
if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
|
|
|
|
_selected_object_class = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
this->SelectOtherObject(-1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const NWidgetPart _nested_build_object_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2013-05-26 19:30:07 +00:00
|
|
|
NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
|
2010-08-28 19:43:41 +00:00
|
|
|
EndContainer(),
|
2011-01-13 21:55:41 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
|
|
|
|
NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
|
|
|
|
NWidget(NWID_VERTICAL),
|
|
|
|
NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
|
2013-06-30 14:36:31 +00:00
|
|
|
NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BO_SCROLLBAR),
|
2011-01-13 21:55:41 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
|
2011-01-13 21:55:41 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
2012-04-24 20:01:34 +00:00
|
|
|
NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(2, 5, 2, 5),
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
|
2011-01-13 21:55:41 +00:00
|
|
|
EndContainer(),
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
|
2011-01-13 21:55:41 +00:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
|
|
|
|
SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL),
|
2011-01-13 21:55:41 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
|
2011-01-13 21:55:41 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
2010-08-28 19:43:41 +00:00
|
|
|
EndContainer(),
|
2011-01-13 21:55:41 +00:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 18:06:16 +00:00
|
|
|
NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
|
2011-01-13 21:55:41 +00:00
|
|
|
NWidget(NWID_VERTICAL),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
|
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
|
2010-12-10 21:34:36 +00:00
|
|
|
EndContainer(),
|
2010-08-28 19:43:41 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _build_object_desc(
|
2013-05-26 19:25:01 +00:00
|
|
|
WDP_AUTO, "build_object", 0, 0,
|
2010-08-28 19:43:41 +00:00
|
|
|
WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
|
|
|
|
WDF_CONSTRUCTION,
|
|
|
|
_nested_build_object_widgets, lengthof(_nested_build_object_widgets)
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show our object picker.
|
|
|
|
* @param w The toolbar window we're associated with.
|
|
|
|
*/
|
|
|
|
void ShowBuildObjectPicker(Window *w)
|
|
|
|
{
|
|
|
|
new BuildObjectWindow(&_build_object_desc, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reset all data of the object GUI. */
|
|
|
|
void InitializeObjectGui()
|
|
|
|
{
|
|
|
|
_selected_object_class = (ObjectClassID)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PlaceProc function, called when someone pressed the button if the
|
|
|
|
* object-tool is selected
|
|
|
|
* @param tile on which to place the object
|
|
|
|
*/
|
|
|
|
void PlaceProc_Object(TileIndex tile)
|
|
|
|
{
|
2012-04-22 16:28:14 +00:00
|
|
|
DoCommandP(tile, ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index)->Index(), _selected_object_view, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT), CcTerraform);
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|