roadveh_gui.cpp

Go to the documentation of this file.
00001 /* $Id: roadveh_gui.cpp 15480 2009-02-14 18:42:03Z michi_cc $ */
00002 
00005 #include "stdafx.h"
00006 #include "roadveh.h"
00007 #include "window_gui.h"
00008 #include "gfx_func.h"
00009 #include "vehicle_gui.h"
00010 #include "strings_func.h"
00011 #include "vehicle_func.h"
00012 #include "string_func.h"
00013 
00014 #include "table/sprites.h"
00015 #include "table/strings.h"
00016 
00017 void DrawRoadVehDetails(const Vehicle *v, int x, int y)
00018 {
00019   uint y_offset = RoadVehHasArticPart(v) ? 15 : 0;
00020   StringID str;
00021   Money feeder_share = 0;
00022 
00023   SetDParam(0, v->engine_type);
00024   SetDParam(1, v->build_year);
00025   SetDParam(2, v->value);
00026   DrawString(x, y + y_offset, STR_9011_BUILT_VALUE, TC_FROMSTRING);
00027 
00028   if (RoadVehHasArticPart(v)) {
00029     AcceptedCargo max_cargo;
00030     StringID subtype_text[NUM_CARGO];
00031     char capacity[512];
00032 
00033     memset(max_cargo, 0, sizeof(max_cargo));
00034     memset(subtype_text, 0, sizeof(subtype_text));
00035 
00036     for (const Vehicle *u = v; u != NULL; u = u->Next()) {
00037       max_cargo[u->cargo_type] += u->cargo_cap;
00038       if (u->cargo_cap > 0) {
00039         StringID text = GetCargoSubtypeText(u);
00040         if (text != STR_EMPTY) subtype_text[u->cargo_type] = text;
00041       }
00042     }
00043 
00044     GetString(capacity, STR_ARTICULATED_RV_CAPACITY, lastof(capacity));
00045 
00046     bool first = true;
00047     for (CargoID i = 0; i < NUM_CARGO; i++) {
00048       if (max_cargo[i] > 0) {
00049         char buffer[128];
00050 
00051         SetDParam(0, i);
00052         SetDParam(1, max_cargo[i]);
00053         GetString(buffer, STR_BARE_CARGO, lastof(buffer));
00054 
00055         if (!first) strecat(capacity, ", ", lastof(capacity));
00056         strecat(capacity, buffer, lastof(capacity));
00057 
00058         if (subtype_text[i] != 0) {
00059           GetString(buffer, subtype_text[i], lastof(buffer));
00060           strecat(capacity, buffer, lastof(capacity));
00061         }
00062 
00063         first = false;
00064       }
00065     }
00066 
00067     SetDParamStr(0, capacity);
00068     DrawStringTruncated(x, y + 10 + y_offset, STR_JUST_RAW_STRING, TC_BLUE, 380 - x);
00069 
00070     for (const Vehicle *u = v; u != NULL; u = u->Next()) {
00071       if (u->cargo_cap == 0) continue;
00072 
00073       str = STR_8812_EMPTY;
00074       if (!u->cargo.Empty()) {
00075         SetDParam(0, u->cargo_type);
00076         SetDParam(1, u->cargo.Count());
00077         SetDParam(2, u->cargo.Source());
00078         str = STR_8813_FROM;
00079         feeder_share += u->cargo.FeederShare();
00080       }
00081       DrawString(x, y + 21 + y_offset, str, TC_FROMSTRING);
00082 
00083       y_offset += 11;
00084     }
00085 
00086     y_offset -= 11;
00087   } else {
00088     SetDParam(0, v->cargo_type);
00089     SetDParam(1, v->cargo_cap);
00090     SetDParam(2, GetCargoSubtypeText(v));
00091     DrawString(x, y + 10 + y_offset, STR_9012_CAPACITY, TC_FROMSTRING);
00092 
00093     str = STR_8812_EMPTY;
00094     if (!v->cargo.Empty()) {
00095       SetDParam(0, v->cargo_type);
00096       SetDParam(1, v->cargo.Count());
00097       SetDParam(2, v->cargo.Source());
00098       str = STR_8813_FROM;
00099       feeder_share += v->cargo.FeederShare();
00100     }
00101     DrawString(x, y + 21 + y_offset, str, TC_FROMSTRING);
00102   }
00103 
00104   /* Draw Transfer credits text */
00105   SetDParam(0, feeder_share);
00106   DrawString(x, y + 33 + y_offset, STR_FEEDER_CARGO_VALUE, TC_FROMSTRING);
00107 }
00108 
00109 
00110 static inline int RoadVehLengthToPixels(int length)
00111 {
00112   return (length * 28) / 8;
00113 }
00114 
00115 void DrawRoadVehImage(const Vehicle *v, int x, int y, VehicleID selection, int count)
00116 {
00117   /* Road vehicle lengths are measured in eighths of the standard length, so
00118    * count is the number of standard vehicles that should be drawn. If it is
00119    * 0, we draw enough vehicles for 10 standard vehicle lengths. */
00120   int max_length = (count == 0) ? 80 : count * 8;
00121 
00122   /* Width of highlight box */
00123   int highlight_w = 0;
00124 
00125   for (int dx = 0; v != NULL && dx < max_length ; v = v->Next()) {
00126     int width = v->u.road.cached_veh_length;
00127 
00128     if (dx + width > 0 && dx <= max_length) {
00129       SpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
00130       DrawSprite(v->GetImage(DIR_W), pal, x + 14 + RoadVehLengthToPixels(dx), y + 6);
00131 
00132       if (v->index == selection) {
00133         /* Set the highlight position */
00134         highlight_w = RoadVehLengthToPixels(width);
00135       } else if (_cursor.vehchain && highlight_w != 0) {
00136         highlight_w += RoadVehLengthToPixels(width);
00137       }
00138     }
00139 
00140     dx += width;
00141   }
00142 
00143   if (highlight_w != 0) {
00144     DrawFrameRect(x - 1, y - 1, x - 1 + highlight_w, y + 12, COLOUR_WHITE, FR_BORDERONLY);
00145   }
00146 }
00147 
00148 void CcBuildRoadVeh(bool success, TileIndex tile, uint32 p1, uint32 p2)
00149 {
00150   const Vehicle *v;
00151 
00152   if (!success) return;
00153 
00154   v = GetVehicle(_new_vehicle_id);
00155   if (v->tile == _backup_orders_tile) {
00156     _backup_orders_tile = 0;
00157     RestoreVehicleOrders(v);
00158   }
00159   ShowVehicleViewWindow(v);
00160 }

Generated on Sun Nov 15 15:40:14 2009 for OpenTTD by  doxygen 1.5.6