00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "train.h"
00014 #include "roadveh.h"
00015 #include "ship.h"
00016 #include "aircraft.h"
00017 #include "gui.h"
00018 #include "textbuf_gui.h"
00019 #include "viewport_func.h"
00020 #include "command_func.h"
00021 #include "depot_base.h"
00022 #include "spritecache.h"
00023 #include "strings_func.h"
00024 #include "vehicle_func.h"
00025 #include "company_func.h"
00026 #include "tilehighlight_func.h"
00027 #include "window_gui.h"
00028 #include "vehiclelist.h"
00029 #include "order_backup.h"
00030 #include "zoom_func.h"
00031
00032 #include "widgets/depot_widget.h"
00033
00034 #include "table/strings.h"
00035
00036
00037
00038
00039
00040
00041
00043 static const NWidgetPart _nested_train_depot_widgets[] = {
00044 NWidget(NWID_HORIZONTAL),
00045 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00046 NWidget(WWT_CAPTION, COLOUR_GREY, WID_D_CAPTION), SetDataTip(STR_DEPOT_CAPTION, STR_NULL),
00047 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00048 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00049 EndContainer(),
00050 NWidget(NWID_HORIZONTAL),
00051 NWidget(NWID_VERTICAL),
00052 NWidget(WWT_MATRIX, COLOUR_GREY, WID_D_MATRIX), SetDataTip(0x0, STR_NULL), SetResize(1, 1), SetScrollbar(WID_D_V_SCROLL),
00053 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_D_SHOW_H_SCROLL),
00054 NWidget(NWID_HSCROLLBAR, COLOUR_GREY, WID_D_H_SCROLL),
00055 EndContainer(),
00056 EndContainer(),
00057 NWidget(NWID_VERTICAL),
00058 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_D_SELL), SetDataTip(0x0, STR_NULL), SetResize(0, 1), SetFill(0, 1),
00059 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_D_SHOW_SELL_CHAIN),
00060 NWidget(WWT_IMGBTN, COLOUR_GREY, WID_D_SELL_CHAIN), SetDataTip(SPR_SELL_CHAIN_TRAIN, STR_DEPOT_DRAG_WHOLE_TRAIN_TO_SELL_TOOLTIP), SetResize(0, 1), SetFill(0, 1),
00061 EndContainer(),
00062 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_D_SELL_ALL), SetDataTip(0x0, STR_NULL),
00063 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_D_AUTOREPLACE), SetDataTip(0x0, STR_NULL),
00064 EndContainer(),
00065 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_D_V_SCROLL),
00066 EndContainer(),
00067 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00068 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_D_BUILD), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00069 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_D_CLONE), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00070 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_D_LOCATION), SetDataTip(STR_BUTTON_LOCATION, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00071 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_D_SHOW_RENAME),
00072 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_D_RENAME), SetDataTip(STR_BUTTON_RENAME, STR_DEPOT_RENAME_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
00073 EndContainer(),
00074 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_D_VEHICLE_LIST), SetDataTip(0x0, STR_NULL), SetFill(0, 1),
00075 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_D_STOP_ALL), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_NULL), SetFill(0, 1),
00076 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_D_START_ALL), SetDataTip(SPR_FLAG_VEH_RUNNING, STR_NULL), SetFill(0, 1),
00077 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00078 EndContainer(),
00079 };
00080
00081 static const WindowDesc _train_depot_desc(
00082 WDP_AUTO, 362, 123,
00083 WC_VEHICLE_DEPOT, WC_NONE,
00084 0,
00085 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00086 );
00087
00088 static const WindowDesc _road_depot_desc(
00089 WDP_AUTO, 316, 97,
00090 WC_VEHICLE_DEPOT, WC_NONE,
00091 0,
00092 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00093 );
00094
00095 static const WindowDesc _ship_depot_desc(
00096 WDP_AUTO, 306, 99,
00097 WC_VEHICLE_DEPOT, WC_NONE,
00098 0,
00099 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00100 );
00101
00102 static const WindowDesc _aircraft_depot_desc(
00103 WDP_AUTO, 332, 99,
00104 WC_VEHICLE_DEPOT, WC_NONE,
00105 0,
00106 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00107 );
00108
00109 extern void DepotSortList(VehicleList *list);
00110
00118 void CcCloneVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00119 {
00120 if (result.Failed()) return;
00121
00122 const Vehicle *v = Vehicle::Get(_new_vehicle_id);
00123
00124 ShowVehicleViewWindow(v);
00125 }
00126
00127 static void TrainDepotMoveVehicle(const Vehicle *wagon, VehicleID sel, const Vehicle *head)
00128 {
00129 const Vehicle *v = Vehicle::Get(sel);
00130
00131 if (v == wagon) return;
00132
00133 if (wagon == NULL) {
00134 if (head != NULL) wagon = head->Last();
00135 } else {
00136 wagon = wagon->Previous();
00137 if (wagon == NULL) return;
00138 }
00139
00140 if (wagon == v) return;
00141
00142 DoCommandP(v->tile, v->index | (_ctrl_pressed ? 1 : 0) << 20, wagon == NULL ? INVALID_VEHICLE : wagon->index, CMD_MOVE_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_MOVE_VEHICLE));
00143 }
00144
00145 static VehicleCellSize _base_block_sizes_depot[VEH_COMPANY_END];
00146 static VehicleCellSize _base_block_sizes_purchase[VEH_COMPANY_END];
00147
00155 VehicleCellSize GetVehicleImageCellSize(VehicleType type, EngineImageType image_type)
00156 {
00157 switch (image_type) {
00158 case EIT_IN_DEPOT: return _base_block_sizes_depot[type];
00159 case EIT_PURCHASE: return _base_block_sizes_purchase[type];
00160 default: NOT_REACHED();
00161 }
00162 }
00163
00164 static void InitBlocksizeForVehicles(VehicleType type, EngineImageType image_type)
00165 {
00166 int max_extend_left = 0;
00167 int max_extend_right = 0;
00168 uint max_height = 0;
00169
00170 const Engine *e;
00171 FOR_ALL_ENGINES_OF_TYPE(e, type) {
00172 EngineID eid = e->index;
00173 uint x, y;
00174 int x_offs, y_offs;
00175
00176 switch (type) {
00177 default: NOT_REACHED();
00178 case VEH_TRAIN: GetTrainSpriteSize( eid, x, y, x_offs, y_offs, image_type); break;
00179 case VEH_ROAD: GetRoadVehSpriteSize( eid, x, y, x_offs, y_offs, image_type); break;
00180 case VEH_SHIP: GetShipSpriteSize( eid, x, y, x_offs, y_offs, image_type); break;
00181 case VEH_AIRCRAFT: GetAircraftSpriteSize(eid, x, y, x_offs, y_offs, image_type); break;
00182 }
00183 if (y > max_height) max_height = y;
00184 if (-x_offs > max_extend_left) max_extend_left = -x_offs;
00185 if ((int)x + x_offs > max_extend_right) max_extend_right = x + x_offs;
00186 }
00187
00188 switch (image_type) {
00189 case EIT_IN_DEPOT:
00190 _base_block_sizes_depot[type].height = max(GetVehicleHeight(type), max_height);
00191 _base_block_sizes_depot[type].extend_left = Clamp(max_extend_left, 16, 98);
00192 _base_block_sizes_depot[type].extend_right = Clamp(max_extend_right, 16, 98);
00193 break;
00194 case EIT_PURCHASE:
00195 _base_block_sizes_purchase[type].height = max(GetVehicleHeight(type), max_height);
00196 _base_block_sizes_purchase[type].extend_left = Clamp(max_extend_left, 16, 98);
00197 _base_block_sizes_purchase[type].extend_right = Clamp(max_extend_right, 16, 98);
00198 break;
00199
00200 default: NOT_REACHED();
00201 }
00202 }
00203
00208 void InitDepotWindowBlockSizes()
00209 {
00210 for (VehicleType vt = VEH_BEGIN; vt < VEH_COMPANY_END; vt++) {
00211 InitBlocksizeForVehicles(vt, EIT_IN_DEPOT);
00212 InitBlocksizeForVehicles(vt, EIT_PURCHASE);
00213 }
00214 }
00215
00216 static void DepotSellAllConfirmationCallback(Window *w, bool confirmed);
00217 const Sprite *GetAircraftSprite(EngineID engine);
00218
00219 struct DepotWindow : Window {
00220 VehicleID sel;
00221 VehicleID vehicle_over;
00222 VehicleType type;
00223 bool generate_list;
00224 VehicleList vehicle_list;
00225 VehicleList wagon_list;
00226 uint num_columns;
00227 Scrollbar *hscroll;
00228 Scrollbar *vscroll;
00229
00230 DepotWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window()
00231 {
00232 assert(IsCompanyBuildableVehicleType(type));
00233
00234 this->sel = INVALID_VEHICLE;
00235 this->vehicle_over = INVALID_VEHICLE;
00236 this->generate_list = true;
00237 this->type = type;
00238 this->num_columns = 1;
00239
00240 this->CreateNestedTree(desc);
00241 this->hscroll = (this->type == VEH_TRAIN ? this->GetScrollbar(WID_D_H_SCROLL) : NULL);
00242 this->vscroll = this->GetScrollbar(WID_D_V_SCROLL);
00243
00244 this->GetWidget<NWidgetStacked>(WID_D_SHOW_RENAME)->SetDisplayedPlane(type == VEH_AIRCRAFT ? SZSP_NONE : 0);
00245
00246 this->GetWidget<NWidgetStacked>(WID_D_SHOW_H_SCROLL)->SetDisplayedPlane(type == VEH_TRAIN ? 0 : SZSP_HORIZONTAL);
00247 this->GetWidget<NWidgetStacked>(WID_D_SHOW_SELL_CHAIN)->SetDisplayedPlane(type == VEH_TRAIN ? 0 : SZSP_NONE);
00248 this->SetupWidgetData(type);
00249 this->FinishInitNested(desc, tile);
00250
00251 this->owner = GetTileOwner(tile);
00252 OrderBackup::Reset();
00253 }
00254
00255 ~DepotWindow()
00256 {
00257 DeleteWindowById(WC_BUILD_VEHICLE, this->window_number);
00258 OrderBackup::Reset(this->window_number);
00259 }
00260
00268 void DrawVehicleInDepot(const Vehicle *v, int left, int right, int y) const
00269 {
00270 bool free_wagon = false;
00271 int sprite_y = y + (this->resize.step_height - GetVehicleHeight(v->type)) / 2;
00272
00273 bool rtl = _current_text_dir == TD_RTL;
00274 int image_left = rtl ? left + this->count_width : left + this->header_width;
00275 int image_right = rtl ? right - this->header_width : right - this->count_width;
00276
00277 switch (v->type) {
00278 case VEH_TRAIN: {
00279 const Train *u = Train::From(v);
00280 free_wagon = u->IsFreeWagon();
00281
00282 uint x_space = free_wagon ? TRAININFO_DEFAULT_VEHICLE_WIDTH : 0;
00283 DrawTrainImage(u, image_left + (rtl ? 0 : x_space), image_right - (rtl ? x_space : 0), sprite_y - 1,
00284 this->sel, EIT_IN_DEPOT, free_wagon ? 0 : this->hscroll->GetPosition(), this->vehicle_over);
00285
00286
00287 SetDParam(0, CeilDiv(u->gcache.cached_total_length * 10, TILE_SIZE));
00288 SetDParam(1, 1);
00289 DrawString(rtl ? left + WD_FRAMERECT_LEFT : right - this->count_width, rtl ? left + this->count_width : right - WD_FRAMERECT_RIGHT, y + (this->resize.step_height - FONT_HEIGHT_SMALL) / 2, STR_TINY_BLACK_DECIMAL, TC_FROMSTRING, SA_RIGHT);
00290 break;
00291 }
00292
00293 case VEH_ROAD: DrawRoadVehImage( v, image_left, image_right, sprite_y, this->sel, EIT_IN_DEPOT); break;
00294 case VEH_SHIP: DrawShipImage( v, image_left, image_right, sprite_y, this->sel, EIT_IN_DEPOT); break;
00295 case VEH_AIRCRAFT: {
00296 const Sprite *spr = GetSprite(v->GetImage(DIR_W, EIT_IN_DEPOT), ST_NORMAL);
00297 DrawAircraftImage(v, image_left, image_right,
00298 y + max(UnScaleByZoom(spr->height, ZOOM_LVL_GUI) + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI) - 14, 0),
00299 this->sel, EIT_IN_DEPOT);
00300 break;
00301 }
00302 default: NOT_REACHED();
00303 }
00304
00305 uint diff_x, diff_y;
00306 if (v->IsGroundVehicle()) {
00307
00308 diff_x = this->flag_width + WD_FRAMERECT_LEFT;
00309 diff_y = (this->resize.step_height - this->flag_height) / 2 - 2;
00310 } else {
00311
00312 diff_x = WD_FRAMERECT_LEFT;
00313 diff_y = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00314 }
00315 int text_left = rtl ? right - this->header_width - 1 : left + diff_x;
00316 int text_right = rtl ? right - diff_x : left + this->header_width - 1;
00317
00318 if (free_wagon) {
00319 DrawString(text_left, text_right, y + 2, STR_DEPOT_NO_ENGINE);
00320 } else {
00321 DrawSprite((v->vehstatus & VS_STOPPED) ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, PAL_NONE, rtl ? right - this->flag_width : left + WD_FRAMERECT_LEFT, y + diff_y);
00322
00323 SetDParam(0, v->unitnumber);
00324 DrawString(text_left, text_right, y + 2, (uint16)(v->max_age - DAYS_IN_LEAP_YEAR) >= v->age ? STR_BLACK_COMMA : STR_RED_COMMA);
00325 }
00326 }
00327
00328 void DrawWidget(const Rect &r, int widget) const
00329 {
00330 if (widget != WID_D_MATRIX) return;
00331
00332 bool rtl = _current_text_dir == TD_RTL;
00333
00334
00335 uint16 mat_data = this->GetWidget<NWidgetCore>(WID_D_MATRIX)->widget_data;
00336 uint16 rows_in_display = GB(mat_data, MAT_ROW_START, MAT_ROW_BITS);
00337 uint16 boxes_in_each_row = GB(mat_data, MAT_COL_START, MAT_COL_BITS);
00338
00339 uint16 num = this->vscroll->GetPosition() * boxes_in_each_row;
00340 int maxval = min(this->vehicle_list.Length(), num + (rows_in_display * boxes_in_each_row));
00341 int y;
00342 for (y = r.top + 1; num < maxval; y += this->resize.step_height) {
00343 for (byte i = 0; i < boxes_in_each_row && num < maxval; i++, num++) {
00344
00345 const Vehicle *v = this->vehicle_list[num];
00346 if (boxes_in_each_row == 1) {
00347 this->DrawVehicleInDepot(v, r.left, r.right, y);
00348 } else {
00349 int x = r.left + (rtl ? (boxes_in_each_row - i - 1) : i) * this->resize.step_width;
00350 this->DrawVehicleInDepot(v, x, x + this->resize.step_width - 1, y);
00351 }
00352 }
00353 }
00354
00355 maxval = min(this->vehicle_list.Length() + this->wagon_list.Length(), (this->vscroll->GetPosition() * boxes_in_each_row) + (rows_in_display * boxes_in_each_row));
00356
00357
00358 for (; num < maxval; num++, y += this->resize.step_height) {
00359 const Vehicle *v = this->wagon_list[num - this->vehicle_list.Length()];
00360 this->DrawVehicleInDepot(v, r.left, r.right, y);
00361 }
00362 }
00363
00364 void SetStringParameters(int widget) const
00365 {
00366 if (widget != WID_D_CAPTION) return;
00367
00368
00369 TileIndex tile = this->window_number;
00370 SetDParam(0, this->type);
00371 SetDParam(1, (this->type == VEH_AIRCRAFT) ? GetStationIndex(tile) : GetDepotIndex(tile));
00372 }
00373
00374 struct GetDepotVehiclePtData {
00375 const Vehicle *head;
00376 const Vehicle *wagon;
00377 };
00378
00379 enum DepotGUIAction {
00380 MODE_ERROR,
00381 MODE_DRAG_VEHICLE,
00382 MODE_SHOW_VEHICLE,
00383 MODE_START_STOP,
00384 };
00385
00386 DepotGUIAction GetVehicleFromDepotWndPt(int x, int y, const Vehicle **veh, GetDepotVehiclePtData *d) const
00387 {
00388 const NWidgetCore *matrix_widget = this->GetWidget<NWidgetCore>(WID_D_MATRIX);
00389
00390 if (_current_text_dir == TD_RTL) x = matrix_widget->current_x - x;
00391
00392 uint xt = 0, xm = 0, ym = 0;
00393 if (this->type == VEH_TRAIN) {
00394 xm = x;
00395 } else {
00396 xt = x / this->resize.step_width;
00397 xm = x % this->resize.step_width;
00398 if (xt >= this->num_columns) return MODE_ERROR;
00399 }
00400 ym = y % this->resize.step_height;
00401
00402 uint row = y / this->resize.step_height;
00403 if (row >= this->vscroll->GetCapacity()) return MODE_ERROR;
00404
00405 uint boxes_in_each_row = GB(matrix_widget->widget_data, MAT_COL_START, MAT_COL_BITS);
00406 uint pos = ((row + this->vscroll->GetPosition()) * boxes_in_each_row) + xt;
00407
00408 if (this->vehicle_list.Length() + this->wagon_list.Length() <= pos) {
00409
00410 if (this->type == VEH_TRAIN) {
00411
00412 d->head = NULL;
00413 d->wagon = NULL;
00414 return MODE_DRAG_VEHICLE;
00415 } else {
00416 return MODE_ERROR;
00417 }
00418 }
00419
00420 bool wagon = false;
00421 if (this->vehicle_list.Length() > pos) {
00422 *veh = this->vehicle_list[pos];
00423
00424 if (this->type == VEH_TRAIN) x += this->hscroll->GetPosition();
00425 } else {
00426 pos -= this->vehicle_list.Length();
00427 *veh = this->wagon_list[pos];
00428
00429 x -= VEHICLEINFO_FULL_VEHICLE_WIDTH;
00430 wagon = true;
00431 }
00432
00433 const Train *v = NULL;
00434 if (this->type == VEH_TRAIN) {
00435 v = Train::From(*veh);
00436 d->head = d->wagon = v;
00437 }
00438
00439 if (xm <= this->header_width) {
00440 switch (this->type) {
00441 case VEH_TRAIN:
00442 if (wagon) return MODE_ERROR;
00443 case VEH_ROAD:
00444 if (xm <= this->flag_width) return MODE_START_STOP;
00445 break;
00446
00447 case VEH_SHIP:
00448 case VEH_AIRCRAFT:
00449 if (xm <= this->flag_width && ym >= (uint)(FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL)) return MODE_START_STOP;
00450 break;
00451
00452 default: NOT_REACHED();
00453 }
00454 return MODE_SHOW_VEHICLE;
00455 }
00456
00457 if (this->type != VEH_TRAIN) return MODE_DRAG_VEHICLE;
00458
00459
00460 if (xm >= matrix_widget->current_x - this->count_width) return wagon ? MODE_ERROR : MODE_SHOW_VEHICLE;
00461
00462
00463 x -= this->header_width;
00464
00465
00466 for (; v != NULL; v = v->Next()) {
00467 x -= v->GetDisplayImageWidth();
00468 if (x < 0) break;
00469 }
00470
00471 d->wagon = (v != NULL ? v->GetFirstEnginePart() : NULL);
00472
00473 return MODE_DRAG_VEHICLE;
00474 }
00475
00481 void DepotClick(int x, int y)
00482 {
00483 GetDepotVehiclePtData gdvp = { NULL, NULL };
00484 const Vehicle *v = NULL;
00485 DepotGUIAction mode = this->GetVehicleFromDepotWndPt(x, y, &v, &gdvp);
00486
00487 if (this->type == VEH_TRAIN) v = gdvp.wagon;
00488
00489 switch (mode) {
00490 case MODE_ERROR:
00491 return;
00492
00493 case MODE_DRAG_VEHICLE: {
00494 if (v != NULL && VehicleClicked(v)) return;
00495
00496 VehicleID sel = this->sel;
00497
00498 if (this->type == VEH_TRAIN && sel != INVALID_VEHICLE) {
00499 this->sel = INVALID_VEHICLE;
00500 TrainDepotMoveVehicle(v, sel, gdvp.head);
00501 } else if (v != NULL) {
00502 bool rtl = _current_text_dir == TD_RTL;
00503 int image = v->GetImage(rtl ? DIR_E : DIR_W, EIT_IN_DEPOT);
00504 SetObjectToPlaceWnd(image, GetVehiclePalette(v), HT_DRAG, this);
00505
00506 this->sel = v->index;
00507 this->SetDirty();
00508
00509 _cursor.short_vehicle_offset = v->IsGroundVehicle() ? (16 - v->GetGroundVehicleCache()->cached_veh_length * 2) * (rtl ? -1 : 1) : 0;
00510 _cursor.vehchain = _ctrl_pressed;
00511 }
00512 break;
00513 }
00514
00515 case MODE_SHOW_VEHICLE:
00516 ShowVehicleViewWindow(v);
00517 break;
00518
00519 case MODE_START_STOP:
00520 StartStopVehicle(v, false);
00521 break;
00522
00523 default: NOT_REACHED();
00524 }
00525 }
00526
00533 void SetupWidgetData(VehicleType type)
00534 {
00535 this->GetWidget<NWidgetCore>(WID_D_STOP_ALL)->tool_tip = STR_DEPOT_MASS_STOP_DEPOT_TRAIN_TOOLTIP + type;
00536 this->GetWidget<NWidgetCore>(WID_D_START_ALL)->tool_tip = STR_DEPOT_MASS_START_DEPOT_TRAIN_TOOLTIP + type;
00537 this->GetWidget<NWidgetCore>(WID_D_SELL)->tool_tip = STR_DEPOT_TRAIN_SELL_TOOLTIP + type;
00538 this->GetWidget<NWidgetCore>(WID_D_SELL_ALL)->tool_tip = STR_DEPOT_SELL_ALL_BUTTON_TRAIN_TOOLTIP + type;
00539
00540 this->GetWidget<NWidgetCore>(WID_D_BUILD)->SetDataTip(STR_DEPOT_TRAIN_NEW_VEHICLES_BUTTON + type, STR_DEPOT_TRAIN_NEW_VEHICLES_TOOLTIP + type);
00541 this->GetWidget<NWidgetCore>(WID_D_CLONE)->SetDataTip(STR_DEPOT_CLONE_TRAIN + type, STR_DEPOT_CLONE_TRAIN_DEPOT_INFO + type);
00542
00543 this->GetWidget<NWidgetCore>(WID_D_LOCATION)->tool_tip = STR_DEPOT_TRAIN_LOCATION_TOOLTIP + type;
00544 this->GetWidget<NWidgetCore>(WID_D_VEHICLE_LIST)->tool_tip = STR_DEPOT_VEHICLE_ORDER_LIST_TRAIN_TOOLTIP + type;
00545 this->GetWidget<NWidgetCore>(WID_D_AUTOREPLACE)->tool_tip = STR_DEPOT_AUTOREPLACE_TRAIN_TOOLTIP + type;
00546 this->GetWidget<NWidgetCore>(WID_D_MATRIX)->tool_tip = STR_DEPOT_TRAIN_LIST_TOOLTIP + this->type;
00547
00548 switch (type) {
00549 default: NOT_REACHED();
00550
00551 case VEH_TRAIN:
00552 this->GetWidget<NWidgetCore>(WID_D_VEHICLE_LIST)->widget_data = STR_TRAIN;
00553
00554
00555 this->GetWidget<NWidgetCore>(WID_D_SELL)->widget_data = SPR_SELL_TRAIN;
00556 this->GetWidget<NWidgetCore>(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_TRAIN;
00557 this->GetWidget<NWidgetCore>(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_TRAIN;
00558 break;
00559
00560 case VEH_ROAD:
00561 this->GetWidget<NWidgetCore>(WID_D_VEHICLE_LIST)->widget_data = STR_LORRY;
00562
00563
00564 this->GetWidget<NWidgetCore>(WID_D_SELL)->widget_data = SPR_SELL_ROADVEH;
00565 this->GetWidget<NWidgetCore>(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_ROADVEH;
00566 this->GetWidget<NWidgetCore>(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_ROADVEH;
00567 break;
00568
00569 case VEH_SHIP:
00570 this->GetWidget<NWidgetCore>(WID_D_VEHICLE_LIST)->widget_data = STR_SHIP;
00571
00572
00573 this->GetWidget<NWidgetCore>(WID_D_SELL)->widget_data = SPR_SELL_SHIP;
00574 this->GetWidget<NWidgetCore>(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_SHIP;
00575 this->GetWidget<NWidgetCore>(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_SHIP;
00576 break;
00577
00578 case VEH_AIRCRAFT:
00579 this->GetWidget<NWidgetCore>(WID_D_VEHICLE_LIST)->widget_data = STR_PLANE;
00580
00581
00582 this->GetWidget<NWidgetCore>(WID_D_SELL)->widget_data = SPR_SELL_AIRCRAFT;
00583 this->GetWidget<NWidgetCore>(WID_D_SELL_ALL)->widget_data = SPR_SELL_ALL_AIRCRAFT;
00584 this->GetWidget<NWidgetCore>(WID_D_AUTOREPLACE)->widget_data = SPR_REPLACE_AIRCRAFT;
00585 break;
00586 }
00587 }
00588
00589 uint count_width;
00590 uint header_width;
00591 uint flag_width;
00592 uint flag_height;
00593
00594 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00595 {
00596 switch (widget) {
00597 case WID_D_MATRIX: {
00598 uint min_height = 0;
00599
00600 if (this->type == VEH_TRAIN) {
00601 SetDParamMaxValue(0, 1000);
00602 SetDParam(1, 1);
00603 this->count_width = GetStringBoundingBox(STR_TINY_BLACK_DECIMAL).width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00604 } else {
00605 this->count_width = 0;
00606 }
00607
00608 Dimension unumber = { GetDigitWidth() * 4, FONT_HEIGHT_NORMAL };
00609 const Sprite *spr = GetSprite(SPR_FLAG_VEH_STOPPED, ST_NORMAL);
00610 this->flag_width = UnScaleByZoom(spr->width, ZOOM_LVL_GUI) + WD_FRAMERECT_RIGHT;
00611 this->flag_height = UnScaleByZoom(spr->height, ZOOM_LVL_GUI);
00612
00613 if (this->type == VEH_TRAIN || this->type == VEH_ROAD) {
00614 min_height = max<uint>(unumber.height + WD_MATRIX_TOP, UnScaleByZoom(spr->height, ZOOM_LVL_GUI));
00615 this->header_width = unumber.width + this->flag_width + WD_FRAMERECT_LEFT;
00616 } else {
00617 min_height = unumber.height + UnScaleByZoom(spr->height, ZOOM_LVL_GUI) + WD_MATRIX_TOP + WD_PAR_VSEP_NORMAL + WD_MATRIX_BOTTOM;
00618 this->header_width = max<uint>(unumber.width, this->flag_width) + WD_FRAMERECT_RIGHT;
00619 }
00620 int base_width = this->count_width + this->header_width;
00621
00622 resize->height = max<uint>(GetVehicleImageCellSize(this->type, EIT_IN_DEPOT).height, min_height);
00623 if (this->type == VEH_TRAIN) {
00624 resize->width = 1;
00625 size->width = base_width + 2 * 29;
00626 size->height = resize->height * 6;
00627 } else {
00628 resize->width = base_width + GetVehicleImageCellSize(this->type, EIT_IN_DEPOT).extend_left + GetVehicleImageCellSize(this->type, EIT_IN_DEPOT).extend_right;
00629 size->width = resize->width * (this->type == VEH_ROAD ? 5 : 3);
00630 size->height = resize->height * (this->type == VEH_ROAD ? 5 : 3);
00631 }
00632 fill->width = resize->width;
00633 fill->height = resize->height;
00634 break;
00635 }
00636 }
00637 }
00638
00644 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00645 {
00646 this->generate_list = true;
00647 }
00648
00649 virtual void OnPaint()
00650 {
00651 if (this->generate_list) {
00652
00653
00654 BuildDepotVehicleList(this->type, this->window_number, &this->vehicle_list, &this->wagon_list);
00655 this->generate_list = false;
00656 DepotSortList(&this->vehicle_list);
00657 }
00658
00659
00660 if (this->type == VEH_TRAIN) {
00661 uint max_width = VEHICLEINFO_FULL_VEHICLE_WIDTH;
00662 for (uint num = 0; num < this->vehicle_list.Length(); num++) {
00663 uint width = 0;
00664 for (const Train *v = Train::From(this->vehicle_list[num]); v != NULL; v = v->Next()) {
00665 width += v->GetDisplayImageWidth();
00666 }
00667 max_width = max(max_width, width);
00668 }
00669
00670 this->vscroll->SetCount(this->vehicle_list.Length() + this->wagon_list.Length() + 1);
00671 this->hscroll->SetCount(max_width);
00672 } else {
00673 this->vscroll->SetCount(CeilDiv(this->vehicle_list.Length(), this->num_columns));
00674 }
00675
00676
00677 TileIndex tile = this->window_number;
00678 this->SetWidgetsDisabledState(!IsTileOwner(tile, _local_company),
00679 WID_D_STOP_ALL,
00680 WID_D_START_ALL,
00681 WID_D_SELL,
00682 WID_D_SELL_CHAIN,
00683 WID_D_SELL_ALL,
00684 WID_D_BUILD,
00685 WID_D_CLONE,
00686 WID_D_RENAME,
00687 WID_D_AUTOREPLACE,
00688 WIDGET_LIST_END);
00689
00690 this->DrawWidgets();
00691 }
00692
00693 virtual void OnClick(Point pt, int widget, int click_count)
00694 {
00695 switch (widget) {
00696 case WID_D_MATRIX: {
00697 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_D_MATRIX);
00698 this->DepotClick(pt.x - nwi->pos_x, pt.y - nwi->pos_y);
00699 break;
00700 }
00701
00702 case WID_D_BUILD:
00703 ResetObjectToPlace();
00704 ShowBuildVehicleWindow(this->window_number, this->type);
00705 break;
00706
00707 case WID_D_CLONE:
00708 this->SetWidgetDirty(WID_D_CLONE);
00709 this->ToggleWidgetLoweredState(WID_D_CLONE);
00710
00711 if (this->IsWidgetLowered(WID_D_CLONE)) {
00712 static const CursorID clone_icons[] = {
00713 SPR_CURSOR_CLONE_TRAIN, SPR_CURSOR_CLONE_ROADVEH,
00714 SPR_CURSOR_CLONE_SHIP, SPR_CURSOR_CLONE_AIRPLANE
00715 };
00716
00717 SetObjectToPlaceWnd(clone_icons[this->type], PAL_NONE, HT_VEHICLE, this);
00718 } else {
00719 ResetObjectToPlace();
00720 }
00721 break;
00722
00723 case WID_D_LOCATION:
00724 if (_ctrl_pressed) {
00725 ShowExtraViewPortWindow(this->window_number);
00726 } else {
00727 ScrollMainWindowToTile(this->window_number);
00728 }
00729 break;
00730
00731 case WID_D_RENAME:
00732 SetDParam(0, this->type);
00733 SetDParam(1, Depot::GetByTile((TileIndex)this->window_number)->index);
00734 ShowQueryString(STR_DEPOT_NAME, STR_DEPOT_RENAME_DEPOT_CAPTION, MAX_LENGTH_DEPOT_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00735 break;
00736
00737 case WID_D_STOP_ALL:
00738 case WID_D_START_ALL: {
00739 VehicleListIdentifier vli(VL_DEPOT_LIST, this->type, this->owner);
00740 DoCommandP(this->window_number, (widget == WID_D_START_ALL ? (1 << 0) : 0), vli.Pack(), CMD_MASS_START_STOP);
00741 break;
00742 }
00743
00744 case WID_D_SELL_ALL:
00745
00746 if (this->vehicle_list.Length() != 0 || this->wagon_list.Length() != 0) {
00747 TileIndex tile = this->window_number;
00748 byte vehtype = this->type;
00749
00750 SetDParam(0, vehtype);
00751 SetDParam(1, (vehtype == VEH_AIRCRAFT) ? GetStationIndex(tile) : GetDepotIndex(tile));
00752 ShowQuery(
00753 STR_DEPOT_CAPTION,
00754 STR_DEPOT_SELL_CONFIRMATION_TEXT,
00755 this,
00756 DepotSellAllConfirmationCallback
00757 );
00758 }
00759 break;
00760
00761 case WID_D_VEHICLE_LIST:
00762 ShowVehicleListWindow(GetTileOwner(this->window_number), this->type, (TileIndex)this->window_number);
00763 break;
00764
00765 case WID_D_AUTOREPLACE:
00766 DoCommandP(this->window_number, this->type, 0, CMD_DEPOT_MASS_AUTOREPLACE);
00767 break;
00768
00769 }
00770 }
00771
00772 virtual void OnQueryTextFinished(char *str)
00773 {
00774 if (str == NULL) return;
00775
00776
00777 DoCommandP(0, GetDepotIndex(this->window_number), 0, CMD_RENAME_DEPOT | CMD_MSG(STR_ERROR_CAN_T_RENAME_DEPOT), NULL, str);
00778 }
00779
00780 virtual bool OnRightClick(Point pt, int widget)
00781 {
00782 if (widget != WID_D_MATRIX) return false;
00783
00784 GetDepotVehiclePtData gdvp = { NULL, NULL };
00785 const Vehicle *v = NULL;
00786 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_D_MATRIX);
00787 DepotGUIAction mode = this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, &gdvp);
00788
00789 if (this->type == VEH_TRAIN) v = gdvp.wagon;
00790
00791 if (v == NULL || mode != MODE_DRAG_VEHICLE) return false;
00792
00793 CargoArray capacity, loaded;
00794
00795
00796 bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);
00797
00798
00799 uint num = 0;
00800 for (const Vehicle *w = v; w != NULL; w = w->Next()) {
00801 if (w->cargo_cap > 0 && w->cargo_type < NUM_CARGO) {
00802 capacity[w->cargo_type] += w->cargo_cap;
00803 loaded [w->cargo_type] += w->cargo.Count();
00804 }
00805
00806 if (w->type == VEH_TRAIN && !w->HasArticulatedPart()) {
00807 num++;
00808 if (!whole_chain) break;
00809 }
00810 }
00811
00812
00813 static char details[1024];
00814 details[0] = '\0';
00815 char *pos = details;
00816
00817 for (CargoID cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00818 if (capacity[cargo_type] == 0) continue;
00819
00820 SetDParam(0, cargo_type);
00821 SetDParam(1, loaded[cargo_type]);
00822 SetDParam(2, cargo_type);
00823 SetDParam(3, capacity[cargo_type]);
00824 pos = GetString(pos, STR_DEPOT_VEHICLE_TOOLTIP_CARGO, lastof(details));
00825 }
00826
00827
00828 uint64 args[2];
00829 args[0] = (whole_chain ? num : v->engine_type);
00830 args[1] = (uint64)(size_t)details;
00831 GuiShowTooltips(this, whole_chain ? STR_DEPOT_VEHICLE_TOOLTIP_CHAIN : STR_DEPOT_VEHICLE_TOOLTIP, 2, args, TCC_RIGHT_CLICK);
00832
00833 return true;
00834 }
00835
00841 virtual bool OnVehicleSelect(const Vehicle *v)
00842 {
00843
00844 if (DoCommandP(this->window_number, v->index, _ctrl_pressed ? 1 : 0, CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN + v->type), _ctrl_pressed ? NULL : CcCloneVehicle)) {
00845 ResetObjectToPlace();
00846 }
00847 return true;
00848 }
00849
00850 virtual void OnPlaceObjectAbort()
00851 {
00852
00853 this->RaiseWidget(WID_D_CLONE);
00854 this->SetWidgetDirty(WID_D_CLONE);
00855
00856
00857 this->sel = INVALID_VEHICLE;
00858 this->vehicle_over = INVALID_VEHICLE;
00859 this->SetWidgetDirty(WID_D_MATRIX);
00860 }
00861
00862 virtual void OnMouseDrag(Point pt, int widget)
00863 {
00864 if (this->type != VEH_TRAIN || this->sel == INVALID_VEHICLE) return;
00865
00866
00867 if (widget != WID_D_MATRIX) {
00868 if (this->vehicle_over != INVALID_VEHICLE) {
00869 this->vehicle_over = INVALID_VEHICLE;
00870 this->SetWidgetDirty(WID_D_MATRIX);
00871 }
00872 return;
00873 }
00874
00875 NWidgetBase *matrix = this->GetWidget<NWidgetBase>(widget);
00876 const Vehicle *v = NULL;
00877 GetDepotVehiclePtData gdvp = {NULL, NULL};
00878
00879 if (this->GetVehicleFromDepotWndPt(pt.x - matrix->pos_x, pt.y - matrix->pos_y, &v, &gdvp) != MODE_DRAG_VEHICLE) return;
00880
00881 VehicleID new_vehicle_over = INVALID_VEHICLE;
00882 if (gdvp.head != NULL) {
00883 if (gdvp.wagon == NULL && gdvp.head->Last()->index != this->sel) {
00884
00885
00886
00887
00888 new_vehicle_over = gdvp.head->index;
00889 } else if (gdvp.wagon != NULL && gdvp.head != gdvp.wagon &&
00890 gdvp.wagon->index != this->sel &&
00891 gdvp.wagon->Previous()->index != this->sel) {
00892 new_vehicle_over = gdvp.wagon->index;
00893 }
00894 }
00895
00896 if (this->vehicle_over == new_vehicle_over) return;
00897
00898 this->vehicle_over = new_vehicle_over;
00899 this->SetWidgetDirty(widget);
00900 }
00901
00902 virtual void OnDragDrop(Point pt, int widget)
00903 {
00904 switch (widget) {
00905 case WID_D_MATRIX: {
00906 const Vehicle *v = NULL;
00907 VehicleID sel = this->sel;
00908
00909 this->sel = INVALID_VEHICLE;
00910 this->SetDirty();
00911
00912 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_D_MATRIX);
00913 if (this->type == VEH_TRAIN) {
00914 GetDepotVehiclePtData gdvp = { NULL, NULL };
00915
00916 if (this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, &gdvp) == MODE_DRAG_VEHICLE && sel != INVALID_VEHICLE) {
00917 if (gdvp.wagon != NULL && gdvp.wagon->index == sel && _ctrl_pressed) {
00918 DoCommandP(Vehicle::Get(sel)->tile, Vehicle::Get(sel)->index, true,
00919 CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE));
00920 } else if (gdvp.wagon == NULL || gdvp.wagon->index != sel) {
00921 this->vehicle_over = INVALID_VEHICLE;
00922 TrainDepotMoveVehicle(gdvp.wagon, sel, gdvp.head);
00923 } else if (gdvp.head != NULL && gdvp.head->IsFrontEngine()) {
00924 ShowVehicleViewWindow(gdvp.head);
00925 }
00926 }
00927 } else if (this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, NULL) == MODE_DRAG_VEHICLE && v != NULL && sel == v->index) {
00928 ShowVehicleViewWindow(v);
00929 }
00930 break;
00931 }
00932
00933 case WID_D_SELL: case WID_D_SELL_CHAIN: {
00934 if (this->IsWidgetDisabled(widget)) return;
00935 if (this->sel == INVALID_VEHICLE) return;
00936
00937 this->HandleButtonClick(widget);
00938
00939 const Vehicle *v = Vehicle::Get(this->sel);
00940 this->sel = INVALID_VEHICLE;
00941 this->SetDirty();
00942
00943 int sell_cmd = (v->type == VEH_TRAIN && (widget == WID_D_SELL_CHAIN || _ctrl_pressed)) ? 1 : 0;
00944 DoCommandP(v->tile, v->index | sell_cmd << 20 | MAKE_ORDER_BACKUP_FLAG, 0, GetCmdSellVeh(v->type));
00945 break;
00946 }
00947
00948 default:
00949 this->sel = INVALID_VEHICLE;
00950 this->SetDirty();
00951 }
00952 _cursor.vehchain = false;
00953 }
00954
00955 virtual void OnTimeout()
00956 {
00957 if (!this->IsWidgetDisabled(WID_D_SELL)) {
00958 this->RaiseWidget(WID_D_SELL);
00959 this->SetWidgetDirty(WID_D_SELL);
00960 }
00961 if (this->nested_array[WID_D_SELL] != NULL && !this->IsWidgetDisabled(WID_D_SELL_CHAIN)) {
00962 this->RaiseWidget(WID_D_SELL_CHAIN);
00963 this->SetWidgetDirty(WID_D_SELL_CHAIN);
00964 }
00965 }
00966
00967 virtual void OnResize()
00968 {
00969 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_D_MATRIX);
00970 this->vscroll->SetCapacityFromWidget(this, WID_D_MATRIX);
00971 if (this->type == VEH_TRAIN) {
00972 this->hscroll->SetCapacity(nwi->current_x - this->header_width - this->count_width);
00973 nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00974 } else {
00975 this->num_columns = nwi->current_x / nwi->resize_x;
00976 nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (this->num_columns << MAT_COL_START);
00977 }
00978 }
00979
00980 virtual EventState OnCTRLStateChange()
00981 {
00982 if (this->sel != INVALID_VEHICLE) {
00983 _cursor.vehchain = _ctrl_pressed;
00984 this->SetWidgetDirty(WID_D_MATRIX);
00985 return ES_HANDLED;
00986 }
00987
00988 return ES_NOT_HANDLED;
00989 }
00990 };
00991
00992 static void DepotSellAllConfirmationCallback(Window *win, bool confirmed)
00993 {
00994 if (confirmed) {
00995 DepotWindow *w = (DepotWindow*)win;
00996 TileIndex tile = w->window_number;
00997 byte vehtype = w->type;
00998 DoCommandP(tile, vehtype, 0, CMD_DEPOT_SELL_ALL_VEHICLES);
00999 }
01000 }
01001
01007 void ShowDepotWindow(TileIndex tile, VehicleType type)
01008 {
01009 if (BringWindowToFrontById(WC_VEHICLE_DEPOT, tile) != NULL) return;
01010
01011 const WindowDesc *desc;
01012 switch (type) {
01013 default: NOT_REACHED();
01014 case VEH_TRAIN: desc = &_train_depot_desc; break;
01015 case VEH_ROAD: desc = &_road_depot_desc; break;
01016 case VEH_SHIP: desc = &_ship_depot_desc; break;
01017 case VEH_AIRCRAFT: desc = &_aircraft_depot_desc; break;
01018 }
01019
01020 new DepotWindow(desc, tile, type);
01021 }
01022
01027 void DeleteDepotHighlightOfVehicle(const Vehicle *v)
01028 {
01029 DepotWindow *w;
01030
01031
01032
01033
01034 if (_special_mouse_mode != WSM_DRAGDROP) return;
01035
01036 w = dynamic_cast<DepotWindow*>(FindWindowById(WC_VEHICLE_DEPOT, v->tile));
01037 if (w != NULL) {
01038 if (w->sel == v->index) ResetObjectToPlace();
01039 }
01040 }