00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "train.h"
00014 #include "ship.h"
00015 #include "aircraft.h"
00016 #include "gui.h"
00017 #include "textbuf_gui.h"
00018 #include "viewport_func.h"
00019 #include "command_func.h"
00020 #include "depot_base.h"
00021 #include "vehicle_gui.h"
00022 #include "spritecache.h"
00023 #include "strings_func.h"
00024 #include "window_func.h"
00025 #include "vehicle_func.h"
00026 #include "company_func.h"
00027 #include "tilehighlight_func.h"
00028 #include "window_gui.h"
00029 #include "vehiclelist.h"
00030 #include "order_backup.h"
00031 #include "company_base.h"
00032 #include "infrastructure_func.h"
00033
00034 #include "table/strings.h"
00035
00036
00037
00038
00039
00040
00041
00042
00043 enum DepotWindowWidgets {
00044 DEPOT_WIDGET_CAPTION,
00045 DEPOT_WIDGET_SELL,
00046 DEPOT_WIDGET_SHOW_SELL_CHAIN,
00047 DEPOT_WIDGET_SELL_CHAIN,
00048 DEPOT_WIDGET_SELL_ALL,
00049 DEPOT_WIDGET_AUTOREPLACE,
00050 DEPOT_WIDGET_MATRIX,
00051 DEPOT_WIDGET_V_SCROLL,
00052 DEPOT_WIDGET_SHOW_H_SCROLL,
00053 DEPOT_WIDGET_H_SCROLL,
00054 DEPOT_WIDGET_BUILD,
00055 DEPOT_WIDGET_CLONE,
00056 DEPOT_WIDGET_LOCATION,
00057 DEPOT_WIDGET_SHOW_RENAME,
00058 DEPOT_WIDGET_RENAME,
00059 DEPOT_WIDGET_VEHICLE_LIST,
00060 DEPOT_WIDGET_STOP_ALL,
00061 DEPOT_WIDGET_START_ALL,
00062 };
00063
00065 static const NWidgetPart _nested_train_depot_widgets[] = {
00066 NWidget(NWID_HORIZONTAL),
00067 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00068 NWidget(WWT_CAPTION, COLOUR_GREY, DEPOT_WIDGET_CAPTION), SetDataTip(STR_DEPOT_CAPTION, STR_NULL),
00069 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00070 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00071 EndContainer(),
00072 NWidget(NWID_HORIZONTAL),
00073 NWidget(NWID_VERTICAL),
00074 NWidget(WWT_MATRIX, COLOUR_GREY, DEPOT_WIDGET_MATRIX), SetDataTip(0x0, STR_NULL), SetResize(1, 1), SetScrollbar(DEPOT_WIDGET_V_SCROLL),
00075 NWidget(NWID_SELECTION, INVALID_COLOUR, DEPOT_WIDGET_SHOW_H_SCROLL),
00076 NWidget(NWID_HSCROLLBAR, COLOUR_GREY, DEPOT_WIDGET_H_SCROLL),
00077 EndContainer(),
00078 EndContainer(),
00079 NWidget(NWID_VERTICAL),
00080 NWidget(WWT_IMGBTN, COLOUR_GREY, DEPOT_WIDGET_SELL), SetDataTip(0x0, STR_NULL), SetResize(0, 1), SetFill(0, 1),
00081 NWidget(NWID_SELECTION, INVALID_COLOUR, DEPOT_WIDGET_SHOW_SELL_CHAIN),
00082 NWidget(WWT_IMGBTN, COLOUR_GREY, DEPOT_WIDGET_SELL_CHAIN), SetDataTip(SPR_SELL_CHAIN_TRAIN, STR_DEPOT_DRAG_WHOLE_TRAIN_TO_SELL_TOOLTIP), SetResize(0, 1), SetFill(0, 1),
00083 EndContainer(),
00084 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_SELL_ALL), SetDataTip(0x0, STR_NULL),
00085 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_AUTOREPLACE), SetDataTip(0x0, STR_NULL),
00086 EndContainer(),
00087 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, DEPOT_WIDGET_V_SCROLL),
00088 EndContainer(),
00089 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00090 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_BUILD), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00091 NWidget(WWT_TEXTBTN, COLOUR_GREY, DEPOT_WIDGET_CLONE), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00092 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_LOCATION), SetDataTip(STR_BUTTON_LOCATION, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00093 NWidget(NWID_SELECTION, INVALID_COLOUR, DEPOT_WIDGET_SHOW_RENAME),
00094 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_RENAME), SetDataTip(STR_BUTTON_RENAME, STR_DEPOT_RENAME_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
00095 EndContainer(),
00096 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_VEHICLE_LIST), SetDataTip(0x0, STR_NULL), SetFill(0, 1),
00097 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_STOP_ALL), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_NULL), SetFill(0, 1),
00098 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_START_ALL), SetDataTip(SPR_FLAG_VEH_RUNNING, STR_NULL), SetFill(0, 1),
00099 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00100 EndContainer(),
00101 };
00102
00103 static const WindowDesc _train_depot_desc(
00104 WDP_AUTO, 362, 123,
00105 WC_VEHICLE_DEPOT, WC_NONE,
00106 WDF_UNCLICK_BUTTONS,
00107 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00108 );
00109
00110 static const WindowDesc _road_depot_desc(
00111 WDP_AUTO, 316, 97,
00112 WC_VEHICLE_DEPOT, WC_NONE,
00113 WDF_UNCLICK_BUTTONS,
00114 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00115 );
00116
00117 static const WindowDesc _ship_depot_desc(
00118 WDP_AUTO, 306, 99,
00119 WC_VEHICLE_DEPOT, WC_NONE,
00120 WDF_UNCLICK_BUTTONS,
00121 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00122 );
00123
00124 static const WindowDesc _aircraft_depot_desc(
00125 WDP_AUTO, 332, 99,
00126 WC_VEHICLE_DEPOT, WC_NONE,
00127 WDF_UNCLICK_BUTTONS,
00128 _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00129 );
00130
00131 extern void DepotSortList(VehicleList *list);
00132
00140 void CcCloneVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00141 {
00142 if (result.Failed()) return;
00143
00144 const Vehicle *v = Vehicle::Get(_new_vehicle_id);
00145
00146 if (!_ctrl_pressed) ShowVehicleViewWindow(v);
00147 }
00148
00149 static void TrainDepotMoveVehicle(const Vehicle *wagon, VehicleID sel, const Vehicle *head)
00150 {
00151 const Vehicle *v = Vehicle::Get(sel);
00152
00153 if (v == wagon) return;
00154
00155 if (wagon == NULL) {
00156 if (head != NULL) wagon = head->Last();
00157 } else {
00158 wagon = wagon->Previous();
00159 if (wagon == NULL) return;
00160 }
00161
00162 if (wagon == v) return;
00163
00164 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));
00165 }
00166
00171 static Dimension _base_block_sizes[4];
00172
00173 static void InitBlocksizeForShipAircraft(VehicleType type)
00174 {
00175 uint max_width = 0;
00176 uint max_height = 0;
00177
00178 const Engine *e;
00179 FOR_ALL_ENGINES_OF_TYPE(e, type) {
00180 EngineID eid = e->index;
00181 uint x, y;
00182
00183 switch (type) {
00184 default: NOT_REACHED();
00185 case VEH_SHIP: GetShipSpriteSize( eid, x, y); break;
00186 case VEH_AIRCRAFT: GetAircraftSpriteSize(eid, x, y); break;
00187 }
00188 if (x > max_width) max_width = x;
00189 if (y > max_height) max_height = y;
00190 }
00191
00192 switch (type) {
00193 default: NOT_REACHED();
00194 case VEH_SHIP:
00195 _base_block_sizes[VEH_SHIP].width = max(76U, max_width);
00196 break;
00197 case VEH_AIRCRAFT:
00198 _base_block_sizes[VEH_AIRCRAFT].width = max(67U, max_width);
00199 break;
00200 }
00201 _base_block_sizes[type].height = max(GetVehicleHeight(type), max_height);
00202 }
00203
00208 void InitDepotWindowBlockSizes()
00209 {
00210 _base_block_sizes[VEH_TRAIN].width = 0;
00211 _base_block_sizes[VEH_TRAIN].height = GetVehicleHeight(VEH_TRAIN);
00212
00213 _base_block_sizes[VEH_ROAD].width = 32;
00214 _base_block_sizes[VEH_ROAD].height = GetVehicleHeight(VEH_ROAD);
00215
00216 InitBlocksizeForShipAircraft(VEH_SHIP);
00217 InitBlocksizeForShipAircraft(VEH_AIRCRAFT);
00218 }
00219
00220 static void DepotSellAllConfirmationCallback(Window *w, bool confirmed);
00221 const Sprite *GetAircraftSprite(EngineID engine);
00222
00223 struct DepotWindow : Window {
00224 VehicleID sel;
00225 VehicleID vehicle_over;
00226 VehicleType type;
00227 bool generate_list;
00228 VehicleList vehicle_list;
00229 VehicleList wagon_list;
00230 uint num_columns;
00231 Scrollbar *hscroll;
00232 Scrollbar *vscroll;
00233
00234 DepotWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window()
00235 {
00236 assert(IsCompanyBuildableVehicleType(type));
00237
00238 this->sel = INVALID_VEHICLE;
00239 this->vehicle_over = INVALID_VEHICLE;
00240 this->generate_list = true;
00241 this->type = type;
00242 this->num_columns = 1;
00243
00244 this->CreateNestedTree(desc);
00245 this->hscroll = (this->type == VEH_TRAIN ? this->GetScrollbar(DEPOT_WIDGET_H_SCROLL) : NULL);
00246 this->vscroll = this->GetScrollbar(DEPOT_WIDGET_V_SCROLL);
00247
00248 this->GetWidget<NWidgetStacked>(DEPOT_WIDGET_SHOW_RENAME)->SetDisplayedPlane(type == VEH_AIRCRAFT ? SZSP_NONE : 0);
00249
00250 this->GetWidget<NWidgetStacked>(DEPOT_WIDGET_SHOW_H_SCROLL)->SetDisplayedPlane(type == VEH_TRAIN ? 0 : SZSP_HORIZONTAL);
00251 this->GetWidget<NWidgetStacked>(DEPOT_WIDGET_SHOW_SELL_CHAIN)->SetDisplayedPlane(type == VEH_TRAIN ? 0 : SZSP_NONE);
00252 this->SetupWidgetData(type);
00253 this->FinishInitNested(desc, tile);
00254
00255 this->owner = GetTileOwner(tile);
00256 OrderBackup::Reset();
00257 }
00258
00259 ~DepotWindow()
00260 {
00261 DeleteWindowById(WC_BUILD_VEHICLE, this->window_number);
00262 OrderBackup::Reset(this->window_number);
00263 }
00264
00272 void DrawVehicleInDepot(const Vehicle *v, int left, int right, int y) const
00273 {
00274 bool free_wagon = false;
00275 int sprite_y = y + (this->resize.step_height - GetVehicleHeight(v->type)) / 2;
00276
00277 bool rtl = _current_text_dir == TD_RTL;
00278 int image_left = rtl ? left + this->count_width : left + this->header_width;
00279 int image_right = rtl ? right - this->header_width : right - this->count_width;
00280
00281 switch (v->type) {
00282 case VEH_TRAIN: {
00283 const Train *u = Train::From(v);
00284 free_wagon = u->IsFreeWagon();
00285
00286 uint x_space = free_wagon ? TRAININFO_DEFAULT_VEHICLE_WIDTH : 0;
00287 DrawTrainImage(u, image_left + (rtl ? 0 : x_space), image_right - (rtl ? x_space : 0), sprite_y - 1,
00288 this->sel, free_wagon ? 0 : this->hscroll->GetPosition(), this->vehicle_over);
00289
00290
00291 SetDParam(0, CeilDiv(u->gcache.cached_total_length * 10, TILE_SIZE));
00292 SetDParam(1, 1);
00293 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);
00294 break;
00295 }
00296
00297 case VEH_ROAD: DrawRoadVehImage( v, image_left, image_right, sprite_y, this->sel); break;
00298 case VEH_SHIP: DrawShipImage( v, image_left, image_right, sprite_y, this->sel); break;
00299 case VEH_AIRCRAFT: {
00300 const Sprite *spr = GetSprite(v->GetImage(DIR_W), ST_NORMAL);
00301 DrawAircraftImage(v, image_left, image_right,
00302 y + max(spr->height + spr->y_offs - 14, 0),
00303 this->sel);
00304 break;
00305 }
00306 default: NOT_REACHED();
00307 }
00308
00309 uint diff_x, diff_y;
00310 if (v->IsGroundVehicle()) {
00311
00312 diff_x = this->flag_width + WD_FRAMERECT_LEFT;
00313 diff_y = (this->resize.step_height - this->flag_height) / 2 - 2;
00314 } else {
00315
00316 diff_x = WD_FRAMERECT_LEFT;
00317 diff_y = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00318 }
00319 int text_left = rtl ? right - this->header_width - 1 : left + diff_x;
00320 int text_right = rtl ? right - diff_x : left + this->header_width - 1;
00321
00322 if (free_wagon) {
00323 DrawString(text_left, text_right, y + 2, STR_DEPOT_NO_ENGINE);
00324 } else {
00325 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);
00326
00327 SetDParam(0, v->unitnumber);
00328 DrawString(text_left, text_right, y + 2, (uint16)(v->max_age - DAYS_IN_LEAP_YEAR) >= v->age ? STR_BLACK_COMMA : STR_RED_COMMA);
00329 }
00330 }
00331
00332 void DrawWidget(const Rect &r, int widget) const
00333 {
00334 if (widget != DEPOT_WIDGET_MATRIX) return;
00335
00336 bool rtl = _current_text_dir == TD_RTL;
00337
00338
00339 uint16 mat_data = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->widget_data;
00340 uint16 rows_in_display = GB(mat_data, MAT_ROW_START, MAT_ROW_BITS);
00341 uint16 boxes_in_each_row = GB(mat_data, MAT_COL_START, MAT_COL_BITS);
00342
00343 uint16 num = this->vscroll->GetPosition() * boxes_in_each_row;
00344 int maxval = min(this->vehicle_list.Length(), num + (rows_in_display * boxes_in_each_row));
00345 int y;
00346 for (y = r.top + 1; num < maxval; y += this->resize.step_height) {
00347 for (byte i = 0; i < boxes_in_each_row && num < maxval; i++, num++) {
00348
00349 const Vehicle *v = this->vehicle_list[num];
00350 if (boxes_in_each_row == 1) {
00351 this->DrawVehicleInDepot(v, r.left, r.right, y);
00352 } else {
00353 int x = r.left + (rtl ? (boxes_in_each_row - i - 1) : i) * this->resize.step_width;
00354 this->DrawVehicleInDepot(v, x, x + this->resize.step_width - 1, y);
00355 }
00356 }
00357 }
00358
00359 maxval = min(this->vehicle_list.Length() + this->wagon_list.Length(), (this->vscroll->GetPosition() * boxes_in_each_row) + (rows_in_display * boxes_in_each_row));
00360
00361
00362 for (; num < maxval; num++, y += this->resize.step_height) {
00363 const Vehicle *v = this->wagon_list[num - this->vehicle_list.Length()];
00364 this->DrawVehicleInDepot(v, r.left, r.right, y);
00365 }
00366 }
00367
00368 void SetStringParameters(int widget) const
00369 {
00370 if (widget != DEPOT_WIDGET_CAPTION) return;
00371
00372
00373 TileIndex tile = this->window_number;
00374 SetDParam(0, this->type);
00375 SetDParam(1, (this->type == VEH_AIRCRAFT) ? GetStationIndex(tile) : GetDepotIndex(tile));
00376 }
00377
00378 struct GetDepotVehiclePtData {
00379 const Vehicle *head;
00380 const Vehicle *wagon;
00381 };
00382
00383 enum DepotGUIAction {
00384 MODE_ERROR,
00385 MODE_DRAG_VEHICLE,
00386 MODE_SHOW_VEHICLE,
00387 MODE_START_STOP,
00388 };
00389
00390 DepotGUIAction GetVehicleFromDepotWndPt(int x, int y, const Vehicle **veh, GetDepotVehiclePtData *d) const
00391 {
00392 const NWidgetCore *matrix_widget = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX);
00393
00394 if (_current_text_dir == TD_RTL) x = matrix_widget->current_x - x;
00395
00396 uint xt = 0, xm = 0, ym = 0;
00397 if (this->type == VEH_TRAIN) {
00398 xm = x;
00399 } else {
00400 xt = x / this->resize.step_width;
00401 xm = x % this->resize.step_width;
00402 if (xt >= this->num_columns) return MODE_ERROR;
00403 }
00404 ym = y % this->resize.step_height;
00405
00406 uint row = y / this->resize.step_height;
00407 if (row >= this->vscroll->GetCapacity()) return MODE_ERROR;
00408
00409 uint boxes_in_each_row = GB(matrix_widget->widget_data, MAT_COL_START, MAT_COL_BITS);
00410 uint pos = ((row + this->vscroll->GetPosition()) * boxes_in_each_row) + xt;
00411
00412 if (this->vehicle_list.Length() + this->wagon_list.Length() <= pos) {
00413
00414 if (this->type == VEH_TRAIN) {
00415
00416 d->head = NULL;
00417 d->wagon = NULL;
00418 return MODE_DRAG_VEHICLE;
00419 } else {
00420 return MODE_ERROR;
00421 }
00422 }
00423
00424 bool wagon = false;
00425 if (this->vehicle_list.Length() > pos) {
00426 *veh = this->vehicle_list[pos];
00427
00428 if (this->type == VEH_TRAIN) x += this->hscroll->GetPosition();
00429 } else {
00430 pos -= this->vehicle_list.Length();
00431 *veh = this->wagon_list[pos];
00432
00433 x -= VEHICLEINFO_FULL_VEHICLE_WIDTH;
00434 wagon = true;
00435 }
00436
00437 const Train *v = NULL;
00438 if (this->type == VEH_TRAIN) {
00439 v = Train::From(*veh);
00440 d->head = d->wagon = v;
00441 }
00442
00443 if (xm <= this->header_width) {
00444 switch (this->type) {
00445 case VEH_TRAIN:
00446 if (wagon) return MODE_ERROR;
00447 case VEH_ROAD:
00448 if (xm <= this->flag_width) return MODE_START_STOP;
00449 break;
00450
00451 case VEH_SHIP:
00452 case VEH_AIRCRAFT:
00453 if (xm <= this->flag_width && ym >= (uint)(FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL)) return MODE_START_STOP;
00454 break;
00455
00456 default: NOT_REACHED();
00457 }
00458 return MODE_SHOW_VEHICLE;
00459 }
00460
00461 if (this->type != VEH_TRAIN) return MODE_DRAG_VEHICLE;
00462
00463
00464 if (xm >= matrix_widget->current_x - this->count_width) return wagon ? MODE_ERROR : MODE_SHOW_VEHICLE;
00465
00466
00467 x -= this->header_width;
00468
00469
00470 for (; v != NULL; v = v->Next()) {
00471 x -= v->GetDisplayImageWidth();
00472 if (x < 0) break;
00473 }
00474
00475 d->wagon = (v != NULL ? v->GetFirstEnginePart() : NULL);
00476
00477 return MODE_DRAG_VEHICLE;
00478 }
00479
00485 void DepotClick(int x, int y)
00486 {
00487 GetDepotVehiclePtData gdvp = { NULL, NULL };
00488 const Vehicle *v = NULL;
00489 DepotGUIAction mode = this->GetVehicleFromDepotWndPt(x, y, &v, &gdvp);
00490
00491 if (this->type == VEH_TRAIN) v = gdvp.wagon;
00492
00493 switch (mode) {
00494 case MODE_ERROR:
00495 return;
00496
00497 case MODE_DRAG_VEHICLE: {
00498 if (v != NULL && VehicleClicked(v)) return;
00499
00500 VehicleID sel = this->sel;
00501
00502 if (this->type == VEH_TRAIN && sel != INVALID_VEHICLE) {
00503 this->sel = INVALID_VEHICLE;
00504 TrainDepotMoveVehicle(v, sel, gdvp.head);
00505 } else if (v != NULL) {
00506 int image = v->GetImage(_current_text_dir == TD_RTL ? DIR_E : DIR_W);
00507 SetObjectToPlaceWnd(image, GetVehiclePalette(v), HT_DRAG, this);
00508
00509 this->sel = v->index;
00510 this->SetDirty();
00511
00512 _cursor.short_vehicle_offset = v->IsGroundVehicle() ? 16 - v->GetGroundVehicleCache()->cached_veh_length * 2 : 0;
00513 _cursor.vehchain = _ctrl_pressed;
00514 }
00515 break;
00516 }
00517
00518 case MODE_SHOW_VEHICLE:
00519 ShowVehicleViewWindow(v);
00520 break;
00521
00522 case MODE_START_STOP:
00523 StartStopVehicle(v, false);
00524 break;
00525
00526 default: NOT_REACHED();
00527 }
00528 }
00529
00536 void SetupWidgetData(VehicleType type)
00537 {
00538 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_STOP_ALL)->tool_tip = STR_DEPOT_MASS_STOP_DEPOT_TRAIN_TOOLTIP + type;
00539 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_START_ALL)->tool_tip = STR_DEPOT_MASS_START_DEPOT_TRAIN_TOOLTIP + type;
00540 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->tool_tip = STR_DEPOT_TRAIN_SELL_TOOLTIP + type;
00541 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->tool_tip = STR_DEPOT_SELL_ALL_BUTTON_TRAIN_TOOLTIP + type;
00542
00543 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_BUILD)->SetDataTip(STR_DEPOT_TRAIN_NEW_VEHICLES_BUTTON + type, STR_DEPOT_TRAIN_NEW_VEHICLES_TOOLTIP + type);
00544 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_CLONE)->SetDataTip(STR_DEPOT_CLONE_TRAIN + type, STR_DEPOT_CLONE_TRAIN_DEPOT_INFO + type);
00545
00546 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_LOCATION)->tool_tip = STR_DEPOT_TRAIN_LOCATION_TOOLTIP + type;
00547 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->tool_tip = STR_DEPOT_VEHICLE_ORDER_LIST_TRAIN_TOOLTIP + type;
00548 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->tool_tip = STR_DEPOT_AUTOREPLACE_TRAIN_TOOLTIP + type;
00549 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->tool_tip = STR_DEPOT_TRAIN_LIST_TOOLTIP + this->type;
00550
00551 switch (type) {
00552 default: NOT_REACHED();
00553
00554 case VEH_TRAIN:
00555 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_TRAIN;
00556
00557
00558 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data = SPR_SELL_TRAIN;
00559 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data = SPR_SELL_ALL_TRAIN;
00560 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_TRAIN;
00561 break;
00562
00563 case VEH_ROAD:
00564 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_LORRY;
00565
00566
00567 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data = SPR_SELL_ROADVEH;
00568 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data = SPR_SELL_ALL_ROADVEH;
00569 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_ROADVEH;
00570 break;
00571
00572 case VEH_SHIP:
00573 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_SHIP;
00574
00575
00576 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data = SPR_SELL_SHIP;
00577 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data = SPR_SELL_ALL_SHIP;
00578 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_SHIP;
00579 break;
00580
00581 case VEH_AIRCRAFT:
00582 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_PLANE;
00583
00584
00585 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data = SPR_SELL_AIRCRAFT;
00586 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data = SPR_SELL_ALL_AIRCRAFT;
00587 this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_AIRCRAFT;
00588 break;
00589 }
00590 }
00591
00592 uint count_width;
00593 uint header_width;
00594 uint flag_width;
00595 uint flag_height;
00596
00597 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00598 {
00599 switch (widget) {
00600 case DEPOT_WIDGET_MATRIX: {
00601 uint min_height = 0;
00602
00603 if (this->type == VEH_TRAIN) {
00604 SetDParam(0, 1000);
00605 SetDParam(1, 1);
00606 this->count_width = GetStringBoundingBox(STR_TINY_BLACK_DECIMAL).width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00607 } else {
00608 this->count_width = 0;
00609 }
00610
00611 Dimension unumber = { GetDigitWidth() * 4, FONT_HEIGHT_NORMAL };
00612 const Sprite *spr = GetSprite(SPR_FLAG_VEH_STOPPED, ST_NORMAL);
00613 this->flag_width = spr->width + WD_FRAMERECT_RIGHT;
00614 this->flag_height = spr->height;
00615
00616 if (this->type == VEH_TRAIN || this->type == VEH_ROAD) {
00617 min_height = max<uint>(unumber.height + WD_MATRIX_TOP, spr->height);
00618 this->header_width = unumber.width + this->flag_width + WD_FRAMERECT_LEFT;
00619 } else {
00620 min_height = unumber.height + spr->height + WD_MATRIX_TOP + WD_PAR_VSEP_NORMAL + WD_MATRIX_BOTTOM;
00621 this->header_width = max<uint>(unumber.width, this->flag_width) + WD_FRAMERECT_RIGHT;
00622 }
00623 int base_width = this->count_width + this->header_width;
00624
00625 resize->height = max(_base_block_sizes[this->type].height, min_height);
00626 if (this->type == VEH_TRAIN) {
00627 resize->width = 1;
00628 size->width = base_width + 2 * 29;
00629 size->height = resize->height * 6;
00630 } else {
00631 resize->width = base_width + _base_block_sizes[this->type].width;
00632 size->width = resize->width * (this->type == VEH_ROAD ? 5 : 3);
00633 size->height = resize->height * (this->type == VEH_ROAD ? 5 : 3);
00634 }
00635 fill->width = resize->width;
00636 fill->height = resize->height;
00637 break;
00638 }
00639 }
00640 }
00641
00647 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00648 {
00649 this->generate_list = true;
00650 }
00651
00652 virtual void OnPaint()
00653 {
00654 if (this->generate_list) {
00655
00656
00657 BuildDepotVehicleList(this->type, this->window_number, &this->vehicle_list, &this->wagon_list);
00658 this->generate_list = false;
00659 DepotSortList(&this->vehicle_list);
00660 }
00661
00662
00663 if (this->type == VEH_TRAIN) {
00664 uint max_width = VEHICLEINFO_FULL_VEHICLE_WIDTH;
00665 for (uint num = 0; num < this->vehicle_list.Length(); num++) {
00666 uint width = 0;
00667 for (const Train *v = Train::From(this->vehicle_list[num]); v != NULL; v = v->Next()) {
00668 width += v->GetDisplayImageWidth();
00669 }
00670 max_width = max(max_width, width);
00671 }
00672
00673 this->vscroll->SetCount(this->vehicle_list.Length() + this->wagon_list.Length() + 1);
00674 this->hscroll->SetCount(max_width);
00675 } else {
00676 this->vscroll->SetCount(CeilDiv(this->vehicle_list.Length(), this->num_columns));
00677 }
00678
00679
00680 TileIndex tile = this->window_number;
00681 this->SetWidgetsDisabledState(!Company::IsValidID(_local_company) || !IsInfraTileUsageAllowed(this->type, _local_company, tile),
00682 DEPOT_WIDGET_STOP_ALL,
00683 DEPOT_WIDGET_START_ALL,
00684 DEPOT_WIDGET_SELL,
00685 DEPOT_WIDGET_SELL_CHAIN,
00686 DEPOT_WIDGET_SELL_ALL,
00687 DEPOT_WIDGET_BUILD,
00688 DEPOT_WIDGET_CLONE,
00689 DEPOT_WIDGET_RENAME,
00690 DEPOT_WIDGET_AUTOREPLACE,
00691 WIDGET_LIST_END);
00692
00693 this->DrawWidgets();
00694 }
00695
00696 virtual void OnClick(Point pt, int widget, int click_count)
00697 {
00698 switch (widget) {
00699 case DEPOT_WIDGET_MATRIX: {
00700 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(DEPOT_WIDGET_MATRIX);
00701 this->DepotClick(pt.x - nwi->pos_x, pt.y - nwi->pos_y);
00702 break;
00703 }
00704
00705 case DEPOT_WIDGET_BUILD:
00706 ResetObjectToPlace();
00707 ShowBuildVehicleWindow(this->window_number, this->type);
00708 break;
00709
00710 case DEPOT_WIDGET_CLONE:
00711 this->SetWidgetDirty(DEPOT_WIDGET_CLONE);
00712 this->ToggleWidgetLoweredState(DEPOT_WIDGET_CLONE);
00713
00714 if (this->IsWidgetLowered(DEPOT_WIDGET_CLONE)) {
00715 static const CursorID clone_icons[] = {
00716 SPR_CURSOR_CLONE_TRAIN, SPR_CURSOR_CLONE_ROADVEH,
00717 SPR_CURSOR_CLONE_SHIP, SPR_CURSOR_CLONE_AIRPLANE
00718 };
00719
00720 SetObjectToPlaceWnd(clone_icons[this->type], PAL_NONE, HT_VEHICLE | HT_RECT, this);
00721 } else {
00722 ResetObjectToPlace();
00723 }
00724 break;
00725
00726 case DEPOT_WIDGET_LOCATION:
00727 if (_ctrl_pressed) {
00728 ShowExtraViewPortWindow(this->window_number);
00729 } else {
00730 ScrollMainWindowToTile(this->window_number);
00731 }
00732 break;
00733
00734 case DEPOT_WIDGET_RENAME:
00735 SetDParam(0, this->type);
00736 SetDParam(1, Depot::GetByTile((TileIndex)this->window_number)->index);
00737 ShowQueryString(STR_DEPOT_NAME, STR_DEPOT_RENAME_DEPOT_CAPTION, MAX_LENGTH_DEPOT_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00738 break;
00739
00740 case DEPOT_WIDGET_STOP_ALL:
00741 case DEPOT_WIDGET_START_ALL: {
00742 VehicleListIdentifier vli(VL_DEPOT_LIST, this->type, this->owner);
00743 DoCommandP(this->window_number, (widget == DEPOT_WIDGET_START_ALL ? (1 << 0) : 0), vli.Pack(), CMD_MASS_START_STOP);
00744 break;
00745 }
00746
00747 case DEPOT_WIDGET_SELL_ALL:
00748
00749 if (this->vehicle_list.Length() != 0 || this->wagon_list.Length() != 0) {
00750 TileIndex tile = this->window_number;
00751 byte vehtype = this->type;
00752
00753 SetDParam(0, vehtype);
00754 SetDParam(1, (vehtype == VEH_AIRCRAFT) ? GetStationIndex(tile) : GetDepotIndex(tile));
00755 ShowQuery(
00756 STR_DEPOT_CAPTION,
00757 STR_DEPOT_SELL_CONFIRMATION_TEXT,
00758 this,
00759 DepotSellAllConfirmationCallback
00760 );
00761 }
00762 break;
00763
00764 case DEPOT_WIDGET_VEHICLE_LIST:
00765 ShowVehicleListWindow(GetTileOwner(this->window_number), this->type, (TileIndex)this->window_number);
00766 break;
00767
00768 case DEPOT_WIDGET_AUTOREPLACE:
00769 DoCommandP(this->window_number, this->type, 0, CMD_DEPOT_MASS_AUTOREPLACE);
00770 break;
00771
00772 }
00773 }
00774
00775 virtual void OnQueryTextFinished(char *str)
00776 {
00777 if (str == NULL) return;
00778
00779
00780 DoCommandP(0, GetDepotIndex(this->window_number), 0, CMD_RENAME_DEPOT | CMD_MSG(STR_ERROR_CAN_T_RENAME_DEPOT), NULL, str);
00781 }
00782
00783 virtual bool OnRightClick(Point pt, int widget)
00784 {
00785 if (widget != DEPOT_WIDGET_MATRIX) return false;
00786
00787 GetDepotVehiclePtData gdvp = { NULL, NULL };
00788 const Vehicle *v = NULL;
00789 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(DEPOT_WIDGET_MATRIX);
00790 DepotGUIAction mode = this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, &gdvp);
00791
00792 if (this->type == VEH_TRAIN) v = gdvp.wagon;
00793
00794 if (v == NULL || mode != MODE_DRAG_VEHICLE) return false;
00795
00796 CargoArray capacity, loaded;
00797
00798
00799 bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);
00800
00801
00802 uint num = 0;
00803 for (const Vehicle *w = v; w != NULL; w = w->Next()) {
00804 if (w->cargo_cap > 0 && w->cargo_type < NUM_CARGO) {
00805 capacity[w->cargo_type] += w->cargo_cap;
00806 loaded [w->cargo_type] += w->cargo.Count();
00807 }
00808
00809 if (w->type == VEH_TRAIN && !w->HasArticulatedPart()) {
00810 num++;
00811 if (!whole_chain) break;
00812 }
00813 }
00814
00815
00816 static char details[1024];
00817 details[0] = '\0';
00818 char *pos = details;
00819
00820 for (CargoID cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00821 if (capacity[cargo_type] == 0) continue;
00822
00823 SetDParam(0, cargo_type);
00824 SetDParam(1, loaded[cargo_type]);
00825 SetDParam(2, cargo_type);
00826 SetDParam(3, capacity[cargo_type]);
00827 pos = GetString(pos, STR_DEPOT_VEHICLE_TOOLTIP_CARGO, lastof(details));
00828 }
00829
00830
00831 uint64 args[2];
00832 args[0] = (whole_chain ? num : v->engine_type);
00833 args[1] = (uint64)(size_t)details;
00834 GuiShowTooltips(this, whole_chain ? STR_DEPOT_VEHICLE_TOOLTIP_CHAIN : STR_DEPOT_VEHICLE_TOOLTIP, 2, args, TCC_RIGHT_CLICK);
00835
00836 return true;
00837 }
00838
00843 virtual void OnVehicleSelect(const Vehicle *v)
00844 {
00845 if (DoCommandP(this->window_number, v->index, _ctrl_pressed ? 1 : 0, CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN + v->type), CcCloneVehicle)) {
00846 ResetObjectToPlace();
00847 }
00848 }
00849
00850 virtual void OnPlaceObjectAbort()
00851 {
00852
00853 this->RaiseWidget(DEPOT_WIDGET_CLONE);
00854 this->SetWidgetDirty(DEPOT_WIDGET_CLONE);
00855
00856
00857 this->sel = INVALID_VEHICLE;
00858 this->vehicle_over = INVALID_VEHICLE;
00859 this->SetWidgetDirty(DEPOT_WIDGET_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 != DEPOT_WIDGET_MATRIX) {
00868 if (this->vehicle_over != INVALID_VEHICLE) {
00869 this->vehicle_over = INVALID_VEHICLE;
00870 this->SetWidgetDirty(DEPOT_WIDGET_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 DEPOT_WIDGET_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>(DEPOT_WIDGET_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 DEPOT_WIDGET_SELL: case DEPOT_WIDGET_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 == DEPOT_WIDGET_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(DEPOT_WIDGET_SELL)) {
00958 this->RaiseWidget(DEPOT_WIDGET_SELL);
00959 this->SetWidgetDirty(DEPOT_WIDGET_SELL);
00960 }
00961 if (this->nested_array[DEPOT_WIDGET_SELL] != NULL && !this->IsWidgetDisabled(DEPOT_WIDGET_SELL_CHAIN)) {
00962 this->RaiseWidget(DEPOT_WIDGET_SELL_CHAIN);
00963 this->SetWidgetDirty(DEPOT_WIDGET_SELL_CHAIN);
00964 }
00965 }
00966
00967 virtual void OnResize()
00968 {
00969 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX);
00970 this->vscroll->SetCapacityFromWidget(this, DEPOT_WIDGET_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(DEPOT_WIDGET_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 }