# engine_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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
    00032 StringID GetEngineCategoryName(EngineID engine)
    00033 {
    00034   const Engine *e = Engine::Get(engine);
    00035   switch (e->type) {
    00036     default: NOT_REACHED();
    00037     case VEH_ROAD:              return STR_ENGINE_PREVIEW_ROAD_VEHICLE;
    00038     case VEH_AIRCRAFT:          return STR_ENGINE_PREVIEW_AIRCRAFT;
    00039     case VEH_SHIP:              return STR_ENGINE_PREVIEW_SHIP;
    00040     case VEH_TRAIN:
    00041       return GetRailTypeInfo(e->u.rail.railtype)->strings.new_loco;
    00042   }
    00043 }
    00044
    00046 enum EnginePreviewWidgets {
    00047   EPW_QUESTION,
    00048   EPW_NO,
    00049   EPW_YES,
    00050 };
    00051
    00052 static const NWidgetPart _nested_engine_preview_widgets[] = {
    00053   NWidget(NWID_HORIZONTAL),
    00054     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
    00055     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00056   EndContainer(),
    00057   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
    00058     NWidget(WWT_EMPTY, INVALID_COLOUR, EPW_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
    00059     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
    00060       NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, EPW_NO), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
    00061       NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, EPW_YES), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
    00062     EndContainer(),
    00063     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
    00064   EndContainer(),
    00065 };
    00066
    00067 struct EnginePreviewWindow : Window {
    00068   static const int VEHICLE_SPACE = 40; // The space to show the vehicle image
    00069
    00070   EnginePreviewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00071   {
    00072     this->InitNested(desc, window_number);
    00073   }
    00074
    00075   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00076   {
    00077     if (widget != EPW_QUESTION) return;
    00078
    00079     EngineID engine = this->window_number;
    00080     SetDParam(0, GetEngineCategoryName(engine));
    00081     size->height = GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, size->width) + WD_PAR_VSEP_WIDE + FONT_HEIGHT_NORMAL + VEHICLE_SPACE;
    00082     SetDParam(0, engine);
    00083     size->height += GetStringHeight(GetEngineInfoString(engine), size->width);
    00084   }
    00085
    00086   virtual void DrawWidget(const Rect &r, int widget) const
    00087   {
    00088     if (widget != EPW_QUESTION) return;
    00089
    00090     EngineID engine = this->window_number;
    00091     SetDParam(0, GetEngineCategoryName(engine));
    00092     int y = r.top + GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, r.right - r.top + 1);
    00093     y = DrawStringMultiLine(r.left, r.right, r.top, y, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_CENTER) + WD_PAR_VSEP_WIDE;
    00094
    00095     SetDParam(0, engine);
    00096     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_ENGINE_NAME, TC_BLACK, SA_HOR_CENTER);
    00097     y += FONT_HEIGHT_NORMAL;
    00098
    00099     DrawVehicleEngine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, this->width >> 1, y + VEHICLE_SPACE / 2, engine, GetEnginePalette(engine, _local_company));
    00100
    00101     y += VEHICLE_SPACE;
    00102     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
    00103   }
    00104
    00105   virtual void OnClick(Point pt, int widget, int click_count)
    00106   {
    00107     switch (widget) {
    00108       case EPW_YES:
    00109         DoCommandP(0, this->window_number, 0, CMD_WANT_ENGINE_PREVIEW);
    00110         /* FALL THROUGH */
    00111       case EPW_NO:
    00112         delete this;
    00113         break;
    00114     }
    00115   }
    00116 };
    00117
    00118 static const WindowDesc _engine_preview_desc(
    00119   WDP_CENTER, 0, 0,
    00120   WC_ENGINE_PREVIEW, WC_NONE,
    00121   WDF_CONSTRUCTION,
    00122   _nested_engine_preview_widgets, lengthof(_nested_engine_preview_widgets)
    00123 );
    00124
    00125
    00126 void ShowEnginePreviewWindow(EngineID engine)
    00127 {
    00128   AllocateWindowDescFront<EnginePreviewWindow>(&_engine_preview_desc, engine);
    00129 }
    00130
    00136 uint GetTotalCapacityOfArticulatedParts(EngineID engine)
    00137 {
    00138   uint total = 0;
    00139
    00140   CargoArray cap = GetCapacityOfArticulatedParts(engine);
    00141   for (CargoID c = 0; c < NUM_CARGO; c++) {
    00142     total += cap[c];
    00143   }
    00144
    00145   return total;
    00146 }
    00147
    00148 static StringID GetTrainEngineInfoString(const Engine *e)
    00149 {
    00150   SetDParam(0, e->GetCost());
    00151   SetDParam(2, e->GetDisplayMaxSpeed());
    00152   SetDParam(3, e->GetPower());
    00153   SetDParam(1, e->GetDisplayWeight());
    00154   SetDParam(7, e->GetDisplayMaxTractiveEffort());
    00155
    00156   SetDParam(4, e->GetRunningCost());
    00157
    00158   uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
    00159   if (capacity != 0) {
    00160     SetDParam(5, e->GetDefaultCargoType());
    00161     SetDParam(6, capacity);
    00162   } else {
    00163     SetDParam(5, CT_INVALID);
    00164   }
    00165   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;
    00166 }
    00167
    00168 static StringID GetAircraftEngineInfoString(const Engine *e)
    00169 {
    00170   CargoID cargo = e->GetDefaultCargoType();
    00171   uint16 mail_capacity;
    00172   uint capacity = e->GetDisplayDefaultCapacity(&mail_capacity);
    00173
    00174   SetDParam(0, e->GetCost());
    00175   SetDParam(1, e->GetDisplayMaxSpeed());
    00176   SetDParam(2, cargo);
    00177   SetDParam(3, capacity);
    00178
    00179   if (mail_capacity > 0) {
    00180     SetDParam(4, CT_MAIL);
    00181     SetDParam(5, mail_capacity);
    00182     SetDParam(6, e->GetRunningCost());
    00183     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_CAPACITY_RUNCOST;
    00184   } else {
    00185     SetDParam(4, e->GetRunningCost());
    00186     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
    00187   }
    00188 }
    00189
    00190 static StringID GetRoadVehEngineInfoString(const Engine *e)
    00191 {
    00192   if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) {
    00193     SetDParam(0, e->GetCost());
    00194     SetDParam(1, e->GetDisplayMaxSpeed());
    00195     uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
    00196     if (capacity != 0) {
    00197       SetDParam(2, e->GetDefaultCargoType());
    00198       SetDParam(3, capacity);
    00199     } else {
    00200       SetDParam(2, CT_INVALID);
    00201     }
    00202     SetDParam(4, e->GetRunningCost());
    00203     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
    00204   } else {
    00205     SetDParam(0, e->GetCost());
    00206     SetDParam(2, e->GetDisplayMaxSpeed());
    00207     SetDParam(3, e->GetPower());
    00208     SetDParam(1, e->GetDisplayWeight());
    00209     SetDParam(7, e->GetDisplayMaxTractiveEffort());
    00210
    00211     SetDParam(4, e->GetRunningCost());
    00212
    00213     uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
    00214     if (capacity != 0) {
    00215       SetDParam(5, e->GetDefaultCargoType());
    00216       SetDParam(6, capacity);
    00217     } else {
    00218       SetDParam(5, CT_INVALID);
    00219     }
    00220     return STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER_MAX_TE;
    00221   }
    00222 }
    00223
    00224 static StringID GetShipEngineInfoString(const Engine *e)
    00225 {
    00226   SetDParam(0, e->GetCost());
    00227   SetDParam(1, e->GetDisplayMaxSpeed());
    00228   SetDParam(2, e->GetDefaultCargoType());
    00229   SetDParam(3, e->GetDisplayDefaultCapacity());
    00230   SetDParam(4, e->GetRunningCost());
    00231   return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
    00232 }
    00233
    00234
    00241 StringID GetEngineInfoString(EngineID engine)
    00242 {
    00243   const Engine *e = Engine::Get(engine);
    00244
    00245   switch (e->type) {
    00246     case VEH_TRAIN:
    00247       return GetTrainEngineInfoString(e);
    00248
    00249     case VEH_ROAD:
    00250       return GetRoadVehEngineInfoString(e);
    00251
    00252     case VEH_SHIP:
    00253       return GetShipEngineInfoString(e);
    00254
    00255     case VEH_AIRCRAFT:
    00256       return GetAircraftEngineInfoString(e);
    00257
    00258     default: NOT_REACHED();
    00259   }
    00260 }
    00261
    00271 void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
    00272 {
    00273   const Engine *e = Engine::Get(engine);
    00274
    00275   switch (e->type) {
    00276     case VEH_TRAIN:
    00277       DrawTrainEngine(left, right, preferred_x, y, engine, pal);
    00278       break;
    00279
    00280     case VEH_ROAD:
    00281       DrawRoadVehEngine(left, right, preferred_x, y, engine, pal);
    00282       break;
    00283
    00284     case VEH_SHIP:
    00285       DrawShipEngine(left, right, preferred_x, y, engine, pal);
    00286       break;
    00287
    00288     case VEH_AIRCRAFT:
    00289       DrawAircraftEngine(left, right, preferred_x, y, engine, pal);
    00290       break;
    00291
    00292     default: NOT_REACHED();
    00293   }
    00294 }
    00295
    00301 void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
    00302 {
    00303   uint size = el->Length();
    00304   /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
    00305    * generally, do not sort if there are less than 2 items */
    00306   if (size < 2) return;
    00307   QSortT(el->Begin(), size, compare);
    00308 }
    00309
    00317 void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
    00318 {
    00319   if (num_items < 2) return;
    00320   assert(begin < el->Length());
    00321   assert(begin + num_items <= el->Length());
    00322   QSortT(el->Get(begin), num_items, compare);
    00323 }
    00324