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/>.
8 /** @file object_gui.cpp The GUI for objects. */
11 #include "command_func.h"
13 #include "newgrf_object.h"
14 #include "newgrf_text.h"
15 #include "strings_func.h"
16 #include "viewport_func.h"
17 #include "tilehighlight_func.h"
18 #include "window_gui.h"
19 #include "window_func.h"
20 #include "zoom_func.h"
22 #include "widgets/object_widget.h"
24 #include "table/strings.h"
26 #include "safeguards.h"
28 static ObjectClassID _selected_object_class
; ///< the currently visible object class
29 static int _selected_object_index
; ///< the index of the selected object in the current class or -1
30 static uint8 _selected_object_view
; ///< the view of the selected object
32 /** The window used for building objects. */
33 class BuildObjectWindow
: public Window
{
34 static const int OBJECT_MARGIN
= 4; ///< The margin (in pixels) around an object.
35 int line_height
; ///< The height of a single line.
36 int info_height
; ///< The height of the info box.
37 Scrollbar
*vscroll
; ///< The scrollbar.
39 /** Scroll #WID_BO_CLASS_LIST so that the selected object class is visible. */
40 void EnsureSelectedObjectClassIsVisible()
43 for (int i
= 0; i
< _selected_object_class
; i
++) {
44 if (ObjectClass::Get((ObjectClassID
) i
)->GetUISpecCount() == 0) continue;
47 this->vscroll
->ScrollTowards(pos
);
51 * Tests whether the previously selected object can be selected.
52 * @return \c true if the selected object is available, \c false otherwise.
54 bool CanRestoreSelectedObject()
56 if (_selected_object_index
== -1) return false;
58 ObjectClass
*sel_objclass
= ObjectClass::Get(_selected_object_class
);
59 if ((int)sel_objclass
->GetSpecCount() <= _selected_object_index
) return false;
61 return sel_objclass
->GetSpec(_selected_object_index
)->IsAvailable();
65 * Calculate the number of columns of the #WID_BO_SELECT_MATRIX widget.
66 * @return Number of columns in the matrix.
68 uint
GetMatrixColumnCount()
70 const NWidgetBase
*matrix
= this->GetWidget
<NWidgetBase
>(WID_BO_SELECT_MATRIX
);
71 return 1 + (matrix
->current_x
- matrix
->smallest_x
) / matrix
->resize_x
;
75 BuildObjectWindow(WindowDesc
*desc
, WindowNumber number
) : Window(desc
), info_height(1)
77 this->CreateNestedTree();
78 this->vscroll
= this->GetScrollbar(WID_BO_SCROLLBAR
);
79 this->FinishInitNested(number
);
83 this->vscroll
->SetPosition(0);
84 this->vscroll
->SetCount(ObjectClass::GetUIClassCount());
86 NWidgetMatrix
*matrix
= this->GetWidget
<NWidgetMatrix
>(WID_BO_SELECT_MATRIX
);
87 matrix
->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL
));
89 this->SelectOtherClass(_selected_object_class
);
90 if (this->CanRestoreSelectedObject()) {
91 this->SelectOtherObject(_selected_object_index
);
93 this->SelectFirstAvailableObject(true);
95 assert(ObjectClass::Get(_selected_object_class
)->GetUISpecCount() > 0); // object GUI should be disables elsewise
96 this->EnsureSelectedObjectClassIsVisible();
97 this->GetWidget
<NWidgetMatrix
>(WID_BO_OBJECT_MATRIX
)->SetCount(4);
100 void SetStringParameters(int widget
) const override
103 case WID_BO_OBJECT_NAME
: {
104 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
105 SetDParam(0, spec
!= nullptr ? spec
->name
: STR_EMPTY
);
109 case WID_BO_OBJECT_SIZE
: {
110 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
111 int size
= spec
== nullptr ? 0 : spec
->size
;
112 SetDParam(0, GB(size
, HasBit(_selected_object_view
, 0) ? 4 : 0, 4));
113 SetDParam(1, GB(size
, HasBit(_selected_object_view
, 0) ? 0 : 4, 4));
121 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
124 case WID_BO_CLASS_LIST
: {
125 for (uint i
= 0; i
< ObjectClass::GetClassCount(); i
++) {
126 ObjectClass
*objclass
= ObjectClass::Get((ObjectClassID
)i
);
127 if (objclass
->GetUISpecCount() == 0) continue;
128 size
->width
= max(size
->width
, GetStringBoundingBox(objclass
->name
).width
);
130 size
->width
+= padding
.width
;
131 this->line_height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
132 resize
->height
= this->line_height
;
133 size
->height
= 5 * this->line_height
;
137 case WID_BO_OBJECT_NAME
:
138 case WID_BO_OBJECT_SIZE
:
139 /* We do not want the window to resize when selecting objects; better clip texts */
143 case WID_BO_OBJECT_MATRIX
: {
144 /* Get the right amount of buttons based on the current spec. */
145 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
146 if (spec
!= nullptr) {
147 if (spec
->views
>= 2) size
->width
+= resize
->width
;
148 if (spec
->views
>= 4) size
->height
+= resize
->height
;
155 case WID_BO_OBJECT_SPRITE
: {
156 bool two_wide
= false; // Whether there will be two widgets next to each other in the matrix or not.
157 int height
[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
159 /* Get the height and view information. */
160 for (int i
= 0; i
< NUM_OBJECTS
; i
++) {
161 const ObjectSpec
*spec
= ObjectSpec::Get(i
);
162 if (!spec
->IsEverAvailable()) continue;
163 two_wide
|= spec
->views
>= 2;
164 height
[spec
->views
/ 4] = max
<int>(ObjectSpec::Get(i
)->height
, height
[spec
->views
/ 4]);
167 /* Determine the pixel heights. */
168 for (size_t i
= 0; i
< lengthof(height
); i
++) {
169 height
[i
] *= ScaleGUITrad(TILE_HEIGHT
);
170 height
[i
] += ScaleGUITrad(TILE_PIXELS
) + 2 * OBJECT_MARGIN
;
173 /* Now determine the size of the minimum widgets. When there are two columns, then
174 * we want these columns to be slightly less wide. When there are two rows, then
175 * determine the size of the widgets based on the maximum size for a single row
176 * of widgets, or just the twice the widget height of the two row ones. */
177 size
->height
= max(height
[0], height
[1] * 2 + 2);
179 size
->width
= (3 * ScaleGUITrad(TILE_PIXELS
) + 2 * OBJECT_MARGIN
) * 2 + 2;
181 size
->width
= 4 * ScaleGUITrad(TILE_PIXELS
) + 2 * OBJECT_MARGIN
;
184 /* Get the right size for the single widget based on the current spec. */
185 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
186 if (spec
!= nullptr) {
187 if (spec
->views
>= 2) size
->width
= size
->width
/ 2 - 1;
188 if (spec
->views
>= 4) size
->height
= size
->height
/ 2 - 1;
194 size
->height
= this->info_height
;
197 case WID_BO_SELECT_MATRIX
:
202 case WID_BO_SELECT_IMAGE
:
203 size
->width
= ScaleGUITrad(64) + 2;
204 size
->height
= ScaleGUITrad(58) + 2;
211 void DrawWidget(const Rect
&r
, int widget
) const override
213 switch (GB(widget
, 0, 16)) {
214 case WID_BO_CLASS_LIST
: {
217 for (uint i
= 0; i
< ObjectClass::GetClassCount(); i
++) {
218 ObjectClass
*objclass
= ObjectClass::Get((ObjectClassID
)i
);
219 if (objclass
->GetUISpecCount() == 0) continue;
220 if (!this->vscroll
->IsVisible(pos
++)) continue;
221 DrawString(r
.left
+ WD_MATRIX_LEFT
, r
.right
- WD_MATRIX_RIGHT
, y
+ WD_MATRIX_TOP
, objclass
->name
,
222 ((int)i
== _selected_object_class
) ? TC_WHITE
: TC_BLACK
);
223 y
+= this->line_height
;
228 case WID_BO_OBJECT_SPRITE
: {
229 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
230 if (spec
== nullptr) break;
232 /* Height of the selection matrix.
233 * Depending on the number of views, the matrix has a 1x1, 1x2, 2x1 or 2x2 layout. To make the previews
234 * look nice in all layouts, we use the 4x4 layout (smallest previews) as starting point. For the bigger
235 * previews in the layouts with less views we add space homogeneously on all sides, so the 4x4 preview-rectangle
236 * is centered in the 2x1, 1x2 resp. 1x1 buttons. */
237 uint matrix_height
= this->GetWidget
<NWidgetMatrix
>(WID_BO_OBJECT_MATRIX
)->current_y
;
239 DrawPixelInfo tmp_dpi
;
240 /* Set up a clipping area for the preview. */
241 if (FillDrawPixelInfo(&tmp_dpi
, r
.left
, r
.top
, r
.right
- r
.left
+ 1, r
.bottom
- r
.top
+ 1)) {
242 DrawPixelInfo
*old_dpi
= _cur_dpi
;
244 if (spec
->grf_prop
.grffile
== nullptr) {
245 extern const DrawTileSprites _objects
[];
246 const DrawTileSprites
*dts
= &_objects
[spec
->grf_prop
.local_id
];
247 DrawOrigTileSeqInGUI((r
.right
- r
.left
) / 2 - 1, (r
.bottom
- r
.top
+ matrix_height
/ 2) / 2 - OBJECT_MARGIN
- ScaleGUITrad(TILE_PIXELS
), dts
, PAL_NONE
);
249 DrawNewObjectTileInGUI((r
.right
- r
.left
) / 2 - 1, (r
.bottom
- r
.top
+ matrix_height
/ 2) / 2 - OBJECT_MARGIN
- ScaleGUITrad(TILE_PIXELS
), spec
, GB(widget
, 16, 16));
256 case WID_BO_SELECT_IMAGE
: {
257 ObjectClass
*objclass
= ObjectClass::Get(_selected_object_class
);
258 int obj_index
= objclass
->GetIndexFromUI(GB(widget
, 16, 16));
259 if (obj_index
< 0) break;
260 const ObjectSpec
*spec
= objclass
->GetSpec(obj_index
);
261 if (spec
== nullptr) break;
263 if (!spec
->IsAvailable()) {
264 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, PC_BLACK
, FILLRECT_CHECKER
);
266 DrawPixelInfo tmp_dpi
;
267 /* Set up a clipping area for the preview. */
268 if (FillDrawPixelInfo(&tmp_dpi
, r
.left
+ 1, r
.top
, (r
.right
- 1) - (r
.left
+ 1) + 1, r
.bottom
- r
.top
+ 1)) {
269 DrawPixelInfo
*old_dpi
= _cur_dpi
;
271 if (spec
->grf_prop
.grffile
== nullptr) {
272 extern const DrawTileSprites _objects
[];
273 const DrawTileSprites
*dts
= &_objects
[spec
->grf_prop
.local_id
];
274 DrawOrigTileSeqInGUI((r
.right
- r
.left
) / 2 - 1, r
.bottom
- r
.top
- OBJECT_MARGIN
- ScaleGUITrad(TILE_PIXELS
), dts
, PAL_NONE
);
276 DrawNewObjectTileInGUI((r
.right
- r
.left
) / 2 - 1, r
.bottom
- r
.top
- OBJECT_MARGIN
- ScaleGUITrad(TILE_PIXELS
), spec
,
277 min(_selected_object_view
, spec
->views
- 1));
285 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
286 if (spec
== nullptr) break;
288 /* Get the extra message for the GUI */
289 if (HasBit(spec
->callback_mask
, CBM_OBJ_FUND_MORE_TEXT
)) {
290 uint16 callback_res
= GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT
, 0, 0, spec
, nullptr, INVALID_TILE
, _selected_object_view
);
291 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
292 if (callback_res
> 0x400) {
293 ErrorUnknownCallbackResult(spec
->grf_prop
.grffile
->grfid
, CBID_OBJECT_FUND_MORE_TEXT
, callback_res
);
295 StringID message
= GetGRFStringID(spec
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
);
296 if (message
!= STR_NULL
&& message
!= STR_UNDEFINED
) {
297 StartTextRefStackUsage(spec
->grf_prop
.grffile
, 6);
298 /* Use all the available space left from where we stand up to the
299 * end of the window. We ALSO enlarge the window if needed, so we
300 * can 'go' wild with the bottom of the window. */
301 int y
= DrawStringMultiLine(r
.left
, r
.right
, r
.top
, UINT16_MAX
, message
, TC_ORANGE
) - r
.top
;
302 StopTextRefStackUsage();
303 if (y
> this->info_height
) {
304 BuildObjectWindow
*bow
= const_cast<BuildObjectWindow
*>(this);
305 bow
->info_height
= y
+ 2;
317 * Select the specified object class.
318 * @param object_class_index Object class index to select.
320 void SelectOtherClass(ObjectClassID object_class_index
)
322 _selected_object_class
= object_class_index
;
323 this->GetWidget
<NWidgetMatrix
>(WID_BO_SELECT_MATRIX
)->SetCount(ObjectClass::Get(_selected_object_class
)->GetUISpecCount());
327 * Select the specified object in #_selected_object_class class.
328 * @param object_index Object index to select, \c -1 means select nothing.
330 void SelectOtherObject(int object_index
)
332 _selected_object_index
= object_index
;
333 if (_selected_object_index
!= -1) {
334 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
335 _selected_object_view
= min(_selected_object_view
, spec
->views
- 1);
338 _selected_object_view
= 0;
341 if (_selected_object_index
!= -1) {
342 SetObjectToPlaceWnd(SPR_CURSOR_TRANSMITTER
, PAL_NONE
, HT_RECT
, this);
345 this->UpdateButtons(_selected_object_class
, _selected_object_index
, _selected_object_view
);
348 void UpdateSelectSize()
350 if (_selected_object_index
== -1) {
351 SetTileSelectSize(1, 1);
353 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
354 int w
= GB(spec
->size
, HasBit(_selected_object_view
, 0) ? 4 : 0, 4);
355 int h
= GB(spec
->size
, HasBit(_selected_object_view
, 0) ? 0 : 4, 4);
356 SetTileSelectSize(w
, h
);
361 * Update buttons to show the selection to the user.
362 * @param sel_class The class of the selected object.
363 * @param sel_index Index of the object to select, or \c -1 .
364 * @param sel_view View of the object to select.
366 void UpdateButtons(ObjectClassID sel_class
, int sel_index
, uint sel_view
)
368 int view_number
, object_number
;
369 if (sel_index
== -1) {
370 view_number
= -1; // If no object selected, also hide the selected view.
373 view_number
= sel_view
;
374 object_number
= ObjectClass::Get(sel_class
)->GetUIFromIndex(sel_index
);
377 this->GetWidget
<NWidgetMatrix
>(WID_BO_OBJECT_MATRIX
)->SetClicked(view_number
);
378 this->GetWidget
<NWidgetMatrix
>(WID_BO_SELECT_MATRIX
)->SetClicked(object_number
);
379 this->UpdateSelectSize();
383 void OnResize() override
385 this->vscroll
->SetCapacityFromWidget(this, WID_BO_CLASS_LIST
);
388 void OnClick(Point pt
, int widget
, int click_count
) override
390 switch (GB(widget
, 0, 16)) {
391 case WID_BO_CLASS_LIST
: {
392 int num_clicked
= this->vscroll
->GetPosition() + (pt
.y
- this->nested_array
[widget
]->pos_y
) / this->line_height
;
393 if (num_clicked
>= (int)ObjectClass::GetUIClassCount()) break;
395 this->SelectOtherClass(ObjectClass::GetUIClass(num_clicked
));
396 this->SelectFirstAvailableObject(false);
400 case WID_BO_SELECT_IMAGE
: {
401 ObjectClass
*objclass
= ObjectClass::Get(_selected_object_class
);
402 int num_clicked
= objclass
->GetIndexFromUI(GB(widget
, 16, 16));
403 if (num_clicked
>= 0 && objclass
->GetSpec(num_clicked
)->IsAvailable()) this->SelectOtherObject(num_clicked
);
407 case WID_BO_OBJECT_SPRITE
:
408 if (_selected_object_index
!= -1) {
409 _selected_object_view
= GB(widget
, 16, 16);
410 this->SelectOtherObject(_selected_object_index
); // Re-select the object for a different view.
416 void OnPlaceObject(Point pt
, TileIndex tile
) override
418 DoCommandP(tile
, ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
)->Index(),
419 _selected_object_view
, CMD_BUILD_OBJECT
| CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT
), CcTerraform
);
422 void OnPlaceObjectAbort() override
424 this->UpdateButtons(_selected_object_class
, -1, _selected_object_view
);
428 * Select the first available object.
429 * @param change_class If true, change the class if no object in the current
430 * class is available.
432 void SelectFirstAvailableObject(bool change_class
)
434 /* First try to select an object in the selected class. */
435 ObjectClass
*sel_objclass
= ObjectClass::Get(_selected_object_class
);
436 for (uint i
= 0; i
< sel_objclass
->GetSpecCount(); i
++) {
437 const ObjectSpec
*spec
= sel_objclass
->GetSpec(i
);
438 if (spec
->IsAvailable()) {
439 this->SelectOtherObject(i
);
444 /* If that fails, select the first available object
445 * from a random class. */
446 for (ObjectClassID j
= OBJECT_CLASS_BEGIN
; j
< OBJECT_CLASS_MAX
; j
++) {
447 ObjectClass
*objclass
= ObjectClass::Get(j
);
448 for (uint i
= 0; i
< objclass
->GetSpecCount(); i
++) {
449 const ObjectSpec
*spec
= objclass
->GetSpec(i
);
450 if (spec
->IsAvailable()) {
451 this->SelectOtherClass(j
);
452 this->SelectOtherObject(i
);
458 /* If all objects are unavailable, select nothing... */
459 if (ObjectClass::Get(_selected_object_class
)->GetUISpecCount() == 0) {
460 /* ... but make sure that the class is not empty. */
461 for (ObjectClassID j
= OBJECT_CLASS_BEGIN
; j
< OBJECT_CLASS_MAX
; j
++) {
462 if (ObjectClass::Get(j
)->GetUISpecCount() > 0) {
463 this->SelectOtherClass(j
);
468 this->SelectOtherObject(-1);
472 static const NWidgetPart _nested_build_object_widgets
[] = {
473 NWidget(NWID_HORIZONTAL
),
474 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
475 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_OBJECT_BUILD_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
476 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
478 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
479 NWidget(NWID_HORIZONTAL
), SetPadding(2, 0, 0, 0),
480 NWidget(NWID_VERTICAL
),
481 NWidget(NWID_HORIZONTAL
), SetPadding(0, 5, 2, 5),
482 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_BO_CLASS_LIST
), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_OBJECT_BUILD_CLASS_TOOLTIP
), SetScrollbar(WID_BO_SCROLLBAR
),
483 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_BO_SCROLLBAR
),
485 NWidget(NWID_HORIZONTAL
), SetPadding(0, 5, 0, 5),
486 NWidget(NWID_MATRIX
, COLOUR_DARK_GREEN
, WID_BO_OBJECT_MATRIX
), SetPIP(0, 2, 0),
487 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BO_OBJECT_SPRITE
), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP
), EndContainer(),
490 NWidget(WWT_TEXT
, COLOUR_DARK_GREEN
, WID_BO_OBJECT_NAME
), SetDataTip(STR_ORANGE_STRING
, STR_NULL
), SetPadding(2, 5, 2, 5),
491 NWidget(WWT_TEXT
, COLOUR_DARK_GREEN
, WID_BO_OBJECT_SIZE
), SetDataTip(STR_OBJECT_BUILD_SIZE
, STR_NULL
), SetPadding(2, 5, 2, 5),
493 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetScrollbar(WID_BO_SELECT_SCROLL
),
494 NWidget(NWID_HORIZONTAL
),
495 NWidget(NWID_MATRIX
, COLOUR_DARK_GREEN
, WID_BO_SELECT_MATRIX
), SetFill(0, 1), SetPIP(0, 2, 0),
496 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BO_SELECT_IMAGE
), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP
),
497 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL
),
500 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_BO_SELECT_SCROLL
),
504 NWidget(NWID_HORIZONTAL
),
505 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_BO_INFO
), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
506 NWidget(NWID_VERTICAL
),
507 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetFill(0, 1), EndContainer(),
508 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
514 static WindowDesc
_build_object_desc(
515 WDP_AUTO
, "build_object", 0, 0,
516 WC_BUILD_OBJECT
, WC_BUILD_TOOLBAR
,
518 _nested_build_object_widgets
, lengthof(_nested_build_object_widgets
)
521 /** Show our object picker. */
522 void ShowBuildObjectPicker()
524 /* Don't show the place object button when there are no objects to place. */
525 if (ObjectClass::GetUIClassCount() > 0) {
526 AllocateWindowDescFront
<BuildObjectWindow
>(&_build_object_desc
, 0);
530 /** Reset all data of the object GUI. */
531 void InitializeObjectGui()
533 _selected_object_class
= (ObjectClassID
)0;