# engine_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: engine_gui.cpp 18966 2010-01-30 18:34:48Z frosch $ */
    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, int click_count)
    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 != 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;
    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   SetDParam(0, e->GetCost());
    00174   SetDParam(1, e->GetDisplayMaxSpeed());
    00175   SetDParam(2, cargo);
    00176   SetDParam(3, capacity);
    00177
    00178   if (mail_capacity > 0) {
    00179     SetDParam(4, CT_MAIL);
    00180     SetDParam(5, mail_capacity);
    00181     SetDParam(6, e->GetRunningCost());
    00182     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_CAPACITY_RUNCOST;
    00183   } else {
    00184     SetDParam(4, e->GetRunningCost());
    00185     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
    00186   }
    00187 }
    00188
    00189 static StringID GetRoadVehEngineInfoString(const Engine *e)
    00190 {
    00191   SetDParam(0, e->GetCost());
    00192   SetDParam(1, e->GetDisplayMaxSpeed());
    00193   uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
    00194   if (capacity != 0) {
    00195     SetDParam(2, e->GetDefaultCargoType());
    00196     SetDParam(3, capacity);
    00197   } else {
    00198     SetDParam(2, CT_INVALID);
    00199   }
    00200   SetDParam(4, e->GetRunningCost());
    00201   return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
    00202 }
    00203
    00204 static StringID GetShipEngineInfoString(const Engine *e)
    00205 {
    00206   SetDParam(0, e->GetCost());
    00207   SetDParam(1, e->GetDisplayMaxSpeed());
    00208   SetDParam(2, e->GetDefaultCargoType());
    00209   SetDParam(3, e->GetDisplayDefaultCapacity());
    00210   SetDParam(4, e->GetRunningCost());
    00211   return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
    00212 }
    00213
    00214
    00221 StringID GetEngineInfoString(EngineID engine)
    00222 {
    00223   const Engine *e = Engine::Get(engine);
    00224
    00225   switch (e->type) {
    00226     case VEH_TRAIN:
    00227       return GetTrainEngineInfoString(e);
    00228
    00229     case VEH_ROAD:
    00230       return GetRoadVehEngineInfoString(e);
    00231
    00232     case VEH_SHIP:
    00233       return GetShipEngineInfoString(e);
    00234
    00235     case VEH_AIRCRAFT:
    00236       return GetAircraftEngineInfoString(e);
    00237
    00238     default: NOT_REACHED();
    00239   }
    00240 }
    00241
    00251 void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
    00252 {
    00253   const Engine *e = Engine::Get(engine);
    00254
    00255   switch (e->type) {
    00256     case VEH_TRAIN:
    00257       DrawTrainEngine(left, right, preferred_x, y, engine, pal);
    00258       break;
    00259
    00260     case VEH_ROAD:
    00261       DrawRoadVehEngine(left, right, preferred_x, y, engine, pal);
    00262       break;
    00263
    00264     case VEH_SHIP:
    00265       DrawShipEngine(left, right, preferred_x, y, engine, pal);
    00266       break;
    00267
    00268     case VEH_AIRCRAFT:
    00269       DrawAircraftEngine(left, right, preferred_x, y, engine, pal);
    00270       break;
    00271
    00272     default: NOT_REACHED();
    00273   }
    00274 }
    00275
    00280 void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
    00281 {
    00282   uint size = el->Length();
    00283   /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
    00284    * generally, do not sort if there are less than 2 items */
    00285   if (size < 2) return;
    00286   QSortT(el->Begin(), size, compare);
    00287 }
    00288
    00295 void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
    00296 {
    00297   if (num_items < 2) return;
    00298   assert(begin < el->Length());
    00299   assert(begin + num_items <= el->Length());
    00300   QSortT(el->Get(begin), num_items, compare);
    00301 }
    00302