00001
00002
00005 #include "stdafx.h"
00006 #include "window_gui.h"
00007 #include "gfx_func.h"
00008 #include "engine_func.h"
00009 #include "engine_base.h"
00010 #include "command_func.h"
00011 #include "news_type.h"
00012 #include "newgrf_engine.h"
00013 #include "strings_func.h"
00014 #include "engine_gui.h"
00015 #include "articulated_vehicles.h"
00016 #include "rail.h"
00017
00018 #include "table/strings.h"
00019 #include "table/sprites.h"
00020
00021 StringID GetEngineCategoryName(EngineID engine)
00022 {
00023 switch (GetEngine(engine)->type) {
00024 default: NOT_REACHED();
00025 case VEH_ROAD: return STR_8103_ROAD_VEHICLE;
00026 case VEH_AIRCRAFT: return STR_8104_AIRCRAFT;
00027 case VEH_SHIP: return STR_8105_SHIP;
00028 case VEH_TRAIN:
00029 return GetRailTypeInfo(RailVehInfo(engine)->railtype)->strings.new_loco;
00030 }
00031 }
00032
00033 static const Widget _engine_preview_widgets[] = {
00034 { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_LIGHT_BLUE, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
00035 { WWT_CAPTION, RESIZE_NONE, COLOUR_LIGHT_BLUE, 11, 299, 0, 13, STR_8100_MESSAGE_FROM_VEHICLE_MANUFACTURE, STR_018C_WINDOW_TITLE_DRAG_THIS},
00036 { WWT_PANEL, RESIZE_NONE, COLOUR_LIGHT_BLUE, 0, 299, 14, 191, 0x0, STR_NULL},
00037 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_LIGHT_BLUE, 85, 144, 172, 183, STR_00C9_NO, STR_NULL},
00038 { WWT_PUSHTXTBTN, RESIZE_NONE, COLOUR_LIGHT_BLUE, 155, 214, 172, 183, STR_00C8_YES, STR_NULL},
00039 { WIDGETS_END},
00040 };
00041
00042 typedef void DrawEngineProc(int x, int y, EngineID engine, SpriteID pal);
00043 typedef void DrawEngineInfoProc(EngineID, int x, int y, int maxw);
00044
00045 struct DrawEngineInfo {
00046 DrawEngineProc *engine_proc;
00047 DrawEngineInfoProc *info_proc;
00048 };
00049
00050 static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw);
00051 static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw);
00052 static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw);
00053 static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw);
00054
00055 static const DrawEngineInfo _draw_engine_list[4] = {
00056 { DrawTrainEngine, DrawTrainEngineInfo },
00057 { DrawRoadVehEngine, DrawRoadVehEngineInfo },
00058 { DrawShipEngine, DrawShipEngineInfo },
00059 { DrawAircraftEngine, DrawAircraftEngineInfo },
00060 };
00061
00062 struct EnginePreviewWindow : Window {
00063 EnginePreviewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
00064 {
00065 this->FindWindowPlacementAndResize(desc);
00066 }
00067
00068 virtual void OnPaint()
00069 {
00070 this->DrawWidgets();
00071
00072 EngineID engine = this->window_number;
00073 SetDParam(0, GetEngineCategoryName(engine));
00074 DrawStringMultiCenter(150, 44, STR_8101_WE_HAVE_JUST_DESIGNED_A, 296);
00075
00076 SetDParam(0, engine);
00077 DrawStringCentered(this->width >> 1, 80, STR_ENGINE_NAME, TC_BLACK);
00078
00079 const DrawEngineInfo *dei = &_draw_engine_list[GetEngine(engine)->type];
00080
00081 int width = this->width;
00082 dei->engine_proc(width >> 1, 100, engine, 0);
00083 dei->info_proc(engine, width >> 1, 130, width - 52);
00084 }
00085
00086 virtual void OnClick(Point pt, int widget)
00087 {
00088 switch (widget) {
00089 case 4:
00090 DoCommandP(0, this->window_number, 0, CMD_WANT_ENGINE_PREVIEW);
00091
00092 case 3:
00093 delete this;
00094 break;
00095 }
00096 }
00097 };
00098
00099 static const WindowDesc _engine_preview_desc = {
00100 WDP_CENTER, WDP_CENTER, 300, 192, 300, 192,
00101 WC_ENGINE_PREVIEW, WC_NONE,
00102 WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_CONSTRUCTION,
00103 _engine_preview_widgets,
00104 };
00105
00106
00107 void ShowEnginePreviewWindow(EngineID engine)
00108 {
00109 AllocateWindowDescFront<EnginePreviewWindow>(&_engine_preview_desc, engine);
00110 }
00111
00112 static uint GetTotalCapacityOfArticulatedParts(EngineID engine, VehicleType type)
00113 {
00114 uint total = 0;
00115
00116 uint16 *cap = GetCapacityOfArticulatedParts(engine, type);
00117 for (uint c = 0; c < NUM_CARGO; c++) {
00118 total += cap[c];
00119 }
00120
00121 return total;
00122 }
00123
00124 static void DrawTrainEngineInfo(EngineID engine, int x, int y, int maxw)
00125 {
00126 const RailVehicleInfo *rvi = RailVehInfo(engine);
00127 const Engine *e = GetEngine(engine);
00128
00129 SetDParam(0, e->GetCost());
00130 SetDParam(2, e->GetDisplayMaxSpeed());
00131 SetDParam(3, e->GetPower());
00132 SetDParam(1, e->GetDisplayWeight());
00133
00134 SetDParam(4, e->GetRunningCost());
00135
00136 uint capacity = GetTotalCapacityOfArticulatedParts(engine, VEH_TRAIN);
00137 if (capacity != 0) {
00138 SetDParam(5, rvi->cargo_type);
00139 SetDParam(6, capacity);
00140 } else {
00141 SetDParam(5, CT_INVALID);
00142 }
00143 DrawStringMultiCenter(x, y, STR_VEHICLE_INFO_COST_WEIGHT_SPEED_POWER, maxw);
00144 }
00145
00146 static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw)
00147 {
00148 const AircraftVehicleInfo *avi = AircraftVehInfo(engine);
00149 const Engine *e = GetEngine(engine);
00150
00151 SetDParam(0, e->GetCost());
00152 SetDParam(1, e->GetDisplayMaxSpeed());
00153 SetDParam(2, avi->passenger_capacity);
00154 SetDParam(3, avi->mail_capacity);
00155 SetDParam(4, e->GetRunningCost());
00156
00157 DrawStringMultiCenter(x, y, STR_A02E_COST_MAX_SPEED_CAPACITY, maxw);
00158 }
00159
00160 static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw)
00161 {
00162 const RoadVehicleInfo *rvi = RoadVehInfo(engine);
00163 const Engine *e = GetEngine(engine);
00164
00165 SetDParam(0, e->GetCost());
00166 SetDParam(1, e->GetDisplayMaxSpeed());
00167 SetDParam(2, e->GetRunningCost());
00168 SetDParam(3, rvi->cargo_type);
00169 SetDParam(4, GetTotalCapacityOfArticulatedParts(engine, VEH_ROAD));
00170
00171 DrawStringMultiCenter(x, y, STR_902A_COST_SPEED_RUNNING_COST, maxw);
00172 }
00173
00174 static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw)
00175 {
00176 const ShipVehicleInfo *svi = ShipVehInfo(engine);
00177 const Engine *e = GetEngine(engine);
00178
00179 SetDParam(0, e->GetCost());
00180 SetDParam(1, e->GetDisplayMaxSpeed());
00181 SetDParam(2, svi->cargo_type);
00182 SetDParam(3, GetEngineProperty(engine, 0x0D, svi->capacity));
00183 SetDParam(4, e->GetRunningCost());
00184 DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw);
00185 }
00186
00187 void DrawNewsNewVehicleAvail(Window *w, const NewsItem *ni)
00188 {
00189 EngineID engine = ni->data_a;
00190 const DrawEngineInfo *dei = &_draw_engine_list[GetEngine(engine)->type];
00191
00192 SetDParam(0, GetEngineCategoryName(engine));
00193 DrawStringMultiCenter(w->width >> 1, 20, STR_NEW_VEHICLE_NOW_AVAILABLE, w->width - 2);
00194
00195 GfxFillRect(25, 56, w->width - 25, w->height - 2, 10);
00196
00197 SetDParam(0, engine);
00198 DrawStringMultiCenter(w->width >> 1, 57, STR_NEW_VEHICLE_TYPE, w->width - 2);
00199
00200 dei->engine_proc(w->width >> 1, 88, engine, 0);
00201 GfxFillRect(25, 56, w->width - 56, 112, PALETTE_TO_STRUCT_GREY, FILLRECT_RECOLOUR);
00202 dei->info_proc(engine, w->width >> 1, 129, w->width - 52);
00203 }
00204
00205
00210 void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
00211 {
00212 uint size = el->Length();
00213
00214
00215 if (size < 2) return;
00216 qsort(el->Begin(), size, sizeof(*el->Begin()), compare);
00217 }
00218
00225 void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
00226 {
00227 if (num_items < 2) return;
00228 assert(begin < el->Length());
00229 assert(begin + num_items <= el->Length());
00230 qsort(el->Get(begin), num_items, sizeof(*el->Begin()), compare);
00231 }
00232