Fix ICU iterators on leading/trailing whitespace
[openttd/fttd.git] / src / object_gui.cpp
blob36893a6e514816dc65ab146ab28d4782e4fb7bce
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file object_gui.cpp The GUI for objects. */
12 #include "stdafx.h"
13 #include "map/subcoord.h"
14 #include "command_func.h"
15 #include "newgrf.h"
16 #include "newgrf_object.h"
17 #include "newgrf_text.h"
18 #include "strings_func.h"
19 #include "viewport_func.h"
20 #include "window_gui.h"
21 #include "window_func.h"
23 #include "widgets/object_widget.h"
25 #include "table/strings.h"
27 static ObjectClassID _selected_object_class; ///< the currently visible object class
28 static int _selected_object_index; ///< the index of the selected object in the current class or -1
29 static uint8 _selected_object_view; ///< the view of the selected object
31 /** The window used for building objects. */
32 class BuildObjectWindow : public PickerWindowBase {
33 static const int OBJECT_MARGIN = 4; ///< The margin (in pixels) around an object.
34 int line_height; ///< The height of a single line.
35 int info_height; ///< The height of the info box.
36 Scrollbar *vscroll; ///< The scrollbar.
38 /** Scroll #WID_BO_CLASS_LIST so that the selected object class is visible. */
39 void EnsureSelectedObjectClassIsVisible()
41 uint pos = 0;
42 for (int i = 0; i < _selected_object_class; i++) {
43 if (ObjectClass::Get((ObjectClassID) i)->GetUISpecCount() == 0) continue;
44 pos++;
46 this->vscroll->ScrollTowards(pos);
49 /**
50 * Tests whether the previously selected object can be selected.
51 * @return \c true if the selected object is available, \c false otherwise.
53 bool CanRestoreSelectedObject()
55 if (_selected_object_index == -1) return false;
57 ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
58 if ((int)sel_objclass->GetSpecCount() <= _selected_object_index) return false;
60 return sel_objclass->GetSpec(_selected_object_index)->IsAvailable();
63 /**
64 * Calculate the number of columns of the #WID_BO_SELECT_MATRIX widget.
65 * @return Number of columns in the matrix.
67 uint GetMatrixColumnCount()
69 const NWidgetBase *matrix = this->GetWidget<NWidgetBase>(WID_BO_SELECT_MATRIX);
70 return 1 + (matrix->current_x - matrix->smallest_x) / matrix->resize_x;
73 public:
74 BuildObjectWindow(WindowDesc *desc, Window *w) : PickerWindowBase(desc, w), info_height(1)
76 this->CreateNestedTree();
77 this->vscroll = this->GetScrollbar(WID_BO_SCROLLBAR);
78 this->FinishInitNested(0);
80 this->vscroll->SetPosition(0);
81 this->vscroll->SetCount(ObjectClass::GetUIClassCount());
83 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX);
84 matrix->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL));
86 this->SelectOtherClass(_selected_object_class);
87 if (this->CanRestoreSelectedObject()) {
88 this->SelectOtherObject(_selected_object_index);
89 } else {
90 this->SelectFirstAvailableObject(true);
92 assert(ObjectClass::Get(_selected_object_class)->GetUISpecCount() > 0); // object GUI should be disables elsewise
93 this->EnsureSelectedObjectClassIsVisible();
94 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetCount(4);
97 virtual void SetStringParameters(int widget) const
99 switch (widget) {
100 case WID_BO_OBJECT_NAME: {
101 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
102 SetDParam(0, spec != NULL ? spec->name : STR_EMPTY);
103 break;
106 case WID_BO_OBJECT_SIZE: {
107 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
108 int size = spec == NULL ? 0 : spec->size;
109 SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
110 SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
111 break;
114 default: break;
118 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
120 switch (widget) {
121 case WID_BO_CLASS_LIST: {
122 for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
123 ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
124 if (objclass->GetUISpecCount() == 0) continue;
125 size->width = max(size->width, GetStringBoundingBox(objclass->name).width);
127 size->width += padding.width;
128 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
129 resize->height = this->line_height;
130 size->height = 5 * this->line_height;
131 break;
134 case WID_BO_OBJECT_NAME:
135 case WID_BO_OBJECT_SIZE:
136 /* We do not want the window to resize when selecting objects; better clip texts */
137 size->width = 0;
138 break;
140 case WID_BO_OBJECT_MATRIX: {
141 /* Get the right amount of buttons based on the current spec. */
142 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
143 if (spec != NULL) {
144 if (spec->views >= 2) size->width += resize->width;
145 if (spec->views >= 4) size->height += resize->height;
147 resize->width = 0;
148 resize->height = 0;
149 break;
152 case WID_BO_OBJECT_SPRITE: {
153 bool two_wide = false; // Whether there will be two widgets next to each other in the matrix or not.
154 int height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
156 /* Get the height and view information. */
157 for (int i = 0; i < NUM_OBJECTS; i++) {
158 const ObjectSpec *spec = ObjectSpec::Get(i);
159 if (!spec->IsEverAvailable()) continue;
160 two_wide |= spec->views >= 2;
161 height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
164 /* Determine the pixel heights. */
165 for (size_t i = 0; i < lengthof(height); i++) {
166 height[i] *= TILE_HEIGHT;
167 height[i] += TILE_PIXELS + 2 * OBJECT_MARGIN;
170 /* Now determine the size of the minimum widgets. When there are two columns, then
171 * we want these columns to be slightly less wide. When there are two rows, then
172 * determine the size of the widgets based on the maximum size for a single row
173 * of widgets, or just the twice the widget height of the two row ones. */
174 size->height = max(height[0], height[1] * 2 + 2);
175 if (two_wide) {
176 size->width = (3 * TILE_PIXELS + 2 * OBJECT_MARGIN) * 2 + 2;
177 } else {
178 size->width = 4 * TILE_PIXELS + 2 * OBJECT_MARGIN;
181 /* Get the right size for the single widget based on the current spec. */
182 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
183 if (spec != NULL) {
184 if (spec->views >= 2) size->width = size->width / 2 - 1;
185 if (spec->views >= 4) size->height = size->height / 2 - 1;
187 break;
190 case WID_BO_INFO:
191 size->height = this->info_height;
192 break;
194 case WID_BO_SELECT_MATRIX:
195 fill->height = 1;
196 resize->height = 1;
197 break;
199 default: break;
203 virtual void DrawWidget(const Rect &r, int widget) const
205 switch (GB(widget, 0, 16)) {
206 case WID_BO_CLASS_LIST: {
207 int y = r.top;
208 uint pos = 0;
209 for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
210 ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
211 if (objclass->GetUISpecCount() == 0) continue;
212 if (!this->vscroll->IsVisible(pos++)) continue;
213 SetDParam(0, objclass->name);
214 DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, STR_JUST_STRING,
215 ((int)i == _selected_object_class) ? TC_WHITE : TC_BLACK);
216 y += this->line_height;
218 break;
221 case WID_BO_OBJECT_SPRITE: {
222 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
223 if (spec == NULL) break;
225 /* Height of the selection matrix.
226 * Depending on the number of views, the matrix has a 1x1, 1x2, 2x1 or 2x2 layout. To make the previews
227 * look nice in all layouts, we use the 4x4 layout (smallest previews) as starting point. For the bigger
228 * previews in the layouts with less views we add space homogeneously on all sides, so the 4x4 preview-rectangle
229 * is centered in the 2x1, 1x2 resp. 1x1 buttons. */
230 uint matrix_height = this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->current_y;
232 DrawPixelInfo tmp_dpi;
233 /* Set up a clipping area for the preview. */
234 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
235 DrawPixelInfo *old_dpi = _cur_dpi;
236 _cur_dpi = &tmp_dpi;
237 if (spec->grf_prop.grffile == NULL) {
238 extern const DrawTileSprites _objects[];
239 const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
240 DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
241 } else {
242 DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
244 _cur_dpi = old_dpi;
246 break;
249 case WID_BO_SELECT_IMAGE: {
250 ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
251 int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
252 if (obj_index < 0) break;
253 const ObjectSpec *spec = objclass->GetSpec(obj_index);
254 if (spec == NULL) break;
256 if (!spec->IsAvailable()) {
257 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
259 DrawPixelInfo tmp_dpi;
260 /* Set up a clipping area for the preview. */
261 if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
262 DrawPixelInfo *old_dpi = _cur_dpi;
263 _cur_dpi = &tmp_dpi;
264 if (spec->grf_prop.grffile == NULL) {
265 extern const DrawTileSprites _objects[];
266 const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
267 DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
268 } else {
269 DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec,
270 min(_selected_object_view, spec->views - 1));
272 _cur_dpi = old_dpi;
274 break;
277 case WID_BO_INFO: {
278 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
279 if (spec == NULL) break;
281 /* Get the extra message for the GUI */
282 if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
283 uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
284 if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
285 if (callback_res > 0x400) {
286 ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_FUND_MORE_TEXT, callback_res);
287 } else {
288 StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
289 if (message != STR_NULL && message != STR_UNDEFINED) {
290 StartTextRefStackUsage(spec->grf_prop.grffile, 6);
291 /* Use all the available space left from where we stand up to the
292 * end of the window. We ALSO enlarge the window if needed, so we
293 * can 'go' wild with the bottom of the window. */
294 int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
295 StopTextRefStackUsage();
296 if (y > this->info_height) {
297 BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
298 bow->info_height = y + 2;
299 bow->ReInit();
310 * Select the specified object class.
311 * @param object_class_index Object class index to select.
313 void SelectOtherClass(ObjectClassID object_class_index)
315 _selected_object_class = object_class_index;
316 this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
320 * Select the specified object in #_selected_object_class class.
321 * @param object_index Object index to select, \c -1 means select nothing.
323 void SelectOtherObject(int object_index)
325 _selected_object_index = object_index;
326 if (_selected_object_index != -1) {
327 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
328 _selected_object_view = min(_selected_object_view, spec->views - 1);
329 this->ReInit();
330 } else {
331 _selected_object_view = 0;
334 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
335 this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index != -1 ? ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index) : -1);
336 this->UpdateSelectSize();
337 this->SetDirty();
340 void UpdateSelectSize()
342 if (_selected_object_index == -1) {
343 SetTileSelectSize(1, 1);
344 } else {
345 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
346 int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
347 int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
348 SetTileSelectSize(w, h);
352 virtual void OnResize()
354 this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
357 virtual void OnClick(Point pt, int widget, int click_count)
359 switch (GB(widget, 0, 16)) {
360 case WID_BO_CLASS_LIST: {
361 int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
362 if (num_clicked >= (int)ObjectClass::GetUIClassCount()) break;
364 this->SelectOtherClass(ObjectClass::GetUIClass(num_clicked));
365 this->SelectFirstAvailableObject(false);
366 break;
369 case WID_BO_SELECT_IMAGE: {
370 ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
371 int num_clicked = objclass->GetIndexFromUI(GB(widget, 16, 16));
372 if (num_clicked >= 0 && objclass->GetSpec(num_clicked)->IsAvailable()) this->SelectOtherObject(num_clicked);
373 break;
376 case WID_BO_OBJECT_SPRITE:
377 if (_selected_object_index != -1) {
378 _selected_object_view = GB(widget, 16, 16);
379 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
380 this->UpdateSelectSize();
381 this->SetDirty();
383 break;
388 * Select the first available object.
389 * @param change_class If true, change the class if no object in the current
390 * class is available.
392 void SelectFirstAvailableObject(bool change_class)
394 /* First try to select an object in the selected class. */
395 ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
396 for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
397 const ObjectSpec *spec = sel_objclass->GetSpec(i);
398 if (spec->IsAvailable()) {
399 this->SelectOtherObject(i);
400 return;
403 if (change_class) {
404 /* If that fails, select the first available object
405 * from a random class. */
406 for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
407 ObjectClass *objclass = ObjectClass::Get(j);
408 for (uint i = 0; i < objclass->GetSpecCount(); i++) {
409 const ObjectSpec *spec = objclass->GetSpec(i);
410 if (spec->IsAvailable()) {
411 this->SelectOtherClass(j);
412 this->SelectOtherObject(i);
413 return;
418 /* If all objects are unavailable, select nothing... */
419 if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
420 /* ... but make sure that the class is not empty. */
421 for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
422 if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
423 this->SelectOtherClass(j);
424 break;
428 this->SelectOtherObject(-1);
432 static const NWidgetPart _nested_build_object_widgets[] = {
433 NWidget(NWID_HORIZONTAL),
434 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
435 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
436 NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
437 EndContainer(),
438 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
439 NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
440 NWidget(NWID_VERTICAL),
441 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
442 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
443 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BO_SCROLLBAR),
444 EndContainer(),
445 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
446 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
447 NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
448 EndContainer(),
449 EndContainer(),
450 NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(2, 5, 2, 5),
451 NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
452 EndContainer(),
453 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
454 NWidget(NWID_HORIZONTAL),
455 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
456 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
457 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL),
458 EndContainer(),
459 EndContainer(),
460 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
461 EndContainer(),
462 EndContainer(),
463 EndContainer(),
464 NWidget(NWID_HORIZONTAL),
465 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
466 NWidget(NWID_VERTICAL),
467 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
468 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
469 EndContainer(),
470 EndContainer(),
471 EndContainer(),
474 static WindowDesc _build_object_desc(
475 WDP_AUTO, "build_object", 0, 0,
476 WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
477 WDF_CONSTRUCTION,
478 _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
482 * Show our object picker.
483 * @param w The toolbar window we're associated with.
485 void ShowBuildObjectPicker(Window *w)
487 new BuildObjectWindow(&_build_object_desc, w);
490 /** Reset all data of the object GUI. */
491 void InitializeObjectGui()
493 _selected_object_class = (ObjectClassID)0;
497 * PlaceProc function, called when someone pressed the button if the
498 * object-tool is selected
499 * @param tile on which to place the object
501 void PlaceProc_Object(TileIndex tile)
503 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);