436 if (*num_wagons != wagon_length) *wagons = realloc((void*)*wagons, (*num_wagons) * sizeof((*wagons)[0])); |
436 if (*num_wagons != wagon_length) *wagons = realloc((void*)*wagons, (*num_wagons) * sizeof((*wagons)[0])); |
437 } |
437 } |
438 |
438 |
439 static void SortTrainBuildList(Window *w) |
439 static void SortTrainBuildList(Window *w) |
440 { |
440 { |
441 _internal_sort_order = WP(w,buildtrain_d).decenting_sort_order; |
441 _internal_sort_order = WP(w,buildvehicle_d).decenting_sort_order; |
442 qsort((void*)WP(w, buildtrain_d).list_a, WP(w, buildtrain_d).list_a_length, sizeof(WP(w, buildtrain_d).list_a[0]), |
442 qsort((void*)WP(w, buildvehicle_d).list_a, WP(w, buildvehicle_d).list_a_length, sizeof(WP(w, buildvehicle_d).list_a[0]), |
443 _engine_sorter[WP(w,buildtrain_d).sort_criteria]); |
443 _engine_sorter[WP(w,buildvehicle_d).sort_criteria]); |
444 } |
444 } |
445 |
445 |
446 static void DrawTrainBuildWindow(Window *w) |
446 static void DrawTrainBuildWindow(Window *w) |
447 { |
447 { |
448 int x = 1; |
448 int x = 1; |
449 int y = 27; |
449 int y = 27; |
450 EngineID position = w->vscroll.pos; |
450 EngineID position = w->vscroll.pos; |
451 EngineID selected_id = WP(w,buildtrain_d).sel_engine; |
451 EngineID selected_id = WP(w,buildvehicle_d).sel_engine; |
452 int max = w->vscroll.pos + w->vscroll.cap; |
452 int max = w->vscroll.pos + w->vscroll.cap; |
453 uint16 scrollcount = 0; |
453 uint16 scrollcount = 0; |
454 |
454 |
455 SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in |
455 SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in |
456 |
456 |
457 /* Draw the clicked engine/wagon/both button pressed and unpress the other two */ |
457 /* Draw the clicked engine/wagon/both button pressed and unpress the other two */ |
458 SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_ENGINES, WP(w,buildtrain_d).show_engine_button == 1); |
458 SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_ENGINES, WP(w,buildvehicle_d).show_engine_button == 1); |
459 SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_WAGONS, WP(w,buildtrain_d).show_engine_button == 2); |
459 SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_WAGONS, WP(w,buildvehicle_d).show_engine_button == 2); |
460 SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_BOTH, WP(w,buildtrain_d).show_engine_button == 3); |
460 SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_BOTH, WP(w,buildvehicle_d).show_engine_button == 3); |
461 |
461 |
462 if (WP(w,buildtrain_d).data_invalidated) { |
462 if (WP(w,buildvehicle_d).data_invalidated) { |
463 GenerateBuildList(&WP(w, buildtrain_d).list_a, &WP(w, buildtrain_d).list_a_length, &WP(w, buildtrain_d).list_b, &WP(w, buildtrain_d).list_b_length, WP(w,buildtrain_d).railtype); |
463 GenerateBuildList(&WP(w, buildvehicle_d).list_a, &WP(w, buildvehicle_d).list_a_length, &WP(w, buildvehicle_d).list_b, &WP(w, buildvehicle_d).list_b_length, WP(w,buildvehicle_d).railtype); |
464 WP(w,buildtrain_d).data_invalidated = false; |
464 WP(w,buildvehicle_d).data_invalidated = false; |
465 SortTrainBuildList(w); |
465 SortTrainBuildList(w); |
466 |
466 |
467 /* Make sure that the selected engine is still in the list*/ |
467 /* Make sure that the selected engine is still in the list*/ |
468 if (WP(w,buildtrain_d).sel_engine != INVALID_ENGINE) { |
468 if (WP(w,buildvehicle_d).sel_engine != INVALID_ENGINE) { |
469 int i; |
469 int i; |
470 bool found = false; |
470 bool found = false; |
471 if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) { |
471 if (HASBIT(WP(w,buildvehicle_d).show_engine_button, 0)) { |
472 for (i = 0; i < WP(w, buildtrain_d).list_a_length; i++) { |
472 for (i = 0; i < WP(w, buildvehicle_d).list_a_length; i++) { |
473 if (WP(w,buildtrain_d).sel_engine != WP(w, buildtrain_d).list_a[i]) continue; |
473 if (WP(w,buildvehicle_d).sel_engine != WP(w, buildvehicle_d).list_a[i]) continue; |
474 found = true; |
474 found = true; |
475 break; |
475 break; |
476 } |
476 } |
477 } |
477 } |
478 if (!found && HASBIT(WP(w,buildtrain_d).show_engine_button, 1)) { |
478 if (!found && HASBIT(WP(w,buildvehicle_d).show_engine_button, 1)) { |
479 for (i = 0; i < WP(w, buildtrain_d).list_b_length; i++) { |
479 for (i = 0; i < WP(w, buildvehicle_d).list_b_length; i++) { |
480 if (WP(w,buildtrain_d).sel_engine != WP(w, buildtrain_d).list_b[i]) continue; |
480 if (WP(w,buildvehicle_d).sel_engine != WP(w, buildvehicle_d).list_b[i]) continue; |
481 found = true; |
481 found = true; |
482 break; |
482 break; |
483 } |
483 } |
484 } |
484 } |
485 if (!found) WP(w,buildtrain_d).sel_engine = INVALID_ENGINE; |
485 if (!found) WP(w,buildvehicle_d).sel_engine = INVALID_ENGINE; |
486 } |
486 } |
487 } |
487 } |
488 |
488 |
489 if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) scrollcount += WP(w, buildtrain_d).list_a_length; |
489 if (HASBIT(WP(w,buildvehicle_d).show_engine_button, 0)) scrollcount += WP(w, buildvehicle_d).list_a_length; |
490 if (HASBIT(WP(w,buildtrain_d).show_engine_button, 1)) scrollcount += WP(w, buildtrain_d).list_b_length; |
490 if (HASBIT(WP(w,buildvehicle_d).show_engine_button, 1)) scrollcount += WP(w, buildvehicle_d).list_b_length; |
491 |
491 |
492 SetVScrollCount(w, scrollcount); |
492 SetVScrollCount(w, scrollcount); |
493 SetDParam(0, WP(w,buildtrain_d).railtype + STR_881C_NEW_RAIL_VEHICLES); |
493 SetDParam(0, WP(w,buildvehicle_d).railtype + STR_881C_NEW_RAIL_VEHICLES); |
494 DrawWindowWidgets(w); |
494 DrawWindowWidgets(w); |
495 |
495 |
496 if (selected_id == INVALID_ENGINE && scrollcount != 0) { |
496 if (selected_id == INVALID_ENGINE && scrollcount != 0) { |
497 if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0) && WP(w, buildtrain_d).list_a_length != 0) { |
497 if (HASBIT(WP(w,buildvehicle_d).show_engine_button, 0) && WP(w, buildvehicle_d).list_a_length != 0) { |
498 selected_id = WP(w, buildtrain_d).list_a[0]; |
498 selected_id = WP(w, buildvehicle_d).list_a[0]; |
499 } else { |
499 } else { |
500 selected_id = WP(w, buildtrain_d).list_b[0]; |
500 selected_id = WP(w, buildvehicle_d).list_b[0]; |
501 } |
501 } |
502 WP(w,buildtrain_d).sel_engine = selected_id; |
502 WP(w,buildvehicle_d).sel_engine = selected_id; |
503 } |
503 } |
504 |
504 |
505 /* Draw the engines */ |
505 /* Draw the engines */ |
506 if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) { |
506 if (HASBIT(WP(w,buildvehicle_d).show_engine_button, 0)) { |
507 engine_drawing_loop(WP(w, buildtrain_d).list_a, WP(w, buildtrain_d).list_a_length, x, &y, selected_id, &position, max); |
507 engine_drawing_loop(WP(w, buildvehicle_d).list_a, WP(w, buildvehicle_d).list_a_length, x, &y, selected_id, &position, max); |
508 |
508 |
509 /* Magically set the number 0 line to the one right after the last engine |
509 /* Magically set the number 0 line to the one right after the last engine |
510 * This way the line numbers fit the indexes in the wagon array */ |
510 * This way the line numbers fit the indexes in the wagon array */ |
511 position -= WP(w, buildtrain_d).list_a_length; |
511 position -= WP(w, buildvehicle_d).list_a_length; |
512 max -= WP(w, buildtrain_d).list_a_length; |
512 max -= WP(w, buildvehicle_d).list_a_length; |
513 } |
513 } |
514 |
514 |
515 /* Draw the wagons */ |
515 /* Draw the wagons */ |
516 if (HASBIT(WP(w,buildtrain_d).show_engine_button, 1)) { |
516 if (HASBIT(WP(w,buildvehicle_d).show_engine_button, 1)) { |
517 engine_drawing_loop(WP(w, buildtrain_d).list_b, WP(w, buildtrain_d).list_b_length, x, &y, selected_id, &position, max); |
517 engine_drawing_loop(WP(w, buildvehicle_d).list_b, WP(w, buildvehicle_d).list_b_length, x, &y, selected_id, &position, max); |
518 } |
518 } |
519 |
519 |
520 if (selected_id != INVALID_ENGINE) { |
520 if (selected_id != INVALID_ENGINE) { |
521 const RailVehicleInfo *rvi = RailVehInfo(selected_id); |
521 const RailVehicleInfo *rvi = RailVehInfo(selected_id); |
522 |
522 |
524 DrawTrainWagonPurchaseInfo(2, w->widget[BUILD_TRAIN_WIDGET_PANEL].top + 1, selected_id); |
524 DrawTrainWagonPurchaseInfo(2, w->widget[BUILD_TRAIN_WIDGET_PANEL].top + 1, selected_id); |
525 } else { |
525 } else { |
526 DrawTrainEnginePurchaseInfo(2, w->widget[BUILD_TRAIN_WIDGET_PANEL].top + 1, selected_id); |
526 DrawTrainEnginePurchaseInfo(2, w->widget[BUILD_TRAIN_WIDGET_PANEL].top + 1, selected_id); |
527 } |
527 } |
528 } |
528 } |
529 DrawString(85, 15, _engine_sort_listing[WP(w,buildtrain_d).sort_criteria], 0x10); |
529 DrawString(85, 15, _engine_sort_listing[WP(w,buildvehicle_d).sort_criteria], 0x10); |
530 DoDrawString(WP(w,buildtrain_d).decenting_sort_order ? DOWNARROW : UPARROW, 69, 15, 0x10); |
530 DoDrawString(WP(w,buildvehicle_d).decenting_sort_order ? DOWNARROW : UPARROW, 69, 15, 0x10); |
531 } |
531 } |
532 |
532 |
533 static void NewRailVehicleWndProc(Window *w, WindowEvent *e) |
533 static void NewRailVehicleWndProc(Window *w, WindowEvent *e) |
534 { |
534 { |
535 switch (e->event) { |
535 switch (e->event) { |
536 case WE_CREATE: |
536 case WE_CREATE: |
537 WP(w, buildtrain_d).list_a_length = 0; |
537 WP(w, buildvehicle_d).list_a_length = 0; |
538 WP(w, buildtrain_d).list_b_length = 0; |
538 WP(w, buildvehicle_d).list_b_length = 0; |
539 WP(w, buildtrain_d).list_a = NULL; |
539 WP(w, buildvehicle_d).list_a = NULL; |
540 WP(w, buildtrain_d).list_b = NULL; |
540 WP(w, buildvehicle_d).list_b = NULL; |
541 WP(w, buildtrain_d).show_engine_button = 3; |
541 WP(w, buildvehicle_d).show_engine_button = 3; |
542 WP(w, buildtrain_d).data_invalidated = true; |
542 WP(w, buildvehicle_d).data_invalidated = true; |
543 WP(w, buildtrain_d).sel_engine = INVALID_ENGINE; |
543 WP(w, buildvehicle_d).sel_engine = INVALID_ENGINE; |
544 WP(w, buildtrain_d).sort_criteria = _last_sort_criteria; |
544 WP(w, buildvehicle_d).sort_criteria = _last_sort_criteria; |
545 WP(w, buildtrain_d).decenting_sort_order = _last_sort_order; |
545 WP(w, buildvehicle_d).decenting_sort_order = _last_sort_order; |
546 break; |
546 break; |
547 |
547 |
548 case WE_INVALIDATE_DATA: |
548 case WE_INVALIDATE_DATA: |
549 WP(w,buildtrain_d).data_invalidated = true; |
549 WP(w,buildvehicle_d).data_invalidated = true; |
550 break; |
550 break; |
551 |
551 |
552 case WE_DESTROY: |
552 case WE_DESTROY: |
553 free((void*)WP(w, buildtrain_d).list_a); |
553 free((void*)WP(w, buildvehicle_d).list_a); |
554 free((void*)WP(w, buildtrain_d).list_b); |
554 free((void*)WP(w, buildvehicle_d).list_b); |
555 break; |
555 break; |
556 |
556 |
557 case WE_PAINT: |
557 case WE_PAINT: |
558 DrawTrainBuildWindow(w); |
558 DrawTrainBuildWindow(w); |
559 break; |
559 break; |
560 |
560 |
561 |
561 |
562 case WE_CLICK: { |
562 case WE_CLICK: { |
563 switch (e->we.click.widget) { |
563 switch (e->we.click.widget) { |
564 case BUILD_TRAIN_WIDGET_SORT_ASSENDING_DESENTING: |
564 case BUILD_TRAIN_WIDGET_SORT_ASSENDING_DESENTING: |
565 WP(w,buildtrain_d).decenting_sort_order = !WP(w,buildtrain_d).decenting_sort_order; |
565 WP(w,buildvehicle_d).decenting_sort_order = !WP(w,buildvehicle_d).decenting_sort_order; |
566 _last_sort_order = WP(w,buildtrain_d).decenting_sort_order; |
566 _last_sort_order = WP(w,buildvehicle_d).decenting_sort_order; |
567 SortTrainBuildList(w); |
567 SortTrainBuildList(w); |
568 SetWindowDirty(w); |
568 SetWindowDirty(w); |
569 break; |
569 break; |
570 |
570 |
571 case BUILD_TRAIN_WIDGET_SORT_TEXT: case BUILD_TRAIN_WIDGET_SORT_DROPDOWN:/* Select sorting criteria dropdown menu */ |
571 case BUILD_TRAIN_WIDGET_SORT_TEXT: case BUILD_TRAIN_WIDGET_SORT_DROPDOWN:/* Select sorting criteria dropdown menu */ |
572 ShowDropDownMenu(w, _engine_sort_listing, WP(w,buildtrain_d).sort_criteria, BUILD_TRAIN_WIDGET_SORT_DROPDOWN, 0, 0); |
572 ShowDropDownMenu(w, _engine_sort_listing, WP(w,buildvehicle_d).sort_criteria, BUILD_TRAIN_WIDGET_SORT_DROPDOWN, 0, 0); |
573 return; |
573 return; |
574 |
574 |
575 case BUILD_TRAIN_WIDGET_LIST: { |
575 case BUILD_TRAIN_WIDGET_LIST: { |
576 uint i = ((e->we.click.pt.y - 26) / 14) + w->vscroll.pos; |
576 uint i = ((e->we.click.pt.y - 26) / 14) + w->vscroll.pos; |
577 if (i < (uint)(WP(w, buildtrain_d).list_a_length + WP(w, buildtrain_d).list_b_length)) { |
577 if (i < (uint)(WP(w, buildvehicle_d).list_a_length + WP(w, buildvehicle_d).list_b_length)) { |
578 if (i < WP(w, buildtrain_d).list_a_length && HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) { |
578 if (i < WP(w, buildvehicle_d).list_a_length && HASBIT(WP(w,buildvehicle_d).show_engine_button, 0)) { |
579 WP(w,buildtrain_d).sel_engine = WP(w, buildtrain_d).list_a[i]; |
579 WP(w,buildvehicle_d).sel_engine = WP(w, buildvehicle_d).list_a[i]; |
580 } else { |
580 } else { |
581 WP(w,buildtrain_d).sel_engine = WP(w, buildtrain_d).list_b[i - (HASBIT(WP(w,buildtrain_d).show_engine_button, 0) ? WP(w, buildtrain_d).list_a_length : 0)]; |
581 WP(w,buildvehicle_d).sel_engine = WP(w, buildvehicle_d).list_b[i - (HASBIT(WP(w,buildvehicle_d).show_engine_button, 0) ? WP(w, buildvehicle_d).list_a_length : 0)]; |
582 } |
582 } |
583 SetWindowDirty(w); |
583 SetWindowDirty(w); |
584 } |
584 } |
585 } break; |
585 } break; |
586 |
586 |
591 * Because the button widgets are ordered as they are (in a row), we can calculate as following: |
591 * Because the button widgets are ordered as they are (in a row), we can calculate as following: |
592 * engines = bit 0 (1 for set), wagons bit 1 (2 for set), both (2 | 1 = 3) |
592 * engines = bit 0 (1 for set), wagons bit 1 (2 for set), both (2 | 1 = 3) |
593 * Those numbers are the same as the clicked button - BUILD_TRAIN_WIDGET_ENGINES + 1 */ |
593 * Those numbers are the same as the clicked button - BUILD_TRAIN_WIDGET_ENGINES + 1 */ |
594 |
594 |
595 byte click_state = e->we.click.widget - BUILD_TRAIN_WIDGET_ENGINES + 1; |
595 byte click_state = e->we.click.widget - BUILD_TRAIN_WIDGET_ENGINES + 1; |
596 if (WP(w,buildtrain_d).show_engine_button == click_state) break; // We clicked the pressed button |
596 if (WP(w,buildvehicle_d).show_engine_button == click_state) break; // We clicked the pressed button |
597 WP(w,buildtrain_d).sel_engine = INVALID_ENGINE; |
597 WP(w,buildvehicle_d).sel_engine = INVALID_ENGINE; |
598 WP(w,buildtrain_d).show_engine_button = click_state; |
598 WP(w,buildvehicle_d).show_engine_button = click_state; |
599 w->vscroll.pos = 0; |
599 w->vscroll.pos = 0; |
600 SetWindowDirty(w); |
600 SetWindowDirty(w); |
601 } |
601 } |
602 break; |
602 break; |
603 |
603 |
604 case BUILD_TRAIN_WIDGET_BUILD: { |
604 case BUILD_TRAIN_WIDGET_BUILD: { |
605 EngineID sel_eng = WP(w,buildtrain_d).sel_engine; |
605 EngineID sel_eng = WP(w,buildvehicle_d).sel_engine; |
606 if (sel_eng != INVALID_ENGINE) |
606 if (sel_eng != INVALID_ENGINE) |
607 DoCommandP(w->window_number, sel_eng, 0, (RailVehInfo(sel_eng)->flags & RVI_WAGON) ? CcBuildWagon : CcBuildLoco, CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE)); |
607 DoCommandP(w->window_number, sel_eng, 0, (RailVehInfo(sel_eng)->flags & RVI_WAGON) ? CcBuildWagon : CcBuildLoco, CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE)); |
608 } break; |
608 } break; |
609 |
609 |
610 case BUILD_TRAIN_WIDGET_RENAME: { |
610 case BUILD_TRAIN_WIDGET_RENAME: { |
611 EngineID sel_eng = WP(w,buildtrain_d).sel_engine; |
611 EngineID sel_eng = WP(w,buildvehicle_d).sel_engine; |
612 if (sel_eng != INVALID_ENGINE) { |
612 if (sel_eng != INVALID_ENGINE) { |
613 WP(w,buildtrain_d).rename_engine = sel_eng; |
613 WP(w,buildvehicle_d).rename_engine = sel_eng; |
614 ShowQueryString(GetCustomEngineName(sel_eng), |
614 ShowQueryString(GetCustomEngineName(sel_eng), |
615 STR_886A_RENAME_TRAIN_VEHICLE_TYPE, 31, 160, w->window_class, w->window_number, CS_ALPHANUMERAL); |
615 STR_886A_RENAME_TRAIN_VEHICLE_TYPE, 31, 160, w->window_class, w->window_number, CS_ALPHANUMERAL); |
616 } |
616 } |
617 } break; |
617 } break; |
618 } |
618 } |
619 } break; |
619 } break; |
620 |
620 |
621 case WE_ON_EDIT_TEXT: { |
621 case WE_ON_EDIT_TEXT: { |
622 if (e->we.edittext.str[0] != '\0') { |
622 if (e->we.edittext.str[0] != '\0') { |
623 _cmd_text = e->we.edittext.str; |
623 _cmd_text = e->we.edittext.str; |
624 DoCommandP(0, WP(w,buildtrain_d).rename_engine, 0, NULL, |
624 DoCommandP(0, WP(w,buildvehicle_d).rename_engine, 0, NULL, |
625 CMD_RENAME_ENGINE | CMD_MSG(STR_886B_CAN_T_RENAME_TRAIN_VEHICLE)); |
625 CMD_RENAME_ENGINE | CMD_MSG(STR_886B_CAN_T_RENAME_TRAIN_VEHICLE)); |
626 } |
626 } |
627 } break; |
627 } break; |
628 |
628 |
629 case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */ |
629 case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */ |
630 if (WP(w,buildtrain_d).sort_criteria != e->we.dropdown.index) { |
630 if (WP(w,buildvehicle_d).sort_criteria != e->we.dropdown.index) { |
631 WP(w,buildtrain_d).sort_criteria = e->we.dropdown.index; |
631 WP(w,buildvehicle_d).sort_criteria = e->we.dropdown.index; |
632 _last_sort_criteria = e->we.dropdown.index; |
632 _last_sort_criteria = e->we.dropdown.index; |
633 SortTrainBuildList(w); |
633 SortTrainBuildList(w); |
634 } |
634 } |
635 SetWindowDirty(w); |
635 SetWindowDirty(w); |
636 break; |
636 break; |