1 /* $Id: engine_gui.cpp 25290 2013-05-26 19:25:01Z frosch $ */
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 engine_gui.cpp GUI to show engine related information. */
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"
23 #include "settings_type.h"
29 #include "widgets/engine_widget.h"
31 #include "table/strings.h"
33 #include "safeguards.h"
36 * Return the category of an engine.
37 * @param engine Engine to examine.
38 * @return String describing the category ("road veh", "train". "airplane", or "ship") of the engine.
40 StringID
GetEngineCategoryName(EngineID engine
)
42 const Engine
*e
= Engine::Get(engine
);
44 default: NOT_REACHED();
46 return GetRoadTypeInfo(e
->GetRoadType())->strings
.new_engine
;
47 case VEH_AIRCRAFT
: return STR_ENGINE_PREVIEW_AIRCRAFT
;
48 case VEH_SHIP
: return STR_ENGINE_PREVIEW_SHIP
;
50 return GetRailTypeInfo(e
->u
.rail
.railtype
)->strings
.new_loco
;
54 static const NWidgetPart _nested_engine_preview_widgets
[] = {
55 NWidget(NWID_HORIZONTAL
),
56 NWidget(WWT_CLOSEBOX
, COLOUR_LIGHT_BLUE
),
57 NWidget(WWT_CAPTION
, COLOUR_LIGHT_BLUE
), SetDataTip(STR_ENGINE_PREVIEW_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
59 NWidget(WWT_PANEL
, COLOUR_LIGHT_BLUE
),
60 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_EP_QUESTION
), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
61 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(85, 10, 85),
62 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_EP_NO
), SetDataTip(STR_QUIT_NO
, STR_NULL
), SetFill(1, 0),
63 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_EP_YES
), SetDataTip(STR_QUIT_YES
, STR_NULL
), SetFill(1, 0),
65 NWidget(NWID_SPACER
), SetMinimalSize(0, 8),
69 struct EnginePreviewWindow
: Window
{
70 int vehicle_space
; // The space to show the vehicle image
72 EnginePreviewWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
74 this->InitNested(window_number
);
76 /* There is no way to recover the window; so disallow closure via DEL; unless SHIFT+DEL */
77 this->flags
|= WF_STICKY
;
80 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
82 if (widget
!= WID_EP_QUESTION
) return;
84 /* Get size of engine sprite, on loan from depot_gui.cpp */
85 EngineID engine
= this->window_number
;
86 EngineImageType image_type
= EIT_PREVIEW
;
90 const Engine
*e
= Engine::Get(engine
);
92 default: NOT_REACHED();
93 case VEH_TRAIN
: GetTrainSpriteSize( engine
, x
, y
, x_offs
, y_offs
, image_type
); break;
94 case VEH_ROAD
: GetRoadVehSpriteSize( engine
, x
, y
, x_offs
, y_offs
, image_type
); break;
95 case VEH_SHIP
: GetShipSpriteSize( engine
, x
, y
, x_offs
, y_offs
, image_type
); break;
96 case VEH_AIRCRAFT
: GetAircraftSpriteSize(engine
, x
, y
, x_offs
, y_offs
, image_type
); break;
98 this->vehicle_space
= max
<int>(40, y
- y_offs
);
100 size
->width
= max(size
->width
, x
- x_offs
);
101 SetDParam(0, GetEngineCategoryName(engine
));
102 size
->height
= GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE
, size
->width
) + WD_PAR_VSEP_WIDE
+ FONT_HEIGHT_NORMAL
+ this->vehicle_space
;
103 SetDParam(0, engine
);
104 size
->height
+= GetStringHeight(GetEngineInfoString(engine
), size
->width
);
107 virtual void DrawWidget(const Rect
&r
, int widget
) const
109 if (widget
!= WID_EP_QUESTION
) return;
111 EngineID engine
= this->window_number
;
112 SetDParam(0, GetEngineCategoryName(engine
));
113 int y
= r
.top
+ GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE
, r
.right
- r
.top
+ 1);
114 y
= DrawStringMultiLine(r
.left
, r
.right
, r
.top
, y
, STR_ENGINE_PREVIEW_MESSAGE
, TC_FROMSTRING
, SA_CENTER
) + WD_PAR_VSEP_WIDE
;
116 SetDParam(0, engine
);
117 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_ENGINE_NAME
, TC_BLACK
, SA_HOR_CENTER
);
118 y
+= FONT_HEIGHT_NORMAL
;
120 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
);
122 y
+= this->vehicle_space
;
123 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, r
.bottom
, GetEngineInfoString(engine
), TC_FROMSTRING
, SA_CENTER
);
126 virtual void OnClick(Point pt
, int widget
, int click_count
)
130 DoCommandP(0, this->window_number
, 0, CMD_WANT_ENGINE_PREVIEW
);
133 if (!_shift_pressed
) delete this;
138 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
140 if (!gui_scope
) return;
142 EngineID engine
= this->window_number
;
143 if (Engine::Get(engine
)->preview_company
!= _local_company
) delete this;
147 static WindowDesc
_engine_preview_desc(
148 WDP_CENTER
, "engine_preview", 0, 0,
149 WC_ENGINE_PREVIEW
, WC_NONE
,
151 _nested_engine_preview_widgets
, lengthof(_nested_engine_preview_widgets
)
155 void ShowEnginePreviewWindow(EngineID engine
)
157 AllocateWindowDescFront
<EnginePreviewWindow
>(&_engine_preview_desc
, engine
);
161 * Get the capacity of an engine with articulated parts.
162 * @param engine The engine to get the capacity of.
163 * @return The capacity.
165 uint
GetTotalCapacityOfArticulatedParts(EngineID engine
)
167 CargoArray cap
= GetCapacityOfArticulatedParts(engine
);
168 return cap
.GetSum
<uint
>();
171 static StringID
GetTrainEngineInfoString(const Engine
*e
)
173 SetDParam(0, e
->GetCost());
174 SetDParam(2, e
->GetDisplayMaxSpeed());
175 SetDParam(3, e
->GetPower());
176 SetDParam(1, e
->GetDisplayWeight());
177 SetDParam(7, e
->GetDisplayMaxTractiveEffort());
179 SetDParam(4, e
->GetDisplayRunningCost());
181 uint capacity
= GetTotalCapacityOfArticulatedParts(e
->index
);
183 SetDParam(5, e
->GetDefaultCargoType());
184 SetDParam(6, capacity
);
186 SetDParam(5, CT_INVALID
);
188 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
;
191 static StringID
GetAircraftEngineInfoString(const Engine
*e
)
193 CargoID cargo
= e
->GetDefaultCargoType();
194 uint16 mail_capacity
;
195 uint capacity
= e
->GetDisplayDefaultCapacity(&mail_capacity
);
196 uint16 range
= e
->GetRange();
199 SetDParam(i
++, e
->GetCost());
200 SetDParam(i
++, e
->GetDisplayMaxSpeed());
201 SetDParam(i
++, e
->GetAircraftTypeText());
202 if (range
> 0) SetDParam(i
++, range
);
203 SetDParam(i
++, cargo
);
204 SetDParam(i
++, capacity
);
206 if (mail_capacity
> 0) {
207 SetDParam(i
++, CT_MAIL
);
208 SetDParam(i
++, mail_capacity
);
209 SetDParam(i
++, e
->GetDisplayRunningCost());
210 return range
> 0 ? STR_ENGINE_PREVIEW_COST_MAX_SPEED_TYPE_RANGE_CAP_CAP_RUNCOST
: STR_ENGINE_PREVIEW_COST_MAX_SPEED_TYPE_CAP_CAP_RUNCOST
;
212 SetDParam(i
++, e
->GetDisplayRunningCost());
213 return range
> 0 ? STR_ENGINE_PREVIEW_COST_MAX_SPEED_TYPE_RANGE_CAP_RUNCOST
: STR_ENGINE_PREVIEW_COST_MAX_SPEED_TYPE_CAP_RUNCOST
;
217 static StringID
GetRoadVehEngineInfoString(const Engine
*e
)
219 if (_settings_game
.vehicle
.roadveh_acceleration_model
== AM_ORIGINAL
) {
220 SetDParam(0, e
->GetCost());
221 SetDParam(1, e
->GetDisplayMaxSpeed());
222 uint capacity
= GetTotalCapacityOfArticulatedParts(e
->index
);
224 SetDParam(2, e
->GetDefaultCargoType());
225 SetDParam(3, capacity
);
227 SetDParam(2, CT_INVALID
);
229 SetDParam(4, e
->GetDisplayRunningCost());
230 return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAP_RUNCOST
;
232 SetDParam(0, e
->GetCost());
233 SetDParam(2, e
->GetDisplayMaxSpeed());
234 SetDParam(3, e
->GetPower());
235 SetDParam(1, e
->GetDisplayWeight());
236 SetDParam(7, e
->GetDisplayMaxTractiveEffort());
238 SetDParam(4, e
->GetDisplayRunningCost());
240 uint capacity
= GetTotalCapacityOfArticulatedParts(e
->index
);
242 SetDParam(5, e
->GetDefaultCargoType());
243 SetDParam(6, capacity
);
245 SetDParam(5, CT_INVALID
);
247 return STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER_MAX_TE
;
251 static StringID
GetShipEngineInfoString(const Engine
*e
)
253 SetDParam(0, e
->GetCost());
254 SetDParam(1, e
->GetDisplayMaxSpeed());
255 SetDParam(2, e
->GetDefaultCargoType());
256 SetDParam(3, e
->GetDisplayDefaultCapacity());
257 SetDParam(4, e
->GetDisplayRunningCost());
258 return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAP_RUNCOST
;
263 * Get a multi-line string with some technical data, describing the engine.
264 * @param engine Engine to describe.
265 * @return String describing the engine.
266 * @post \c DParam array is set up for printing the string.
268 StringID
GetEngineInfoString(EngineID engine
)
270 const Engine
*e
= Engine::Get(engine
);
274 return GetTrainEngineInfoString(e
);
277 return GetRoadVehEngineInfoString(e
);
280 return GetShipEngineInfoString(e
);
283 return GetAircraftEngineInfoString(e
);
285 default: NOT_REACHED();
291 * @param left Minimum horizontal position to use for drawing the engine
292 * @param right Maximum horizontal position to use for drawing the engine
293 * @param preferred_x Horizontal position to use for drawing the engine.
294 * @param y Vertical position to use for drawing the engine.
295 * @param engine Engine to draw.
296 * @param pal Palette to use for drawing.
298 void DrawVehicleEngine(int left
, int right
, int preferred_x
, int y
, EngineID engine
, PaletteID pal
, EngineImageType image_type
)
300 const Engine
*e
= Engine::Get(engine
);
304 DrawTrainEngine(left
, right
, preferred_x
, y
, engine
, pal
, image_type
);
308 DrawRoadVehEngine(left
, right
, preferred_x
, y
, engine
, pal
, image_type
);
312 DrawShipEngine(left
, right
, preferred_x
, y
, engine
, pal
, image_type
);
316 DrawAircraftEngine(left
, right
, preferred_x
, y
, engine
, pal
, image_type
);
319 default: NOT_REACHED();
324 * Sort all items using quick sort and given 'CompareItems' function
325 * @param el list to be sorted
326 * @param compare function for evaluation of the quicksort
328 void EngList_Sort(GUIEngineList
*el
, EngList_SortTypeFunction compare
)
330 uint size
= el
->Length();
331 /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
332 * generally, do not sort if there are less than 2 items */
333 if (size
< 2) return;
334 QSortT(el
->Begin(), size
, compare
);
338 * Sort selected range of items (on indices @ <begin, begin+num_items-1>)
339 * @param el list to be sorted
340 * @param compare function for evaluation of the quicksort
341 * @param begin start of sorting
342 * @param num_items count of items to be sorted
344 void EngList_SortPartial(GUIEngineList
*el
, EngList_SortTypeFunction compare
, uint begin
, uint num_items
)
346 if (num_items
< 2) return;
347 assert(begin
< el
->Length());
348 assert(begin
+ num_items
<= el
->Length());
349 QSortT(el
->Get(begin
), num_items
, compare
);