Translations update
[openttd/fttd.git] / src / engine_gui.cpp
blob9d315a7e269f11ffaaea5c8f8e5b41a21aad9537
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 engine_gui.cpp GUI to show engine related information. */
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"
28 #include "widgets/engine_widget.h"
30 #include "table/strings.h"
32 /**
33 * Return the category of an engine.
34 * @param engine Engine to examine.
35 * @return String describing the category ("road veh", "train". "airplane", or "ship") of the engine.
37 StringID GetEngineCategoryName(EngineID engine)
39 const Engine *e = Engine::Get(engine);
40 switch (e->type) {
41 default: NOT_REACHED();
42 case VEH_ROAD: return STR_ENGINE_PREVIEW_ROAD_VEHICLE;
43 case VEH_AIRCRAFT: return STR_ENGINE_PREVIEW_AIRCRAFT;
44 case VEH_SHIP: return STR_ENGINE_PREVIEW_SHIP;
45 case VEH_TRAIN:
46 return GetRailTypeInfo(e->u.rail.railtype)->strings.new_loco;
50 static const NWidgetPart _nested_engine_preview_widgets[] = {
51 NWidget(NWID_HORIZONTAL),
52 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
53 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
54 EndContainer(),
55 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
56 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_EP_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
57 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
58 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_NO), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
59 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_YES), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
60 EndContainer(),
61 NWidget(NWID_SPACER), SetMinimalSize(0, 8),
62 EndContainer(),
65 struct EnginePreviewWindow : Window {
66 int vehicle_space; // The space to show the vehicle image
68 EnginePreviewWindow (const WindowDesc *desc, WindowNumber window_number)
69 : Window (desc), vehicle_space (0)
71 this->InitNested(window_number);
73 /* There is no way to recover the window; so disallow closure via DEL; unless SHIFT+DEL */
74 this->flags |= WF_STICKY;
77 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
79 if (widget != WID_EP_QUESTION) return;
81 /* Get size of engine sprite, on loan from depot_gui.cpp */
82 EngineID engine = this->window_number;
83 EngineImageType image_type = EIT_PURCHASE;
84 uint x, y;
85 int x_offs, y_offs;
87 const Engine *e = Engine::Get(engine);
88 switch (e->type) {
89 default: NOT_REACHED();
90 case VEH_TRAIN: GetTrainSpriteSize( engine, x, y, x_offs, y_offs, image_type); break;
91 case VEH_ROAD: GetRoadVehSpriteSize( engine, x, y, x_offs, y_offs, image_type); break;
92 case VEH_SHIP: GetShipSpriteSize( engine, x, y, x_offs, y_offs, image_type); break;
93 case VEH_AIRCRAFT: GetAircraftSpriteSize(engine, x, y, x_offs, y_offs, image_type); break;
95 this->vehicle_space = max<int>(40, y - y_offs);
97 size->width = max(size->width, x - x_offs);
98 SetDParam(0, GetEngineCategoryName(engine));
99 size->height = GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, size->width) + WD_PAR_VSEP_WIDE + FONT_HEIGHT_NORMAL + this->vehicle_space;
100 SetDParam(0, engine);
101 size->height += GetStringHeight(GetEngineInfoString(engine), size->width);
104 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
106 if (widget != WID_EP_QUESTION) return;
108 EngineID engine = this->window_number;
109 SetDParam(0, GetEngineCategoryName(engine));
110 int y = r.top + GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, r.right - r.top + 1);
111 y = DrawStringMultiLine (dpi, r.left, r.right, r.top, y, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_CENTER) + WD_PAR_VSEP_WIDE;
113 SetDParam(0, engine);
114 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_ENGINE_NAME, TC_BLACK, SA_HOR_CENTER);
115 y += FONT_HEIGHT_NORMAL;
117 DrawVehicleEngine (dpi, 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 y += this->vehicle_space;
120 DrawStringMultiLine (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
123 virtual void OnClick(Point pt, int widget, int click_count)
125 switch (widget) {
126 case WID_EP_YES:
127 DoCommandP(0, this->window_number, 0, CMD_WANT_ENGINE_PREVIEW);
128 /* FALL THROUGH */
129 case WID_EP_NO:
130 this->Delete();
131 break;
135 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
137 if (!gui_scope) return;
139 EngineID engine = this->window_number;
140 if (Engine::Get(engine)->preview_company != _local_company) this->Delete();
144 static WindowDesc::Prefs _engine_preview_prefs ("engine_preview");
146 static const WindowDesc _engine_preview_desc(
147 WDP_CENTER, 0, 0,
148 WC_ENGINE_PREVIEW, WC_NONE,
149 WDF_CONSTRUCTION,
150 _nested_engine_preview_widgets, lengthof(_nested_engine_preview_widgets),
151 &_engine_preview_prefs
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->GetRunningCost());
181 uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
182 if (capacity != 0) {
183 SetDParam(5, e->GetDefaultCargoType());
184 SetDParam(6, capacity);
185 } else {
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();
198 uint i = 0;
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->GetRunningCost());
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;
211 } else {
212 SetDParam(i++, e->GetRunningCost());
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);
223 if (capacity != 0) {
224 SetDParam(2, e->GetDefaultCargoType());
225 SetDParam(3, capacity);
226 } else {
227 SetDParam(2, CT_INVALID);
229 SetDParam(4, e->GetRunningCost());
230 return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAP_RUNCOST;
231 } else {
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->GetRunningCost());
240 uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
241 if (capacity != 0) {
242 SetDParam(5, e->GetDefaultCargoType());
243 SetDParam(6, capacity);
244 } else {
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->GetRunningCost());
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);
272 switch (e->type) {
273 case VEH_TRAIN:
274 return GetTrainEngineInfoString(e);
276 case VEH_ROAD:
277 return GetRoadVehEngineInfoString(e);
279 case VEH_SHIP:
280 return GetShipEngineInfoString(e);
282 case VEH_AIRCRAFT:
283 return GetAircraftEngineInfoString(e);
285 default: NOT_REACHED();
290 * Draw an engine.
291 * @param dpi Area to draw on.
292 * @param left Minimum horizontal position to use for drawing the engine
293 * @param right Maximum horizontal position to use for drawing the engine
294 * @param preferred_x Horizontal position to use for drawing the engine.
295 * @param y Vertical position to use for drawing the engine.
296 * @param engine Engine to draw.
297 * @param pal Palette to use for drawing.
299 void DrawVehicleEngine (BlitArea *dpi, int left, int right, int preferred_x,
300 int y, EngineID engine, PaletteID pal, EngineImageType image_type)
302 const Engine *e = Engine::Get(engine);
304 switch (e->type) {
305 case VEH_TRAIN:
306 DrawTrainEngine (dpi, left, right, preferred_x, y, engine, pal, image_type);
307 break;
309 case VEH_ROAD:
310 DrawRoadVehEngine (dpi, left, right, preferred_x, y, engine, pal, image_type);
311 break;
313 case VEH_SHIP:
314 DrawShipEngine (dpi, left, right, preferred_x, y, engine, pal, image_type);
315 break;
317 case VEH_AIRCRAFT:
318 DrawAircraftEngine (dpi, left, right, preferred_x, y, engine, pal, image_type);
319 break;
321 default: NOT_REACHED();