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