(svn r6513) -Codechange: unified the code to draw depot windows

This change is intended to make it easier to make depot behaviour consistent
   and faster to code when adding more features in the future
   The user interface should hopefully not be affected by this
This commit is contained in:
bjarni
2006-09-26 16:47:51 +00:00
parent 65a3777a40
commit f7769e885e
15 changed files with 827 additions and 1264 deletions

View File

@@ -614,288 +614,6 @@ void ShowBuildRoadVehWindow(TileIndex tile)
}
}
static void DrawRoadDepotWindow(Window *w)
{
Vehicle **vl = WP(w, traindepot_d).vehicle_list;
TileIndex tile;
int x, y, max;
uint16 num = WP(w, traindepot_d).engine_count;
Depot *depot;
tile = w->window_number;
/* setup disabled buttons */
w->disabled_state =
IsTileOwner(tile, _local_player) ? 0 : ((1<<4) | (1<<7) | (1<<8));
/* determine amount of items for scroller */
SetVScrollCount(w, (num + w->hscroll.cap - 1) / w->hscroll.cap);
/* locate the depot struct */
depot = GetDepotByTile(tile);
assert(depot != NULL);
SetDParam(0, depot->town_index);
DrawWindowWidgets(w);
x = 2;
y = 15;
num = w->vscroll.pos * w->hscroll.cap;
max = min(WP(w, traindepot_d).engine_count, num + (w->vscroll.cap * w->hscroll.cap));
for (; num < max; num++) {
const Vehicle *v = vl[num];
DrawRoadVehImage(v, x + 24, y, WP(w,traindepot_d).sel);
SetDParam(0, v->unitnumber);
DrawString(x, y + 2, (uint16)(v->max_age-366) >= v->age ? STR_00E2 : STR_00E3, 0);
DrawSprite((v->vehstatus & VS_STOPPED) ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, x + 16, y);
if ((x += 56) == 2 + 56 * w->hscroll.cap) {
x = 2;
y += 14;
}
}
}
static int GetVehicleFromRoadDepotWndPt(const Window *w, int x, int y, Vehicle **veh)
{
Vehicle **vl = WP(w, traindepot_d).vehicle_list;
uint xt,row,xm;
int pos;
xt = x / 56;
xm = x % 56;
if (xt >= w->hscroll.cap) return 1;
row = (y - 14) / 14;
if (row >= w->vscroll.cap) return 1;
pos = (row + w->vscroll.pos) * w->hscroll.cap + xt;
if (WP(w, traindepot_d).engine_count <= pos) return 1; // empty block, so no vehicle is selected
*veh = vl[pos];
if (xm >= 24) return 0; // drag vehicle
if (xm <= 16) return -1; // show window
return -2; // start stop
}
static void RoadDepotClickVeh(Window *w, int x, int y)
{
Vehicle *v;
int mode;
mode = GetVehicleFromRoadDepotWndPt(w, x, y, &v);
if (mode > 0) return;
// share / copy orders
if (_thd.place_mode && mode <= 0) {
_place_clicked_vehicle = v;
return;
}
switch (mode) {
case 0: // start dragging of vehicle
if (v != NULL) {
WP(w,traindepot_d).sel = v->index;
SetWindowDirty(w);
SetObjectToPlaceWnd(GetVehiclePalette(v) | GetRoadVehImage(v, DIR_W), 4, w);
}
break;
case -1: // show info window
ShowRoadVehViewWindow(v);
break;
case -2: // click start/stop flag
DoCommandP(v->tile, v->index, 0, NULL, CMD_START_STOP_ROADVEH | CMD_MSG(STR_9015_CAN_T_STOP_START_ROAD_VEHICLE));
break;
default:
NOT_REACHED();
}
}
/**
* Clones a road vehicle
* @param *v is the original vehicle to clone
* @param *w is the window of the depot where the clone is build
*/
static void HandleCloneVehClick(const Vehicle *v, const Window *w)
{
if (v == NULL || v->type != VEH_Road) return;
DoCommandP(w->window_number, v->index, _ctrl_pressed ? 1 : 0, CcCloneRoadVeh,
CMD_CLONE_VEHICLE | CMD_MSG(STR_9009_CAN_T_BUILD_ROAD_VEHICLE)
);
ResetObjectToPlace();
}
static void ClonePlaceObj(const Window *w)
{
const Vehicle *v = CheckMouseOverVehicle();
if (v != NULL) HandleCloneVehClick(v, w);
}
static void RoadDepotWndProc(Window *w, WindowEvent *e)
{
switch (e->event) {
case WE_CREATE:
WP(w, traindepot_d).vehicle_list = NULL;
WP(w, traindepot_d).engine_list_length = 0;
break;
case WE_PAINT:
BuildDepotVehicleList(VEH_Road, w->window_number, &WP(w, traindepot_d).vehicle_list, &WP(w, traindepot_d).engine_list_length, &WP(w, traindepot_d).engine_count, NULL, NULL, NULL);
DrawRoadDepotWindow(w);
break;
case WE_CLICK: {
switch (e->we.click.widget) {
case 5:
RoadDepotClickVeh(w, e->we.click.pt.x, e->we.click.pt.y);
break;
case 7:
ResetObjectToPlace();
ShowBuildRoadVehWindow(w->window_number);
break;
case 8: /* clone button */
InvalidateWidget(w, 8);
TOGGLEBIT(w->click_state, 8);
if (HASBIT(w->click_state, 8)) {
_place_clicked_vehicle = NULL;
SetObjectToPlaceWnd(SPR_CURSOR_CLONE, VHM_RECT, w);
} else {
ResetObjectToPlace();
}
break;
case 9: ScrollMainWindowToTile(w->window_number); break;
}
} break;
case WE_PLACE_OBJ:
ClonePlaceObj(w);
break;
case WE_ABORT_PLACE_OBJ:
CLRBIT(w->click_state, 8);
InvalidateWidget(w, 8);
break;
// check if a vehicle in a depot was clicked..
case WE_MOUSELOOP: {
const Vehicle *v = _place_clicked_vehicle;
// since OTTD checks all open depot windows, we will make sure that it triggers the one with a clicked clone button
if (v != NULL && HASBIT(w->click_state, 8)) {
_place_clicked_vehicle = NULL;
HandleCloneVehClick(v, w);
}
} break;
case WE_DESTROY:
DeleteWindowById(WC_BUILD_VEHICLE, w->window_number);
free((void*)WP(w, traindepot_d).vehicle_list);
break;
case WE_DRAGDROP:
switch (e->we.click.widget) {
case 5: {
Vehicle *v;
VehicleID sel = WP(w,traindepot_d).sel;
WP(w,traindepot_d).sel = INVALID_VEHICLE;
SetWindowDirty(w);
if (GetVehicleFromRoadDepotWndPt(w, e->we.dragdrop.pt.x, e->we.dragdrop.pt.y, &v) == 0 &&
v != NULL &&
sel == v->index) {
ShowRoadVehViewWindow(v);
}
} break;
case 4:
if (!HASBIT(w->disabled_state, 4) &&
WP(w,traindepot_d).sel != INVALID_VEHICLE) {
Vehicle *v;
HandleButtonClick(w, 4);
v = GetVehicle(WP(w,traindepot_d).sel);
WP(w,traindepot_d).sel = INVALID_VEHICLE;
_backup_orders_tile = v->tile;
BackupVehicleOrders(v, _backup_orders_data);
if (!DoCommandP(v->tile, v->index, 0, NULL, CMD_SELL_ROAD_VEH | CMD_MSG(STR_9014_CAN_T_SELL_ROAD_VEHICLE)))
_backup_orders_tile = 0;
}
break;
default:
WP(w,traindepot_d).sel = INVALID_VEHICLE;
SetWindowDirty(w);
}
break;
case WE_RESIZE:
/* Update the scroll + matrix */
w->vscroll.cap += e->we.sizing.diff.y / 14;
w->hscroll.cap += e->we.sizing.diff.x / 56;
w->widget[5].data = (w->vscroll.cap << 8) + w->hscroll.cap;
break;
}
}
static const Widget _road_depot_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
{ WWT_CAPTION, RESIZE_RIGHT, 14, 11, 302, 0, 13, STR_9003_ROAD_VEHICLE_DEPOT, STR_018C_WINDOW_TITLE_DRAG_THIS},
{ WWT_STICKYBOX, RESIZE_LR, 14, 303, 314, 0, 13, 0x0, STR_STICKY_BUTTON},
{ WWT_PANEL, RESIZE_LRB, 14, 280, 302, 14, 13, 0x0, STR_NULL},
{ WWT_IMGBTN, RESIZE_LRTB, 14, 280, 302, 14, 55, 0x2A9, STR_9024_DRAG_ROAD_VEHICLE_TO_HERE},
{ WWT_MATRIX, RESIZE_RB, 14, 0, 279, 14, 55, 0x305, STR_9022_VEHICLES_CLICK_ON_VEHICLE},
{ WWT_SCROLLBAR, RESIZE_LRB, 14, 303, 314, 14, 55, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 100, 56, 67, STR_9004_NEW_VEHICLES, STR_9023_BUILD_NEW_ROAD_VEHICLE},
{WWT_NODISTXTBTN, RESIZE_TB, 14, 101, 200, 56, 67, STR_CLONE_ROAD_VEHICLE, STR_CLONE_ROAD_VEHICLE_DEPOT_INFO},
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 201, 302, 56, 67, STR_00E4_LOCATION, STR_9025_CENTER_MAIN_VIEW_ON_ROAD},
{ WWT_PANEL, RESIZE_RTB, 14, 303, 302, 56, 67, 0x0, STR_NULL},
{ WWT_RESIZEBOX, RESIZE_LRTB, 14, 303, 314, 56, 67, 0x0, STR_RESIZE_BUTTON},
{ WIDGETS_END},
};
static const WindowDesc _road_depot_desc = {
-1, -1, 315, 68,
WC_VEHICLE_DEPOT,0,
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
_road_depot_widgets,
RoadDepotWndProc
};
void ShowRoadDepotWindow(TileIndex tile)
{
Window *w = AllocateWindowDescFront(&_road_depot_desc, tile);
if (w != NULL) {
w->caption_color = GetTileOwner(w->window_number);
w->hscroll.cap = 5;
w->vscroll.cap = 3;
w->resize.step_width = 56;
w->resize.step_height = 14;
WP(w,traindepot_d).sel = INVALID_VEHICLE;
_backup_orders_tile = 0;
}
}
static const Widget _player_roadveh_widgets[] = {
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
{ WWT_CAPTION, RESIZE_RIGHT, 14, 11, 247, 0, 13, STR_9001_ROAD_VEHICLES, STR_018C_WINDOW_TITLE_DRAG_THIS},