train_gui.c
changeset 2676 2ba71e034d97
parent 2639 8a7342eb3a78
child 2725 5c632bc5be28
equal deleted inserted replaced
2675:269c38d892f9 2676:2ba71e034d97
    16 #include "command.h"
    16 #include "command.h"
    17 #include "player.h"
    17 #include "player.h"
    18 #include "engine.h"
    18 #include "engine.h"
    19 #include "vehicle_gui.h"
    19 #include "vehicle_gui.h"
    20 #include "depot.h"
    20 #include "depot.h"
       
    21 #include "train.h"
    21 
    22 
    22 int _traininfo_vehicle_pitch = 0;
    23 int _traininfo_vehicle_pitch = 0;
    23 
    24 
    24 /**
    25 /**
    25  * Draw the purchase info details of train engine at a given location.
    26  * Draw the purchase info details of train engine at a given location.
   126 		return;
   127 		return;
   127 
   128 
   128 	// find a locomotive in the depot.
   129 	// find a locomotive in the depot.
   129 	found = NULL;
   130 	found = NULL;
   130 	FOR_ALL_VEHICLES(v) {
   131 	FOR_ALL_VEHICLES(v) {
   131 		if (v->type == VEH_Train && v->subtype == TS_Front_Engine &&
   132 		if (v->type == VEH_Train && IsFrontEngine(v) &&
   132 				v->tile == tile &&
   133 				v->tile == tile &&
   133 				v->u.rail.track == 0x80) {
   134 				v->u.rail.track == 0x80) {
   134 			if (found != NULL) // must be exactly one.
   135 			if (found != NULL) // must be exactly one.
   135 				return;
   136 				return;
   136 			found = v;
   137 			found = v;
   390 	/* determine amount of items for scroller */
   391 	/* determine amount of items for scroller */
   391 	num = 0;
   392 	num = 0;
   392 	hnum = 8;
   393 	hnum = 8;
   393 	FOR_ALL_VEHICLES(v) {
   394 	FOR_ALL_VEHICLES(v) {
   394 		if (v->type == VEH_Train &&
   395 		if (v->type == VEH_Train &&
   395 			  (v->subtype == TS_Front_Engine || v->subtype == TS_Free_Car) &&
   396 			  (IsFrontEngine(v) || IsFreeWagon(v)) &&
   396 				v->tile == tile &&
   397 				v->tile == tile &&
   397 				v->u.rail.track == 0x80) {
   398 				v->u.rail.track == 0x80) {
   398 			num++;
   399 			num++;
   399 			// determine number of items in the X direction.
   400 			// determine number of items in the X direction.
   400 			if (v->subtype == TS_Front_Engine) {
   401 			if (IsFrontEngine(v)) {
   401 				hnum = max(hnum, v->u.rail.cached_total_length);
   402 				hnum = max(hnum, v->u.rail.cached_total_length);
   402 			}
   403 			}
   403 		}
   404 		}
   404 	}
   405 	}
   405 
   406 
   420 	y = 15;
   421 	y = 15;
   421 	num = w->vscroll.pos;
   422 	num = w->vscroll.pos;
   422 
   423 
   423 	// draw all trains
   424 	// draw all trains
   424 	FOR_ALL_VEHICLES(v) {
   425 	FOR_ALL_VEHICLES(v) {
   425 		if (v->type == VEH_Train && v->subtype == TS_Front_Engine &&
   426 		if (v->type == VEH_Train && IsFrontEngine(v) &&
   426 				v->tile == tile && v->u.rail.track == 0x80 &&
   427 				v->tile == tile && v->u.rail.track == 0x80 &&
   427 				--num < 0 && num >= -w->vscroll.cap) {
   428 				--num < 0 && num >= -w->vscroll.cap) {
   428 			DrawTrainImage(v, x+21, y, w->hscroll.cap, w->hscroll.pos, WP(w,traindepot_d).sel);
   429 			DrawTrainImage(v, x+21, y, w->hscroll.cap, w->hscroll.pos, WP(w,traindepot_d).sel);
   429 			/* Draw the train number */
   430 			/* Draw the train number */
   430 			SetDParam(0, v->unitnumber);
   431 			SetDParam(0, v->unitnumber);
   441 		}
   442 		}
   442 	}
   443 	}
   443 
   444 
   444 	// draw all remaining vehicles
   445 	// draw all remaining vehicles
   445 	FOR_ALL_VEHICLES(v) {
   446 	FOR_ALL_VEHICLES(v) {
   446 		if (v->type == VEH_Train && v->subtype == TS_Free_Car &&
   447 		if (v->type == VEH_Train && IsFreeWagon(v) &&
   447 				v->tile == tile && v->u.rail.track == 0x80 &&
   448 				v->tile == tile && v->u.rail.track == 0x80 &&
   448 				--num < 0 && num >= -w->vscroll.cap) {
   449 				--num < 0 && num >= -w->vscroll.cap) {
   449 			DrawTrainImage(v, x+50, y, w->hscroll.cap - 1, 0, WP(w,traindepot_d).sel);
   450 			DrawTrainImage(v, x+50, y, w->hscroll.cap - 1, 0, WP(w,traindepot_d).sel);
   450 			DrawString(x, y+2, STR_8816, 0);
   451 			DrawString(x, y+2, STR_8816, 0);
   451 
   452 
   480 	row += w->vscroll.pos;
   481 	row += w->vscroll.pos;
   481 
   482 
   482 	/* go through all the locomotives */
   483 	/* go through all the locomotives */
   483 	FOR_ALL_VEHICLES(v) {
   484 	FOR_ALL_VEHICLES(v) {
   484 		if (v->type == VEH_Train &&
   485 		if (v->type == VEH_Train &&
   485 				v->subtype == TS_Front_Engine &&
   486 				IsFrontEngine(v) &&
   486 				v->tile == w->window_number &&
   487 				v->tile == w->window_number &&
   487 				v->u.rail.track == 0x80 &&
   488 				v->u.rail.track == 0x80 &&
   488 				--row < 0) {
   489 				--row < 0) {
   489 					skip = w->hscroll.pos;
   490 					skip = w->hscroll.pos;
   490 					goto found_it;
   491 					goto found_it;
   494 	x -= 29; /* free wagons don't have an initial loco. */
   495 	x -= 29; /* free wagons don't have an initial loco. */
   495 
   496 
   496 	/* and then the list of free wagons */
   497 	/* and then the list of free wagons */
   497 	FOR_ALL_VEHICLES(v) {
   498 	FOR_ALL_VEHICLES(v) {
   498 		if (v->type == VEH_Train &&
   499 		if (v->type == VEH_Train &&
   499 				v->subtype == TS_Free_Car &&
   500 				IsFreeWagon(v) &&
   500 				v->tile == w->window_number &&
   501 				v->tile == w->window_number &&
   501 				v->u.rail.track == 0x80 &&
   502 				v->u.rail.track == 0x80 &&
   502 				--row < 0)
   503 				--row < 0)
   503 					goto found_it;
   504 					goto found_it;
   504 	}
   505 	}
   511 
   512 
   512 found_it:
   513 found_it:
   513 	d->head = d->wagon = v;
   514 	d->head = d->wagon = v;
   514 
   515 
   515 	/* either pressed the flag or the number, but only when it's a loco */
   516 	/* either pressed the flag or the number, but only when it's a loco */
   516 	if (x < 0 && v->subtype == TS_Front_Engine)
   517 	if (x < 0 && IsFrontEngine(v))
   517 		return (x >= -10) ? -2 : -1;
   518 		return (x >= -10) ? -2 : -1;
   518 
   519 
   519 	// skip vehicles that are scrolled off the left side
   520 	// skip vehicles that are scrolled off the left side
   520 	while (skip--) v = v->next;
   521 	while (skip--) v = v->next;
   521 
   522 
   524 		v = v->next;
   525 		v = v->next;
   525 		if (v == NULL) break;
   526 		if (v == NULL) break;
   526 	}
   527 	}
   527 
   528 
   528 	// if an articulated part was selected, find its parent
   529 	// if an articulated part was selected, find its parent
   529 	while (v != NULL && v->subtype == TS_Artic_Part) v = GetPrevVehicleInChain(v);
   530 	while (v != NULL && IsArticulatedPart(v)) v = GetPrevVehicleInChain(v);
   530 
   531 
   531 	d->wagon = v;
   532 	d->wagon = v;
   532 
   533 
   533 	return 0;
   534 	return 0;
   534 }
   535 }
   537 {
   538 {
   538 	Vehicle *v;
   539 	Vehicle *v;
   539 
   540 
   540 	v = GetVehicle(sel);
   541 	v = GetVehicle(sel);
   541 
   542 
   542 	if (/*v->subtype == TS_Front_Engine ||*/ v == wagon)
   543 	if (v == wagon)
   543 		return;
   544 		return;
   544 
   545 
   545 	if (wagon == NULL) {
   546 	if (wagon == NULL) {
   546 		if (head != NULL)
   547 		if (head != NULL)
   547 			wagon = GetLastVehicleInChain(head);
   548 			wagon = GetLastVehicleInChain(head);
   603 static void HandleCloneVehClick(const Vehicle* v, const Window* w)
   604 static void HandleCloneVehClick(const Vehicle* v, const Window* w)
   604 {
   605 {
   605 	if (v == NULL || v->type != VEH_Train) return;
   606 	if (v == NULL || v->type != VEH_Train) return;
   606 
   607 
   607 	// for train vehicles: subtype 0 for locs and not zero for others
   608 	// for train vehicles: subtype 0 for locs and not zero for others
   608 	if (v->subtype != TS_Front_Engine) {
   609 	if (!IsFrontEngine(v)) {
   609 		v = GetFirstVehicleInChain(v);
   610 		v = GetFirstVehicleInChain(v);
   610 		// Do nothing when clicking on a train in depot with no loc attached
   611 		// Do nothing when clicking on a train in depot with no loc attached
   611 		if (v->subtype != TS_Front_Engine) return;
   612 		if (!IsFrontEngine(v)) return;
   612 	}
   613 	}
   613 
   614 
   614 	DoCommandP(w->window_number, v->index, _ctrl_pressed ? 1 : 0, CcCloneTrain,
   615 	DoCommandP(w->window_number, v->index, _ctrl_pressed ? 1 : 0, CcCloneTrain,
   615 		CMD_CLONE_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE)
   616 		CMD_CLONE_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE)
   616 	);
   617 	);
   705 
   706 
   706 			HandleButtonClick(w, e->click.widget);
   707 			HandleButtonClick(w, e->click.widget);
   707 
   708 
   708 			sell_cmd = (e->click.widget == 5 || _ctrl_pressed) ? 1 : 0;
   709 			sell_cmd = (e->click.widget == 5 || _ctrl_pressed) ? 1 : 0;
   709 
   710 
   710 			if (v->subtype != TS_Front_Engine) {
   711 			if (!IsFrontEngine(v)) {
   711 				DoCommandP(v->tile, v->index, sell_cmd, NULL, CMD_SELL_RAIL_WAGON | CMD_MSG(STR_8839_CAN_T_SELL_RAILROAD_VEHICLE));
   712 				DoCommandP(v->tile, v->index, sell_cmd, NULL, CMD_SELL_RAIL_WAGON | CMD_MSG(STR_8839_CAN_T_SELL_RAILROAD_VEHICLE));
   712 			} else {
   713 			} else {
   713 				_backup_orders_tile = v->tile;
   714 				_backup_orders_tile = v->tile;
   714 				BackupVehicleOrders(v, _backup_orders_data);
   715 				BackupVehicleOrders(v, _backup_orders_data);
   715 				if (!DoCommandP(v->tile, v->index, sell_cmd, NULL, CMD_SELL_RAIL_WAGON | CMD_MSG(STR_8839_CAN_T_SELL_RAILROAD_VEHICLE)))
   716 				if (!DoCommandP(v->tile, v->index, sell_cmd, NULL, CMD_SELL_RAIL_WAGON | CMD_MSG(STR_8839_CAN_T_SELL_RAILROAD_VEHICLE)))
   726 
   727 
   727 				if (GetVehicleFromTrainDepotWndPt(w, e->dragdrop.pt.x, e->dragdrop.pt.y, &gdvp) == 0 &&
   728 				if (GetVehicleFromTrainDepotWndPt(w, e->dragdrop.pt.x, e->dragdrop.pt.y, &gdvp) == 0 &&
   728 						sel != INVALID_VEHICLE) {
   729 						sel != INVALID_VEHICLE) {
   729 					if (gdvp.wagon == NULL || gdvp.wagon->index != sel) {
   730 					if (gdvp.wagon == NULL || gdvp.wagon->index != sel) {
   730 						TrainDepotMoveVehicle(gdvp.wagon, sel, gdvp.head);
   731 						TrainDepotMoveVehicle(gdvp.wagon, sel, gdvp.head);
   731 					} else if (gdvp.head != NULL && gdvp.head->subtype == TS_Front_Engine) {
   732 					} else if (gdvp.head != NULL && IsFrontEngine(gdvp.head)) {
   732 						ShowTrainViewWindow(gdvp.head);
   733 						ShowTrainViewWindow(gdvp.head);
   733 					}
   734 					}
   734 				}
   735 				}
   735 			} break;
   736 			} break;
   736 
   737 
  1199 				u = v;
  1200 				u = v;
  1200 				do {
  1201 				do {
  1201 					DrawTrainImage(u, x + WagonLengthToPixels(dx), y, 1, 0, INVALID_VEHICLE);
  1202 					DrawTrainImage(u, x + WagonLengthToPixels(dx), y, 1, 0, INVALID_VEHICLE);
  1202 					dx += u->u.rail.cached_veh_length;
  1203 					dx += u->u.rail.cached_veh_length;
  1203 					u = u->next;
  1204 					u = u->next;
  1204 				} while (u != NULL && u->subtype == TS_Artic_Part);
  1205 				} while (u != NULL && IsArticulatedPart(u));
  1205 				_train_details_drawer_proc[WP(w,traindetails_d).tab](v, x + WagonLengthToPixels(dx) + 2, y + 2);
  1206 				_train_details_drawer_proc[WP(w,traindetails_d).tab](v, x + WagonLengthToPixels(dx) + 2, y + 2);
  1206 				y += 14;
  1207 				y += 14;
  1207 			}
  1208 			}
  1208 			if ((v = GetNextVehicle(v)) == NULL)
  1209 			if ((v = GetNextVehicle(v)) == NULL)
  1209 				return;
  1210 				return;
  1462 
  1463 
  1463 				if (id_v >= vl->list_length) return; // click out of list bound
  1464 				if (id_v >= vl->list_length) return; // click out of list bound
  1464 
  1465 
  1465 				v = GetVehicle(vl->sort_list[id_v].index);
  1466 				v = GetVehicle(vl->sort_list[id_v].index);
  1466 
  1467 
  1467 				assert(v->type == VEH_Train && v->subtype == TS_Front_Engine && v->owner == owner);
  1468 				assert(v->type == VEH_Train && IsFrontEngine(v) && v->owner == owner);
  1468 
  1469 
  1469 				ShowTrainViewWindow(v);
  1470 				ShowTrainViewWindow(v);
  1470 			}
  1471 			}
  1471 		} break;
  1472 		} break;
  1472 
  1473