engine_gui.cpp

Go to the documentation of this file.
00001 /* $Id: engine_gui.cpp 25290 2013-05-26 19:25:01Z 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 "engine_base.h"
00015 #include "command_func.h"
00016 #include "strings_func.h"
00017 #include "engine_gui.h"
00018 #include "articulated_vehicles.h"
00019 #include "vehicle_func.h"
00020 #include "company_func.h"
00021 #include "rail.h"
00022 #include "settings_type.h"
00023 
00024 #include "widgets/engine_widget.h"
00025 
00026 #include "table/strings.h"
00027 
00033 StringID GetEngineCategoryName(EngineID engine)
00034 {
00035   const Engine *e = Engine::Get(engine);
00036   switch (e->type) {
00037     default: NOT_REACHED();
00038     case VEH_ROAD:              return STR_ENGINE_PREVIEW_ROAD_VEHICLE;
00039     case VEH_AIRCRAFT:          return STR_ENGINE_PREVIEW_AIRCRAFT;
00040     case VEH_SHIP:              return STR_ENGINE_PREVIEW_SHIP;
00041     case VEH_TRAIN:
00042       return GetRailTypeInfo(e->u.rail.railtype)->strings.new_loco;
00043   }
00044 }
00045 
00046 static const NWidgetPart _nested_engine_preview_widgets[] = {
00047   NWidget(NWID_HORIZONTAL),
00048     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
00049     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE), SetDataTip(STR_ENGINE_PREVIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00050   EndContainer(),
00051   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
00052     NWidget(WWT_EMPTY, INVALID_COLOUR, WID_EP_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
00053     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
00054       NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_NO), SetDataTip(STR_QUIT_NO, STR_NULL), SetFill(1, 0),
00055       NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_EP_YES), SetDataTip(STR_QUIT_YES, STR_NULL), SetFill(1, 0),
00056     EndContainer(),
00057     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
00058   EndContainer(),
00059 };
00060 
00061 struct EnginePreviewWindow : Window {
00062   static const int VEHICLE_SPACE = 40; // The space to show the vehicle image
00063 
00064   EnginePreviewWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
00065   {
00066     this->InitNested(window_number);
00067 
00068     /* There is no way to recover the window; so disallow closure via DEL; unless SHIFT+DEL */
00069     this->flags |= WF_STICKY;
00070   }
00071 
00072   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00073   {
00074     if (widget != WID_EP_QUESTION) return;
00075 
00076     EngineID engine = this->window_number;
00077     SetDParam(0, GetEngineCategoryName(engine));
00078     size->height = GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, size->width) + WD_PAR_VSEP_WIDE + FONT_HEIGHT_NORMAL + VEHICLE_SPACE;
00079     SetDParam(0, engine);
00080     size->height += GetStringHeight(GetEngineInfoString(engine), size->width);
00081   }
00082 
00083   virtual void DrawWidget(const Rect &r, int widget) const
00084   {
00085     if (widget != WID_EP_QUESTION) return;
00086 
00087     EngineID engine = this->window_number;
00088     SetDParam(0, GetEngineCategoryName(engine));
00089     int y = r.top + GetStringHeight(STR_ENGINE_PREVIEW_MESSAGE, r.right - r.top + 1);
00090     y = DrawStringMultiLine(r.left, r.right, r.top, y, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_CENTER) + WD_PAR_VSEP_WIDE;
00091 
00092     SetDParam(0, engine);
00093     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_ENGINE_NAME, TC_BLACK, SA_HOR_CENTER);
00094     y += FONT_HEIGHT_NORMAL;
00095 
00096     DrawVehicleEngine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, this->width >> 1, y + VEHICLE_SPACE / 2, engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW);
00097 
00098     y += VEHICLE_SPACE;
00099     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER);
00100   }
00101 
00102   virtual void OnClick(Point pt, int widget, int click_count)
00103   {
00104     switch (widget) {
00105       case WID_EP_YES:
00106         DoCommandP(0, this->window_number, 0, CMD_WANT_ENGINE_PREVIEW);
00107         /* FALL THROUGH */
00108       case WID_EP_NO:
00109         delete this;
00110         break;
00111     }
00112   }
00113 
00114   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00115   {
00116     if (!gui_scope) return;
00117 
00118     EngineID engine = this->window_number;
00119     if (Engine::Get(engine)->preview_company != _local_company) delete this;
00120   }
00121 };
00122 
00123 static WindowDesc _engine_preview_desc(
00124   WDP_CENTER, "engine_preview", 0, 0,
00125   WC_ENGINE_PREVIEW, WC_NONE,
00126   WDF_CONSTRUCTION,
00127   _nested_engine_preview_widgets, lengthof(_nested_engine_preview_widgets)
00128 );
00129 
00130 
00131 void ShowEnginePreviewWindow(EngineID engine)
00132 {
00133   AllocateWindowDescFront<EnginePreviewWindow>(&_engine_preview_desc, engine);
00134 }
00135 
00141 uint GetTotalCapacityOfArticulatedParts(EngineID engine)
00142 {
00143   uint total = 0;
00144 
00145   CargoArray cap = GetCapacityOfArticulatedParts(engine);
00146   for (CargoID c = 0; c < NUM_CARGO; c++) {
00147     total += cap[c];
00148   }
00149 
00150   return total;
00151 }
00152 
00153 static StringID GetTrainEngineInfoString(const Engine *e)
00154 {
00155   SetDParam(0, e->GetCost());
00156   SetDParam(2, e->GetDisplayMaxSpeed());
00157   SetDParam(3, e->GetPower());
00158   SetDParam(1, e->GetDisplayWeight());
00159   SetDParam(7, e->GetDisplayMaxTractiveEffort());
00160 
00161   SetDParam(4, e->GetRunningCost());
00162 
00163   uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
00164   if (capacity != 0) {
00165     SetDParam(5, e->GetDefaultCargoType());
00166     SetDParam(6, capacity);
00167   } else {
00168     SetDParam(5, CT_INVALID);
00169   }
00170   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;
00171 }
00172 
00173 static StringID GetAircraftEngineInfoString(const Engine *e)
00174 {
00175   CargoID cargo = e->GetDefaultCargoType();
00176   uint16 mail_capacity;
00177   uint capacity = e->GetDisplayDefaultCapacity(&mail_capacity);
00178   uint16 range = e->GetRange();
00179 
00180   uint i = 0;
00181   SetDParam(i++, e->GetCost());
00182   SetDParam(i++, e->GetDisplayMaxSpeed());
00183   if (range > 0) SetDParam(i++, range);
00184   SetDParam(i++, cargo);
00185   SetDParam(i++, capacity);
00186 
00187   if (mail_capacity > 0) {
00188     SetDParam(i++, CT_MAIL);
00189     SetDParam(i++, mail_capacity);
00190     SetDParam(i++, e->GetRunningCost());
00191     return range > 0 ? STR_ENGINE_PREVIEW_COST_MAX_SPEED_RANGE_CAPACITY_CAPACITY_RUNCOST : STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_CAPACITY_RUNCOST;
00192   } else {
00193     SetDParam(i++, e->GetRunningCost());
00194     return range > 0 ? STR_ENGINE_PREVIEW_COST_MAX_SPEED_RANGE_CAPACITY_RUNCOST : STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
00195   }
00196 }
00197 
00198 static StringID GetRoadVehEngineInfoString(const Engine *e)
00199 {
00200   if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) {
00201     SetDParam(0, e->GetCost());
00202     SetDParam(1, e->GetDisplayMaxSpeed());
00203     uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
00204     if (capacity != 0) {
00205       SetDParam(2, e->GetDefaultCargoType());
00206       SetDParam(3, capacity);
00207     } else {
00208       SetDParam(2, CT_INVALID);
00209     }
00210     SetDParam(4, e->GetRunningCost());
00211     return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
00212   } else {
00213     SetDParam(0, e->GetCost());
00214     SetDParam(2, e->GetDisplayMaxSpeed());
00215     SetDParam(3, e->GetPower());
00216     SetDParam(1, e->GetDisplayWeight());
00217     SetDParam(7, e->GetDisplayMaxTractiveEffort());
00218 
00219     SetDParam(4, e->GetRunningCost());
00220 
00221     uint capacity = GetTotalCapacityOfArticulatedParts(e->index);
00222     if (capacity != 0) {
00223       SetDParam(5, e->GetDefaultCargoType());
00224       SetDParam(6, capacity);
00225     } else {
00226       SetDParam(5, CT_INVALID);
00227     }
00228     return STR_ENGINE_PREVIEW_COST_WEIGHT_SPEED_POWER_MAX_TE;
00229   }
00230 }
00231 
00232 static StringID GetShipEngineInfoString(const Engine *e)
00233 {
00234   SetDParam(0, e->GetCost());
00235   SetDParam(1, e->GetDisplayMaxSpeed());
00236   SetDParam(2, e->GetDefaultCargoType());
00237   SetDParam(3, e->GetDisplayDefaultCapacity());
00238   SetDParam(4, e->GetRunningCost());
00239   return STR_ENGINE_PREVIEW_COST_MAX_SPEED_CAPACITY_RUNCOST;
00240 }
00241 
00242 
00249 StringID GetEngineInfoString(EngineID engine)
00250 {
00251   const Engine *e = Engine::Get(engine);
00252 
00253   switch (e->type) {
00254     case VEH_TRAIN:
00255       return GetTrainEngineInfoString(e);
00256 
00257     case VEH_ROAD:
00258       return GetRoadVehEngineInfoString(e);
00259 
00260     case VEH_SHIP:
00261       return GetShipEngineInfoString(e);
00262 
00263     case VEH_AIRCRAFT:
00264       return GetAircraftEngineInfoString(e);
00265 
00266     default: NOT_REACHED();
00267   }
00268 }
00269 
00279 void DrawVehicleEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
00280 {
00281   const Engine *e = Engine::Get(engine);
00282 
00283   switch (e->type) {
00284     case VEH_TRAIN:
00285       DrawTrainEngine(left, right, preferred_x, y, engine, pal, image_type);
00286       break;
00287 
00288     case VEH_ROAD:
00289       DrawRoadVehEngine(left, right, preferred_x, y, engine, pal, image_type);
00290       break;
00291 
00292     case VEH_SHIP:
00293       DrawShipEngine(left, right, preferred_x, y, engine, pal, image_type);
00294       break;
00295 
00296     case VEH_AIRCRAFT:
00297       DrawAircraftEngine(left, right, preferred_x, y, engine, pal, image_type);
00298       break;
00299 
00300     default: NOT_REACHED();
00301   }
00302 }
00303 
00309 void EngList_Sort(GUIEngineList *el, EngList_SortTypeFunction compare)
00310 {
00311   uint size = el->Length();
00312   /* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
00313    * generally, do not sort if there are less than 2 items */
00314   if (size < 2) return;
00315   QSortT(el->Begin(), size, compare);
00316 }
00317 
00325 void EngList_SortPartial(GUIEngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
00326 {
00327   if (num_items < 2) return;
00328   assert(begin < el->Length());
00329   assert(begin + num_items <= el->Length());
00330   QSortT(el->Get(begin), num_items, compare);
00331 }
00332