roadveh_gui.cpp

Go to the documentation of this file.
00001 /* $Id: roadveh_gui.cpp 23740 2012-01-03 21:32:51Z 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 "roadveh.h"
00014 #include "window_gui.h"
00015 #include "strings_func.h"
00016 #include "vehicle_func.h"
00017 #include "string_func.h"
00018 
00019 #include "table/strings.h"
00020 
00029 void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y)
00030 {
00031   uint y_offset = v->HasArticulatedPart() ? 15 : 0; // Draw the first line below the sprite of an articulated RV instead of after it.
00032   StringID str;
00033   Money feeder_share = 0;
00034 
00035   SetDParam(0, v->engine_type);
00036   SetDParam(1, v->build_year);
00037   SetDParam(2, v->value);
00038   DrawString(left, right, y + y_offset, STR_VEHICLE_INFO_BUILT_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00039 
00040   if (v->HasArticulatedPart()) {
00041     CargoArray max_cargo;
00042     StringID subtype_text[NUM_CARGO];
00043     char capacity[512];
00044 
00045     memset(subtype_text, 0, sizeof(subtype_text));
00046 
00047     for (const Vehicle *u = v; u != NULL; u = u->Next()) {
00048       max_cargo[u->cargo_type] += u->cargo_cap;
00049       if (u->cargo_cap > 0) {
00050         StringID text = GetCargoSubtypeText(u);
00051         if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
00052       }
00053     }
00054 
00055     GetString(capacity, STR_VEHICLE_DETAILS_TRAIN_ARTICULATED_RV_CAPACITY, lastof(capacity));
00056 
00057     bool first = true;
00058     for (CargoID i = 0; i < NUM_CARGO; i++) {
00059       if (max_cargo[i] > 0) {
00060         char buffer[128];
00061 
00062         SetDParam(0, i);
00063         SetDParam(1, max_cargo[i]);
00064         GetString(buffer, STR_JUST_CARGO, lastof(buffer));
00065 
00066         if (!first) strecat(capacity, ", ", lastof(capacity));
00067         strecat(capacity, buffer, lastof(capacity));
00068 
00069         if (subtype_text[i] != 0) {
00070           GetString(buffer, subtype_text[i], lastof(buffer));
00071           strecat(capacity, buffer, lastof(capacity));
00072         }
00073 
00074         first = false;
00075       }
00076     }
00077 
00078     DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, capacity, TC_BLUE);
00079 
00080     for (const Vehicle *u = v; u != NULL; u = u->Next()) {
00081       if (u->cargo_cap == 0) continue;
00082 
00083       str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
00084       if (!u->cargo.Empty()) {
00085         SetDParam(0, u->cargo_type);
00086         SetDParam(1, u->cargo.Count());
00087         SetDParam(2, u->cargo.Source());
00088         str = STR_VEHICLE_DETAILS_CARGO_FROM;
00089         feeder_share += u->cargo.FeederShare();
00090       }
00091       DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
00092 
00093       y_offset += FONT_HEIGHT_NORMAL + 1;
00094     }
00095 
00096     y_offset -= FONT_HEIGHT_NORMAL + 1;
00097   } else {
00098     SetDParam(0, v->cargo_type);
00099     SetDParam(1, v->cargo_cap);
00100     SetDParam(4, GetCargoSubtypeText(v));
00101     DrawString(left, right, y + FONT_HEIGHT_NORMAL + y_offset, STR_VEHICLE_INFO_CAPACITY);
00102 
00103     str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
00104     if (!v->cargo.Empty()) {
00105       SetDParam(0, v->cargo_type);
00106       SetDParam(1, v->cargo.Count());
00107       SetDParam(2, v->cargo.Source());
00108       str = STR_VEHICLE_DETAILS_CARGO_FROM;
00109       feeder_share += v->cargo.FeederShare();
00110     }
00111     DrawString(left, right, y + 2 * FONT_HEIGHT_NORMAL + 1 + y_offset, str);
00112   }
00113 
00114   /* Draw Transfer credits text */
00115   SetDParam(0, feeder_share);
00116   DrawString(left, right, y + 3 * FONT_HEIGHT_NORMAL + 3 + y_offset, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
00117 }
00118 
00128 void DrawRoadVehImage(const Vehicle *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip)
00129 {
00130   bool rtl = _current_text_dir == TD_RTL;
00131   Direction dir = rtl ? DIR_E : DIR_W;
00132   const RoadVehicle *u = RoadVehicle::From(v);
00133 
00134   DrawPixelInfo tmp_dpi, *old_dpi;
00135   int max_width = right - left + 1;
00136 
00137   if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, 14)) return;
00138 
00139   old_dpi = _cur_dpi;
00140   _cur_dpi = &tmp_dpi;
00141 
00142   int px = rtl ? max_width + skip : -skip;
00143   for (; u != NULL && (rtl ? px > 0 : px < max_width); u = u->Next()) {
00144     Point offset;
00145     int width = u->GetDisplayImageWidth(&offset);
00146 
00147     if (rtl ? px + width > 0 : px - width < max_width) {
00148       PaletteID pal = (u->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(u);
00149       DrawSprite(u->GetImage(dir, image_type), pal, px + (rtl ? -offset.x : offset.x), 6 + offset.y);
00150     }
00151 
00152     px += rtl ? -width : width;
00153   }
00154 
00155   if (v->index == selection) {
00156     DrawFrameRect((rtl ? px : left) - 1, y - 1, (rtl ? px : right) - 1, y + 12, COLOUR_WHITE, FR_BORDERONLY);
00157   }
00158 
00159   _cur_dpi = old_dpi;
00160 }