2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file engine_gui.cpp GUI to show engine related information. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2007-12-19 20:45:46 +00:00
|
|
|
#include "window_gui.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "gfx_func.h"
|
2008-03-31 00:17:39 +00:00
|
|
|
#include "engine_func.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "engine_base.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2009-01-31 20:16:06 +00:00
|
|
|
#include "news_type.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-05-27 12:24:23 +00:00
|
|
|
#include "engine_gui.h"
|
2008-06-20 07:23:00 +00:00
|
|
|
#include "articulated_vehicles.h"
|
2009-05-27 17:18:53 +00:00
|
|
|
#include "vehicle_func.h"
|
|
|
|
#include "company_func.h"
|
2008-08-24 21:31:24 +00:00
|
|
|
#include "rail.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/sprites.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-30 17:54:11 +00:00
|
|
|
/** Return the category of an engine.
|
|
|
|
* @param engine Engine to examine.
|
|
|
|
* @return String describing the category ("road veh", "train". "airplane", or "ship") of the engine.
|
|
|
|
*/
|
2008-05-13 10:17:04 +00:00
|
|
|
StringID GetEngineCategoryName(EngineID engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-16 23:34:14 +00:00
|
|
|
switch (Engine::Get(engine)->type) {
|
2007-03-03 22:03:15 +00:00
|
|
|
default: NOT_REACHED();
|
2009-04-21 23:40:56 +00:00
|
|
|
case VEH_ROAD: return STR_ENGINE_PREVIEW_ROAD_VEHICLE;
|
|
|
|
case VEH_AIRCRAFT: return STR_ENGINE_PREVIEW_AIRCRAFT;
|
|
|
|
case VEH_SHIP: return STR_ENGINE_PREVIEW_SHIP;
|
2007-03-08 16:27:54 +00:00
|
|
|
case VEH_TRAIN:
|
2008-08-24 21:31:24 +00:00
|
|
|
return GetRailTypeInfo(RailVehInfo(engine)->railtype)->strings.new_loco;
|
2004-11-19 19:13:32 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-21 20:39:37 +00:00
|
|
|
/** Widgets used for the engine preview window */
|
|
|
|
enum EnginePreviewWidgets {
|
|
|
|
EPW_CLOSE, ///< Close button
|
|
|
|
EPW_CAPTION, ///< Title bar/caption
|
|
|
|
EPW_BACKGROUND, ///< Background
|
|
|
|
EPW_NO, ///< No button
|
|
|
|
EPW_YES, ///< Yes button
|
|
|
|
};
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static const Widget _engine_preview_widgets[] = {
|
2009-04-21 23:40:56 +00:00
|
|
|
{ WWT_CLOSEBOX, RESIZE_NONE, COLOUR_LIGHT_BLUE, 0, 10, 0, 13, STR_BLACK_CROSS, STR_TOOLTIP_CLOSE_WINDOW}, // EPW_CLOSE
|
|
|
|
{ WWT_CAPTION, RESIZE_NONE, COLOUR_LIGHT_BLUE, 11, 299, 0, 13, STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS}, // EPW_CAPTION
|
|
|
|
{ WWT_PANEL, RESIZE_NONE, COLOUR_LIGHT_BLUE, 0, 299, 14, 191, 0x0, STR_NULL}, // EPW_BACKGROUND
|
2009-08-05 17:59:21 +00:00
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_LIGHT_BLUE, 85, 144, 172, 183, STR_QUIT_NO, STR_NULL}, // EPW_NO
|
|
|
|
{ WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_LIGHT_BLUE, 155, 214, 172, 183, STR_QUIT_YES, STR_NULL}, // EPW_YES
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2009-03-28 13:35:07 +00:00
|
|
|
static const NWidgetPart _nested_engine_preview_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE, EPW_CLOSE),
|
2009-04-21 23:40:56 +00:00
|
|
|
NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, EPW_CAPTION), SetDataTip(STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2009-03-28 13:35:07 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE, EPW_BACKGROUND),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 158),
|
|
|
|
NWidget(NWID_HORIZONTAL), SetPIP(85, 10, 85),
|
2009-08-05 17:59:21 +00:00
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, EPW_NO), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_NO, STR_NULL),
|
|
|
|
NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, EPW_YES), SetMinimalSize(60, 12), SetDataTip(STR_QUIT_YES, STR_NULL),
|
2009-03-28 13:35:07 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_SPACER), SetMinimalSize(0, 8),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2008-05-18 20:49:22 +00:00
|
|
|
struct EnginePreviewWindow : Window {
|
|
|
|
EnginePreviewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
|
|
|
|
{
|
2008-05-23 23:02:13 +00:00
|
|
|
this->FindWindowPlacementAndResize(desc);
|
2008-05-18 20:49:22 +00:00
|
|
|
}
|
2005-10-01 12:43:34 +00:00
|
|
|
|
2008-05-18 20:49:22 +00:00
|
|
|
virtual void OnPaint()
|
|
|
|
{
|
|
|
|
this->DrawWidgets();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-18 20:49:22 +00:00
|
|
|
EngineID engine = this->window_number;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, GetEngineCategoryName(engine));
|
2009-04-26 10:45:07 +00:00
|
|
|
DrawStringMultiLine(this->widget[EPW_BACKGROUND].left + 2, this->widget[EPW_BACKGROUND].right - 2, 18, 80, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_CENTER);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-25 14:46:32 +00:00
|
|
|
SetDParam(0, engine);
|
2009-03-21 22:00:00 +00:00
|
|
|
DrawString(this->widget[EPW_BACKGROUND].left + 2, this->widget[EPW_BACKGROUND].right - 2, 80, STR_ENGINE_NAME, TC_BLACK, SA_CENTER);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-18 20:49:22 +00:00
|
|
|
int width = this->width;
|
2009-08-30 17:38:28 +00:00
|
|
|
DrawVehicleEngine(width >> 1, 100, engine, GetEnginePalette(engine, _local_company));
|
2009-08-30 18:41:03 +00:00
|
|
|
DrawStringMultiLine(this->widget[EPW_BACKGROUND].left + 26, this->widget[EPW_BACKGROUND].right - 26, 100, 170, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
|
2005-10-01 12:43:34 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-18 20:49:22 +00:00
|
|
|
virtual void OnClick(Point pt, int widget)
|
|
|
|
{
|
|
|
|
switch (widget) {
|
2009-03-21 20:39:37 +00:00
|
|
|
case EPW_YES:
|
2008-12-28 14:37:19 +00:00
|
|
|
DoCommandP(0, this->window_number, 0, CMD_WANT_ENGINE_PREVIEW);
|
2006-11-18 17:04:44 +00:00
|
|
|
/* Fallthrough */
|
2009-03-21 20:39:37 +00:00
|
|
|
case EPW_NO:
|
2008-05-18 20:49:22 +00:00
|
|
|
delete this;
|
2005-11-14 19:48:04 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-18 20:49:22 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-15 15:12:06 +00:00
|
|
|
static const WindowDesc _engine_preview_desc(
|
2007-07-27 12:49:04 +00:00
|
|
|
WDP_CENTER, WDP_CENTER, 300, 192, 300, 192,
|
2007-02-01 15:49:12 +00:00
|
|
|
WC_ENGINE_PREVIEW, WC_NONE,
|
2009-02-04 16:59:41 +00:00
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_CONSTRUCTION,
|
2009-03-28 13:35:07 +00:00
|
|
|
_engine_preview_widgets, _nested_engine_preview_widgets, lengthof(_nested_engine_preview_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2005-10-01 12:43:34 +00:00
|
|
|
void ShowEnginePreviewWindow(EngineID engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-05-18 20:49:22 +00:00
|
|
|
AllocateWindowDescFront<EnginePreviewWindow>(&_engine_preview_desc, engine);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-10 21:17:00 +00:00
|
|
|
uint GetTotalCapacityOfArticulatedParts(EngineID engine, VehicleType type)
|
2008-06-20 07:23:00 +00:00
|
|
|
{
|
|
|
|
uint total = 0;
|
|
|
|
|
2009-06-27 21:36:04 +00:00
|
|
|
CargoArray cap = GetCapacityOfArticulatedParts(engine, type);
|
|
|
|
for (CargoID c = 0; c < NUM_CARGO; c++) {
|
2008-06-20 07:23:00 +00:00
|
|
|
total += cap[c];
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2009-08-30 18:41:03 +00:00
|
|
|
static StringID GetTrainEngineInfoString(const Engine *e)
|
2005-07-01 14:05:44 +00:00
|
|
|
{
|
2009-01-25 00:57:03 +00:00
|
|
|
SetDParam(0, e->GetCost());
|
2009-02-01 16:10:06 +00:00
|
|
|
SetDParam(2, e->GetDisplayMaxSpeed());
|
|
|
|
SetDParam(3, e->GetPower());
|
|
|
|
SetDParam(1, e->GetDisplayWeight());
|
2005-07-01 14:05:44 +00:00
|
|
|
|
2009-01-25 00:57:03 +00:00
|
|
|
SetDParam(4, e->GetRunningCost());
|
2005-07-01 14:05:44 +00:00
|
|
|
|
2009-08-30 18:41:03 +00:00
|
|
|
uint capacity = GetTotalCapacityOfArticulatedParts(e->index, VEH_TRAIN);
|
2008-06-20 07:23:00 +00:00
|
|
|
if (capacity != 0) {
|
2009-02-21 12:52:41 +00:00
|
|
|
SetDParam(5, e->GetDefaultCargoType());
|
2008-07-26 22:00:59 +00:00
|
|
|
SetDParam(6, capacity);
|
2005-10-23 13:04:44 +00:00
|
|
|
} else {
|
2006-10-20 19:48:25 +00:00
|
|
|
SetDParam(5, CT_INVALID);
|
2005-07-01 14:05:44 +00:00
|
|
|
}
|
2009-08-30 18:41:03 +00:00
|
|
|
return STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER;
|
2005-07-01 14:05:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-30 18:41:03 +00:00
|
|
|
static StringID GetAircraftEngineInfoString(const Engine *e)
|
2005-07-01 14:05:44 +00:00
|
|
|
{
|
2009-03-13 23:49:12 +00:00
|
|
|
CargoID cargo = e->GetDefaultCargoType();
|
2009-01-25 00:57:03 +00:00
|
|
|
|
2009-03-13 23:49:12 +00:00
|
|
|
if (cargo == CT_INVALID || cargo == CT_PASSENGERS) {
|
|
|
|
SetDParam(0, e->GetCost());
|
|
|
|
SetDParam(1, e->GetDisplayMaxSpeed());
|
2009-04-20 22:41:09 +00:00
|
|
|
SetDParam(2, CT_PASSENGERS),
|
|
|
|
SetDParam(3, e->GetDisplayDefaultCapacity());
|
|
|
|
SetDParam(4, CT_MAIL),
|
|
|
|
SetDParam(5, e->u.air.mail_capacity);
|
|
|
|
SetDParam(6, e->GetRunningCost());
|
2009-08-30 18:41:03 +00:00
|
|
|
return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_CAPACITY_RUNCOST;
|
2009-03-13 23:49:12 +00:00
|
|
|
} else {
|
|
|
|
SetDParam(0, e->GetCost());
|
|
|
|
SetDParam(1, e->GetDisplayMaxSpeed());
|
|
|
|
SetDParam(2, cargo);
|
2009-03-18 19:32:13 +00:00
|
|
|
SetDParam(3, e->GetDisplayDefaultCapacity());
|
2009-03-13 23:49:12 +00:00
|
|
|
SetDParam(4, e->GetRunningCost());
|
2009-08-30 18:41:03 +00:00
|
|
|
return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
|
2009-03-13 23:49:12 +00:00
|
|
|
}
|
2005-07-01 14:05:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-30 18:41:03 +00:00
|
|
|
static StringID GetRoadVehEngineInfoString(const Engine *e)
|
2005-07-01 14:05:44 +00:00
|
|
|
{
|
2009-01-25 00:57:03 +00:00
|
|
|
SetDParam(0, e->GetCost());
|
2009-02-01 16:10:06 +00:00
|
|
|
SetDParam(1, e->GetDisplayMaxSpeed());
|
2009-08-30 18:41:03 +00:00
|
|
|
uint capacity = GetTotalCapacityOfArticulatedParts(e->index, VEH_ROAD);
|
2009-02-21 12:52:41 +00:00
|
|
|
if (capacity != 0) {
|
2009-04-20 21:29:41 +00:00
|
|
|
SetDParam(2, e->GetDefaultCargoType());
|
|
|
|
SetDParam(3, capacity);
|
2009-02-21 12:52:41 +00:00
|
|
|
} else {
|
2009-04-20 21:29:41 +00:00
|
|
|
SetDParam(2, CT_INVALID);
|
2009-02-21 12:52:41 +00:00
|
|
|
}
|
2009-04-20 21:29:41 +00:00
|
|
|
SetDParam(4, e->GetRunningCost());
|
2009-08-30 18:41:03 +00:00
|
|
|
return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
|
2005-07-01 14:05:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-30 18:41:03 +00:00
|
|
|
static StringID GetShipEngineInfoString(const Engine *e)
|
2005-07-01 14:05:44 +00:00
|
|
|
{
|
2009-01-25 00:57:03 +00:00
|
|
|
SetDParam(0, e->GetCost());
|
2009-02-01 16:10:06 +00:00
|
|
|
SetDParam(1, e->GetDisplayMaxSpeed());
|
2009-02-21 12:52:41 +00:00
|
|
|
SetDParam(2, e->GetDefaultCargoType());
|
2009-03-18 19:32:13 +00:00
|
|
|
SetDParam(3, e->GetDisplayDefaultCapacity());
|
2009-01-25 00:57:03 +00:00
|
|
|
SetDParam(4, e->GetRunningCost());
|
2009-08-30 18:41:03 +00:00
|
|
|
return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a multi-line string with some technical data, describing the engine.
|
|
|
|
* @param engine Engine to describe.
|
|
|
|
* @return String describing the engine.
|
|
|
|
* @post \c DParam array is set up for printing the string.
|
|
|
|
*/
|
|
|
|
StringID GetEngineInfoString(EngineID engine)
|
|
|
|
{
|
|
|
|
const Engine *e = Engine::Get(engine);
|
|
|
|
|
|
|
|
switch (e->type) {
|
|
|
|
case VEH_TRAIN:
|
|
|
|
return GetTrainEngineInfoString(e);
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
return GetRoadVehEngineInfoString(e);
|
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
return GetShipEngineInfoString(e);
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
return GetAircraftEngineInfoString(e);
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
2005-07-01 14:05:44 +00:00
|
|
|
}
|
|
|
|
|
2009-08-30 17:38:28 +00:00
|
|
|
/**
|
|
|
|
* Draw an engine.
|
|
|
|
* @param x Horizontal position to use for drawing the engine.
|
|
|
|
* @param y Vertical position to use for drawing the engine.
|
|
|
|
* @param engine Engine to draw.
|
|
|
|
* @para, pal Palette to use for drawing.
|
|
|
|
*/
|
|
|
|
void DrawVehicleEngine(int x, int y, EngineID engine, SpriteID pal)
|
|
|
|
{
|
|
|
|
const Engine *e = Engine::Get(engine);
|
|
|
|
|
|
|
|
switch (e->type) {
|
|
|
|
case VEH_TRAIN:
|
|
|
|
DrawTrainEngine(x, y, engine, pal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
DrawRoadVehEngine(x, y, engine, pal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
DrawShipEngine(x, y, engine, pal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
DrawAircraftEngine(x, y, engine, pal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 12:24:23 +00:00
|
|
|
/** Sort all items using qsort() and given 'CompareItems' function
|
|
|
|
* @param el list to be sorted
|
|
|
|
* @param compare function for evaluation of the quicksort
|
|
|
|
*/
|
2008-05-28 17:29:27 +00:00
|
|
|
void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
|
2008-05-27 12:24:23 +00:00
|
|
|
{
|
2008-05-28 17:29:27 +00:00
|
|
|
uint size = el->Length();
|
2008-05-27 12:24:23 +00:00
|
|
|
/* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
|
|
|
|
* generally, do not sort if there are less than 2 items */
|
|
|
|
if (size < 2) return;
|
2008-05-28 17:29:27 +00:00
|
|
|
qsort(el->Begin(), size, sizeof(*el->Begin()), compare); // MorphOS doesn't know vector::at(int) ...
|
2008-05-27 12:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
|
|
|
|
* @param el list to be sorted
|
|
|
|
* @param compare function for evaluation of the quicksort
|
|
|
|
* @param begin start of sorting
|
|
|
|
* @param num_items count of items to be sorted
|
|
|
|
*/
|
2008-05-28 17:29:27 +00:00
|
|
|
void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
|
2008-05-27 12:24:23 +00:00
|
|
|
{
|
2008-05-28 20:06:00 +00:00
|
|
|
if (num_items < 2) return;
|
2008-05-28 17:29:27 +00:00
|
|
|
assert(begin < el->Length());
|
|
|
|
assert(begin + num_items <= el->Length());
|
|
|
|
qsort(el->Get(begin), num_items, sizeof(*el->Begin()), compare);
|
2008-05-27 12:24:23 +00:00
|
|
|
}
|
|
|
|
|