OpenTTD
engine_gui.cpp
Go to the documentation of this file.
1 /* $Id: engine_gui.cpp 26934 2014-09-28 09:21:51Z peter1138 $ */
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 "window_gui.h"
14 #include "engine_base.h"
15 #include "command_func.h"
16 #include "strings_func.h"
17 #include "engine_gui.h"
18 #include "articulated_vehicles.h"
19 #include "vehicle_func.h"
20 #include "company_func.h"
21 #include "rail.h"
22 #include "settings_type.h"
23 #include "train.h"
24 #include "roadveh.h"
25 #include "ship.h"
26 #include "aircraft.h"
27 
28 #include "widgets/engine_widget.h"
29 
30 #include "table/strings.h"
31 
32 #include "safeguards.h"
33 
40 {
41  const Engine *e = Engine::Get(engine);
42  switch (e->type) {
43  default: NOT_REACHED();
44  case VEH_ROAD: return STR_ENGINE_PREVIEW_ROAD_VEHICLE;
45  case VEH_AIRCRAFT: return STR_ENGINE_PREVIEW_AIRCRAFT;
46  case VEH_SHIP: return STR_ENGINE_PREVIEW_SHIP;
47  case VEH_TRAIN:
48  return GetRailTypeInfo(e->u.rail.railtype)->strings.new_loco;
49  }
50 }
51 
52 static const NWidgetPart _nested_engine_preview_widgets[] = {
54  NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
55  NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
56  EndContainer(),
57  NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
58  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_EP_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
60  NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_NO), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
61  NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_YES), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
62  EndContainer(),
64  EndContainer(),
65 };
66 
68  int vehicle_space; // The space to show the vehicle image
69 
71  {
72  this->InitNested(window_number);
73 
74  /* There is no way to recover the window; so disallow closure via DEL; unless SHIFT+DEL */
75  this->flags |= WF_STICKY;
76  }
77 
78  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
79  {
80  if (widget != WID_EP_QUESTION) return;
81 
82  /* Get size of engine sprite, on loan from depot_gui.cpp */
83  EngineID engine = this->window_number;
84  EngineImageType image_type = EIT_PURCHASE;
85  uint x, y;
86  int x_offs, y_offs;
87 
88  const Engine *e = Engine::Get(engine);
89  switch (e->type) {
90  default: NOT_REACHED();
91  case VEH_TRAIN: GetTrainSpriteSize( engine, x, y, x_offs, y_offs, image_type); break;
92  case VEH_ROAD: GetRoadVehSpriteSize( engine, x, y, x_offs, y_offs, image_type); break;
93  case VEH_SHIP: GetShipSpriteSize( engine, x, y, x_offs, y_offs, image_type); break;
94  case VEH_AIRCRAFT: GetAircraftSpriteSize(engine, x, y, x_offs, y_offs, image_type); break;
95  }
96  this->vehicle_space = max<int>(40, y - y_offs);
97 
98  size->width = max(size->width, x - x_offs);
99  SetDParam(0, GetEngineCategoryName(engine));
100  size->height = GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, size->width) + WD_PAR_VSEP_WIDE + FONT_HEIGHT_NORMAL + this->vehicle_space;
101  SetDParam(0, engine);
102  size->height += GetStringHeight(GetEngineInfoString(engine), size->width);
103  }
104 
105  virtual void DrawWidget(const Rect &r, int widget) const
106  {
107  if (widget != WID_EP_QUESTION) return;
108 
109  EngineID engine = this->window_number;
110  SetDParam(0, GetEngineCategoryName(engine));
111  int y = r.top + GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, r.right - r.top + 1);
112  y = DrawStringMultiLine(r.left, r.right, r.top, y, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_CENTER) + WD_PAR_VSEP_WIDE;
113 
114  SetDParam(0, engine);
115  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_ENGINE_NAME, TC_BLACK, SA_HOR_CENTER);
116  y += FONT_HEIGHT_NORMAL;
117 
118  DrawVehicleEngine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, this->width >> 1, y + this->vehicle_space / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW);
119 
120  y += this->vehicle_space;
121  DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
122  }
123 
124  virtual void OnClick(Point pt, int widget, int click_count)
125  {
126  switch (widget) {
127  case WID_EP_YES:
128  DoCommandP(0, this->window_number, 0, CMD_WANT_ENGINE_PREVIEW);
129  /* FALL THROUGH */
130  case WID_EP_NO:
131  delete this;
132  break;
133  }
134  }
135 
136  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
137  {
138  if (!gui_scope) return;
139 
140  EngineID engine = this->window_number;
141  if (Engine::Get(engine)->preview_company != _local_company) delete this;
142  }
143 };
144 
145 static WindowDesc _engine_preview_desc(
146  WDP_CENTER, "engine_preview", 0, 0,
149  _nested_engine_preview_widgets, lengthof(_nested_engine_preview_widgets)
150 );
151 
152 
153 void ShowEnginePreviewWindow(EngineID engine)
154 {
155  AllocateWindowDescFront<EnginePreviewWindow>(&_engine_preview_desc, engine);
156 }
157 
164 {
166  return cap.GetSum<uint>();
167 }
168 
169 static StringID GetTrainEngineInfoString(const Engine *e)
170 {
171  SetDParam(0, e->GetCost());
172  SetDParam(2, e->GetDisplayMaxSpeed());
173  SetDParam(3, e->GetPower());
174  SetDParam(1, e->GetDisplayWeight());
176 
177  SetDParam(4, e->GetRunningCost());
178 
179  uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
180  if (capacity != 0) {
182  SetDParam(6, capacity);
183  } else {
184  SetDParam(5, CT_INVALID);
185  }
186  return (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(e->u.rail.railtype)->acceleration_type != 2) ? STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER_MAX_TE : STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER;
187 }
188 
189 static StringID GetAircraftEngineInfoString(const Engine *e)
190 {
191  CargoID cargo = e->GetDefaultCargoType();
192  uint16 mail_capacity;
193  uint capacity = e->GetDisplayDefaultCapacity(&mail_capacity);
194  uint16 range = e->GetRange();
195 
196  uint i = 0;
197  SetDParam(i++, e->GetCost());
198  SetDParam(i++, e->GetDisplayMaxSpeed());
199  if (range > 0) SetDParam(i++, range);
200  SetDParam(i++, cargo);
201  SetDParam(i++, capacity);
202 
203  if (mail_capacity > 0) {
204  SetDParam(i++, CT_MAIL);
205  SetDParam(i++, mail_capacity);
206  SetDParam(i++, e->GetRunningCost());
207  return range > 0 ? STR_ENGINE_PREVIEW_COST_MAX_SPEED_RANGE_CAPACITY_CAPACITY_RUNCOST : STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_CAPACITY_RUNCOST;
208  } else {
209  SetDParam(i++, e->GetRunningCost());
210  return range > 0 ? STR_ENGINE_PREVIEW_COST_MAX_SPEED_RANGE_CAPACITY_RUNCOST : STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
211  }
212 }
213 
214 static StringID GetRoadVehEngineInfoString(const Engine *e)
215 {
217  SetDParam(0, e->GetCost());
218  SetDParam(1, e->GetDisplayMaxSpeed());
219  uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
220  if (capacity != 0) {
222  SetDParam(3, capacity);
223  } else {
224  SetDParam(2, CT_INVALID);
225  }
226  SetDParam(4, e->GetRunningCost());
227  return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
228  } else {
229  SetDParam(0, e->GetCost());
230  SetDParam(2, e->GetDisplayMaxSpeed());
231  SetDParam(3, e->GetPower());
232  SetDParam(1, e->GetDisplayWeight());
234 
235  SetDParam(4, e->GetRunningCost());
236 
237  uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
238  if (capacity != 0) {
240  SetDParam(6, capacity);
241  } else {
242  SetDParam(5, CT_INVALID);
243  }
244  return STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER_MAX_TE;
245  }
246 }
247 
248 static StringID GetShipEngineInfoString(const Engine *e)
249 {
250  SetDParam(0, e->GetCost());
251  SetDParam(1, e->GetDisplayMaxSpeed());
254  SetDParam(4, e->GetRunningCost());
255  return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
256 }
257 
258 
266 {
267  const Engine *e = Engine::Get(engine);
268 
269  switch (e->type) {
270  case VEH_TRAIN:
271  return GetTrainEngineInfoString(e);
272 
273  case VEH_ROAD:
274  return GetRoadVehEngineInfoString(e);
275 
276  case VEH_SHIP:
277  return GetShipEngineInfoString(e);
278 
279  case VEH_AIRCRAFT:
280  return GetAircraftEngineInfoString(e);
281 
282  default: NOT_REACHED();
283  }
284 }
285 
295 void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
296 {
297  const Engine *e = Engine::Get(engine);
298 
299  switch (e->type) {
300  case VEH_TRAIN:
301  DrawTrainEngine(left, right, preferred_x, y, engine, pal, image_type);
302  break;
303 
304  case VEH_ROAD:
305  DrawRoadVehEngine(left, right, preferred_x, y, engine, pal, image_type);
306  break;
307 
308  case VEH_SHIP:
309  DrawShipEngine(left, right, preferred_x, y, engine, pal, image_type);
310  break;
311 
312  case VEH_AIRCRAFT:
313  DrawAircraftEngine(left, right, preferred_x, y, engine, pal, image_type);
314  break;
315 
316  default: NOT_REACHED();
317  }
318 }
319 
326 {
327  uint size = el->Length();
328  /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
329  * generally, do not sort if there are less than 2 items */
330  if (size < 2) return;
331  QSortT(el->Begin(), size, compare);
332 }
333 
341 void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
342 {
343  if (num_items < 2) return;
344  assert(begin < el->Length());
345  assert(begin + num_items <= el->Length());
346  QSortT(el->Get(begin), num_items, compare);
347 }
348