diff options
Diffstat (limited to 'src/vehicle_gui.cpp')
-rw-r--r-- | src/vehicle_gui.cpp | 389 |
1 files changed, 368 insertions, 21 deletions
diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index da36d99f9..316295ef9 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1273,6 +1273,374 @@ void ShowVehicleListWindow(PlayerID player, VehicleType vehicle_type, TileIndex } +/* Unified vehicle GUI - Vehicle Details Window */ + +/** Constants of vehicle details widget indices */ +enum VehicleDetailsWindowWidgets { + VLD_WIDGET_CLOSEBOX = 0, + VLD_WIDGET_CAPTION, + VLD_WIDGET_RENAME_VEHICLE, + VLD_WIDGET_TOP_DETAILS, + VLD_WIDGET_INCREASE_SERVICING_INTERVAL, + VLD_WIDGET_DECREASE_SERVICING_INTERVAL, + VLD_WIDGET_BOTTOM_RIGHT, + VLD_WIDGET_MIDDLE_DETAILS, + VLD_WIDGET_SCROLLBAR, + VLD_WIDGET_DETAILS_CARGO_CARRIED, + VLD_WIDGET_DETAILS_TRAIN_VEHICLES, + VLD_WIDGET_DETAILS_CAPACITY_OF_EACH, + VLD_WIDGET_DETAILS_TOTAL_CARGO, + VLD_WIDGET_RESIZE, +}; + +/** Vehicle details widgets. */ +static const Widget _vehicle_details_widgets[] = { + { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // VLD_WIDGET_CLOSEBOX + { WWT_CAPTION, RESIZE_RIGHT, 14, 11, 364, 0, 13, 0x0, STR_018C_WINDOW_TITLE_DRAG_THIS}, // VLD_WIDGET_CAPTION + { WWT_PUSHTXTBTN, RESIZE_LR, 14, 365, 404, 0, 13, STR_01AA_NAME, STR_NULL /* filled in later */}, // VLD_WIDGET_RENAME_VEHICLE + { WWT_PANEL, RESIZE_RIGHT, 14, 0, 404, 14, 55, 0x0, STR_NULL}, // VLD_WIDGET_TOP_DETAILS + { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 10, 101, 106, STR_0188, STR_884D_INCREASE_SERVICING_INTERVAL}, // VLD_WIDGET_INCREASE_SERVICING_INTERVAL + { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 10, 107, 112, STR_0189, STR_884E_DECREASE_SERVICING_INTERVAL}, // VLD_WIDGET_DECREASE_SERVICING_INTERVAL + { WWT_PANEL, RESIZE_RTB, 14, 11, 404, 101, 112, 0x0, STR_NULL}, // VLD_WIDGET_BOTTOM_RIGHT + { WWT_MATRIX, RESIZE_RB, 14, 0, 392, 56, 100, 0x701, STR_NULL}, // VLD_WIDGET_MIDDLE_DETAILS + { WWT_SCROLLBAR, RESIZE_LRB, 14, 393, 404, 56, 100, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, // VLD_WIDGET_SCROLLBAR + { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 95, 113, 124, STR_013C_CARGO, STR_884F_SHOW_DETAILS_OF_CARGO_CARRIED}, // VLD_WIDGET_DETAILS_CARGO_CARRIED + { WWT_PUSHTXTBTN, RESIZE_TB, 14, 96, 194, 113, 124, STR_013D_INFORMATION, STR_8850_SHOW_DETAILS_OF_TRAIN_VEHICLES},// VLD_WIDGET_DETAILS_TRAIN_VEHICLES + { WWT_PUSHTXTBTN, RESIZE_TB, 14, 195, 293, 113, 124, STR_013E_CAPACITIES, STR_8851_SHOW_CAPACITIES_OF_EACH}, // VLD_WIDGET_DETAILS_CAPACITY_OF_EACH + { WWT_PUSHTXTBTN, RESIZE_RTB, 14, 294, 392, 113, 124, STR_013E_TOTAL_CARGO, STR_8852_SHOW_TOTAL_CARGO}, // VLD_WIDGET_DETAILS_TOTAL_CARGO + { WWT_RESIZEBOX, RESIZE_LRTB, 14, 393, 404, 113, 124, 0x0, STR_RESIZE_BUTTON}, // VLD_RESIZE + { WIDGETS_END}, +}; + + +/** Command indices for the _vehicle_command_translation_table. */ +enum VehicleStringTranslation { + VST_VEHICLE_AGE_RUNNING_COST_YR, + VST_VEHICLE_MAX_SPEED, + VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR, + VST_VEHICLE_RELIABILITY_BREAKDOWNS, +}; + +/** Command codes for the shared buttons indexed by VehicleCommandTranslation and vehicle type. */ +static const StringID _vehicle_translation_table[][4] = { + { // VST_VEHICLE_AGE_RUNNING_COST_YR + STR_885D_AGE_RUNNING_COST_YR, + STR_900D_AGE_RUNNING_COST_YR, + STR_9812_AGE_RUNNING_COST_YR, + STR_A00D_AGE_RUNNING_COST_YR, + }, + { // VST_VEHICLE_MAX_SPEED + STR_NULL, + STR_900E_MAX_SPEED, + STR_9813_MAX_SPEED, + STR_A00E_MAX_SPEED, + }, + { // VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR + STR_885F_PROFIT_THIS_YEAR_LAST_YEAR, + STR_900F_PROFIT_THIS_YEAR_LAST_YEAR, + STR_9814_PROFIT_THIS_YEAR_LAST_YEAR, + STR_A00F_PROFIT_THIS_YEAR_LAST_YEAR, + }, + { // VST_VEHICLE_RELIABILITY_BREAKDOWNS + STR_8860_RELIABILITY_BREAKDOWNS, + STR_9010_RELIABILITY_BREAKDOWNS, + STR_9815_RELIABILITY_BREAKDOWNS, + STR_A010_RELIABILITY_BREAKDOWNS, + }, +}; + +/** Initialize a newly created vehicle details window */ +void CreateVehicleDetailsWindow(Window *w) +{ + const Vehicle *v = GetVehicle(w->window_number); + + switch (v->type) { + case VEH_TRAIN: + ResizeWindow(w, 0, 39); + + w->vscroll.cap = 6; + w->height += 12; + w->resize.step_height = 14; + w->resize.height = w->height - 14 * 2; // Minimum of 4 wagons in the display + + w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_8867_NAME_TRAIN; + w->widget[VLD_WIDGET_CAPTION].data = STR_8802_DETAILS; + break; + + case VEH_ROAD: { + w->widget[VLD_WIDGET_CAPTION].data = STR_900C_DETAILS; + w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_902E_NAME_ROAD_VEHICLE; + + if (!RoadVehHasArticPart(v)) break; + + /* Draw the text under the vehicle instead of next to it, minus the + * height already allocated for the cargo of the first vehicle. */ + uint height_extension = 15 - 11; + + /* Add space for the cargo amount for each part. */ + for (const Vehicle *u = v; u != NULL; u = u->Next()) { + height_extension += 11; + } + + ResizeWindow(w, 0, height_extension); + } break; + + case VEH_SHIP: + w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_982F_NAME_SHIP; + w->widget[VLD_WIDGET_CAPTION].data = STR_9811_DETAILS; + break; + + case VEH_AIRCRAFT: + ResizeWindow(w, 0, 11); + w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_A032_NAME_AIRCRAFT; + w->widget[VLD_WIDGET_CAPTION].data = STR_A00C_DETAILS; + break; + default: NOT_REACHED(); + } + + if (v->type != VEH_TRAIN) { + w->vscroll.cap = 1; + w->widget[VLD_WIDGET_MIDDLE_DETAILS].right += 12; + } + + w->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (w->vscroll.cap << 8) + 1; + w->caption_color = v->owner; + + WP(w, vehicledetails_d).tab = 0; +} + +/** Checks whether service interval is enabled for the vehicle. */ +static bool inline IsVehicleServiceIntervalEnabled(const VehicleType vehicle_type) +{ + switch (vehicle_type) { + case VEH_TRAIN: return _patches.servint_trains != 0; break; + case VEH_ROAD: return _patches.servint_roadveh != 0; break; + case VEH_SHIP: return _patches.servint_ships != 0; break; + case VEH_AIRCRAFT: return _patches.servint_aircraft != 0; break; + default: NOT_REACHED(); + } +} + +extern int GetTrainDetailsWndVScroll(VehicleID veh_id, byte det_tab); +extern void DrawTrainDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint16 vscroll_cap, byte det_tab); +extern void DrawRoadVehDetails(const Vehicle *v, int x, int y); +extern void DrawShipDetails(const Vehicle *v, int x, int y); +extern void DrawAircraftDetails(const Vehicle *v, int x, int y); + +/** +* Draw the details for the given vehicle at the position (x,y) of the Details windows +* +* @param v current vehicle +* @param x The x coordinate +* @param y The y coordinate +* @param vscroll_pos (train only) +* @param vscroll_cap (train only) +* @param det_tab (train only) +*/ +static inline void DrawVehicleDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint vscroll_cap, byte det_tab) +{ + switch (v->type) { + case VEH_TRAIN: DrawTrainDetails(v, x, y, vscroll_pos, vscroll_cap, det_tab); break; + case VEH_ROAD: DrawRoadVehDetails(v, x, y); break; + case VEH_SHIP: DrawShipDetails(v, x, y); break; + case VEH_AIRCRAFT: DrawAircraftDetails(v, x, y); break; + default: NOT_REACHED(); + } +} + +/** Repaint vehicle details window. */ +static void DrawVehicleDetailsWindow(Window *w) +{ + const Vehicle *v = GetVehicle(w->window_number); + byte det_tab = WP(w, vehicledetails_d).tab; + + SetWindowWidgetDisabledState(w, VLD_WIDGET_RENAME_VEHICLE, v->owner != _local_player); + + if (v->type == VEH_TRAIN) { + DisableWindowWidget(w, det_tab + 9); + SetVScrollCount(w, GetTrainDetailsWndVScroll(v->index, det_tab)); + } + + SetWindowWidgetsHiddenState(w, v->type != VEH_TRAIN, + VLD_WIDGET_SCROLLBAR, + VLD_WIDGET_DETAILS_CARGO_CARRIED, + VLD_WIDGET_DETAILS_TRAIN_VEHICLES, + VLD_WIDGET_DETAILS_CAPACITY_OF_EACH, + VLD_WIDGET_DETAILS_TOTAL_CARGO, + VLD_WIDGET_RESIZE, + WIDGET_LIST_END); + + /* Disable service-scroller when interval is set to disabled */ + SetWindowWidgetsDisabledState(w, !IsVehicleServiceIntervalEnabled(v->type), + VLD_WIDGET_INCREASE_SERVICING_INTERVAL, + VLD_WIDGET_DECREASE_SERVICING_INTERVAL, + WIDGET_LIST_END); + + + SetDParam(0, v->index); + DrawWindowWidgets(w); + + /* Draw running cost */ + SetDParam(1, v->age / 366); + SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED); + SetDParam(2, v->max_age / 366); + SetDParam(3, v->GetDisplayRunningCost()); + DrawString(2, 15, _vehicle_translation_table[VST_VEHICLE_AGE_RUNNING_COST_YR][v->type], 0); + + /* Draw max speed */ + switch (v->type) { + case VEH_TRAIN: + SetDParam(2, v->GetDisplayMaxSpeed()); + SetDParam(1, v->u.rail.cached_power); + SetDParam(0, v->u.rail.cached_weight); + SetDParam(3, v->u.rail.cached_max_te / 1000); + DrawString(2, 25, (_patches.realistic_acceleration && v->u.rail.railtype != RAILTYPE_MAGLEV) ? + STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE : + STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED, 0); + break; + + case VEH_ROAD: + case VEH_SHIP: + case VEH_AIRCRAFT: + SetDParam(0, v->GetDisplayMaxSpeed()); + DrawString(2, 25, _vehicle_translation_table[VST_VEHICLE_MAX_SPEED][v->type], 0); + break; + + default: NOT_REACHED(); + } + + /* Draw profit */ + SetDParam(0, v->profit_this_year); + SetDParam(1, v->profit_last_year); + DrawString(2, 35, _vehicle_translation_table[VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR][v->type], 0); + + /* Draw breakdown & reliability */ + SetDParam(0, v->reliability * 100 >> 16); + SetDParam(1, v->breakdowns_since_last_service); + DrawString(2, 45, _vehicle_translation_table[VST_VEHICLE_RELIABILITY_BREAKDOWNS][v->type], 0); + + /* Draw service interval text */ + SetDParam(0, v->service_interval); + SetDParam(1, v->date_of_last_service); + DrawString(13, w->height - (v->type != VEH_TRAIN ? 11 : 23), _patches.servint_ispercent ? STR_SERVICING_INTERVAL_PERCENT : STR_883C_SERVICING_INTERVAL_DAYS, 0); + + switch (v->type) { + case VEH_TRAIN: + DrawVehicleDetails(v, 2, 57, w->vscroll.pos, w->vscroll.cap, det_tab); + break; + + case VEH_ROAD: + case VEH_SHIP: + case VEH_AIRCRAFT: + DrawVehicleImage(v, 3, 57, 0, 0, INVALID_VEHICLE); + DrawVehicleDetails(v, 75, 57, w->vscroll.pos, w->vscroll.cap, det_tab); + break; + + default: NOT_REACHED(); + } +} + +/** Message strings for renaming vehicles indexed by vehicle type. */ +static const StringID _name_vehicle_title[] = { + STR_8865_NAME_TRAIN, + STR_902C_NAME_ROAD_VEHICLE, + STR_9831_NAME_SHIP, + STR_A030_NAME_AIRCRAFT +}; + +/** Message strings for error while renaming indexed by vehicle type. */ +static const StringID _name_vehicle_error[] = { + STR_8866_CAN_T_NAME_TRAIN, + STR_902D_CAN_T_NAME_ROAD_VEHICLE, + STR_9832_CAN_T_NAME_SHIP, + STR_A031_CAN_T_NAME_AIRCRAFT +}; + +/** Window event hook for vehicle details. */ +static void VehicleDetailsWndProc(Window *w, WindowEvent *e) +{ + switch (e->event) { + case WE_CREATE: + CreateVehicleDetailsWindow(w); + break; + + case WE_PAINT: + DrawVehicleDetailsWindow(w); + break; + + case WE_CLICK: { + switch (e->we.click.widget) { + case VLD_WIDGET_RENAME_VEHICLE: {// rename + const Vehicle *v = GetVehicle(w->window_number); + SetDParam(0, v->index); + ShowQueryString(STR_VEHICLE_NAME, _name_vehicle_title[v->type], 31, 150, w, CS_ALPHANUMERAL); + } break; + + case VLD_WIDGET_INCREASE_SERVICING_INTERVAL: // increase int + case VLD_WIDGET_DECREASE_SERVICING_INTERVAL: { // decrease int + int mod = _ctrl_pressed ? 5 : 10; + const Vehicle *v = GetVehicle(w->window_number); + + mod = (e->we.click.widget == VLD_WIDGET_DECREASE_SERVICING_INTERVAL) ? -mod : mod; + mod = GetServiceIntervalClamped(mod + v->service_interval); + if (mod == v->service_interval) return; + + DoCommandP(v->tile, v->index, mod, NULL, CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING)); + } break; + + case VLD_WIDGET_DETAILS_CARGO_CARRIED: + case VLD_WIDGET_DETAILS_TRAIN_VEHICLES: + case VLD_WIDGET_DETAILS_CAPACITY_OF_EACH: + case VLD_WIDGET_DETAILS_TOTAL_CARGO: + SetWindowWidgetsDisabledState(w, false, + VLD_WIDGET_DETAILS_CARGO_CARRIED, + VLD_WIDGET_DETAILS_TRAIN_VEHICLES, + VLD_WIDGET_DETAILS_CAPACITY_OF_EACH, + VLD_WIDGET_DETAILS_TOTAL_CARGO, + e->we.click.widget, + WIDGET_LIST_END); + + WP(w, vehicledetails_d).tab = e->we.click.widget - 9; + SetWindowDirty(w); + break; + } + } break; + + case WE_ON_EDIT_TEXT: + if (!StrEmpty(e->we.edittext.str)) { + _cmd_text = e->we.edittext.str; + DoCommandP(0, w->window_number, 0, NULL, CMD_NAME_VEHICLE | CMD_MSG(_name_vehicle_error[GetVehicle(w->window_number)->type])); + } + break; + + case WE_RESIZE: + if (e->we.sizing.diff.x != 0) ResizeButtons(w, 9, 12); + if (e->we.sizing.diff.y == 0) break; + + w->vscroll.cap += e->we.sizing.diff.y / 14; + w->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (w->vscroll.cap << 8) + 1; + break; + } +} + +/** Vehicle details window descriptor. */ +static const WindowDesc _vehicle_details_desc = { + WDP_AUTO, WDP_AUTO, 405, 113, 405, 113, + WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW, + WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, + _vehicle_details_widgets, + VehicleDetailsWndProc +}; + +/** Shows the vehicle details window of the given vehicle. */ +static void ShowVehicleDetailsWindow(const Vehicle *v) +{ + DeleteWindowById(WC_VEHICLE_ORDERS, v->index); + DeleteWindowById(WC_VEHICLE_DETAILS, v->index); + AllocateWindowDescFront(&_vehicle_details_desc, v->index); +} + + /* Unified vehicle GUI - Vehicle View Window */ /** Constants of vehicle view widget indices */ @@ -1509,27 +1877,6 @@ static void CreateVehicleViewWindow(Window *w) } } - -/* When unified GUI is complete these functions will also be unified to one - * function in this module */ -void ShowAircraftDetailsWindow(const Vehicle *v); -void ShowShipDetailsWindow(const Vehicle *v); -void ShowRoadVehDetailsWindow(const Vehicle *v); -void ShowTrainDetailsWindow(const Vehicle *v); - - -/** Provisional dispatch to vehicle-specific detail window functions. */ -static void ShowVehicleDetailsWindow(const Vehicle *v) -{ - switch (v->type) { - case VEH_TRAIN: ShowTrainDetailsWindow(v); break; - case VEH_ROAD: ShowRoadVehDetailsWindow(v); break; - case VEH_SHIP: ShowShipDetailsWindow(v); break; - case VEH_AIRCRAFT: ShowAircraftDetailsWindow(v); break; - default: NOT_REACHED(); - } -} - /** Checks whether the vehicle may be refitted at the moment.*/ static bool IsVehicleRefitable(const Vehicle *v) { |