# engine_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: engine_gui.cpp 15765 2009-03-18 19:50:34Z rubidium $ */
    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         /* Fallthrough */
    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 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 Engine *e = GetEngine(engine);
    00127
    00128   SetDParam(0, e->GetCost());
    00129   SetDParam(2, e->GetDisplayMaxSpeed());
    00130   SetDParam(3, e->GetPower());
    00131   SetDParam(1, e->GetDisplayWeight());
    00132
    00133   SetDParam(4, e->GetRunningCost());
    00134
    00135   uint capacity = GetTotalCapacityOfArticulatedParts(engine, VEH_TRAIN);
    00136   if (capacity != 0) {
    00137     SetDParam(5, e->GetDefaultCargoType());
    00138     SetDParam(6, capacity);
    00139   } else {
    00140     SetDParam(5, CT_INVALID);
    00141   }
    00142   DrawStringMultiCenter(x, y, STR_VEHICLE_INFO_COST_WEIGHT_SPEED_POWER, maxw);
    00143 }
    00144
    00145 static void DrawAircraftEngineInfo(EngineID engine, int x, int y, int maxw)
    00146 {
    00147   const Engine *e = GetEngine(engine);
    00148   CargoID cargo = e->GetDefaultCargoType();
    00149
    00150   if (cargo == CT_INVALID || cargo == CT_PASSENGERS) {
    00151     SetDParam(0, e->GetCost());
    00152     SetDParam(1, e->GetDisplayMaxSpeed());
    00153     SetDParam(2, e->GetDisplayDefaultCapacity());
    00154     SetDParam(3, e->u.air.mail_capacity);
    00155     SetDParam(4, e->GetRunningCost());
    00156
    00157     DrawStringMultiCenter(x, y, STR_A02E_COST_MAX_SPEED_CAPACITY, maxw);
    00158   } else {
    00159     SetDParam(0, e->GetCost());
    00160     SetDParam(1, e->GetDisplayMaxSpeed());
    00161     SetDParam(2, cargo);
    00162     SetDParam(3, e->GetDisplayDefaultCapacity());
    00163     SetDParam(4, e->GetRunningCost());
    00164
    00165     DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw);
    00166   }
    00167 }
    00168
    00169 static void DrawRoadVehEngineInfo(EngineID engine, int x, int y, int maxw)
    00170 {
    00171   const Engine *e = GetEngine(engine);
    00172
    00173   SetDParam(0, e->GetCost());
    00174   SetDParam(1, e->GetDisplayMaxSpeed());
    00175   SetDParam(2, e->GetRunningCost());
    00176   uint capacity = GetTotalCapacityOfArticulatedParts(engine, VEH_ROAD);
    00177   if (capacity != 0) {
    00178     SetDParam(3, e->GetDefaultCargoType());
    00179     SetDParam(4, capacity);
    00180   } else {
    00181     SetDParam(3, CT_INVALID);
    00182   }
    00183
    00184   DrawStringMultiCenter(x, y, STR_902A_COST_SPEED_RUNNING_COST, maxw);
    00185 }
    00186
    00187 static void DrawShipEngineInfo(EngineID engine, int x, int y, int maxw)
    00188 {
    00189   const Engine *e = GetEngine(engine);
    00190
    00191   SetDParam(0, e->GetCost());
    00192   SetDParam(1, e->GetDisplayMaxSpeed());
    00193   SetDParam(2, e->GetDefaultCargoType());
    00194   SetDParam(3, e->GetDisplayDefaultCapacity());
    00195   SetDParam(4, e->GetRunningCost());
    00196   DrawStringMultiCenter(x, y, STR_982E_COST_MAX_SPEED_CAPACITY, maxw);
    00197 }
    00198
    00199 void DrawNewsNewVehicleAvail(Window *w, const NewsItem *ni)
    00200 {
    00201   EngineID engine = ni->data_a;
    00202   const DrawEngineInfo *dei = &_draw_engine_list[GetEngine(engine)->type];
    00203
    00204   SetDParam(0, GetEngineCategoryName(engine));
    00205   DrawStringMultiCenter(w->width >> 1, 20, STR_NEW_VEHICLE_NOW_AVAILABLE, w->width - 2);
    00206
    00207   GfxFillRect(25, 56, w->width - 25, w->height - 2, 10);
    00208
    00209   SetDParam(0, engine);
    00210   DrawStringMultiCenter(w->width >> 1, 57, STR_NEW_VEHICLE_TYPE, w->width - 2);
    00211
    00212   dei->engine_proc(w->width >> 1, 88, engine, 0);
    00213   GfxFillRect(25, 56, w->width - 56, 112, PALETTE_TO_STRUCT_GREY, FILLRECT_RECOLOUR);
    00214   dei->info_proc(engine, w->width >> 1, 129, w->width - 52);
    00215 }
    00216
    00217
    00222 void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
    00223 {
    00224   uint size = el->Length();
    00225   /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
    00226    * generally, do not sort if there are less than 2 items */
    00227   if (size < 2) return;
    00228   qsort(el->Begin(), size, sizeof(*el->Begin()), compare); // MorphOS doesn't know vector::at(int) ...
    00229 }
    00230
    00237 void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
    00238 {
    00239   if (num_items < 2) return;
    00240   assert(begin < el->Length());
    00241   assert(begin + num_items <= el->Length());
    00242   qsort(el->Get(begin), num_items, sizeof(*el->Begin()), compare);
    00243 }
    00244