OpenTTD
object_gui.cpp
Go to the documentation of this file.
1 /* $Id: object_gui.cpp 27134 2015-02-01 20:54:24Z frosch $ */
2 
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  */
9 
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "newgrf.h"
15 #include "newgrf_object.h"
16 #include "newgrf_text.h"
17 #include "strings_func.h"
18 #include "viewport_func.h"
19 #include "window_gui.h"
20 #include "window_func.h"
21 #include "zoom_func.h"
22 
23 #include "widgets/object_widget.h"
24 
25 #include "table/strings.h"
26 
27 #include "safeguards.h"
28 
31 static uint8 _selected_object_view;
32 
35  static const int OBJECT_MARGIN = 4;
39 
42  {
43  uint pos = 0;
44  for (int i = 0; i < _selected_object_class; i++) {
45  if (ObjectClass::Get((ObjectClassID) i)->GetUISpecCount() == 0) continue;
46  pos++;
47  }
48  this->vscroll->ScrollTowards(pos);
49  }
50 
56  {
57  if (_selected_object_index == -1) return false;
58 
60  if ((int)sel_objclass->GetSpecCount() <= _selected_object_index) return false;
61 
62  return sel_objclass->GetSpec(_selected_object_index)->IsAvailable();
63  }
64 
70  {
71  const NWidgetBase *matrix = this->GetWidget<NWidgetBase>(WID_BO_SELECT_MATRIX);
72  return 1 + (matrix->current_x - matrix->smallest_x) / matrix->resize_x;
73  }
74 
75 public:
77  {
78  this->CreateNestedTree();
79  this->vscroll = this->GetScrollbar(WID_BO_SCROLLBAR);
80  this->FinishInitNested(0);
81 
82  this->vscroll->SetPosition(0);
83  this->vscroll->SetCount(ObjectClass::GetUIClassCount());
84 
85  NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX);
87 
89  if (this->CanRestoreSelectedObject()) {
91  } else {
92  this->SelectFirstAvailableObject(true);
93  }
94  assert(ObjectClass::Get(_selected_object_class)->GetUISpecCount() > 0); // object GUI should be disables elsewise
96  this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetCount(4);
97  }
98 
99  virtual void SetStringParameters(int widget) const
100  {
101  switch (widget) {
102  case WID_BO_OBJECT_NAME: {
104  SetDParam(0, spec != NULL ? spec->name : STR_EMPTY);
105  break;
106  }
107 
108  case WID_BO_OBJECT_SIZE: {
110  int size = spec == NULL ? 0 : spec->size;
111  SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
112  SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
113  break;
114  }
115 
116  default: break;
117  }
118  }
119 
120  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
121  {
122  switch (widget) {
123  case WID_BO_CLASS_LIST: {
124  for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
126  if (objclass->GetUISpecCount() == 0) continue;
127  size->width = max(size->width, GetStringBoundingBox(objclass->name).width);
128  }
129  size->width += padding.width;
131  resize->height = this->line_height;
132  size->height = 5 * this->line_height;
133  break;
134  }
135 
136  case WID_BO_OBJECT_NAME:
137  case WID_BO_OBJECT_SIZE:
138  /* We do not want the window to resize when selecting objects; better clip texts */
139  size->width = 0;
140  break;
141 
142  case WID_BO_OBJECT_MATRIX: {
143  /* Get the right amount of buttons based on the current spec. */
145  if (spec != NULL) {
146  if (spec->views >= 2) size->width += resize->width;
147  if (spec->views >= 4) size->height += resize->height;
148  }
149  resize->width = 0;
150  resize->height = 0;
151  break;
152  }
153 
154  case WID_BO_OBJECT_SPRITE: {
155  bool two_wide = false; // Whether there will be two widgets next to each other in the matrix or not.
156  int height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
157 
158  /* Get the height and view information. */
159  for (int i = 0; i < NUM_OBJECTS; i++) {
160  const ObjectSpec *spec = ObjectSpec::Get(i);
161  if (!spec->IsEverAvailable()) continue;
162  two_wide |= spec->views >= 2;
163  height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
164  }
165 
166  /* Determine the pixel heights. */
167  for (size_t i = 0; i < lengthof(height); i++) {
168  height[i] *= ScaleGUITrad(TILE_HEIGHT);
169  height[i] += ScaleGUITrad(TILE_PIXELS) + 2 * OBJECT_MARGIN;
170  }
171 
172  /* Now determine the size of the minimum widgets. When there are two columns, then
173  * we want these columns to be slightly less wide. When there are two rows, then
174  * determine the size of the widgets based on the maximum size for a single row
175  * of widgets, or just the twice the widget height of the two row ones. */
176  size->height = max(height[0], height[1] * 2 + 2);
177  if (two_wide) {
178  size->width = (3 * ScaleGUITrad(TILE_PIXELS) + 2 * OBJECT_MARGIN) * 2 + 2;
179  } else {
180  size->width = 4 * ScaleGUITrad(TILE_PIXELS) + 2 * OBJECT_MARGIN;
181  }
182 
183  /* Get the right size for the single widget based on the current spec. */
185  if (spec != NULL) {
186  if (spec->views >= 2) size->width = size->width / 2 - 1;
187  if (spec->views >= 4) size->height = size->height / 2 - 1;
188  }
189  break;
190  }
191 
192  case WID_BO_INFO:
193  size->height = this->info_height;
194  break;
195 
197  fill->height = 1;
198  resize->height = 1;
199  break;
200 
201  case WID_BO_SELECT_IMAGE:
202  size->width = ScaleGUITrad(64) + 2;
203  size->height = ScaleGUITrad(58) + 2;
204  break;
205 
206  default: break;
207  }
208  }
209 
210  virtual void DrawWidget(const Rect &r, int widget) const
211  {
212  switch (GB(widget, 0, 16)) {
213  case WID_BO_CLASS_LIST: {
214  int y = r.top;
215  uint pos = 0;
216  for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
218  if (objclass->GetUISpecCount() == 0) continue;
219  if (!this->vscroll->IsVisible(pos++)) continue;
220  DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, objclass->name,
221  ((int)i == _selected_object_class) ? TC_WHITE : TC_BLACK);
222  y += this->line_height;
223  }
224  break;
225  }
226 
227  case WID_BO_OBJECT_SPRITE: {
229  if (spec == NULL) break;
230 
231  /* Height of the selection matrix.
232  * Depending on the number of views, the matrix has a 1x1, 1x2, 2x1 or 2x2 layout. To make the previews
233  * look nice in all layouts, we use the 4x4 layout (smallest previews) as starting point. For the bigger
234  * previews in the layouts with less views we add space homogeneously on all sides, so the 4x4 preview-rectangle
235  * is centered in the 2x1, 1x2 resp. 1x1 buttons. */
236  uint matrix_height = this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->current_y;
237 
238  DrawPixelInfo tmp_dpi;
239  /* Set up a clipping area for the preview. */
240  if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
241  DrawPixelInfo *old_dpi = _cur_dpi;
242  _cur_dpi = &tmp_dpi;
243  if (spec->grf_prop.grffile == NULL) {
244  extern const DrawTileSprites _objects[];
245  const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
246  DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - ScaleGUITrad(TILE_PIXELS), dts, PAL_NONE);
247  } else {
248  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));
249  }
250  _cur_dpi = old_dpi;
251  }
252  break;
253  }
254 
255  case WID_BO_SELECT_IMAGE: {
257  int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
258  if (obj_index < 0) break;
259  const ObjectSpec *spec = objclass->GetSpec(obj_index);
260  if (spec == NULL) break;
261 
262  if (!spec->IsAvailable()) {
263  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
264  }
265  DrawPixelInfo tmp_dpi;
266  /* Set up a clipping area for the preview. */
267  if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
268  DrawPixelInfo *old_dpi = _cur_dpi;
269  _cur_dpi = &tmp_dpi;
270  if (spec->grf_prop.grffile == NULL) {
271  extern const DrawTileSprites _objects[];
272  const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
273  DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - ScaleGUITrad(TILE_PIXELS), dts, PAL_NONE);
274  } else {
275  DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - ScaleGUITrad(TILE_PIXELS), spec,
276  min(_selected_object_view, spec->views - 1));
277  }
278  _cur_dpi = old_dpi;
279  }
280  break;
281  }
282 
283  case WID_BO_INFO: {
285  if (spec == NULL) break;
286 
287  /* Get the extra message for the GUI */
289  uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
290  if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
291  if (callback_res > 0x400) {
293  } else {
294  StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
295  if (message != STR_NULL && message != STR_UNDEFINED) {
297  /* Use all the available space left from where we stand up to the
298  * end of the window. We ALSO enlarge the window if needed, so we
299  * can 'go' wild with the bottom of the window. */
300  int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
302  if (y > this->info_height) {
303  BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
304  bow->info_height = y + 2;
305  bow->ReInit();
306  }
307  }
308  }
309  }
310  }
311  }
312  }
313  }
314 
319  void SelectOtherClass(ObjectClassID object_class_index)
320  {
321  _selected_object_class = object_class_index;
322  this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
323  }
324 
329  void SelectOtherObject(int object_index)
330  {
331  _selected_object_index = object_index;
332  if (_selected_object_index != -1) {
335  this->ReInit();
336  } else {
338  }
339 
340  this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
341  this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index != -1 ? ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index) : -1);
342  this->UpdateSelectSize();
343  this->SetDirty();
344  }
345 
346  void UpdateSelectSize()
347  {
348  if (_selected_object_index == -1) {
349  SetTileSelectSize(1, 1);
350  } else {
352  int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
353  int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
354  SetTileSelectSize(w, h);
355  }
356  }
357 
358  virtual void OnResize()
359  {
361  }
362 
363  virtual void OnClick(Point pt, int widget, int click_count)
364  {
365  switch (GB(widget, 0, 16)) {
366  case WID_BO_CLASS_LIST: {
367  int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
368  if (num_clicked >= (int)ObjectClass::GetUIClassCount()) break;
369 
370  this->SelectOtherClass(ObjectClass::GetUIClass(num_clicked));
371  this->SelectFirstAvailableObject(false);
372  break;
373  }
374 
375  case WID_BO_SELECT_IMAGE: {
377  int num_clicked = objclass->GetIndexFromUI(GB(widget, 16, 16));
378  if (num_clicked >= 0 && objclass->GetSpec(num_clicked)->IsAvailable()) this->SelectOtherObject(num_clicked);
379  break;
380  }
381 
383  if (_selected_object_index != -1) {
384  _selected_object_view = GB(widget, 16, 16);
385  this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
386  this->UpdateSelectSize();
387  this->SetDirty();
388  }
389  break;
390  }
391  }
392 
398  void SelectFirstAvailableObject(bool change_class)
399  {
400  /* First try to select an object in the selected class. */
402  for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
403  const ObjectSpec *spec = sel_objclass->GetSpec(i);
404  if (spec->IsAvailable()) {
405  this->SelectOtherObject(i);
406  return;
407  }
408  }
409  if (change_class) {
410  /* If that fails, select the first available object
411  * from a random class. */
413  ObjectClass *objclass = ObjectClass::Get(j);
414  for (uint i = 0; i < objclass->GetSpecCount(); i++) {
415  const ObjectSpec *spec = objclass->GetSpec(i);
416  if (spec->IsAvailable()) {
417  this->SelectOtherClass(j);
418  this->SelectOtherObject(i);
419  return;
420  }
421  }
422  }
423  }
424  /* If all objects are unavailable, select nothing... */
425  if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
426  /* ... but make sure that the class is not empty. */
428  if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
429  this->SelectOtherClass(j);
430  break;
431  }
432  }
433  }
434  this->SelectOtherObject(-1);
435  }
436 };
437 
438 static const NWidgetPart _nested_build_object_widgets[] = {
440  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
441  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
442  NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
443  EndContainer(),
444  NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
445  NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
447  NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
448  NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
450  EndContainer(),
451  NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
452  NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
453  NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
454  EndContainer(),
455  EndContainer(),
456  NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(2, 5, 2, 5),
457  NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
458  EndContainer(),
459  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
461  NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
462  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
464  EndContainer(),
465  EndContainer(),
466  NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
467  EndContainer(),
468  EndContainer(),
469  EndContainer(),
471  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
473  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
474  NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
475  EndContainer(),
476  EndContainer(),
477  EndContainer(),
478 };
479 
480 static WindowDesc _build_object_desc(
481  WDP_AUTO, "build_object", 0, 0,
484  _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
485 );
486 
492 {
493  new BuildObjectWindow(&_build_object_desc, w);
494 }
495 
498 {
500 }
501 
508 {
509  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);
510 }