# train_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "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   /* find a locomotive in the depot. */
    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; // must be exactly one.
    00039       found = t;
    00040     }
    00041   }
    00042
    00043   /* if we found a loco, */
    00044   if (found != NULL) {
    00045     found = found->Last();
    00046     /* put the new wagon at the end of the loco. */
    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   int dragged_width = WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00065   for (Train *t = Train::Get(selection); t != NULL; t = t->HasArticulatedPart() ? t->GetNextArticulatedPart() : NULL) {
    00066     dragged_width += t->GetDisplayImageWidth(NULL);
    00067   }
    00068
    00069   int drag_hlight_left = rtl ? max(px -dragged_width, 0) : px;
    00070   int drag_hlight_right = rtl ? px : min(px + dragged_width, max_width);
    00071   int drag_hlight_width = max(drag_hlight_right - drag_hlight_left, 0);
    00072
    00073   if (drag_hlight_width > 0) {
    00074     GfxFillRect(drag_hlight_left + WD_FRAMERECT_LEFT, WD_FRAMERECT_TOP + 1,
    00075         drag_hlight_right - WD_FRAMERECT_RIGHT, 13 - WD_FRAMERECT_BOTTOM, _colour_gradient[COLOUR_GREY][7]);
    00076   }
    00077
    00078   return drag_hlight_width;
    00079 }
    00080
    00091 void DrawTrainImage(const Train *v, int left, int right, int y, VehicleID selection, EngineImageType image_type, int skip, VehicleID drag_dest)
    00092 {
    00093   bool rtl = _current_text_dir == TD_RTL;
    00094   Direction dir = rtl ? DIR_E : DIR_W;
    00095
    00096   DrawPixelInfo tmp_dpi, *old_dpi;
    00097   /* Position of highlight box */
    00098   int highlight_l = 0;
    00099   int highlight_r = 0;
    00100   int max_width = right - left + 1;
    00101
    00102   if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, 14)) return;
    00103
    00104   old_dpi = _cur_dpi;
    00105   _cur_dpi = &tmp_dpi;
    00106
    00107   int px = rtl ? max_width + skip : -skip;
    00108   bool sel_articulated = false;
    00109   bool dragging = (drag_dest != INVALID_VEHICLE);
    00110   bool drag_at_end_of_train = (drag_dest == v->index); // Head index is used to mark dragging at end of train.
    00111   for (; v != NULL && (rtl ? px > 0 : px < max_width); v = v->Next()) {
    00112     if (dragging && !drag_at_end_of_train && drag_dest == v->index) {
    00113       /* Highlight the drag-and-drop destination inside the train. */
    00114       int drag_hlight_width = HighlightDragPosition(px, max_width, selection);
    00115       px += rtl ? -drag_hlight_width : drag_hlight_width;
    00116     }
    00117
    00118     Point offset;
    00119     int width = Train::From(v)->GetDisplayImageWidth(&offset);
    00120
    00121     if (rtl ? px + width > 0 : px - width < max_width) {
    00122       PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
    00123       DrawSprite(v->GetImage(dir, image_type), pal, px + (rtl ? -offset.x : offset.x), 7 + offset.y);
    00124     }
    00125
    00126     if (!v->IsArticulatedPart()) sel_articulated = false;
    00127
    00128     if (v->index == selection) {
    00129       /* Set the highlight position */
    00130       highlight_l = rtl ? px - width : px;
    00131       highlight_r = rtl ? px - 1 : px + width - 1;
    00132       sel_articulated = true;
    00133     } else if ((_cursor.vehchain && highlight_r != 0) || sel_articulated) {
    00134       if (rtl) {
    00135         highlight_l -= width;
    00136       } else {
    00137         highlight_r += width;
    00138       }
    00139     }
    00140
    00141     px += rtl ? -width : width;
    00142   }
    00143
    00144   if (dragging && drag_at_end_of_train) {
    00145     /* Highlight the drag-and-drop destination at the end of the train. */
    00146     HighlightDragPosition(px, max_width, selection);
    00147   }
    00148
    00149   if (highlight_l != highlight_r) {
    00150     /* Draw the highlight. Now done after drawing all the engines, as
    00151      * the next engine after the highlight could overlap it. */
    00152     DrawFrameRect(highlight_l, 0, highlight_r, 13, COLOUR_WHITE, FR_BORDERONLY);
    00153   }
    00154
    00155   _cur_dpi = old_dpi;
    00156 }
    00157
    00159 struct CargoSummaryItem {
    00160   CargoID cargo;
    00161   StringID subtype;
    00162   uint capacity;
    00163   uint amount;
    00164   StationID source;
    00165
    00167   inline bool operator != (const CargoSummaryItem &other) const
    00168   {
    00169     return this->cargo != other.cargo || this->subtype != other.subtype;
    00170   }
    00171 };
    00172
    00173 static const uint TRAIN_DETAILS_MIN_INDENT = 32;
    00174 static const uint TRAIN_DETAILS_MAX_INDENT = 72;
    00175
    00177 typedef SmallVector<CargoSummaryItem, 2> CargoSummary;
    00179 static CargoSummary _cargo_summary;
    00180
    00189 static void TrainDetailsCargoTab(const CargoSummaryItem *item, int left, int right, int y)
    00190 {
    00191   StringID str;
    00192   if (item->amount > 0) {
    00193     SetDParam(0, item->cargo);
    00194     SetDParam(1, item->amount);
    00195     SetDParam(2, item->source);
    00196     SetDParam(3, _settings_game.vehicle.freight_trains);
    00197     str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
    00198   } else {
    00199     SetDParam(0, STR_QUANTITY_N_A);
    00200     str = item->cargo == INVALID_CARGO ? STR_LTBLUE_STRING : STR_VEHICLE_DETAILS_CARGO_EMPTY;
    00201   }
    00202
    00203   DrawString(left, right, y, str);
    00204 }
    00205
    00214 static void TrainDetailsInfoTab(const Vehicle *v, int left, int right, int y)
    00215 {
    00216   if (RailVehInfo(v->engine_type)->railveh_type == RAILVEH_WAGON) {
    00217     SetDParam(0, v->engine_type);
    00218     SetDParam(1, v->value);
    00219     DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
    00220   } else {
    00221     SetDParam(0, v->engine_type);
    00222     SetDParam(1, v->build_year);
    00223     SetDParam(2, v->value);
    00224     DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
    00225   }
    00226 }
    00227
    00236 static void TrainDetailsCapacityTab(const CargoSummaryItem *item, int left, int right, int y)
    00237 {
    00238   StringID str;
    00239   if (item->cargo != INVALID_CARGO) {
    00240     SetDParam(0, item->cargo);
    00241     SetDParam(1, item->capacity);
    00242     SetDParam(4, item->subtype);
    00243     SetDParam(5, _settings_game.vehicle.freight_trains);
    00244     str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY;
    00245   } else {
    00246     /* Draw subtype only */
    00247     SetDParam(0, item->subtype);
    00248     str = STR_VEHICLE_INFO_NO_CAPACITY;
    00249   }
    00250   DrawString(left, right, y, str);
    00251 }
    00252
    00258 static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *summary)
    00259 {
    00260   summary->Clear();
    00261   do {
    00262     if (!v->GetEngine()->CanCarryCargo()) continue;
    00263
    00264     CargoSummaryItem new_item;
    00265     new_item.cargo = v->cargo_cap > 0 ? v->cargo_type : INVALID_CARGO;
    00266     new_item.subtype = GetCargoSubtypeText(v);
    00267     if (new_item.cargo == INVALID_CARGO && new_item.subtype == STR_EMPTY) continue;
    00268
    00269     CargoSummaryItem *item = summary->Find(new_item);
    00270     if (item == summary->End()) {
    00271       item = summary->Append();
    00272       item->cargo = new_item.cargo;
    00273       item->subtype = new_item.subtype;
    00274       item->capacity = 0;
    00275       item->amount = 0;
    00276       item->source = INVALID_STATION;
    00277     }
    00278
    00279     item->capacity += v->cargo_cap;
    00280     item->amount += v->cargo.Count();
    00281     if (item->source == INVALID_STATION) item->source = v->cargo.Source();
    00282   } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
    00283 }
    00284
    00290 static uint GetLengthOfArticulatedVehicle(const Train *v)
    00291 {
    00292   uint length = 0;
    00293
    00294   do {
    00295     length += v->GetDisplayImageWidth();
    00296   } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
    00297
    00298   return length;
    00299 }
    00300
    00307 int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
    00308 {
    00309   int num = 0;
    00310
    00311   if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
    00312     CargoArray act_cargo;
    00313     CargoArray max_cargo;
    00314     for (const Vehicle *v = Vehicle::Get(veh_id); v != NULL; v = v->Next()) {
    00315       act_cargo[v->cargo_type] += v->cargo.Count();
    00316       max_cargo[v->cargo_type] += v->cargo_cap;
    00317     }
    00318
    00319     /* Set scroll-amount seperately from counting, as to not compute num double
    00320      * for more carriages of the same type
    00321      */
    00322     for (CargoID i = 0; i < NUM_CARGO; i++) {
    00323       if (max_cargo[i] > 0) num++; // only count carriages that the train has
    00324     }
    00325     num++; // needs one more because first line is description string
    00326   } else {
    00327     for (const Train *v = Train::Get(veh_id); v != NULL; v = v->GetNextVehicle()) {
    00328       GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
    00329       num += max(1u, _cargo_summary.Length());
    00330
    00331       uint length = GetLengthOfArticulatedVehicle(v);
    00332       if (length > TRAIN_DETAILS_MAX_INDENT) num++;
    00333     }
    00334   }
    00335
    00336   return num;
    00337 }
    00338
    00350 void DrawTrainDetails(const Train *v, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, TrainDetailsWindowTabs det_tab)
    00351 {
    00352   /* draw the first 3 details tabs */
    00353   if (det_tab != TDW_TAB_TOTALS) {
    00354     bool rtl = _current_text_dir == TD_RTL;
    00355     Direction dir = rtl ? DIR_E : DIR_W;
    00356     int x = rtl ? right : left;
    00357     int sprite_y_offset = 4 + (FONT_HEIGHT_NORMAL - 10) / 2;
    00358     int line_height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    00359     for (; v != NULL && vscroll_pos > -vscroll_cap; v = v->GetNextVehicle()) {
    00360       GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
    00361
    00362       /* Draw sprites */
    00363       uint dx = 0;
    00364       int px = x;
    00365       const Train *u = v;
    00366       do {
    00367         Point offset;
    00368         int width = u->GetDisplayImageWidth(&offset);
    00369         if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
    00370           PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
    00371           DrawSprite(u->GetImage(dir, EIT_IN_DETAILS), pal, px + (rtl ? -offset.x : offset.x), y - line_height * vscroll_pos + sprite_y_offset + offset.y);
    00372         }
    00373         px += rtl ? -width : width;
    00374         dx += width;
    00375         u = u->Next();
    00376       } while (u != NULL && u->IsArticulatedPart());
    00377
    00378       bool separate_sprite_row = (dx > TRAIN_DETAILS_MAX_INDENT);
    00379       if (separate_sprite_row) {
    00380         vscroll_pos--;
    00381         dx = 0;
    00382       }
    00383
    00384       uint num_lines = max(1u, _cargo_summary.Length());
    00385       for (uint i = 0; i < num_lines; i++) {
    00386         int sprite_width = max<int>(dx, TRAIN_DETAILS_MIN_INDENT) + 3;
    00387         int data_left  = left + (rtl ? 0 : sprite_width);
    00388         int data_right = right - (rtl ? sprite_width : 0);
    00389         if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
    00390           int py = y - line_height * vscroll_pos;
    00391           if (i > 0 || separate_sprite_row) {
    00392             if (vscroll_pos != 0) GfxFillRect(left, py - WD_MATRIX_TOP - 1, right, py - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][5]);
    00393           }
    00394           switch (det_tab) {
    00395             case TDW_TAB_CARGO:
    00396               if (i < _cargo_summary.Length()) {
    00397                 TrainDetailsCargoTab(&_cargo_summary[i], data_left, data_right, py);
    00398               } else {
    00399                 DrawString(data_left, data_right, py, STR_QUANTITY_N_A, TC_LIGHT_BLUE);
    00400               }
    00401               break;
    00402
    00403             case TDW_TAB_INFO:
    00404               if (i == 0) TrainDetailsInfoTab(v, data_left, data_right, py);
    00405               break;
    00406
    00407             case TDW_TAB_CAPACITY:
    00408               if (i < _cargo_summary.Length()) {
    00409                 TrainDetailsCapacityTab(&_cargo_summary[i], data_left, data_right, py);
    00410               } else {
    00411                 SetDParam(0, STR_EMPTY);
    00412                 DrawString(data_left, data_right, py, STR_VEHICLE_INFO_NO_CAPACITY);
    00413               }
    00414               break;
    00415
    00416             default: NOT_REACHED();
    00417           }
    00418         }
    00419         vscroll_pos--;
    00420       }
    00421     }
    00422   } else {
    00423     CargoArray act_cargo;
    00424     CargoArray max_cargo;
    00425     Money feeder_share = 0;
    00426
    00427     for (const Vehicle *u = v; u != NULL; u = u->Next()) {
    00428       act_cargo[u->cargo_type] += u->cargo.Count();
    00429       max_cargo[u->cargo_type] += u->cargo_cap;
    00430       feeder_share             += u->cargo.FeederShare();
    00431     }
    00432
    00433     /* draw total cargo tab */
    00434     DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT);
    00435     y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    00436
    00437     for (CargoID i = 0; i < NUM_CARGO; i++) {
    00438       if (max_cargo[i] > 0 && --vscroll_pos < 0 && vscroll_pos > -vscroll_cap) {
    00439         SetDParam(0, i);            // {CARGO} #1
    00440         SetDParam(1, act_cargo[i]); // {CARGO} #2
    00441         SetDParam(2, i);            // {SHORTCARGO} #1
    00442         SetDParam(3, max_cargo[i]); // {SHORTCARGO} #2
    00443         SetDParam(4, _settings_game.vehicle.freight_trains);
    00444         DrawString(left, right, y, FreightWagonMult(i) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT : STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY);
    00445         y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    00446       }
    00447     }
    00448     SetDParam(0, feeder_share);
    00449     DrawString(left, right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
    00450   }
    00451 }