(svn r13120) -Codechange: make a class of the VehicleDetailsWindow.

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
rubidium 17 years ago
parent e1cce4dd3c
commit 2da844b146

@ -40,11 +40,6 @@
#include "table/sprites.h" #include "table/sprites.h"
#include "table/strings.h" #include "table/strings.h"
struct vehicledetails_d {
byte tab;
};
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(vehicledetails_d));
struct refit_d { struct refit_d {
int sel; int sel;
struct RefitOption *cargo; struct RefitOption *cargo;
@ -1374,27 +1369,37 @@ static const StringID _vehicle_translation_table[][4] = {
}, },
}; };
/** Initialize a newly created vehicle details window */
void CreateVehicleDetailsWindow(Window *w) 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);
const Vehicle *v = GetVehicle(w->window_number); 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);
struct VehicleDetailsWindow : Window {
int tab;
/** Initialize a newly created vehicle details window */
VehicleDetailsWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
{
const Vehicle *v = GetVehicle(this->window_number);
switch (v->type) { switch (v->type) {
case VEH_TRAIN: case VEH_TRAIN:
ResizeWindow(w, 0, 39); ResizeWindow(this, 0, 39);
w->vscroll.cap = 6; this->vscroll.cap = 6;
w->height += 12; this->height += 12;
w->resize.step_height = 14; this->resize.step_height = 14;
w->resize.height = w->height - 14 * 2; // Minimum of 4 wagons in the display this->resize.height = this->height - 14 * 2; // Minimum of 4 wagons in the display
w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_8867_NAME_TRAIN; this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_8867_NAME_TRAIN;
w->widget[VLD_WIDGET_CAPTION].data = STR_8802_DETAILS; this->widget[VLD_WIDGET_CAPTION].data = STR_8802_DETAILS;
break; break;
case VEH_ROAD: { case VEH_ROAD: {
w->widget[VLD_WIDGET_CAPTION].data = STR_900C_DETAILS; this->widget[VLD_WIDGET_CAPTION].data = STR_900C_DETAILS;
w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_902E_NAME_ROAD_VEHICLE; this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_902E_NAME_ROAD_VEHICLE;
if (!RoadVehHasArticPart(v)) break; if (!RoadVehHasArticPart(v)) break;
@ -1407,36 +1412,36 @@ void CreateVehicleDetailsWindow(Window *w)
height_extension += 11; height_extension += 11;
} }
ResizeWindow(w, 0, height_extension); ResizeWindow(this, 0, height_extension);
} break; } break;
case VEH_SHIP: case VEH_SHIP:
w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_982F_NAME_SHIP; this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_982F_NAME_SHIP;
w->widget[VLD_WIDGET_CAPTION].data = STR_9811_DETAILS; this->widget[VLD_WIDGET_CAPTION].data = STR_9811_DETAILS;
break; break;
case VEH_AIRCRAFT: case VEH_AIRCRAFT:
ResizeWindow(w, 0, 11); ResizeWindow(this, 0, 11);
w->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_A032_NAME_AIRCRAFT; this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_A032_NAME_AIRCRAFT;
w->widget[VLD_WIDGET_CAPTION].data = STR_A00C_DETAILS; this->widget[VLD_WIDGET_CAPTION].data = STR_A00C_DETAILS;
break; break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
if (v->type != VEH_TRAIN) { if (v->type != VEH_TRAIN) {
w->vscroll.cap = 1; this->vscroll.cap = 1;
w->widget[VLD_WIDGET_MIDDLE_DETAILS].right += 12; this->widget[VLD_WIDGET_MIDDLE_DETAILS].right += 12;
} }
w->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (w->vscroll.cap << 8) + 1; this->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (this->vscroll.cap << 8) + 1;
w->caption_color = v->owner; this->caption_color = v->owner;
WP(w, vehicledetails_d).tab = 0; this->tab = 0;
} }
/** Checks whether service interval is enabled for the vehicle. */ /** Checks whether service interval is enabled for the vehicle. */
static inline bool IsVehicleServiceIntervalEnabled(const VehicleType vehicle_type) static bool IsVehicleServiceIntervalEnabled(const VehicleType vehicle_type)
{ {
switch (vehicle_type) { switch (vehicle_type) {
default: NOT_REACHED(); default: NOT_REACHED();
case VEH_TRAIN: return _patches.servint_trains != 0; break; case VEH_TRAIN: return _patches.servint_trains != 0; break;
@ -1445,26 +1450,20 @@ static inline bool IsVehicleServiceIntervalEnabled(const VehicleType vehicle_typ
case VEH_AIRCRAFT: return _patches.servint_aircraft != 0; break; case VEH_AIRCRAFT: return _patches.servint_aircraft != 0; break;
} }
return false; // kill a compiler warning return false; // kill a compiler warning
} }
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 * Draw the details for the given vehicle at the position (x, y) of the Details windows
* *
* @param v current vehicle * @param v current vehicle
* @param x The x coordinate * @param x The x coordinate
* @param y The y coordinate * @param y The y coordinate
* @param vscroll_pos (train only) * @param vscroll_pos (train only)
* @param vscroll_cap (train only) * @param vscroll_cap (train only)
* @param det_tab (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) static void DrawVehicleDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint vscroll_cap, byte det_tab)
{ {
switch (v->type) { switch (v->type) {
case VEH_TRAIN: DrawTrainDetails(v, x, y, vscroll_pos, vscroll_cap, det_tab); break; case VEH_TRAIN: DrawTrainDetails(v, x, y, vscroll_pos, vscroll_cap, det_tab); break;
case VEH_ROAD: DrawRoadVehDetails(v, x, y); break; case VEH_ROAD: DrawRoadVehDetails(v, x, y); break;
@ -1472,22 +1471,22 @@ static inline void DrawVehicleDetails(const Vehicle *v, int x, int y, int vscrol
case VEH_AIRCRAFT: DrawAircraftDetails(v, x, y); break; case VEH_AIRCRAFT: DrawAircraftDetails(v, x, y); break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
} }
/** Repaint vehicle details window. */ /** Repaint vehicle details window. */
static void DrawVehicleDetailsWindow(Window *w) virtual void OnPaint()
{ {
const Vehicle *v = GetVehicle(w->window_number); const Vehicle *v = GetVehicle(this->window_number);
byte det_tab = WP(w, vehicledetails_d).tab; byte det_tab = this->tab;
w->SetWidgetDisabledState(VLD_WIDGET_RENAME_VEHICLE, v->owner != _local_player); this->SetWidgetDisabledState(VLD_WIDGET_RENAME_VEHICLE, v->owner != _local_player);
if (v->type == VEH_TRAIN) { if (v->type == VEH_TRAIN) {
w->DisableWidget(det_tab + VLD_WIDGET_DETAILS_CARGO_CARRIED); this->DisableWidget(det_tab + VLD_WIDGET_DETAILS_CARGO_CARRIED);
SetVScrollCount(w, GetTrainDetailsWndVScroll(v->index, det_tab)); SetVScrollCount(this, GetTrainDetailsWndVScroll(v->index, det_tab));
} }
w->SetWidgetsHiddenState(v->type != VEH_TRAIN, this->SetWidgetsHiddenState(v->type != VEH_TRAIN,
VLD_WIDGET_SCROLLBAR, VLD_WIDGET_SCROLLBAR,
VLD_WIDGET_DETAILS_CARGO_CARRIED, VLD_WIDGET_DETAILS_CARGO_CARRIED,
VLD_WIDGET_DETAILS_TRAIN_VEHICLES, VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
@ -1497,14 +1496,14 @@ static void DrawVehicleDetailsWindow(Window *w)
WIDGET_LIST_END); WIDGET_LIST_END);
/* Disable service-scroller when interval is set to disabled */ /* Disable service-scroller when interval is set to disabled */
w->SetWidgetsDisabledState(!IsVehicleServiceIntervalEnabled(v->type), this->SetWidgetsDisabledState(!IsVehicleServiceIntervalEnabled(v->type),
VLD_WIDGET_INCREASE_SERVICING_INTERVAL, VLD_WIDGET_INCREASE_SERVICING_INTERVAL,
VLD_WIDGET_DECREASE_SERVICING_INTERVAL, VLD_WIDGET_DECREASE_SERVICING_INTERVAL,
WIDGET_LIST_END); WIDGET_LIST_END);
SetDParam(0, v->index); SetDParam(0, v->index);
DrawWindowWidgets(w); DrawWindowWidgets(this);
/* Draw running cost */ /* Draw running cost */
SetDParam(1, v->age / 366); SetDParam(1, v->age / 366);
@ -1548,66 +1547,47 @@ static void DrawVehicleDetailsWindow(Window *w)
/* Draw service interval text */ /* Draw service interval text */
SetDParam(0, v->service_interval); SetDParam(0, v->service_interval);
SetDParam(1, v->date_of_last_service); 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, TC_FROMSTRING); DrawString(13, this->height - (v->type != VEH_TRAIN ? 11 : 23), _patches.servint_ispercent ? STR_SERVICING_INTERVAL_PERCENT : STR_883C_SERVICING_INTERVAL_DAYS, TC_FROMSTRING);
switch (v->type) { switch (v->type) {
case VEH_TRAIN: case VEH_TRAIN:
DrawVehicleDetails(v, 2, 57, w->vscroll.pos, w->vscroll.cap, det_tab); DrawVehicleDetails(v, 2, 57, this->vscroll.pos, this->vscroll.cap, det_tab);
break; break;
case VEH_ROAD: case VEH_ROAD:
case VEH_SHIP: case VEH_SHIP:
case VEH_AIRCRAFT: case VEH_AIRCRAFT:
DrawVehicleImage(v, 3, 57, INVALID_VEHICLE, 0, 0); DrawVehicleImage(v, 3, 57, INVALID_VEHICLE, 0, 0);
DrawVehicleDetails(v, 75, 57, w->vscroll.pos, w->vscroll.cap, det_tab); DrawVehicleDetails(v, 75, 57, this->vscroll.pos, this->vscroll.cap, det_tab);
break; break;
default: NOT_REACHED(); default: NOT_REACHED();
} }
} }
/** Message strings for renaming vehicles indexed by vehicle type. */ virtual void OnClick(Point pt, int widget)
static const StringID _name_vehicle_title[] = { {
/** Message strings for renaming vehicles indexed by vehicle type. */
static const StringID _name_vehicle_title[] = {
STR_8865_NAME_TRAIN, STR_8865_NAME_TRAIN,
STR_902C_NAME_ROAD_VEHICLE, STR_902C_NAME_ROAD_VEHICLE,
STR_9831_NAME_SHIP, STR_9831_NAME_SHIP,
STR_A030_NAME_AIRCRAFT 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 (widget) {
switch (e->we.click.widget) {
case VLD_WIDGET_RENAME_VEHICLE: {// rename case VLD_WIDGET_RENAME_VEHICLE: {// rename
const Vehicle *v = GetVehicle(w->window_number); const Vehicle *v = GetVehicle(this->window_number);
SetDParam(0, v->index); SetDParam(0, v->index);
ShowQueryString(STR_VEHICLE_NAME, _name_vehicle_title[v->type], 31, 150, w, CS_ALPHANUMERAL); ShowQueryString(STR_VEHICLE_NAME, _name_vehicle_title[v->type], 31, 150, this, CS_ALPHANUMERAL);
} break; } break;
case VLD_WIDGET_INCREASE_SERVICING_INTERVAL: // increase int case VLD_WIDGET_INCREASE_SERVICING_INTERVAL: // increase int
case VLD_WIDGET_DECREASE_SERVICING_INTERVAL: { // decrease int case VLD_WIDGET_DECREASE_SERVICING_INTERVAL: { // decrease int
int mod = _ctrl_pressed ? 5 : 10; int mod = _ctrl_pressed ? 5 : 10;
const Vehicle *v = GetVehicle(w->window_number); const Vehicle *v = GetVehicle(this->window_number);
mod = (e->we.click.widget == VLD_WIDGET_DECREASE_SERVICING_INTERVAL) ? -mod : mod; mod = (widget == VLD_WIDGET_DECREASE_SERVICING_INTERVAL) ? -mod : mod;
mod = GetServiceIntervalClamped(mod + v->service_interval); mod = GetServiceIntervalClamped(mod + v->service_interval);
if (mod == v->service_interval) return; if (mod == v->service_interval) return;
@ -1618,36 +1598,45 @@ static void VehicleDetailsWndProc(Window *w, WindowEvent *e)
case VLD_WIDGET_DETAILS_TRAIN_VEHICLES: case VLD_WIDGET_DETAILS_TRAIN_VEHICLES:
case VLD_WIDGET_DETAILS_CAPACITY_OF_EACH: case VLD_WIDGET_DETAILS_CAPACITY_OF_EACH:
case VLD_WIDGET_DETAILS_TOTAL_CARGO: case VLD_WIDGET_DETAILS_TOTAL_CARGO:
w->SetWidgetsDisabledState(false, this->SetWidgetsDisabledState(false,
VLD_WIDGET_DETAILS_CARGO_CARRIED, VLD_WIDGET_DETAILS_CARGO_CARRIED,
VLD_WIDGET_DETAILS_TRAIN_VEHICLES, VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
VLD_WIDGET_DETAILS_CAPACITY_OF_EACH, VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
VLD_WIDGET_DETAILS_TOTAL_CARGO, VLD_WIDGET_DETAILS_TOTAL_CARGO,
e->we.click.widget, widget,
WIDGET_LIST_END); WIDGET_LIST_END);
WP(w, vehicledetails_d).tab = e->we.click.widget - VLD_WIDGET_DETAILS_CARGO_CARRIED; this->tab = widget - VLD_WIDGET_DETAILS_CARGO_CARRIED;
w->SetDirty(); this->SetDirty();
break; break;
} }
} break; }
case WE_ON_EDIT_TEXT: virtual void OnQueryTextFinished(char *str)
if (!StrEmpty(e->we.edittext.str)) { {
_cmd_text = e->we.edittext.str; /** Message strings for error while renaming indexed by vehicle type. */
DoCommandP(0, w->window_number, 0, NULL, CMD_NAME_VEHICLE | CMD_MSG(_name_vehicle_error[GetVehicle(w->window_number)->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
};
if (!StrEmpty(str)) {
_cmd_text = str;
DoCommandP(0, this->window_number, 0, NULL, CMD_NAME_VEHICLE | CMD_MSG(_name_vehicle_error[GetVehicle(this->window_number)->type]));
}
} }
break;
case WE_RESIZE: virtual void OnResize(Point new_size, Point delta)
if (e->we.sizing.diff.x != 0) ResizeButtons(w, VLD_WIDGET_DETAILS_CARGO_CARRIED, VLD_WIDGET_DETAILS_TOTAL_CARGO); {
if (e->we.sizing.diff.y == 0) break; if (delta.x != 0) ResizeButtons(this, VLD_WIDGET_DETAILS_CARGO_CARRIED, VLD_WIDGET_DETAILS_TOTAL_CARGO);
if (delta.y == 0) return;
w->vscroll.cap += e->we.sizing.diff.y / 14; this->vscroll.cap += delta.y / 14;
w->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (w->vscroll.cap << 8) + 1; this->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (this->vscroll.cap << 8) + 1;
break;
} }
} };
/** Vehicle details window descriptor. */ /** Vehicle details window descriptor. */
static const WindowDesc _vehicle_details_desc = { static const WindowDesc _vehicle_details_desc = {
@ -1655,7 +1644,7 @@ static const WindowDesc _vehicle_details_desc = {
WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW, WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE, WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE,
_vehicle_details_widgets, _vehicle_details_widgets,
VehicleDetailsWndProc NULL
}; };
/** Shows the vehicle details window of the given vehicle. */ /** Shows the vehicle details window of the given vehicle. */
@ -1663,7 +1652,7 @@ static void ShowVehicleDetailsWindow(const Vehicle *v)
{ {
DeleteWindowById(WC_VEHICLE_ORDERS, v->index); DeleteWindowById(WC_VEHICLE_ORDERS, v->index);
DeleteWindowById(WC_VEHICLE_DETAILS, v->index); DeleteWindowById(WC_VEHICLE_DETAILS, v->index);
AllocateWindowDescFront<Window>(&_vehicle_details_desc, v->index); AllocateWindowDescFront<VehicleDetailsWindow>(&_vehicle_details_desc, v->index);
} }

Loading…
Cancel
Save