396 _cur_dpi = old_dpi; |
396 _cur_dpi = old_dpi; |
397 } |
397 } |
398 |
398 |
399 static void DrawTrainDepotWindow(Window *w) |
399 static void DrawTrainDepotWindow(Window *w) |
400 { |
400 { |
|
401 Vehicle **vl = WP(w, traindepot_d).vehicle_list; |
401 TileIndex tile; |
402 TileIndex tile; |
402 Vehicle *v, *u; |
403 int x, y, i, hnum, max; |
403 int num,x,y,i, hnum; |
|
404 Depot *depot; |
404 Depot *depot; |
|
405 uint16 num; |
405 |
406 |
406 tile = w->window_number; |
407 tile = w->window_number; |
407 |
408 |
408 /* setup disabled buttons */ |
409 /* setup disabled buttons */ |
409 w->disabled_state = |
410 w->disabled_state = |
410 IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 5) | (1 << 8) | (1<<9)); |
411 IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 5) | (1 << 8) | (1<<9)); |
411 |
412 |
412 /* determine amount of items for scroller */ |
413 /* determine amount of items for scroller */ |
413 num = 0; |
|
414 hnum = 8; |
414 hnum = 8; |
415 FOR_ALL_VEHICLES(v) { |
415 for (num = 0; num < WP(w, traindepot_d).engine_count; num++) { |
416 if (v->type == VEH_Train && |
416 const Vehicle *v = vl[num]; |
417 (IsFrontEngine(v) || IsFreeWagon(v)) && |
417 hnum = maxu(hnum, v->u.rail.cached_total_length); |
418 v->tile == tile && |
|
419 v->u.rail.track == 0x80) { |
|
420 num++; |
|
421 // determine number of items in the X direction. |
|
422 if (IsFrontEngine(v)) { |
|
423 hnum = max(hnum, v->u.rail.cached_total_length); |
|
424 } |
|
425 } |
|
426 } |
418 } |
427 |
419 |
428 /* Always have 1 empty row, so people can change the setting of the train */ |
420 /* Always have 1 empty row, so people can change the setting of the train */ |
429 num++; |
421 SetVScrollCount(w, WP(w, traindepot_d).engine_count + WP(w, traindepot_d).wagon_count + 1); |
430 |
|
431 SetVScrollCount(w, num); |
|
432 SetHScrollCount(w, WagonLengthToPixels(hnum)); |
422 SetHScrollCount(w, WagonLengthToPixels(hnum)); |
433 |
423 |
434 /* locate the depot struct */ |
424 /* locate the depot struct */ |
435 depot = GetDepotByTile(tile); |
425 depot = GetDepotByTile(tile); |
436 assert(depot != NULL); |
426 assert(depot != NULL); |
439 DrawWindowWidgets(w); |
429 DrawWindowWidgets(w); |
440 |
430 |
441 x = 2; |
431 x = 2; |
442 y = 15; |
432 y = 15; |
443 num = w->vscroll.pos; |
433 num = w->vscroll.pos; |
444 |
434 max = min(WP(w, traindepot_d).engine_count, w->vscroll.pos + w->vscroll.cap); |
445 // draw all trains |
435 |
446 FOR_ALL_VEHICLES(v) { |
436 |
447 if (v->type == VEH_Train && IsFrontEngine(v) && |
437 /* draw all trains */ |
448 v->tile == tile && v->u.rail.track == 0x80 && |
438 for (; num < max; num++) { |
449 --num < 0 && num >= -w->vscroll.cap) { |
439 const Vehicle *v = vl[num]; |
450 DrawTrainImage(v, x+21, y, w->hscroll.cap + 4, w->hscroll.pos, WP(w,traindepot_d).sel); |
440 |
451 /* Draw the train number */ |
441 DrawTrainImage(v, x + 21, y, w->hscroll.cap + 4, w->hscroll.pos, WP(w,traindepot_d).sel); |
452 SetDParam(0, v->unitnumber); |
442 /* Draw the train number */ |
453 DrawString(x, y, (v->max_age - 366 < v->age) ? STR_00E3 : STR_00E2, 0); |
443 SetDParam(0, v->unitnumber); |
454 |
444 DrawString(x, y, (v->max_age - 366 < v->age) ? STR_00E3 : STR_00E2, 0); |
455 // Number of wagons relative to a standard length wagon (rounded up) |
445 |
456 SetDParam(0, (v->u.rail.cached_total_length + 7) / 8); |
446 /* Number of wagons relative to a standard length wagon (rounded up) */ |
457 DrawStringRightAligned(w->widget[6].right - 1, y + 4, STR_TINY_BLACK, 0); // Draw the counter |
447 SetDParam(0, (v->u.rail.cached_total_length + 7) / 8); |
458 |
448 DrawStringRightAligned(w->widget[6].right - 1, y + 4, STR_TINY_BLACK, 0); // Draw the counter |
459 /* Draw the pretty flag */ |
449 |
460 DrawSprite(v->vehstatus & VS_STOPPED ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, x + 15, y); |
450 /* Draw the pretty flag */ |
461 |
451 DrawSprite(v->vehstatus & VS_STOPPED ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, x + 15, y); |
462 y += 14; |
452 |
463 } |
453 y += 14; |
464 } |
454 } |
465 |
455 |
466 // draw all remaining vehicles |
456 max = min(WP(w, traindepot_d).engine_count + WP(w, traindepot_d).wagon_count, w->vscroll.pos + w->vscroll.cap); |
467 FOR_ALL_VEHICLES(v) { |
457 |
468 if (v->type == VEH_Train && IsFreeWagon(v) && |
458 /* draw all remaining vehicles */ |
469 v->tile == tile && v->u.rail.track == 0x80 && |
459 for (; num < max; num++) { |
470 --num < 0 && num >= -w->vscroll.cap) { |
460 const Vehicle *v = WP(w, traindepot_d).wagon_list[num - WP(w, traindepot_d).engine_count]; |
471 DrawTrainImage(v, x+50, y, w->hscroll.cap - 29, 0, WP(w,traindepot_d).sel); |
461 const Vehicle *u; |
472 DrawString(x, y+2, STR_8816, 0); |
462 |
473 |
463 DrawTrainImage(v, x + 50, y, w->hscroll.cap - 29, 0, WP(w,traindepot_d).sel); |
474 /*Draw the train counter */ |
464 DrawString(x, y + 2, STR_8816, 0); |
475 i = 0; |
465 |
476 u = v; |
466 /*Draw the train counter */ |
477 do i++; while ( (u=u->next) != NULL); // Determine length of train |
467 i = 0; |
478 SetDParam(0, i); // Set the counter |
468 u = v; |
479 DrawStringRightAligned(w->widget[6].right - 1, y + 4, STR_TINY_BLACK, 0); // Draw the counter |
469 do i++; while ( (u=u->next) != NULL); // Determine length of train |
480 y += 14; |
470 SetDParam(0, i); // Set the counter |
481 } |
471 DrawStringRightAligned(w->widget[6].right - 1, y + 4, STR_TINY_BLACK, 0); // Draw the counter |
|
472 y += 14; |
482 } |
473 } |
483 } |
474 } |
484 |
475 |
485 typedef struct GetDepotVehiclePtData { |
476 typedef struct GetDepotVehiclePtData { |
486 Vehicle *head; |
477 Vehicle *head; |
487 Vehicle *wagon; |
478 Vehicle *wagon; |
488 } GetDepotVehiclePtData; |
479 } GetDepotVehiclePtData; |
489 |
480 |
490 static int GetVehicleFromTrainDepotWndPt(const Window *w, int x, int y, GetDepotVehiclePtData *d) |
481 static int GetVehicleFromTrainDepotWndPt(const Window *w, int x, int y, GetDepotVehiclePtData *d) |
491 { |
482 { |
|
483 Vehicle **vl = WP(w, traindepot_d).vehicle_list; |
492 int row; |
484 int row; |
493 int skip = 0; |
485 int skip = 0; |
494 Vehicle *v; |
486 Vehicle *v; |
495 |
487 |
496 x = x - 23; |
488 x = x - 23; |
498 row = (y - 14) / 14; |
490 row = (y - 14) / 14; |
499 if ((uint)row >= w->vscroll.cap) return 1; /* means err */ |
491 if ((uint)row >= w->vscroll.cap) return 1; /* means err */ |
500 |
492 |
501 row += w->vscroll.pos; |
493 row += w->vscroll.pos; |
502 |
494 |
503 /* go through all the locomotives */ |
495 if (WP(w, traindepot_d).engine_count + WP(w, traindepot_d).wagon_count <= row) { |
504 FOR_ALL_VEHICLES(v) { |
496 /* empty row, so no vehicle is selected */ |
505 if (v->type == VEH_Train && |
497 d->head = NULL; |
506 IsFrontEngine(v) && |
498 d->wagon = NULL; |
507 v->tile == w->window_number && |
499 return 0; |
508 v->u.rail.track == 0x80 && |
500 } |
509 --row < 0) { |
501 |
510 skip = w->hscroll.pos; |
502 if (WP(w, traindepot_d).engine_count > row) { |
511 goto found_it; |
503 v = vl[row]; |
512 } |
504 skip = w->hscroll.pos; |
513 } |
505 } else { |
514 |
506 vl = WP(w, traindepot_d).wagon_list; |
515 x -= _traininfo_vehicle_width; /* free wagons don't have an initial loco. */ |
507 v = vl[row - WP(w, traindepot_d).engine_count]; |
516 |
508 /* free wagons don't have an initial loco. */ |
517 /* and then the list of free wagons */ |
509 x -= _traininfo_vehicle_width; |
518 FOR_ALL_VEHICLES(v) { |
510 } |
519 if (v->type == VEH_Train && |
511 |
520 IsFreeWagon(v) && |
|
521 v->tile == w->window_number && |
|
522 v->u.rail.track == 0x80 && |
|
523 --row < 0) { |
|
524 goto found_it; |
|
525 } |
|
526 } |
|
527 |
|
528 d->head = NULL; |
|
529 d->wagon = NULL; |
|
530 |
|
531 /* didn't find anything, get out */ |
|
532 return 0; |
|
533 |
|
534 found_it: |
|
535 d->head = d->wagon = v; |
512 d->head = d->wagon = v; |
536 |
513 |
537 /* either pressed the flag or the number, but only when it's a loco */ |
514 /* either pressed the flag or the number, but only when it's a loco */ |
538 if (x < 0 && IsFrontEngine(v)) return (x >= -10) ? -2 : -1; |
515 if (x < 0 && IsFrontEngine(v)) return (x >= -10) ? -2 : -1; |
539 |
516 |
646 } |
623 } |
647 |
624 |
648 static void TrainDepotWndProc(Window *w, WindowEvent *e) |
625 static void TrainDepotWndProc(Window *w, WindowEvent *e) |
649 { |
626 { |
650 switch (e->event) { |
627 switch (e->event) { |
|
628 case WE_CREATE: |
|
629 WP(w, traindepot_d).vehicle_list = NULL; |
|
630 WP(w, traindepot_d).wagon_list = NULL; |
|
631 WP(w, traindepot_d).engine_count = 0; |
|
632 WP(w, traindepot_d).wagon_count = 0; |
|
633 break; |
|
634 |
651 case WE_PAINT: |
635 case WE_PAINT: |
|
636 BuildDepotVehicleList(VEH_Train, w->window_number, &WP(w, traindepot_d).vehicle_list, &WP(w, traindepot_d).engine_list_length, &WP(w, traindepot_d).engine_count, |
|
637 &WP(w, traindepot_d).wagon_list, &WP(w, traindepot_d).wagon_list_length, &WP(w, traindepot_d).wagon_count); |
652 DrawTrainDepotWindow(w); |
638 DrawTrainDepotWindow(w); |
653 break; |
639 break; |
654 |
640 |
655 case WE_CLICK: { |
641 case WE_CLICK: { |
656 switch (e->we.click.widget) { |
642 switch (e->we.click.widget) { |