373 } |
373 } |
374 } |
374 } |
375 |
375 |
376 static void GenerateBuildList(Window *w) |
376 static void GenerateBuildList(Window *w) |
377 { |
377 { |
378 EngineID eid; |
378 EngineID eid, sel_id; |
379 int num_engines = 0; |
379 int num_engines = 0; |
380 int num_wagons = 0; |
380 int num_wagons = 0; |
381 buildvehicle_d *bv = &WP(w, buildvehicle_d); |
381 buildvehicle_d *bv = &WP(w, buildvehicle_d); |
382 |
382 |
383 bv->filter.railtype = (w->window_number == 0) ? RAILTYPE_END : GetRailType(w->window_number); |
383 bv->filter.railtype = (w->window_number == 0) ? RAILTYPE_END : GetRailType(w->window_number); |
384 |
384 |
385 EngList_RemoveAll(&bv->eng_list); |
385 EngList_RemoveAll(&bv->eng_list); |
386 |
386 |
387 // make a list of all available cars |
387 /* Make list of all available train engines and wagons. |
388 for (eid = 0; eid < NUM_TRAIN_ENGINES; eid++) { |
388 * Also check to see if the previously selected engine is still available, |
|
389 * and if not, reset selection to INVALID_ENGINE. This could be the case |
|
390 * when engines become obsolete and are removed */ |
|
391 for (sel_id = INVALID_ENGINE, eid = 0; eid < NUM_TRAIN_ENGINES; eid++) { |
389 const Engine *e = GetEngine(eid); |
392 const Engine *e = GetEngine(eid); |
390 const RailVehicleInfo *rvi = RailVehInfo(eid); |
393 const RailVehicleInfo *rvi = RailVehInfo(eid); |
391 |
394 |
392 if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(e->railtype, bv->filter.railtype)) continue; |
395 if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(e->railtype, bv->filter.railtype)) continue; |
393 if (!IsEngineBuildable(eid, VEH_Train)) continue; |
396 if (!IsEngineBuildable(eid, VEH_Train)) continue; |
396 if ((rvi->flags & RVI_WAGON) == 0) { |
399 if ((rvi->flags & RVI_WAGON) == 0) { |
397 num_engines++; |
400 num_engines++; |
398 } else { |
401 } else { |
399 num_wagons++; |
402 num_wagons++; |
400 } |
403 } |
401 } |
404 |
|
405 if (eid == bv->sel_engine) sel_id = eid; |
|
406 } |
|
407 |
|
408 bv->sel_engine = sel_id; |
402 |
409 |
403 // make engines first, and then wagons, sorted by ListPositionOfEngine() |
410 // make engines first, and then wagons, sorted by ListPositionOfEngine() |
404 _internal_sort_order = false; |
411 _internal_sort_order = false; |
405 EngList_Sort(&bv->eng_list, TrainEnginesThenWagonsSorter); |
412 EngList_Sort(&bv->eng_list, TrainEnginesThenWagonsSorter); |
406 |
413 |
418 int num_engines = EngList_Count(&bv->eng_list); |
425 int num_engines = EngList_Count(&bv->eng_list); |
419 int x = 1; |
426 int x = 1; |
420 int y = 27; |
427 int y = 27; |
421 EngineID selected_id = bv->sel_engine; |
428 EngineID selected_id = bv->sel_engine; |
422 int max = w->vscroll.pos + w->vscroll.cap; |
429 int max = w->vscroll.pos + w->vscroll.cap; |
423 uint16 scrollcount = 0; |
|
424 |
430 |
425 SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in |
431 SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in |
426 |
432 |
427 /* Make sure that the selected engine is still in the list*/ |
433 SetVScrollCount(w, EngList_Count(&bv->eng_list)); |
428 if (bv->sel_engine != INVALID_ENGINE) { |
|
429 int i; |
|
430 bool found = false; |
|
431 for (i = 0; i < num_engines; i++) { |
|
432 if (bv->sel_engine != bv->eng_list[i]) continue; |
|
433 found = true; |
|
434 break; |
|
435 } |
|
436 if (!found) bv->sel_engine = INVALID_ENGINE; |
|
437 } |
|
438 |
|
439 scrollcount = EngList_Count(&bv->eng_list); |
|
440 |
|
441 SetVScrollCount(w, scrollcount); |
|
442 SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); |
434 SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); |
443 DrawWindowWidgets(w); |
435 DrawWindowWidgets(w); |
444 |
436 |
445 /* Draw the engines */ |
437 /* Draw the engines */ |
446 engine_drawing_loop(&bv->eng_list, x, &y, selected_id, w->vscroll.pos, max); |
438 engine_drawing_loop(&bv->eng_list, x, &y, selected_id, w->vscroll.pos, max); |
467 EngList_Create(&bv->eng_list); |
459 EngList_Create(&bv->eng_list); |
468 bv->sel_engine = INVALID_ENGINE; |
460 bv->sel_engine = INVALID_ENGINE; |
469 bv->sort_criteria = _last_sort_criteria; |
461 bv->sort_criteria = _last_sort_criteria; |
470 bv->descending_sort_order = _last_sort_order; |
462 bv->descending_sort_order = _last_sort_order; |
471 GenerateBuildList(w); |
463 GenerateBuildList(w); |
|
464 /* Select the first engine in the list as default when opening the window */ |
|
465 if (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0]; |
472 break; |
466 break; |
473 |
467 |
474 case WE_INVALIDATE_DATA: |
468 case WE_INVALIDATE_DATA: |
475 GenerateBuildList(w); |
469 GenerateBuildList(w); |
476 break; |
470 break; |