2010-08-28 19:43:41 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file object_gui.cpp The GUI for objects. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "command_func.h"
|
2021-03-23 19:16:27 +00:00
|
|
|
#include "hotkeys.h"
|
2010-08-28 19:43:41 +00:00
|
|
|
#include "newgrf.h"
|
|
|
|
#include "newgrf_object.h"
|
|
|
|
#include "newgrf_text.h"
|
2021-03-23 19:16:27 +00:00
|
|
|
#include "object.h"
|
2021-02-19 09:26:54 +00:00
|
|
|
#include "querystring_gui.h"
|
|
|
|
#include "sortlist_type.h"
|
|
|
|
#include "stringfilter_type.h"
|
|
|
|
#include "string_func.h"
|
2010-08-28 19:43:41 +00:00
|
|
|
#include "strings_func.h"
|
|
|
|
#include "viewport_func.h"
|
2015-07-28 18:20:54 +00:00
|
|
|
#include "tilehighlight_func.h"
|
2010-08-28 19:43:41 +00:00
|
|
|
#include "window_gui.h"
|
2013-05-24 19:13:59 +00:00
|
|
|
#include "window_func.h"
|
2014-10-04 11:49:05 +00:00
|
|
|
#include "zoom_func.h"
|
2023-02-10 17:18:39 +00:00
|
|
|
#include "core/backup_type.hpp"
|
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"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2021-03-07 18:25:48 +00:00
|
|
|
static ObjectClassID _selected_object_class; ///< Currently selected available object class.
|
|
|
|
static int _selected_object_index; ///< Index of the currently selected object if existing, else \c -1.
|
|
|
|
static uint8 _selected_object_view; ///< the view of the selected object
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2021-03-23 19:16:27 +00:00
|
|
|
/** Enum referring to the Hotkeys in the build object window */
|
|
|
|
enum BuildObjectHotkeys {
|
|
|
|
BOHK_FOCUS_FILTER_BOX, ///< Focus the edit box for editing the filter string
|
|
|
|
};
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
/** The window used for building objects. */
|
2015-07-28 18:20:54 +00:00
|
|
|
class BuildObjectWindow : public Window {
|
2021-03-07 18:25:48 +00:00
|
|
|
typedef GUIList<ObjectClassID, StringFilter &> GUIObjectClassList; ///< Type definition for the list to hold available object classes.
|
2021-02-19 09:26:54 +00:00
|
|
|
|
|
|
|
static const uint EDITBOX_MAX_SIZE = 16; ///< The maximum number of characters for the filter edit box.
|
|
|
|
|
2022-11-28 00:27:57 +00:00
|
|
|
int object_margin; ///< The margin (in pixels) around an object.
|
2021-02-19 09:26:54 +00:00
|
|
|
int line_height; ///< The height of a single line.
|
|
|
|
int info_height; ///< The height of the info box.
|
2021-02-19 15:06:00 +00:00
|
|
|
Scrollbar *vscroll; ///< The scrollbar.
|
2021-02-19 09:26:54 +00:00
|
|
|
|
|
|
|
static Listing last_sorting; ///< Default sorting of #GUIObjectClassList.
|
|
|
|
static Filtering last_filtering; ///< Default filtering of #GUIObjectClassList.
|
|
|
|
static GUIObjectClassList::SortFunction * const sorter_funcs[]; ///< Sort functions of the #GUIObjectClassList.
|
|
|
|
static GUIObjectClassList::FilterFunction * const filter_funcs[]; ///< Filter functions of the #GUIObjectClassList.
|
|
|
|
GUIObjectClassList object_classes; ///< Available object classes.
|
|
|
|
StringFilter string_filter; ///< Filter for available objects.
|
|
|
|
QueryString filter_editbox; ///< Filter editbox.
|
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;
|
2021-03-07 18:25:48 +00:00
|
|
|
for (auto object_class_id : this->object_classes) {
|
|
|
|
if (object_class_id == _selected_object_class) break;
|
2013-05-24 19:08:35 +00:00
|
|
|
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;
|
|
|
|
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
if ((int)objclass->GetSpecCount() <= _selected_object_index) return false;
|
2013-05-24 19:11:30 +00:00
|
|
|
|
2021-03-07 18:25:48 +00:00
|
|
|
return objclass->GetSpec(_selected_object_index)->IsAvailable();
|
2013-05-24 19:11:30 +00:00
|
|
|
}
|
|
|
|
|
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:
|
2021-03-23 19:16:27 +00:00
|
|
|
BuildObjectWindow(WindowDesc *desc, WindowNumber number) : Window(desc), info_height(1), filter_editbox(EDITBOX_MAX_SIZE * MAX_CHAR_LENGTH, EDITBOX_MAX_SIZE)
|
2010-08-28 19:43:41 +00:00
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->CreateNestedTree();
|
2021-02-19 09:26:54 +00:00
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
this->vscroll = this->GetScrollbar(WID_BO_SCROLLBAR);
|
2015-07-28 18:20:54 +00:00
|
|
|
|
2021-02-19 09:26:54 +00:00
|
|
|
this->querystrings[WID_BO_FILTER] = &this->filter_editbox;
|
2013-06-30 14:34:23 +00:00
|
|
|
|
2021-02-19 09:26:54 +00:00
|
|
|
this->object_classes.SetListing(this->last_sorting);
|
|
|
|
this->object_classes.SetFiltering(this->last_filtering);
|
|
|
|
this->object_classes.SetSortFuncs(this->sorter_funcs);
|
|
|
|
this->object_classes.SetFilterFuncs(this->filter_funcs);
|
|
|
|
this->object_classes.ForceRebuild();
|
|
|
|
|
|
|
|
BuildObjectClassesAvailable();
|
|
|
|
SelectClassAndObject();
|
2013-06-30 14:34:23 +00:00
|
|
|
|
2021-02-19 09:26:54 +00:00
|
|
|
this->FinishInitNested(number);
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2013-06-30 14:37:24 +00:00
|
|
|
NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX);
|
|
|
|
matrix->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL));
|
2023-01-11 18:26:38 +00:00
|
|
|
matrix->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
|
2013-06-30 14:37:24 +00:00
|
|
|
|
2021-02-19 09:26:54 +00:00
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetCount(4);
|
|
|
|
|
|
|
|
ResetObjectToPlace();
|
|
|
|
|
2023-05-07 15:10:56 +00:00
|
|
|
this->vscroll->SetCount(this->object_classes.size());
|
2021-02-19 09:26:54 +00:00
|
|
|
|
|
|
|
EnsureSelectedObjectClassIsVisible();
|
|
|
|
|
|
|
|
this->InvalidateData();
|
|
|
|
}
|
|
|
|
|
2021-03-07 18:25:48 +00:00
|
|
|
/** Sort object classes by ObjectClassID. */
|
|
|
|
static bool ObjectClassIDSorter(ObjectClassID const &a, ObjectClassID const &b)
|
2021-02-19 09:26:54 +00:00
|
|
|
{
|
2021-03-07 18:25:48 +00:00
|
|
|
return a < b;
|
2021-02-19 09:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Filter object classes by class name. */
|
2022-11-19 21:51:44 +00:00
|
|
|
static bool TagNameFilter(ObjectClassID const *oc, StringFilter &filter)
|
2021-02-19 09:26:54 +00:00
|
|
|
{
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(*oc);
|
2021-02-19 09:26:54 +00:00
|
|
|
|
|
|
|
filter.ResetState();
|
2023-04-30 08:23:05 +00:00
|
|
|
filter.AddLine(GetString(objclass->name));
|
2021-02-19 09:26:54 +00:00
|
|
|
return filter.GetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Builds the filter list of available object classes. */
|
|
|
|
void BuildObjectClassesAvailable()
|
|
|
|
{
|
|
|
|
if (!this->object_classes.NeedRebuild()) return;
|
|
|
|
|
|
|
|
this->object_classes.clear();
|
|
|
|
|
2023-01-03 03:51:40 +00:00
|
|
|
for (uint i = 0; ObjectClass::IsClassIDValid((ObjectClassID)i); i++) {
|
2021-02-19 09:26:54 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
|
|
|
|
if (objclass->GetUISpecCount() == 0) continue; // Is this needed here?
|
2021-03-07 18:25:48 +00:00
|
|
|
object_classes.push_back((ObjectClassID)i);
|
2021-02-19 09:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->object_classes.Filter(this->string_filter);
|
|
|
|
this->object_classes.shrink_to_fit();
|
|
|
|
this->object_classes.RebuildDone();
|
|
|
|
this->object_classes.Sort();
|
|
|
|
|
2023-05-07 15:10:56 +00:00
|
|
|
this->vscroll->SetCount(this->object_classes.size());
|
2021-02-19 09:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the previously selected current object class and object
|
|
|
|
* can be shown as selected to the user when the dialog is opened.
|
|
|
|
*/
|
|
|
|
void SelectClassAndObject()
|
|
|
|
{
|
|
|
|
assert(!this->object_classes.empty()); // object GUI should be disabled elsewise
|
2021-03-07 18:25:48 +00:00
|
|
|
if (_selected_object_class == ObjectClassID::OBJECT_CLASS_BEGIN) {
|
2021-02-19 09:26:54 +00:00
|
|
|
/* This happens during the first time the window is open during the game life cycle. */
|
|
|
|
this->SelectOtherClass(this->object_classes[0]);
|
|
|
|
} else {
|
|
|
|
/* Check if the previously selected object class is not available anymore as a
|
|
|
|
* result of starting a new game without the corresponding NewGRF. */
|
2023-05-13 21:27:32 +00:00
|
|
|
bool available = _selected_object_class < ObjectClass::GetClassCount();
|
|
|
|
this->SelectOtherClass(available ? _selected_object_class : this->object_classes[0]);
|
2021-02-19 09:26:54 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 19:11:30 +00:00
|
|
|
if (this->CanRestoreSelectedObject()) {
|
|
|
|
this->SelectOtherObject(_selected_object_index);
|
|
|
|
} else {
|
|
|
|
this->SelectFirstAvailableObject(true);
|
|
|
|
}
|
2021-04-01 09:16:19 +00:00
|
|
|
assert(ObjectClass::Get(_selected_object_class)->GetUISpecCount() > 0); // object GUI should be disabled elsewise
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void SetStringParameters(int widget) const override
|
2010-08-28 19:43:41 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2012-04-24 20:01:34 +00:00
|
|
|
case WID_BO_OBJECT_NAME: {
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(_selected_object_index);
|
2019-04-10 21:07:06 +00:00
|
|
|
SetDParam(0, spec != nullptr ? spec->name : STR_EMPTY);
|
2012-04-24 20:01:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_OBJECT_SIZE: {
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(_selected_object_index);
|
2019-04-10 21:07:06 +00:00
|
|
|
int size = spec == nullptr ? 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 00:27:57 +00:00
|
|
|
void OnInit() override
|
|
|
|
{
|
|
|
|
this->object_margin = ScaleGUITrad(4);
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
2010-08-28 19:43:41 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_CLASS_LIST: {
|
2021-03-07 18:25:48 +00:00
|
|
|
for (auto object_class_id : this->object_classes) {
|
|
|
|
ObjectClass *objclass = ObjectClass::Get(object_class_id);
|
2012-04-22 16:28:37 +00:00
|
|
|
if (objclass->GetUISpecCount() == 0) continue;
|
2023-04-11 17:45:42 +00:00
|
|
|
size->width = std::max(size->width, GetStringBoundingBox(objclass->name).width + padding.width);
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
2021-05-03 15:56:02 +00:00
|
|
|
this->line_height = FONT_HEIGHT_NORMAL + padding.height;
|
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. */
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(_selected_object_index);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec != nullptr) {
|
2010-12-10 21:34:36 +00:00
|
|
|
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.
|
2022-11-28 00:27:57 +00:00
|
|
|
uint height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
|
2010-12-10 21:34:36 +00:00
|
|
|
|
|
|
|
/* Get the height and view information. */
|
2023-01-15 00:04:53 +00:00
|
|
|
for (const auto &spec : ObjectSpec::Specs()) {
|
|
|
|
if (!spec.IsEverAvailable()) continue;
|
|
|
|
two_wide |= spec.views >= 2;
|
|
|
|
height[spec.views / 4] = std::max<int>(spec.height, height[spec.views / 4]);
|
2010-12-10 21:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the pixel heights. */
|
|
|
|
for (size_t i = 0; i < lengthof(height); i++) {
|
2015-02-01 20:54:24 +00:00
|
|
|
height[i] *= ScaleGUITrad(TILE_HEIGHT);
|
2022-11-28 00:27:57 +00:00
|
|
|
height[i] += ScaleGUITrad(TILE_PIXELS) + 2 * this->object_margin;
|
2010-12-10 21:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
2022-11-28 00:27:57 +00:00
|
|
|
size->height = std::max(height[0], height[1] * 2);
|
2010-12-10 21:34:36 +00:00
|
|
|
if (two_wide) {
|
2022-11-28 00:27:57 +00:00
|
|
|
size->width = (3 * ScaleGUITrad(TILE_PIXELS) + 2 * this->object_margin) * 2;
|
2010-12-10 21:34:36 +00:00
|
|
|
} else {
|
2022-11-28 00:27:57 +00:00
|
|
|
size->width = 4 * ScaleGUITrad(TILE_PIXELS) + 2 * this->object_margin;
|
2010-12-10 21:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the right size for the single widget based on the current spec. */
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(_selected_object_index);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec != nullptr) {
|
2022-11-28 00:27:57 +00:00
|
|
|
if (spec->views <= 1) size->width += WidgetDimensions::scaled.hsep_normal;
|
|
|
|
if (spec->views <= 2) size->height += WidgetDimensions::scaled.vsep_normal;
|
|
|
|
if (spec->views >= 2) size->width /= 2;
|
|
|
|
if (spec->views >= 4) size->height /= 2;
|
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;
|
|
|
|
|
2014-10-04 11:49:05 +00:00
|
|
|
case WID_BO_SELECT_IMAGE:
|
2022-09-23 08:36:22 +00:00
|
|
|
size->width = ScaleGUITrad(64) + WidgetDimensions::scaled.fullbevel.Horizontal();
|
|
|
|
size->height = ScaleGUITrad(58) + WidgetDimensions::scaled.fullbevel.Vertical();
|
2014-10-04 11:49:05 +00:00
|
|
|
break;
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void DrawWidget(const Rect &r, int widget) const override
|
2010-08-28 19:43:41 +00:00
|
|
|
{
|
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: {
|
2022-09-23 08:36:22 +00:00
|
|
|
Rect mr = r.Shrink(WidgetDimensions::scaled.matrix);
|
2012-04-22 16:28:37 +00:00
|
|
|
uint pos = 0;
|
2021-03-07 18:25:48 +00:00
|
|
|
for (auto object_class_id : this->object_classes) {
|
|
|
|
ObjectClass *objclass = ObjectClass::Get(object_class_id);
|
2012-04-22 16:28:37 +00:00
|
|
|
if (objclass->GetUISpecCount() == 0) continue;
|
|
|
|
if (!this->vscroll->IsVisible(pos++)) continue;
|
2022-10-15 15:55:47 +00:00
|
|
|
DrawString(mr, objclass->name,
|
2021-03-07 18:25:48 +00:00
|
|
|
(object_class_id == _selected_object_class) ? TC_WHITE : TC_BLACK);
|
2022-10-15 15:55:47 +00:00
|
|
|
mr.top += this->line_height;
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_OBJECT_SPRITE: {
|
2021-02-19 09:26:54 +00:00
|
|
|
if (_selected_object_index == -1) break;
|
|
|
|
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(_selected_object_index);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec == nullptr) break;
|
2010-08-28 19:43:41 +00:00
|
|
|
|
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. */
|
2022-09-28 23:10:41 +00:00
|
|
|
if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.Width(), r.Height())) {
|
2023-01-26 18:21:36 +00:00
|
|
|
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec->grf_prop.grffile == nullptr) {
|
2010-08-28 19:45:56 +00:00
|
|
|
extern const DrawTileSprites _objects[];
|
|
|
|
const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
|
2022-11-28 00:27:57 +00:00
|
|
|
DrawOrigTileSeqInGUI(r.Width() / 2 - 1, (r.Height() + matrix_height / 2) / 2 - this->object_margin - ScaleSpriteTrad(TILE_PIXELS), dts, PAL_NONE);
|
2010-08-28 19:45:56 +00:00
|
|
|
} else {
|
2022-11-28 00:27:57 +00:00
|
|
|
DrawNewObjectTileInGUI(r.Width() / 2 - 1, (r.Height() + matrix_height / 2) / 2 - this->object_margin - ScaleSpriteTrad(TILE_PIXELS), spec, GB(widget, 16, 16));
|
2010-08-28 19:45:56 +00:00
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_SELECT_IMAGE: {
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
|
2012-04-22 16:28:37 +00:00
|
|
|
if (obj_index < 0) break;
|
2021-03-07 18:25:48 +00:00
|
|
|
const ObjectSpec *spec = objclass->GetSpec(obj_index);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec == nullptr) break;
|
2011-01-13 21:55:41 +00:00
|
|
|
|
|
|
|
if (!spec->IsAvailable()) {
|
2022-09-23 08:36:22 +00:00
|
|
|
GfxFillRect(r.Shrink(WidgetDimensions::scaled.bevel), PC_BLACK, FILLRECT_CHECKER);
|
2011-01-13 21:55:41 +00:00
|
|
|
}
|
|
|
|
DrawPixelInfo tmp_dpi;
|
|
|
|
/* Set up a clipping area for the preview. */
|
2022-10-15 15:55:47 +00:00
|
|
|
if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.Width(), r.Height())) {
|
2023-01-26 18:21:36 +00:00
|
|
|
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec->grf_prop.grffile == nullptr) {
|
2011-01-13 21:55:41 +00:00
|
|
|
extern const DrawTileSprites _objects[];
|
|
|
|
const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
|
2022-11-28 00:27:57 +00:00
|
|
|
DrawOrigTileSeqInGUI(r.Width() / 2 - 1, r.Height() - this->object_margin - ScaleSpriteTrad(TILE_PIXELS), dts, PAL_NONE);
|
2011-01-13 21:55:41 +00:00
|
|
|
} else {
|
2022-11-28 00:27:57 +00:00
|
|
|
DrawNewObjectTileInGUI(r.Width() / 2 - 1, r.Height() - this->object_margin - ScaleSpriteTrad(TILE_PIXELS), spec,
|
2021-01-08 10:16:18 +00:00
|
|
|
std::min<int>(_selected_object_view, spec->views - 1));
|
2011-01-13 21:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-16 18:06:16 +00:00
|
|
|
case WID_BO_INFO: {
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(_selected_object_index);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (spec == nullptr) break;
|
2010-08-28 19:43:41 +00:00
|
|
|
|
|
|
|
/* Get the extra message for the GUI */
|
|
|
|
if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
|
2019-04-10 21:07:06 +00:00
|
|
|
uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, nullptr, 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) {
|
2014-01-12 18:00:39 +00:00
|
|
|
StartTextRefStackUsage(spec->grf_prop.grffile, 6);
|
2011-11-08 17:24:43 +00:00
|
|
|
/* 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. */
|
2022-11-28 00:27:57 +00:00
|
|
|
int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top - 1;
|
2011-11-08 17:24:43 +00:00
|
|
|
StopTextRefStackUsage();
|
|
|
|
if (y > this->info_height) {
|
|
|
|
BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
|
2022-11-28 00:27:57 +00:00
|
|
|
bow->info_height = y;
|
2011-11-08 17:24:43 +00:00
|
|
|
bow->ReInit();
|
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 09:43:10 +00:00
|
|
|
/**
|
|
|
|
* Clear class filter if the selected class is not included in the filter.
|
|
|
|
* @param object_class Object class select.
|
|
|
|
*/
|
|
|
|
void ClearFilterIsClassNotIncluded(ObjectClassID object_class)
|
|
|
|
{
|
|
|
|
/* Filter is not enabled */
|
|
|
|
if (!this->object_classes.IsFilterEnabled()) return;
|
|
|
|
|
|
|
|
for (auto oc : this->object_classes) {
|
|
|
|
if (oc == object_class) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->ClearEditBox(WID_BO_FILTER);
|
|
|
|
}
|
|
|
|
|
2013-06-30 14:37:50 +00:00
|
|
|
/**
|
|
|
|
* Select the specified object class.
|
2021-02-19 09:26:54 +00:00
|
|
|
* @param object_class Object class select.
|
2013-06-30 14:37:50 +00:00
|
|
|
*/
|
2021-03-07 18:25:48 +00:00
|
|
|
void SelectOtherClass(ObjectClassID object_class)
|
2013-06-30 14:37:50 +00:00
|
|
|
{
|
2021-02-19 09:26:54 +00:00
|
|
|
_selected_object_class = object_class;
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(object_class);
|
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(objclass->GetUISpecCount());
|
2013-06-30 14:37:50 +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) {
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->GetSpec(_selected_object_index);
|
2021-01-08 10:16:18 +00:00
|
|
|
_selected_object_view = std::min<int>(_selected_object_view, spec->views - 1);
|
2010-12-10 21:34:36 +00:00
|
|
|
this->ReInit();
|
|
|
|
} else {
|
|
|
|
_selected_object_view = 0;
|
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2015-07-28 18:20:54 +00:00
|
|
|
if (_selected_object_index != -1) {
|
2020-09-14 22:19:45 +00:00
|
|
|
SetObjectToPlaceWnd(SPR_CURSOR_TRANSMITTER, PAL_NONE, HT_RECT | HT_DIAGONAL, this);
|
2020-10-07 23:04:00 +00:00
|
|
|
} else {
|
|
|
|
if (_thd.window_class == this->window_class &&
|
|
|
|
_thd.window_number == this->window_number) {
|
|
|
|
ResetObjectToPlace();
|
|
|
|
}
|
2015-07-28 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 18:18:59 +00:00
|
|
|
this->UpdateButtons(_selected_object_class, _selected_object_index, _selected_object_view);
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-20 09:43:10 +00:00
|
|
|
/**
|
|
|
|
* Scrolls #WID_BO_SCROLLBAR so that the selected class is visible.
|
|
|
|
*/
|
|
|
|
void EnsureSelectedClassIsVisible()
|
|
|
|
{
|
|
|
|
uint pos = 0;
|
|
|
|
for (auto object_class : this->object_classes) {
|
|
|
|
if (object_class == _selected_object_class) {
|
|
|
|
this->vscroll->SetCount(this->object_classes.size());
|
|
|
|
this->vscroll->ScrollTowards(pos);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
void UpdateSelectSize()
|
|
|
|
{
|
|
|
|
if (_selected_object_index == -1) {
|
|
|
|
SetTileSelectSize(1, 1);
|
|
|
|
} else {
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
const ObjectSpec *spec = objclass->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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 18:18:59 +00:00
|
|
|
/**
|
|
|
|
* Update buttons to show the selection to the user.
|
2021-02-19 09:26:54 +00:00
|
|
|
* @param object_class The class of the selected object.
|
2015-07-28 18:18:59 +00:00
|
|
|
* @param sel_index Index of the object to select, or \c -1 .
|
|
|
|
* @param sel_view View of the object to select.
|
|
|
|
*/
|
2021-03-07 18:25:48 +00:00
|
|
|
void UpdateButtons(ObjectClassID object_class, int sel_index, uint sel_view)
|
2015-07-28 18:18:59 +00:00
|
|
|
{
|
|
|
|
int view_number, object_number;
|
|
|
|
if (sel_index == -1) {
|
|
|
|
view_number = -1; // If no object selected, also hide the selected view.
|
|
|
|
object_number = -1;
|
|
|
|
} else {
|
|
|
|
view_number = sel_view;
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
object_number = objclass->GetUIFromIndex(sel_index);
|
2015-07-28 18:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(view_number);
|
|
|
|
this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(object_number);
|
|
|
|
this->UpdateSelectSize();
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
|
2021-02-19 09:26:54 +00:00
|
|
|
void OnInvalidateData(int data = 0, bool gui_scope = true) override
|
|
|
|
{
|
|
|
|
if (!gui_scope) return;
|
|
|
|
|
|
|
|
this->BuildObjectClassesAvailable();
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnResize() override
|
2011-01-13 22:02:13 +00:00
|
|
|
{
|
2011-12-16 18:06:16 +00:00
|
|
|
this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
|
2011-01-13 22:02:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
2010-08-28 19:43:41 +00:00
|
|
|
{
|
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: {
|
2023-05-26 21:29:52 +00:00
|
|
|
auto it = this->vscroll->GetScrolledItemFromWidget(this->object_classes, pt.y, this, widget);
|
2023-05-03 10:17:52 +00:00
|
|
|
if (it == this->object_classes.end()) break;
|
2011-01-13 21:59:39 +00:00
|
|
|
|
2023-05-03 10:17:52 +00:00
|
|
|
this->SelectOtherClass(*it);
|
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: {
|
2021-03-07 18:25:48 +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);
|
2015-07-28 18:20:54 +00:00
|
|
|
this->SelectOtherObject(_selected_object_index); // Re-select the object for a different view.
|
2010-12-10 21:34:36 +00:00
|
|
|
}
|
|
|
|
break;
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnPlaceObject(Point pt, TileIndex tile) override
|
2015-07-28 18:20:54 +00:00
|
|
|
{
|
2021-03-07 18:25:48 +00:00
|
|
|
const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
|
2020-10-07 23:04:00 +00:00
|
|
|
if (spec == nullptr) return;
|
2020-09-14 22:19:45 +00:00
|
|
|
if (_settings_game.construction.build_object_area_permitted && spec->size == 0x11) {
|
|
|
|
VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_BUILD_OBJECT);
|
|
|
|
} else {
|
|
|
|
DoCommandP(tile, spec->Index(),
|
|
|
|
_selected_object_view, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT), CcTerraform);
|
|
|
|
}
|
2015-07-28 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnPlaceObjectAbort() override
|
2015-07-28 18:20:54 +00:00
|
|
|
{
|
|
|
|
this->UpdateButtons(_selected_object_class, -1, _selected_object_view);
|
|
|
|
}
|
|
|
|
|
2020-09-14 22:19:45 +00:00
|
|
|
void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt) override
|
|
|
|
{
|
|
|
|
VpSelectTilesWithMethod(pt.x, pt.y, select_method);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile) override
|
|
|
|
{
|
|
|
|
if (pt.x != -1) {
|
|
|
|
switch (select_proc) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case DDSP_BUILD_OBJECT:
|
|
|
|
if (!_settings_game.construction.freeform_edges) {
|
|
|
|
/* When end_tile is MP_VOID, the error tile will not be visible to the
|
|
|
|
* user. This happens when terraforming at the southern border. */
|
|
|
|
if (TileX(end_tile) == MapMaxX()) end_tile += TileDiffXY(-1, 0);
|
|
|
|
if (TileY(end_tile) == MapMaxY()) end_tile += TileDiffXY(0, -1);
|
|
|
|
}
|
|
|
|
DoCommandP(end_tile, start_tile,
|
2021-03-07 18:25:48 +00:00
|
|
|
( ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index)->Index() << 3) | (_selected_object_view << 1) | (_ctrl_pressed ? 1 : 0),
|
2020-09-14 22:19:45 +00:00
|
|
|
CMD_BUILD_OBJECT_AREA | CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT), CcTerraform);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 19:16:27 +00:00
|
|
|
EventState OnHotkey(int hotkey) override
|
|
|
|
{
|
|
|
|
switch (hotkey) {
|
|
|
|
case BOHK_FOCUS_FILTER_BOX:
|
|
|
|
this->SetFocusedWidget(WID_BO_FILTER);
|
|
|
|
SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return ES_NOT_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ES_HANDLED;
|
|
|
|
}
|
|
|
|
|
2021-02-19 09:26:54 +00:00
|
|
|
void OnEditboxChanged(int wid) override
|
|
|
|
{
|
|
|
|
string_filter.SetFilterTerm(this->filter_editbox.text.buf);
|
|
|
|
this->object_classes.SetFilterState(!string_filter.IsEmpty());
|
|
|
|
this->object_classes.ForceRebuild();
|
|
|
|
this->InvalidateData();
|
|
|
|
}
|
2020-09-14 22:19:45 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2021-03-07 18:25:48 +00:00
|
|
|
ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
/* First try to select an object in the selected class. */
|
2021-03-07 18:25:48 +00:00
|
|
|
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()) {
|
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. */
|
2021-03-07 18:25:48 +00:00
|
|
|
for (auto object_class_id : this->object_classes) {
|
|
|
|
ObjectClass *objclass = ObjectClass::Get(object_class_id);
|
2012-04-22 16:28:14 +00:00
|
|
|
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()) {
|
2021-03-07 18:25:48 +00:00
|
|
|
this->SelectOtherClass(object_class_id);
|
2010-08-28 19:43:41 +00:00
|
|
|
this->SelectOtherObject(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-24 17:26:08 +00:00
|
|
|
/* If all objects are unavailable, select nothing... */
|
2021-03-07 18:25:48 +00:00
|
|
|
if (objclass->GetUISpecCount() == 0) {
|
2012-04-24 17:26:08 +00:00
|
|
|
/* ... but make sure that the class is not empty. */
|
2021-03-07 18:25:48 +00:00
|
|
|
for (auto object_class_id : this->object_classes) {
|
|
|
|
ObjectClass *objclass = ObjectClass::Get(object_class_id);
|
2021-02-19 09:26:54 +00:00
|
|
|
if (objclass->GetUISpecCount() > 0) {
|
2021-03-07 18:25:48 +00:00
|
|
|
this->SelectOtherClass(object_class_id);
|
2012-04-24 17:26:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-28 19:43:41 +00:00
|
|
|
this->SelectOtherObject(-1);
|
|
|
|
}
|
2021-03-23 19:16:27 +00:00
|
|
|
|
|
|
|
static HotkeyList hotkeys;
|
2010-08-28 19:43:41 +00:00
|
|
|
};
|
|
|
|
|
2021-03-23 19:16:27 +00:00
|
|
|
/**
|
|
|
|
* Handler for global hotkeys of the BuildObjectWindow.
|
|
|
|
* @param hotkey Hotkey
|
|
|
|
* @return ES_HANDLED if hotkey was accepted.
|
|
|
|
*/
|
|
|
|
static EventState BuildObjectGlobalHotkeys(int hotkey)
|
|
|
|
{
|
|
|
|
if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
|
|
|
|
Window *w = ShowBuildObjectPicker();
|
|
|
|
if (w == nullptr) return ES_NOT_HANDLED;
|
|
|
|
return w->OnHotkey(hotkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Hotkey buildobject_hotkeys[] = {
|
|
|
|
Hotkey('F', "focus_filter_box", BOHK_FOCUS_FILTER_BOX),
|
|
|
|
HOTKEY_LIST_END
|
2010-08-28 19:43:41 +00:00
|
|
|
};
|
2021-03-23 19:16:27 +00:00
|
|
|
HotkeyList BuildObjectWindow::hotkeys("buildobject", buildobject_hotkeys, BuildObjectGlobalHotkeys);
|
2010-08-28 19:43:41 +00:00
|
|
|
|
2021-02-19 09:26:54 +00:00
|
|
|
Listing BuildObjectWindow::last_sorting = { false, 0 };
|
|
|
|
Filtering BuildObjectWindow::last_filtering = { false, 0 };
|
|
|
|
|
2021-02-19 15:06:00 +00:00
|
|
|
BuildObjectWindow::GUIObjectClassList::SortFunction * const BuildObjectWindow::sorter_funcs[] = {
|
2021-03-07 18:25:48 +00:00
|
|
|
&ObjectClassIDSorter,
|
2021-02-19 09:26:54 +00:00
|
|
|
};
|
|
|
|
|
2021-02-19 15:06:00 +00:00
|
|
|
BuildObjectWindow::GUIObjectClassList::FilterFunction * const BuildObjectWindow::filter_funcs[] = {
|
2021-02-19 09:26:54 +00:00
|
|
|
&TagNameFilter,
|
|
|
|
};
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
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),
|
2022-09-11 19:22:06 +00:00
|
|
|
NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
|
2013-05-26 19:30:07 +00:00
|
|
|
NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
|
2022-09-11 20:15:02 +00:00
|
|
|
NWidget(WWT_STICKYBOX, 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),
|
2021-04-02 08:15:26 +00:00
|
|
|
NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 2),
|
|
|
|
NWidget(NWID_VERTICAL), SetPadding(0, 5, 2, 0), SetPIP(0, 2, 0),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2021-02-19 09:26:54 +00:00
|
|
|
NWidget(WWT_TEXT, COLOUR_DARK_GREEN), SetFill(0, 1), SetDataTip(STR_LIST_FILTER_TITLE, STR_NULL),
|
|
|
|
NWidget(WWT_EDITBOX, COLOUR_GREY, WID_BO_FILTER), SetFill(1, 0), SetResize(1, 0),
|
|
|
|
SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
|
|
|
|
EndContainer(),
|
2021-04-02 08:15:26 +00:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
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(),
|
2021-04-02 08:15:26 +00:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
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(),
|
2023-04-25 09:03:48 +00:00
|
|
|
NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_JUST_STRING, STR_NULL), SetTextStyle(TC_ORANGE),
|
2021-04-02 08:15:26 +00:00
|
|
|
NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL),
|
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),
|
2022-11-28 00:27:57 +00:00
|
|
|
NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(0, 5, 2, 2), 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,
|
2021-03-23 19:16:27 +00:00
|
|
|
_nested_build_object_widgets, lengthof(_nested_build_object_widgets),
|
|
|
|
&BuildObjectWindow::hotkeys
|
2010-08-28 19:43:41 +00:00
|
|
|
);
|
|
|
|
|
2018-10-28 02:17:36 +00:00
|
|
|
/** Show our object picker. */
|
2021-03-23 19:16:27 +00:00
|
|
|
Window *ShowBuildObjectPicker()
|
2010-08-28 19:43:41 +00:00
|
|
|
{
|
2015-11-07 15:07:18 +00:00
|
|
|
/* Don't show the place object button when there are no objects to place. */
|
2023-01-03 03:31:09 +00:00
|
|
|
if (ObjectClass::HasUIClass()) {
|
2021-03-23 19:16:27 +00:00
|
|
|
return AllocateWindowDescFront<BuildObjectWindow>(&_build_object_desc, 0);
|
2015-11-07 15:07:18 +00:00
|
|
|
}
|
2021-03-23 19:16:27 +00:00
|
|
|
return nullptr;
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 18:37:14 +00:00
|
|
|
/** Show our object picker, and select a particular spec. */
|
|
|
|
void ShowBuildObjectPickerAndSelect(const ObjectSpec *spec)
|
|
|
|
{
|
2023-06-13 17:37:30 +00:00
|
|
|
if (spec == nullptr || !spec->IsAvailable() || !ObjectClass::HasUIClass() || spec->cls_id == INVALID_OBJECT_CLASS) return;
|
2023-06-05 18:37:14 +00:00
|
|
|
|
|
|
|
int spec_id = -1;
|
|
|
|
const ObjectClass *objclass = ObjectClass::Get(spec->cls_id);
|
|
|
|
for (int i = 0; i < (int)objclass->GetSpecCount(); i++) {
|
|
|
|
if (objclass->GetSpec(i) == spec) {
|
|
|
|
spec_id = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (spec_id < 0) return;
|
|
|
|
|
|
|
|
BuildObjectWindow *w = AllocateWindowDescFront<BuildObjectWindow>(&_build_object_desc, 0, true);
|
|
|
|
if (w != nullptr) {
|
2023-07-20 09:43:10 +00:00
|
|
|
w->ClearFilterIsClassNotIncluded(spec->cls_id);
|
2023-06-05 18:37:14 +00:00
|
|
|
w->SelectOtherClass(spec->cls_id);
|
|
|
|
w->SelectOtherObject(spec_id);
|
2023-07-20 09:43:10 +00:00
|
|
|
w->EnsureSelectedClassIsVisible();
|
2023-06-05 18:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-28 19:43:41 +00:00
|
|
|
/** Reset all data of the object GUI. */
|
|
|
|
void InitializeObjectGui()
|
|
|
|
{
|
2021-03-07 18:25:48 +00:00
|
|
|
_selected_object_class = ObjectClassID::OBJECT_CLASS_BEGIN;
|
2010-08-28 19:43:41 +00:00
|
|
|
}
|