4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file object_gui.cpp The GUI for objects. */
13 #include "command_func.h"
15 #include "newgrf_object.h"
16 #include "newgrf_text.h"
17 #include "strings_func.h"
18 #include "viewport_func.h"
19 #include "tilehighlight_func.h"
20 #include "window_gui.h"
21 #include "window_func.h"
22 #include "zoom_func.h"
24 #include "widgets/object_widget.h"
26 #include "table/strings.h"
28 #include "safeguards.h"
30 static ObjectClassID _selected_object_class
; ///< the currently visible object class
31 static int _selected_object_index
; ///< the index of the selected object in the current class or -1
32 static uint8 _selected_object_view
; ///< the view of the selected object
34 /** The window used for building objects. */
35 class BuildObjectWindow
: public Window
{
36 static const int OBJECT_MARGIN
= 4; ///< The margin (in pixels) around an object.
37 int line_height
; ///< The height of a single line.
38 int info_height
; ///< The height of the info box.
39 Scrollbar
*vscroll
; ///< The scrollbar.
41 /** Scroll #WID_BO_CLASS_LIST so that the selected object class is visible. */
42 void EnsureSelectedObjectClassIsVisible()
45 for (int i
= 0; i
< _selected_object_class
; i
++) {
46 if (ObjectClass::Get((ObjectClassID
) i
)->GetUISpecCount() == 0) continue;
49 this->vscroll
->ScrollTowards(pos
);
53 * Tests whether the previously selected object can be selected.
54 * @return \c true if the selected object is available, \c false otherwise.
56 bool CanRestoreSelectedObject()
58 if (_selected_object_index
== -1) return false;
60 ObjectClass
*sel_objclass
= ObjectClass::Get(_selected_object_class
);
61 if ((int)sel_objclass
->GetSpecCount() <= _selected_object_index
) return false;
63 return sel_objclass
->GetSpec(_selected_object_index
)->IsAvailable();
67 * Calculate the number of columns of the #WID_BO_SELECT_MATRIX widget.
68 * @return Number of columns in the matrix.
70 uint
GetMatrixColumnCount()
72 const NWidgetBase
*matrix
= this->GetWidget
<NWidgetBase
>(WID_BO_SELECT_MATRIX
);
73 return 1 + (matrix
->current_x
- matrix
->smallest_x
) / matrix
->resize_x
;
77 BuildObjectWindow(WindowDesc
*desc
, WindowNumber number
) : Window(desc
), info_height(1)
79 this->CreateNestedTree();
80 this->vscroll
= this->GetScrollbar(WID_BO_SCROLLBAR
);
81 this->FinishInitNested(number
);
85 this->vscroll
->SetPosition(0);
86 this->vscroll
->SetCount(ObjectClass::GetUIClassCount());
88 NWidgetMatrix
*matrix
= this->GetWidget
<NWidgetMatrix
>(WID_BO_SELECT_MATRIX
);
89 matrix
->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL
));
91 this->SelectOtherClass(_selected_object_class
);
92 if (this->CanRestoreSelectedObject()) {
93 this->SelectOtherObject(_selected_object_index
);
95 this->SelectFirstAvailableObject(true);
97 assert(ObjectClass::Get(_selected_object_class
)->GetUISpecCount() > 0); // object GUI should be disables elsewise
98 this->EnsureSelectedObjectClassIsVisible();
99 this->GetWidget
<NWidgetMatrix
>(WID_BO_OBJECT_MATRIX
)->SetCount(4);
102 virtual void SetStringParameters(int widget
) const
105 case WID_BO_OBJECT_NAME
: {
106 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
107 SetDParam(0, spec
!= NULL
? spec
->name
: STR_EMPTY
);
111 case WID_BO_OBJECT_SIZE
: {
112 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
113 int size
= spec
== NULL
? 0 : spec
->size
;
114 SetDParam(0, GB(size
, HasBit(_selected_object_view
, 0) ? 4 : 0, 4));
115 SetDParam(1, GB(size
, HasBit(_selected_object_view
, 0) ? 0 : 4, 4));
123 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
126 case WID_BO_CLASS_LIST
: {
127 for (uint i
= 0; i
< ObjectClass::GetClassCount(); i
++) {
128 ObjectClass
*objclass
= ObjectClass::Get((ObjectClassID
)i
);
129 if (objclass
->GetUISpecCount() == 0) continue;
130 size
->width
= max(size
->width
, GetStringBoundingBox(objclass
->name
).width
);
132 size
->width
+= padding
.width
;
133 this->line_height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
134 resize
->height
= this->line_height
;
135 size
->height
= 5 * this->line_height
;
139 case WID_BO_OBJECT_NAME
:
140 case WID_BO_OBJECT_SIZE
:
141 /* We do not want the window to resize when selecting objects; better clip texts */
145 case WID_BO_OBJECT_MATRIX
: {
146 /* Get the right amount of buttons based on the current spec. */
147 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
149 if (spec
->views
>= 2) size
->width
+= resize
->width
;
150 if (spec
->views
>= 4) size
->height
+= resize
->height
;
157 case WID_BO_OBJECT_SPRITE
: {
158 bool two_wide
= false; // Whether there will be two widgets next to each other in the matrix or not.
159 int height
[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
161 /* Get the height and view information. */
162 for (int i
= 0; i
< NUM_OBJECTS
; i
++) {
163 const ObjectSpec
*spec
= ObjectSpec::Get(i
);
164 if (!spec
->IsEverAvailable()) continue;
165 two_wide
|= spec
->views
>= 2;
166 height
[spec
->views
/ 4] = max
<int>(ObjectSpec::Get(i
)->height
, height
[spec
->views
/ 4]);
169 /* Determine the pixel heights. */
170 for (size_t i
= 0; i
< lengthof(height
); i
++) {
171 height
[i
] *= ScaleGUITrad(TILE_HEIGHT
);
172 height
[i
] += ScaleGUITrad(TILE_PIXELS
) + 2 * OBJECT_MARGIN
;
175 /* Now determine the size of the minimum widgets. When there are two columns, then
176 * we want these columns to be slightly less wide. When there are two rows, then
177 * determine the size of the widgets based on the maximum size for a single row
178 * of widgets, or just the twice the widget height of the two row ones. */
179 size
->height
= max(height
[0], height
[1] * 2 + 2);
181 size
->width
= (3 * ScaleGUITrad(TILE_PIXELS
) + 2 * OBJECT_MARGIN
) * 2 + 2;
183 size
->width
= 4 * ScaleGUITrad(TILE_PIXELS
) + 2 * OBJECT_MARGIN
;
186 /* Get the right size for the single widget based on the current spec. */
187 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
189 if (spec
->views
>= 2) size
->width
= size
->width
/ 2 - 1;
190 if (spec
->views
>= 4) size
->height
= size
->height
/ 2 - 1;
196 size
->height
= this->info_height
;
199 case WID_BO_SELECT_MATRIX
:
204 case WID_BO_SELECT_IMAGE
:
205 size
->width
= ScaleGUITrad(64) + 2;
206 size
->height
= ScaleGUITrad(58) + 2;
213 virtual void DrawWidget(const Rect
&r
, int widget
) const
215 switch (GB(widget
, 0, 16)) {
216 case WID_BO_CLASS_LIST
: {
219 for (uint i
= 0; i
< ObjectClass::GetClassCount(); i
++) {
220 ObjectClass
*objclass
= ObjectClass::Get((ObjectClassID
)i
);
221 if (objclass
->GetUISpecCount() == 0) continue;
222 if (!this->vscroll
->IsVisible(pos
++)) continue;
223 DrawString(r
.left
+ WD_MATRIX_LEFT
, r
.right
- WD_MATRIX_RIGHT
, y
+ WD_MATRIX_TOP
, objclass
->name
,
224 ((int)i
== _selected_object_class
) ? TC_WHITE
: TC_BLACK
);
225 y
+= this->line_height
;
230 case WID_BO_OBJECT_SPRITE
: {
231 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
232 if (spec
== NULL
) break;
234 /* Height of the selection matrix.
235 * Depending on the number of views, the matrix has a 1x1, 1x2, 2x1 or 2x2 layout. To make the previews
236 * look nice in all layouts, we use the 4x4 layout (smallest previews) as starting point. For the bigger
237 * previews in the layouts with less views we add space homogeneously on all sides, so the 4x4 preview-rectangle
238 * is centered in the 2x1, 1x2 resp. 1x1 buttons. */
239 uint matrix_height
= this->GetWidget
<NWidgetMatrix
>(WID_BO_OBJECT_MATRIX
)->current_y
;
241 DrawPixelInfo tmp_dpi
;
242 /* Set up a clipping area for the preview. */
243 if (FillDrawPixelInfo(&tmp_dpi
, r
.left
, r
.top
, r
.right
- r
.left
+ 1, r
.bottom
- r
.top
+ 1)) {
244 DrawPixelInfo
*old_dpi
= _cur_dpi
;
246 if (spec
->grf_prop
.grffile
== NULL
) {
247 extern const DrawTileSprites _objects
[];
248 const DrawTileSprites
*dts
= &_objects
[spec
->grf_prop
.local_id
];
249 DrawOrigTileSeqInGUI((r
.right
- r
.left
) / 2 - 1, (r
.bottom
- r
.top
+ matrix_height
/ 2) / 2 - OBJECT_MARGIN
- ScaleGUITrad(TILE_PIXELS
), dts
, PAL_NONE
);
251 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));
258 case WID_BO_SELECT_IMAGE
: {
259 ObjectClass
*objclass
= ObjectClass::Get(_selected_object_class
);
260 int obj_index
= objclass
->GetIndexFromUI(GB(widget
, 16, 16));
261 if (obj_index
< 0) break;
262 const ObjectSpec
*spec
= objclass
->GetSpec(obj_index
);
263 if (spec
== NULL
) break;
265 if (!spec
->IsAvailable()) {
266 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, PC_BLACK
, FILLRECT_CHECKER
);
268 DrawPixelInfo tmp_dpi
;
269 /* Set up a clipping area for the preview. */
270 if (FillDrawPixelInfo(&tmp_dpi
, r
.left
+ 1, r
.top
, (r
.right
- 1) - (r
.left
+ 1) + 1, r
.bottom
- r
.top
+ 1)) {
271 DrawPixelInfo
*old_dpi
= _cur_dpi
;
273 if (spec
->grf_prop
.grffile
== NULL
) {
274 extern const DrawTileSprites _objects
[];
275 const DrawTileSprites
*dts
= &_objects
[spec
->grf_prop
.local_id
];
276 DrawOrigTileSeqInGUI((r
.right
- r
.left
) / 2 - 1, r
.bottom
- r
.top
- OBJECT_MARGIN
- ScaleGUITrad(TILE_PIXELS
), dts
, PAL_NONE
);
278 DrawNewObjectTileInGUI((r
.right
- r
.left
) / 2 - 1, r
.bottom
- r
.top
- OBJECT_MARGIN
- ScaleGUITrad(TILE_PIXELS
), spec
,
279 min(_selected_object_view
, spec
->views
- 1));
287 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
288 if (spec
== NULL
) break;
290 /* Get the extra message for the GUI */
291 if (HasBit(spec
->callback_mask
, CBM_OBJ_FUND_MORE_TEXT
)) {
292 uint16 callback_res
= GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT
, 0, 0, spec
, NULL
, INVALID_TILE
, _selected_object_view
);
293 if (callback_res
!= CALLBACK_FAILED
&& callback_res
!= 0x400) {
294 if (callback_res
> 0x400) {
295 ErrorUnknownCallbackResult(spec
->grf_prop
.grffile
->grfid
, CBID_OBJECT_FUND_MORE_TEXT
, callback_res
);
297 StringID message
= GetGRFStringID(spec
->grf_prop
.grffile
->grfid
, 0xD000 + callback_res
);
298 if (message
!= STR_NULL
&& message
!= STR_UNDEFINED
) {
299 StartTextRefStackUsage(spec
->grf_prop
.grffile
, 6);
300 /* Use all the available space left from where we stand up to the
301 * end of the window. We ALSO enlarge the window if needed, so we
302 * can 'go' wild with the bottom of the window. */
303 int y
= DrawStringMultiLine(r
.left
, r
.right
, r
.top
, UINT16_MAX
, message
, TC_ORANGE
) - r
.top
;
304 StopTextRefStackUsage();
305 if (y
> this->info_height
) {
306 BuildObjectWindow
*bow
= const_cast<BuildObjectWindow
*>(this);
307 bow
->info_height
= y
+ 2;
319 * Select the specified object class.
320 * @param object_class_index Object class index to select.
322 void SelectOtherClass(ObjectClassID object_class_index
)
324 _selected_object_class
= object_class_index
;
325 this->GetWidget
<NWidgetMatrix
>(WID_BO_SELECT_MATRIX
)->SetCount(ObjectClass::Get(_selected_object_class
)->GetUISpecCount());
329 * Select the specified object in #_selected_object_class class.
330 * @param object_index Object index to select, \c -1 means select nothing.
332 void SelectOtherObject(int object_index
)
334 _selected_object_index
= object_index
;
335 if (_selected_object_index
!= -1) {
336 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
337 _selected_object_view
= min(_selected_object_view
, spec
->views
- 1);
340 _selected_object_view
= 0;
343 if (_selected_object_index
!= -1) {
344 SetObjectToPlaceWnd(SPR_CURSOR_TRANSMITTER
, PAL_NONE
, HT_RECT
, this);
347 this->UpdateButtons(_selected_object_class
, _selected_object_index
, _selected_object_view
);
350 void UpdateSelectSize()
352 if (_selected_object_index
== -1) {
353 SetTileSelectSize(1, 1);
355 const ObjectSpec
*spec
= ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
);
356 int w
= GB(spec
->size
, HasBit(_selected_object_view
, 0) ? 4 : 0, 4);
357 int h
= GB(spec
->size
, HasBit(_selected_object_view
, 0) ? 0 : 4, 4);
358 SetTileSelectSize(w
, h
);
363 * Update buttons to show the selection to the user.
364 * @param sel_class The class of the selected object.
365 * @param sel_index Index of the object to select, or \c -1 .
366 * @param sel_view View of the object to select.
368 void UpdateButtons(ObjectClassID sel_class
, int sel_index
, uint sel_view
)
370 int view_number
, object_number
;
371 if (sel_index
== -1) {
372 view_number
= -1; // If no object selected, also hide the selected view.
375 view_number
= sel_view
;
376 object_number
= ObjectClass::Get(sel_class
)->GetUIFromIndex(sel_index
);
379 this->GetWidget
<NWidgetMatrix
>(WID_BO_OBJECT_MATRIX
)->SetClicked(view_number
);
380 this->GetWidget
<NWidgetMatrix
>(WID_BO_SELECT_MATRIX
)->SetClicked(object_number
);
381 this->UpdateSelectSize();
385 virtual void OnResize()
387 this->vscroll
->SetCapacityFromWidget(this, WID_BO_CLASS_LIST
);
390 virtual void OnClick(Point pt
, int widget
, int click_count
)
392 switch (GB(widget
, 0, 16)) {
393 case WID_BO_CLASS_LIST
: {
394 int num_clicked
= this->vscroll
->GetPosition() + (pt
.y
- this->nested_array
[widget
]->pos_y
) / this->line_height
;
395 if (num_clicked
>= (int)ObjectClass::GetUIClassCount()) break;
397 this->SelectOtherClass(ObjectClass::GetUIClass(num_clicked
));
398 this->SelectFirstAvailableObject(false);
402 case WID_BO_SELECT_IMAGE
: {
403 ObjectClass
*objclass
= ObjectClass::Get(_selected_object_class
);
404 int num_clicked
= objclass
->GetIndexFromUI(GB(widget
, 16, 16));
405 if (num_clicked
>= 0 && objclass
->GetSpec(num_clicked
)->IsAvailable()) this->SelectOtherObject(num_clicked
);
409 case WID_BO_OBJECT_SPRITE
:
410 if (_selected_object_index
!= -1) {
411 _selected_object_view
= GB(widget
, 16, 16);
412 this->SelectOtherObject(_selected_object_index
); // Re-select the object for a different view.
418 virtual void OnPlaceObject(Point pt
, TileIndex tile
)
420 DoCommandP(tile
, ObjectClass::Get(_selected_object_class
)->GetSpec(_selected_object_index
)->Index(),
421 _selected_object_view
, CMD_BUILD_OBJECT
| CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT
), CcTerraform
);
424 virtual void OnPlaceObjectAbort()
426 this->UpdateButtons(_selected_object_class
, -1, _selected_object_view
);
430 * Select the first available object.
431 * @param change_class If true, change the class if no object in the current
432 * class is available.
434 void SelectFirstAvailableObject(bool change_class
)
436 /* First try to select an object in the selected class. */
437 ObjectClass
*sel_objclass
= ObjectClass::Get(_selected_object_class
);
438 for (uint i
= 0; i
< sel_objclass
->GetSpecCount(); i
++) {
439 const ObjectSpec
*spec
= sel_objclass
->GetSpec(i
);
440 if (spec
->IsAvailable()) {
441 this->SelectOtherObject(i
);
446 /* If that fails, select the first available object
447 * from a random class. */
448 for (ObjectClassID j
= OBJECT_CLASS_BEGIN
; j
< OBJECT_CLASS_MAX
; j
++) {
449 ObjectClass
*objclass
= ObjectClass::Get(j
);
450 for (uint i
= 0; i
< objclass
->GetSpecCount(); i
++) {
451 const ObjectSpec
*spec
= objclass
->GetSpec(i
);
452 if (spec
->IsAvailable()) {
453 this->SelectOtherClass(j
);
454 this->SelectOtherObject(i
);
460 /* If all objects are unavailable, select nothing... */
461 if (ObjectClass::Get(_selected_object_class
)->GetUISpecCount() == 0) {
462 /* ... but make sure that the class is not empty. */
463 for (ObjectClassID j
= OBJECT_CLASS_BEGIN
; j
< OBJECT_CLASS_MAX
; j
++) {
464 if (ObjectClass::Get(j
)->GetUISpecCount() > 0) {
465 this->SelectOtherClass(j
);
470 this->SelectOtherObject(-1);
474 static const NWidgetPart _nested_build_object_widgets
[] = {
475 NWidget(NWID_HORIZONTAL
),
476 NWidget(WWT_CLOSEBOX
, COLOUR_DARK_GREEN
),
477 NWidget(WWT_CAPTION
, COLOUR_DARK_GREEN
), SetDataTip(STR_OBJECT_BUILD_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
478 NWidget(WWT_DEFSIZEBOX
, COLOUR_DARK_GREEN
),
480 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
),
481 NWidget(NWID_HORIZONTAL
), SetPadding(2, 0, 0, 0),
482 NWidget(NWID_VERTICAL
),
483 NWidget(NWID_HORIZONTAL
), SetPadding(0, 5, 2, 5),
484 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_BO_CLASS_LIST
), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_OBJECT_BUILD_CLASS_TOOLTIP
), SetScrollbar(WID_BO_SCROLLBAR
),
485 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_BO_SCROLLBAR
),
487 NWidget(NWID_HORIZONTAL
), SetPadding(0, 5, 0, 5),
488 NWidget(NWID_MATRIX
, COLOUR_DARK_GREEN
, WID_BO_OBJECT_MATRIX
), SetPIP(0, 2, 0),
489 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_BO_OBJECT_SPRITE
), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP
), EndContainer(),
492 NWidget(WWT_TEXT
, COLOUR_DARK_GREEN
, WID_BO_OBJECT_NAME
), SetDataTip(STR_ORANGE_STRING
, STR_NULL
), SetPadding(2, 5, 2, 5),
493 NWidget(WWT_TEXT
, COLOUR_DARK_GREEN
, WID_BO_OBJECT_SIZE
), SetDataTip(STR_OBJECT_BUILD_SIZE
, STR_NULL
), SetPadding(2, 5, 2, 5),
495 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetScrollbar(WID_BO_SELECT_SCROLL
),
496 NWidget(NWID_HORIZONTAL
),
497 NWidget(NWID_MATRIX
, COLOUR_DARK_GREEN
, WID_BO_SELECT_MATRIX
), SetFill(0, 1), SetPIP(0, 2, 0),
498 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
, WID_BO_SELECT_IMAGE
), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP
),
499 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL
),
502 NWidget(NWID_VSCROLLBAR
, COLOUR_DARK_GREEN
, WID_BO_SELECT_SCROLL
),
506 NWidget(NWID_HORIZONTAL
),
507 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_BO_INFO
), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
508 NWidget(NWID_VERTICAL
),
509 NWidget(WWT_PANEL
, COLOUR_DARK_GREEN
), SetFill(0, 1), EndContainer(),
510 NWidget(WWT_RESIZEBOX
, COLOUR_DARK_GREEN
),
516 static WindowDesc
_build_object_desc(
517 WDP_AUTO
, "build_object", 0, 0,
518 WC_BUILD_OBJECT
, WC_BUILD_TOOLBAR
,
520 _nested_build_object_widgets
, lengthof(_nested_build_object_widgets
)
524 * Show our object picker.
525 * @param w The toolbar window we're associated with.
527 void ShowBuildObjectPicker()
529 /* Don't show the place object button when there are no objects to place. */
530 if (ObjectClass::GetUIClassCount() > 0) {
531 AllocateWindowDescFront
<BuildObjectWindow
>(&_build_object_desc
, 0);
535 /** Reset all data of the object GUI. */
536 void InitializeObjectGui()
538 _selected_object_class
= (ObjectClassID
)0;