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 "gfx_func.h"
00015 #include "command_func.h"
00016 #include "vehicle_gui.h"
00017 #include "train.h"
00018 #include "strings_func.h"
00019 #include "vehicle_func.h"
00020 #include "window_func.h"
00021 
00022 #include "table/sprites.h"
00023 #include "table/strings.h"
00024 
00025 void CcBuildWagon(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00026 {
00027   if (result.Failed()) return;
00028 
00029   /* find a locomotive in the depot. */
00030   const Vehicle *found = NULL;
00031   const Train *t;
00032   FOR_ALL_TRAINS(t) {
00033     if (t->IsFrontEngine() && t->tile == tile &&
00034         t->track == TRACK_BIT_DEPOT) {
00035       if (found != NULL) return; // must be exactly one.
00036       found = t;
00037     }
00038   }
00039 
00040   /* if we found a loco, */
00041   if (found != NULL) {
00042     found = found->Last();
00043     /* put the new wagon at the end of the loco. */
00044     DoCommandP(0, _new_vehicle_id, found->index, CMD_MOVE_RAIL_VEHICLE);
00045     InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
00046   }
00047 }
00048 
00056 static int HighlightDragPosition(int px, int max_width, VehicleID selection)
00057 {
00058   bool rtl = _current_text_dir == TD_RTL;
00059 
00060   assert(selection != INVALID_VEHICLE);
00061   Point offset;
00062   int dragged_width = Train::Get(selection)->GetDisplayImageWidth(&offset) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00063 
00064   int drag_hlight_left = rtl ? max(px -dragged_width, 0) : px;
00065   int drag_hlight_right = rtl ? px : min(px + dragged_width, max_width);
00066   int drag_hlight_width = max(drag_hlight_right - drag_hlight_left, 0);
00067 
00068   if (drag_hlight_width > 0) {
00069     GfxFillRect(drag_hlight_left + WD_FRAMERECT_LEFT, WD_FRAMERECT_TOP + 1,
00070         drag_hlight_right - WD_FRAMERECT_RIGHT, 13 - WD_FRAMERECT_BOTTOM, _colour_gradient[COLOUR_GREY][7]);
00071   }
00072 
00073   return drag_hlight_width;
00074 }
00075 
00086 void DrawTrainImage(const Train *v, int left, int right, int y, VehicleID selection, int skip, VehicleID drag_dest)
00087 {
00088   bool rtl = _current_text_dir == TD_RTL;
00089   Direction dir = rtl ? DIR_E : DIR_W;
00090 
00091   DrawPixelInfo tmp_dpi, *old_dpi;
00092   /* Position of highlight box */
00093   int highlight_l = 0;
00094   int highlight_r = 0;
00095   int max_width = right - left + 1;
00096 
00097   if (!FillDrawPixelInfo(&tmp_dpi, left, y, max_width, 14)) return;
00098 
00099   old_dpi = _cur_dpi;
00100   _cur_dpi = &tmp_dpi;
00101 
00102   int px = rtl ? max_width + skip : -skip;
00103   bool sel_articulated = false;
00104   bool dragging = (drag_dest != INVALID_VEHICLE);
00105   bool drag_at_end_of_train = (drag_dest == v->index); // Head index is used to mark dragging at end of train.
00106   for (; v != NULL && (rtl ? px > 0 : px < max_width); v = v->Next()) {
00107     if (dragging && !drag_at_end_of_train && drag_dest == v->index) {
00108       /* Highlight the drag-and-drop destination inside the train. */
00109       int drag_hlight_width = HighlightDragPosition(px, max_width, selection);
00110       px += rtl ? -drag_hlight_width : drag_hlight_width;
00111     }
00112 
00113     Point offset;
00114     int width = Train::From(v)->GetDisplayImageWidth(&offset);
00115 
00116     if (rtl ? px + width > 0 : px - width < max_width) {
00117       PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
00118       DrawSprite(v->GetImage(dir), pal, px + (rtl ? -offset.x : offset.x), 7 + offset.y);
00119     }
00120 
00121     if (!v->IsArticulatedPart()) sel_articulated = false;
00122 
00123     if (v->index == selection) {
00124       /* Set the highlight position */
00125       highlight_l = rtl ? px - width : px;
00126       highlight_r = rtl ? px - 1 : px + width - 1;
00127       sel_articulated = true;
00128     } else if ((_cursor.vehchain && highlight_r != 0) || sel_articulated) {
00129       if (rtl) {
00130         highlight_l -= width;
00131       } else {
00132         highlight_r += width;
00133       }
00134     }
00135 
00136     px += rtl ? -width : width;
00137   }
00138 
00139   if (dragging && drag_at_end_of_train) {
00140     /* Highlight the drag-and-drop destination at the end of the train. */
00141     HighlightDragPosition(px, max_width, selection);
00142   }
00143 
00144   if (highlight_l != highlight_r) {
00145     /* Draw the highlight. Now done after drawing all the engines, as
00146      * the next engine after the highlight could overlap it. */
00147     DrawFrameRect(highlight_l, 0, highlight_r, 13, COLOUR_WHITE, FR_BORDERONLY);
00148   }
00149 
00150   _cur_dpi = old_dpi;
00151 }
00152 
00154 struct CargoSummaryItem {
00155   CargoID cargo;    
00156   StringID subtype; 
00157   uint capacity;    
00158   uint amount;      
00159   StationID source; 
00160 
00162   FORCEINLINE bool operator != (const CargoSummaryItem &other) const
00163   {
00164     return this->cargo != other.cargo || this->subtype != other.subtype;
00165   }
00166 };
00167 
00168 static const uint TRAIN_DETAILS_MIN_INDENT = 32; 
00169 static const uint TRAIN_DETAILS_MAX_INDENT = 72; 
00170 
00172 typedef SmallVector<CargoSummaryItem, 2> CargoSummary;
00174 static CargoSummary _cargo_summary;
00175 
00184 static void TrainDetailsCargoTab(const CargoSummaryItem *item, int left, int right, int y)
00185 {
00186   StringID str = STR_VEHICLE_DETAILS_CARGO_EMPTY;
00187 
00188   if (item->amount > 0) {
00189     SetDParam(0, item->cargo);
00190     SetDParam(1, item->amount);
00191     SetDParam(2, item->source);
00192     SetDParam(3, _settings_game.vehicle.freight_trains);
00193     str = FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_DETAILS_CARGO_FROM_MULT : STR_VEHICLE_DETAILS_CARGO_FROM;
00194   }
00195 
00196   DrawString(left, right, y, str);
00197 }
00198 
00207 static void TrainDetailsInfoTab(const Vehicle *v, int left, int right, int y)
00208 {
00209   if (RailVehInfo(v->engine_type)->railveh_type == RAILVEH_WAGON) {
00210     SetDParam(0, v->engine_type);
00211     SetDParam(1, v->value);
00212     DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_WAGON_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00213   } else {
00214     SetDParam(0, v->engine_type);
00215     SetDParam(1, v->build_year);
00216     SetDParam(2, v->value);
00217     DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_ENGINE_BUILT_AND_VALUE, TC_FROMSTRING, SA_LEFT | SA_STRIP);
00218   }
00219 }
00220 
00229 static void TrainDetailsCapacityTab(const CargoSummaryItem *item, int left, int right, int y)
00230 {
00231   SetDParam(0, item->cargo);
00232   SetDParam(1, item->capacity);
00233   SetDParam(4, item->subtype);
00234   SetDParam(5, _settings_game.vehicle.freight_trains);
00235   DrawString(left, right, y, FreightWagonMult(item->cargo) > 1 ? STR_VEHICLE_INFO_CAPACITY_MULT : STR_VEHICLE_INFO_CAPACITY);
00236 }
00237 
00243 static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary *summary)
00244 {
00245   summary->Clear();
00246   do {
00247     if (v->cargo_cap == 0) continue;
00248 
00249     CargoSummaryItem new_item;
00250     new_item.cargo = v->cargo_type;
00251     new_item.subtype = GetCargoSubtypeText(v);
00252 
00253     CargoSummaryItem *item = summary->Find(new_item);
00254     if (item == summary->End()) {
00255       item = summary->Append();
00256       item->cargo = new_item.cargo;
00257       item->subtype = new_item.subtype;
00258       item->capacity = 0;
00259       item->amount = 0;
00260       item->source = INVALID_STATION;
00261     }
00262 
00263     item->capacity += v->cargo_cap;
00264     item->amount += v->cargo.Count();
00265     if (item->source == INVALID_STATION) item->source = v->cargo.Source();
00266   } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
00267 }
00268 
00274 static uint GetLengthOfArticulatedVehicle(const Train *v)
00275 {
00276   uint length = 0;
00277 
00278   do {
00279     length += v->GetDisplayImageWidth();
00280   } while ((v = v->Next()) != NULL && v->IsArticulatedPart());
00281 
00282   return length;
00283 }
00284 
00291 int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab)
00292 {
00293   int num = 0;
00294 
00295   if (det_tab == TDW_TAB_TOTALS) { // Total cargo tab
00296     CargoArray act_cargo;
00297     CargoArray max_cargo;
00298     for (const Vehicle *v = Vehicle::Get(veh_id); v != NULL; v = v->Next()) {
00299       act_cargo[v->cargo_type] += v->cargo.Count();
00300       max_cargo[v->cargo_type] += v->cargo_cap;
00301     }
00302 
00303     /* Set scroll-amount seperately from counting, as to not compute num double
00304      * for more carriages of the same type
00305      */
00306     for (CargoID i = 0; i < NUM_CARGO; i++) {
00307       if (max_cargo[i] > 0) num++; // only count carriages that the train has
00308     }
00309     num++; // needs one more because first line is description string
00310   } else {
00311     for (const Train *v = Train::Get(veh_id); v != NULL; v = v->GetNextVehicle()) {
00312       GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
00313       num += max(1u, _cargo_summary.Length());
00314 
00315       uint length = GetLengthOfArticulatedVehicle(v);
00316       if (length > TRAIN_DETAILS_MAX_INDENT) num++;
00317     }
00318   }
00319 
00320   return num;
00321 }
00322 
00334 void DrawTrainDetails(const Train *v, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, TrainDetailsWindowTabs det_tab)
00335 {
00336   /* draw the first 3 details tabs */
00337   if (det_tab != TDW_TAB_TOTALS) {
00338     bool rtl = _current_text_dir == TD_RTL;
00339     Direction dir = rtl ? DIR_E : DIR_W;
00340     int x = rtl ? right : left;
00341     int sprite_y_offset = 4 + (FONT_HEIGHT_NORMAL - 10) / 2;
00342     int line_height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00343     for (; v != NULL && vscroll_pos > -vscroll_cap; v = v->GetNextVehicle()) {
00344       GetCargoSummaryOfArticulatedVehicle(v, &_cargo_summary);
00345 
00346       /* Draw sprites */
00347       uint dx = 0;
00348       int px = x;
00349       const Train *u = v;
00350       do {
00351         Point offset;
00352         int width = u->GetDisplayImageWidth(&offset);
00353         if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
00354           PaletteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
00355           DrawSprite(u->GetImage(dir), pal, px + (rtl ? -offset.x : offset.x), y - line_height * vscroll_pos + sprite_y_offset + offset.y);
00356         }
00357         px += rtl ? -width : width;
00358         dx += width;
00359         u = u->Next();
00360       } while (u != NULL && u->IsArticulatedPart());
00361 
00362       bool separate_sprite_row = (dx > TRAIN_DETAILS_MAX_INDENT);
00363       if (separate_sprite_row) {
00364         vscroll_pos--;
00365         dx = 0;
00366       }
00367 
00368       uint num_lines = max(1u, _cargo_summary.Length());
00369       for (uint i = 0; i < num_lines; i++) {
00370         int sprite_width = max<int>(dx, TRAIN_DETAILS_MIN_INDENT) + 3;
00371         int data_left  = left + (rtl ? 0 : sprite_width);
00372         int data_right = right - (rtl ? sprite_width : 0);
00373         if (vscroll_pos <= 0 && vscroll_pos > -vscroll_cap) {
00374           int py = y - line_height * vscroll_pos;
00375           if (i > 0 || separate_sprite_row) {
00376             if (vscroll_pos != 0) GfxFillRect(left, py - WD_MATRIX_TOP - 1, right, py - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][5]);
00377           }
00378           switch (det_tab) {
00379             case TDW_TAB_CARGO:
00380               if (i < _cargo_summary.Length()) {
00381                 TrainDetailsCargoTab(&_cargo_summary[i], data_left, data_right, py);
00382               } else {
00383                 DrawString(data_left, data_right, py, STR_QUANTITY_N_A, TC_LIGHT_BLUE);
00384               }
00385               break;
00386 
00387             case TDW_TAB_INFO:
00388               if (i == 0) TrainDetailsInfoTab(v, data_left, data_right, py);
00389               break;
00390 
00391             case TDW_TAB_CAPACITY:
00392               if (i < _cargo_summary.Length()) {
00393                 TrainDetailsCapacityTab(&_cargo_summary[i], data_left, data_right, py);
00394               } else {
00395                 DrawString(data_left, data_right, py, STR_VEHICLE_INFO_NO_CAPACITY);
00396               }
00397               break;
00398 
00399             default: NOT_REACHED();
00400           }
00401         }
00402         vscroll_pos--;
00403       }
00404     }
00405   } else {
00406     CargoArray act_cargo;
00407     CargoArray max_cargo;
00408     Money feeder_share = 0;
00409 
00410     for (const Vehicle *u = v; u != NULL; u = u->Next()) {
00411       act_cargo[u->cargo_type] += u->cargo.Count();
00412       max_cargo[u->cargo_type] += u->cargo_cap;
00413       feeder_share             += u->cargo.FeederShare();
00414     }
00415 
00416     /* draw total cargo tab */
00417     DrawString(left, right, y, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_TEXT);
00418     y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00419 
00420     for (CargoID i = 0; i < NUM_CARGO; i++) {
00421       if (max_cargo[i] > 0 && --vscroll_pos < 0 && vscroll_pos > -vscroll_cap) {
00422         SetDParam(0, i);            // {CARGO} #1
00423         SetDParam(1, act_cargo[i]); // {CARGO} #2
00424         SetDParam(2, i);            // {SHORTCARGO} #1
00425         SetDParam(3, max_cargo[i]); // {SHORTCARGO} #2
00426         SetDParam(4, _settings_game.vehicle.freight_trains);
00427         DrawString(left, right, y, FreightWagonMult(i) > 1 ? STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY_MULT : STR_VEHICLE_DETAILS_TRAIN_TOTAL_CAPACITY);
00428         y += WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
00429       }
00430     }
00431     SetDParam(0, feeder_share);
00432     DrawString(left, right, y, STR_VEHICLE_INFO_FEEDER_CARGO_VALUE);
00433   }
00434 }

Generated on Wed Apr 13 00:47:56 2011 for OpenTTD by  doxygen 1.6.1