engine_gui.cpp

Go to the documentation of this file.
00001 /* $Id: engine_gui.cpp 18330 2009-11-28 20:35:25Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "window_gui.h"
00014 #include "gfx_func.h"
00015 #include "engine_base.h"
00016 #include "command_func.h"
00017 #include "strings_func.h"
00018 #include "engine_gui.h"
00019 #include "articulated_vehicles.h"
00020 #include "vehicle_func.h"
00021 #include "company_func.h"
00022 #include "rail.h"
00023 #include "settings_type.h"
00024 
00025 #include "table/strings.h"
00026 
00031 StringID GetEngineCategoryName(EngineID engine)
00032 {
00033   const Engine *e = Engine::Get(engine);
00034   switch (e->type) {
00035     default: NOT_REACHED();
00036     case VEH_ROAD:              return STR_ENGINE_PREVIEW_ROAD_VEHICLE;
00037     case VEH_AIRCRAFT:          return STR_ENGINE_PREVIEW_AIRCRAFT;
00038     case VEH_SHIP:              return STR_ENGINE_PREVIEW_SHIP;
00039     case VEH_TRAIN:
00040       return GetRailTypeInfo(e->u.rail.railtype)->strings.new_loco;
00041   }
00042 }
00043 
00045 enum EnginePreviewWidgets {
00046   EPW_QUESTION,   
00047   EPW_NO,         
00048   EPW_YES,        
00049 };
00050 
00051 static const NWidgetPart _nested_engine_preview_widgets[] = {
00052   NWidget(NWID_HORIZONTAL),
00053     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
00054     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00055   EndContainer(),
00056   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
00057     NWidget(WWT_EMPTY, INVALID_COLOUR, EPW_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
00058     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
00059       NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, EPW_NO), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
00060       NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, EPW_YES), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
00061     EndContainer(),
00062     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
00063   EndContainer(),
00064 };
00065 
00066 struct EnginePreviewWindow : Window {
00067   static const int VEHICLE_SPACE = 40; // The space to show the vehicle image
00068 
00069   EnginePreviewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00070   {
00071     this->InitNested(desc, window_number);
00072   }
00073 
00074   virtual void OnPaint()
00075   {
00076     this->DrawWidgets();
00077   }
00078 
00079   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00080   {
00081     if (widget != EPW_QUESTION) return;
00082 
00083     EngineID engine = this->window_number;
00084     SetDParam(0, GetEngineCategoryName(engine));
00085     size->height = GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, size->width) + WD_PAR_VSEP_WIDE + FONT_HEIGHT_NORMAL + VEHICLE_SPACE;
00086     SetDParam(0, engine);
00087     size->height += GetStringHeight(GetEngineInfoString(engine), size->width);
00088   }
00089 
00090   virtual void DrawWidget(const Rect &r, int widget) const
00091   {
00092     if (widget != EPW_QUESTION) return;
00093 
00094     EngineID engine = this->window_number;
00095     SetDParam(0, GetEngineCategoryName(engine));
00096     int y = r.top + GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, r.right - r.top + 1);
00097     y = DrawStringMultiLine(r.left, r.right, r.top, y, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_CENTER) + WD_PAR_VSEP_WIDE;
00098 
00099     SetDParam(0, engine);
00100     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_ENGINE_NAME, TC_BLACK, SA_CENTER);
00101     y += FONT_HEIGHT_NORMAL;
00102 
00103     DrawVehicleEngine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, this->width >> 1, y + VEHICLE_SPACE / 2, engine, GetEnginePalette(engine, _local_company));
00104 
00105     y += VEHICLE_SPACE;
00106     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
00107   }
00108 
00109   virtual void OnClick(Point pt, int widget)
00110   {
00111     switch (widget) {
00112       case EPW_YES:
00113         DoCommandP(0, this->window_number, 0, CMD_WANT_ENGINE_PREVIEW);
00114         /* Fallthrough */
00115       case EPW_NO:
00116         delete this;
00117         break;
00118     }
00119   }
00120 };
00121 
00122 static const WindowDesc _engine_preview_desc(
00123   WDP_CENTER, 0, 0,
00124   WC_ENGINE_PREVIEW, WC_NONE,
00125   WDF_CONSTRUCTION,
00126   _nested_engine_preview_widgets, lengthof(_nested_engine_preview_widgets)
00127 );
00128 
00129 
00130 void ShowEnginePreviewWindow(EngineID engine)
00131 {
00132   AllocateWindowDescFront<EnginePreviewWindow>(&_engine_preview_desc, engine);
00133 }
00134 
00135 uint GetTotalCapacityOfArticulatedParts(EngineID engine)
00136 {
00137   uint total = 0;
00138 
00139   CargoArray cap = GetCapacityOfArticulatedParts(engine);
00140   for (CargoID c = 0; c < NUM_CARGO; c++) {
00141     total += cap[c];
00142   }
00143 
00144   return total;
00145 }
00146 
00147 static StringID GetTrainEngineInfoString(const Engine *e)
00148 {
00149   SetDParam(0, e->GetCost());
00150   SetDParam(2, e->GetDisplayMaxSpeed());
00151   SetDParam(3, e->GetPower());
00152   SetDParam(1, e->GetDisplayWeight());
00153   SetDParam(7, e->GetDisplayMaxTractiveEffort());
00154 
00155   SetDParam(4, e->GetRunningCost());
00156 
00157   uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
00158   if (capacity != 0) {
00159     SetDParam(5, e->GetDefaultCargoType());
00160     SetDParam(6, capacity);
00161   } else {
00162     SetDParam(5, CT_INVALID);
00163   }
00164   return (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && e->u.rail.railtype != RAILTYPE_MAGLEV) ? STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER_MAX_TE : STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER;
00165 }
00166 
00167 static StringID GetAircraftEngineInfoString(const Engine *e)
00168 {
00169   CargoID cargo = e->GetDefaultCargoType();
00170   uint16 mail_capacity;
00171   uint capacity = e->GetDisplayDefaultCapacity(&mail_capacity);
00172 
00173   if (mail_capacity > 0) {
00174     SetDParam(0, e->GetCost());
00175     SetDParam(1, e->GetDisplayMaxSpeed());
00176     SetDParam(2, cargo);
00177     SetDParam(3, capacity);
00178     SetDParam(4, CT_MAIL);
00179     SetDParam(5, mail_capacity);
00180     SetDParam(6, e->GetRunningCost());
00181     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_CAPACITY_RUNCOST;
00182   } else {
00183     SetDParam(0, e->GetCost());
00184     SetDParam(1, e->GetDisplayMaxSpeed());
00185     SetDParam(2, cargo);
00186     SetDParam(3, capacity);
00187     SetDParam(4, e->GetRunningCost());
00188     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
00189   }
00190 }
00191 
00192 static StringID GetRoadVehEngineInfoString(const Engine *e)
00193 {
00194   SetDParam(0, e->GetCost());
00195   SetDParam(1, e->GetDisplayMaxSpeed());
00196   uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
00197   if (capacity != 0) {
00198     SetDParam(2, e->GetDefaultCargoType());
00199     SetDParam(3, capacity);
00200   } else {
00201     SetDParam(2, CT_INVALID);
00202   }
00203   SetDParam(4, e->GetRunningCost());
00204   return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
00205 }
00206 
00207 static StringID GetShipEngineInfoString(const Engine *e)
00208 {
00209   SetDParam(0, e->GetCost());
00210   SetDParam(1, e->GetDisplayMaxSpeed());
00211   SetDParam(2, e->GetDefaultCargoType());
00212   SetDParam(3, e->GetDisplayDefaultCapacity());
00213   SetDParam(4, e->GetRunningCost());
00214   return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
00215 }
00216 
00217 
00224 StringID GetEngineInfoString(EngineID engine)
00225 {
00226   const Engine *e = Engine::Get(engine);
00227 
00228   switch (e->type) {
00229     case VEH_TRAIN:
00230       return GetTrainEngineInfoString(e);
00231 
00232     case VEH_ROAD:
00233       return GetRoadVehEngineInfoString(e);
00234 
00235     case VEH_SHIP:
00236       return GetShipEngineInfoString(e);
00237 
00238     case VEH_AIRCRAFT:
00239       return GetAircraftEngineInfoString(e);
00240 
00241     default: NOT_REACHED();
00242   }
00243 }
00244 
00254 void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID engine, SpriteID pal)
00255 {
00256   const Engine *e = Engine::Get(engine);
00257 
00258   switch (e->type) {
00259     case VEH_TRAIN:
00260       DrawTrainEngine(left, right, preferred_x, y, engine, pal);
00261       break;
00262 
00263     case VEH_ROAD:
00264       DrawRoadVehEngine(left, right, preferred_x, y, engine, pal);
00265       break;
00266 
00267     case VEH_SHIP:
00268       DrawShipEngine(left, right, preferred_x, y, engine, pal);
00269       break;
00270 
00271     case VEH_AIRCRAFT:
00272       DrawAircraftEngine(left, right, preferred_x, y, engine, pal);
00273       break;
00274 
00275     default: NOT_REACHED();
00276   }
00277 }
00278 
00283 void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
00284 {
00285   uint size = el->Length();
00286   /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
00287    * generally, do not sort if there are less than 2 items */
00288   if (size < 2) return;
00289   QSortT(el->Begin(), size, compare);
00290 }
00291 
00298 void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
00299 {
00300   if (num_items < 2) return;
00301   assert(begin < el->Length());
00302   assert(begin + num_items <= el->Length());
00303   QSortT(el->Get(begin), num_items, compare);
00304 }
00305 

Generated on Wed Dec 23 23:27:50 2009 for OpenTTD by  doxygen 1.5.6