00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "window_gui.h"
00014 #include "command_func.h"
00015 #include "train.h"
00016 #include "strings_func.h"
00017 #include "vehicle_func.h"
00018
00019 #include "table/strings.h"
00020
00028 void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00029 {
00030 if (result.Failed()) return;
00031
00032
00033 const Vehicle *found = NULL;
00034 const Train *t;
00035 FOR_ALL_TRAINS(t) {
00036 if (t->IsFrontEngine() && t->tile == tile &&
00037 t->track == TRACK_BIT_DEPOT) {
00038 if (found != NULL) return;
00039 found = t;
00040 }
00041 }
00042
00043
00044 if (found != NULL) {
00045 found = found->Last();
00046
00047 DoCommandP(0, _new_vehicle_id, found->index, CMD_MOVE_RAIL_VEHICLE);
00048 InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
00049 }
00050 }
00051
00059 static int HighlightDragPosition(int px, int max_width, VehicleID selection)
00060 {
00061 bool rtl = _current_text_dir == TD_RTL;
00062
00063 assert(selection != INVALID_VEHICLE);
00064 Point offset;
00065 int dragged_width = Train::Get(selection)->GetDisplayImageWidth(&offset) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00066
00067 int drag_hlight_left = rtl ? max(px -dragged_width, 0) : px;
00068 int drag_hlight_right = rtl ? px : min(px + dragged_width, max_width);
00069 int drag_hlight_width = max(drag_hlight_right - drag_hlight_left, 0);
00070
00071 if (drag_hlight_width > 0) {
00072 GfxFillRect(drag_hlight_left + WD_FRAMERECT_LEFT, WD_FRAMERECT_TOP + 1,
00073 drag_hlight_right - WD_FRAMERECT_RIGHT, 13 - WD_FRAMERECT_BOTTOM, _colour_gradient[COLOUR_GREY][7]);
00074 }
00075
00076 return drag_hlight_width;
00077 }
00078
00089 void DrawTrainImage(const Train *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip, VehicleID drag_dest)
00090 {
00091 bool rtl = _current_text_dir == TD_RTL;
00092 Direction dir = rtl ? DIR_E : DIR_W;
00093
00094 DrawPixelInfo tmp_dpi, *old_dpi;
00095
00096 int highlight_l = 0;
00097 int highlight_r = 0;
00098 int max_width = right - left + 1;
00099
00100 if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, 14)) return;
00101
00102 old_dpi = _cur_dpi;
00103 _cur_dpi = &tmp_dpi;
00104
00105 int px = rtl ? max_width + skip : -skip;
00106 bool sel_articulated = false;
00107 bool dragging = (drag_dest != INVALID_VEHICLE);
00108 bool drag_at_end_of_train = (drag_dest == v->index);
00109 for (; v != NULL && (rtl ? px > 0 : px < max_width); v = v->Next()) {
00110 if (dragging && !drag_at_end_of_train && drag_dest == v->index) {
00111
00112 int drag_hlight_width = HighlightDragPosition(px, max_width, selection);
00113 px += rtl ? -drag_hlight_width : drag_hlight_width;
00114 }
00115
00116 Point offset;
00117 int width = Train::From(v)->GetDisplayImageWidth(&offset);
00118
00119 if (rtl ? px + width > 0 : px - width < max_width) {
00120 PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
00121 DrawSprite(v->GetImage(dir, image_type), pal, px + (rtl ? -offset.x : offset.x), 7 + offset.y);
00122 }
00123
00124 if (!v->IsArticulatedPart()) sel_articulated = false;
00125
00126 if (v->index == selection) {
00127
00128 highlight_l = rtl ? px - width : px;
00129 highlight_r = rtl ? px - 1 : px + width - 1;
00130 sel_articulated = true;
00131 } else if ((_cursor.vehchain && highlight_r != 0) || sel_articulated) {
00132 if (rtl) {
00133 highlight_l -= width;
00134 } else {
00135 highlight_r += width;
00136 }
00137 }
00138
00139 px += rtl ? -width : width;
00140 }
00141
00142 if (dragging && drag_at_end_of_train) {
00143
00144 HighlightDragPosition(px, max_width, selection);
00145 }
00146
00147 if (highlight_l != highlight_r) {
00148
00149
00150 DrawFrameRect(highlight_l, 0, highlight_r, 13, COLOUR_WHITE, FR_BORDERONLY);
00151 }
00152
00153 _cur_dpi = old_dpi;
00154 }
00155
00157 struct CargoSummaryItem {
00158 CargoID cargo;
00159 StringID subtype;
00160 uint capacity;
00161 uint amount;
00162 StationID source;
00163
00165 inline bool operator != (const CargoSummaryItem &other) const
00166 {
00167 return this->cargo != other.cargo || this->subtype != other.subtype;
00168 }
00169 };
00170
00171 static const uint TRAIN_DETAILS_MIN_INDENT = 32;
00172 static const uint TRAIN_DETAILS_MAX_INDENT = 72;
00173
00175 typedef SmallVector<CargoSummaryItem, 2> CargoSummary;
00177 static CargoSummary _cargo_summary;
00178
00187 static void TrainDetailsCargoTab(const CargoSummaryItem *item, int left, int right, int y)
00188 {
00189 StringID str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
00190
00191 if (item->amount > 0) {
00192 SetDParam(0, item->cargo);
00193 SetDParam(1, item->amount);
00194 SetDParam(2, item->source);
00195 SetDParam(3, _settings_game.vehicle.freight_trains);
00196 str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
00197 }
00198
00199 DrawString(left, right, y, str);
00200 }
00201
00210 static void TrainDetailsInfoTab(const Vehicle *v, int left, int right, int y)
00211 {
00212 if (RailVehInfo(v->engine_type)->railveh_type == RAILVEH_WAGON) {
00213 SetDParam(0, v->engine_type);
00214 SetDParam(1, v->value);
00215 DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00216 } else {
00217 SetDParam(0, v->engine_type);
00218 SetDParam(1, v->build_year);
00219 SetDParam(2, v->value);
00220 DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00221 }
00222 }
00223
00232 static void TrainDetailsCapacityTab(const CargoSummaryItem *item, int left, int right, int y)
00233 {
00234 SetDParam(0, item->cargo);
00235 SetDParam(1, item->capacity);
00236 SetDParam(4, item->subtype);
00237 SetDParam(5, _settings_game.vehicle.freight_trains);
00238 DrawString(left, right, y, FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY);
00239 }
00240
00246 static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *summary)
00247 {
00248 summary->Clear();
00249 do {
00250 if (v->cargo_cap == 0) continue;
00251
00252 CargoSummaryItem new_item;
00253 new_item.cargo = v->cargo_type;
00254 new_item.subtype = GetCargoSubtypeText(v);
00255
00256 CargoSummaryItem *item = summary->Find(new_item);
00257 if (item == summary->End()) {
00258 item = summary->Append();
00259 item->cargo = new_item.cargo;
00260 item->subtype = new_item.subtype;
00261 item->capacity = 0;
00262 item->amount = 0;
00263 item->source = INVALID_STATION;
00264 }
00265
00266 item->capacity += v->cargo_cap;
00267 item->amount += v->cargo.Count();
00268 if (item->source == INVALID_STATION) item->source = v->cargo.Source();
00269 } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
00270 }
00271
00277 static uint GetLengthOfArticulatedVehicle(const Train *v)
00278 {
00279 uint length = 0;
00280
00281 do {
00282 length += v->GetDisplayImageWidth();
00283 } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
00284
00285 return length;
00286 }
00287
00294 int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
00295 {
00296 int num = 0;
00297
00298 if (det_tab == TDW_TAB_TOTALS) {
00299 CargoArray act_cargo;
00300 CargoArray max_cargo;
00301 for (const Vehicle *v = Vehicle::Get(veh_id); v != NULL; v = v->Next()) {
00302 act_cargo[v->cargo_type] += v->cargo.Count();
00303 max_cargo[v->cargo_type] += v->cargo_cap;
00304 }
00305
00306
00307
00308
00309 for (CargoID i = 0; i < NUM_CARGO; i++) {
00310 if (max_cargo[i] > 0) num++;
00311 }
00312 num++;
00313 } else {
00314 for (const Train *v = Train::Get(veh_id); v != NULL; v = v->GetNextVehicle()) {
00315 GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
00316 num += max(1u, _cargo_summary.Length());
00317
00318 uint length = GetLengthOfArticulatedVehicle(v);
00319 if (length > TRAIN_DETAILS_MAX_INDENT) num++;
00320 }
00321 }
00322
00323 return num;
00324 }
00325
00337 void DrawTrainDetails(const Train *v, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, TrainDetailsWindowTabs det_tab)
00338 {
00339
00340 if (det_tab != TDW_TAB_TOTALS) {
00341 bool rtl = _current_text_dir == TD_RTL;
00342 Direction dir = rtl ? DIR_E : DIR_W;
00343 int x = rtl ? right : left;
00344 int sprite_y_offset = 4 + (FONT_HEIGHT_NORMAL - 10) / 2;
00345 int line_height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00346 for (; v != NULL && vscroll_pos > -vscroll_cap; v = v->GetNextVehicle()) {
00347 GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
00348
00349
00350 uint dx = 0;
00351 int px = x;
00352 const Train *u = v;
00353 do {
00354 Point offset;
00355 int width = u->GetDisplayImageWidth(&offset);
00356 if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
00357 PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
00358 DrawSprite(u->GetImage(dir, EIT_IN_DETAILS), pal, px + (rtl ? -offset.x : offset.x), y - line_height * vscroll_pos + sprite_y_offset + offset.y);
00359 }
00360 px += rtl ? -width : width;
00361 dx += width;
00362 u = u->Next();
00363 } while (u != NULL && u->IsArticulatedPart());
00364
00365 bool separate_sprite_row = (dx > TRAIN_DETAILS_MAX_INDENT);
00366 if (separate_sprite_row) {
00367 vscroll_pos--;
00368 dx = 0;
00369 }
00370
00371 uint num_lines = max(1u, _cargo_summary.Length());
00372 for (uint i = 0; i < num_lines; i++) {
00373 int sprite_width = max<int>(dx, TRAIN_DETAILS_MIN_INDENT) + 3;
00374 int data_left = left + (rtl ? 0 : sprite_width);
00375 int data_right = right - (rtl ? sprite_width : 0);
00376 if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
00377 int py = y - line_height * vscroll_pos;
00378 if (i > 0 || separate_sprite_row) {
00379 if (vscroll_pos != 0) GfxFillRect(left, py - WD_MATRIX_TOP - 1, right, py - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][5]);
00380 }
00381 switch (det_tab) {
00382 case TDW_TAB_CARGO:
00383 if (i < _cargo_summary.Length()) {
00384 TrainDetailsCargoTab(&_cargo_summary[i], data_left, data_right, py);
00385 } else {
00386 DrawString(data_left, data_right, py, STR_QUANTITY_N_A, TC_LIGHT_BLUE);
00387 }
00388 break;
00389
00390 case TDW_TAB_INFO:
00391 if (i == 0) TrainDetailsInfoTab(v, data_left, data_right, py);
00392 break;
00393
00394 case TDW_TAB_CAPACITY:
00395 if (i < _cargo_summary.Length()) {
00396 TrainDetailsCapacityTab(&_cargo_summary[i], data_left, data_right, py);
00397 } else {
00398 DrawString(data_left, data_right, py, STR_VEHICLE_INFO_NO_CAPACITY);
00399 }
00400 break;
00401
00402 default: NOT_REACHED();
00403 }
00404 }
00405 vscroll_pos--;
00406 }
00407 }
00408 } else {
00409 CargoArray act_cargo;
00410 CargoArray max_cargo;
00411 Money feeder_share = 0;
00412
00413 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
00414 act_cargo[u->cargo_type] += u->cargo.Count();
00415 max_cargo[u->cargo_type] += u->cargo_cap;
00416 feeder_share += u->cargo.FeederShare();
00417 }
00418
00419
00420 DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT);
00421 y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00422
00423 for (CargoID i = 0; i < NUM_CARGO; i++) {
00424 if (max_cargo[i] > 0 && --vscroll_pos < 0 && vscroll_pos > -vscroll_cap) {
00425 SetDParam(0, i);
00426 SetDParam(1, act_cargo[i]);
00427 SetDParam(2, i);
00428 SetDParam(3, max_cargo[i]);
00429 SetDParam(4, _settings_game.vehicle.freight_trains);
00430 DrawString(left, right, y, FreightWagonMult(i) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT : STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY);
00431 y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00432 }
00433 }
00434 SetDParam(0, feeder_share);
00435 DrawString(left, right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
00436 }
00437 }