101
80
void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
103
_rebuild_left_list = false; // left list is only for the vehicles the player owns and is not related to being buildable
104
_rebuild_right_list = true;
105
InvalidateWindowData(WC_REPLACE_VEHICLE, type); // Update the autoreplace window
82
InvalidateWindowData(WC_REPLACE_VEHICLE, type, false); // Update the autoreplace window
106
83
InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
109
/** Get the default cargo type for an engine
110
* @param engine the EngineID to get the cargo for
111
* @return the cargo type carried by the engine (CT_INVALID if engine got no cargo capacity)
113
static CargoID EngineCargo(EngineID engine)
115
if (engine == INVALID_ENGINE) return CT_INVALID; // surely INVALID_ENGINE can't carry anything but CT_INVALID
117
switch (GetEngine(engine)->type) {
118
default: NOT_REACHED();
120
if (RailVehInfo(engine)->capacity == 0) return CT_INVALID; // no capacity -> can't carry cargo
121
return RailVehInfo(engine)->cargo_type;
122
case VEH_ROAD: return RoadVehInfo(engine)->cargo_type;
123
case VEH_SHIP: return ShipVehInfo(engine)->cargo_type;
124
case VEH_AIRCRAFT: return CT_PASSENGERS; // all planes are build with passengers by default
128
/** Figure out if an engine should be added to a list
129
* @param e The EngineID
130
* @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected)
131
* @param show_engines if truem then locomotives are drawn, else wagons (never both)
132
* @return true if the engine should be in the list (based on this check)
134
static bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
136
const RailVehicleInfo *rvi = RailVehInfo(e);
138
/* Ensure that the wagon/engine selection fits the engine. */
139
if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
141
if (draw_left && show_engines) {
142
/* Ensure that the railtype is specific to the selected one */
143
if (rvi->railtype != _railtype_selected_in_replace_gui) return false;
145
/* Ensure that it's a compatible railtype to the selected one (like electric <-> diesel)
146
* The vehicle do not have to have power on the railtype in question, only able to drive (pulled if needed) */
147
if (!IsCompatibleRail(rvi->railtype, _railtype_selected_in_replace_gui)) return false;
152
/** Figure out if two engines got at least one type of cargo in common (refitting if needed)
153
* @param engine_a one of the EngineIDs
154
* @param engine_b the other EngineID
155
* @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all)
157
static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b)
159
CargoID a = EngineCargo(engine_a);
160
CargoID b = EngineCargo(engine_b);
162
/* we should always be able to refit to/from locomotives without capacity
163
* Because of that, CT_INVALID shoudl always return true */
164
if (a == CT_INVALID || b == CT_INVALID || a == b) return true; // they carry no ro the same type by default
165
if (EngInfo(engine_a)->refit_mask & EngInfo(engine_b)->refit_mask) return true; // both can refit to the same
166
if (CanRefitTo(engine_a, b) || CanRefitTo(engine_b, a)) return true; // one can refit to what the other one carries
171
* @param w Window, that contains the list
172
* @param draw_left true if generating the left list, otherwise false
174
static void GenerateReplaceVehList(Window *w, bool draw_left)
177
EngineID selected_engine = INVALID_ENGINE;
178
VehicleType type = (VehicleType)w->window_number;
179
byte i = draw_left ? 0 : 1;
181
EngineList *list = &WP(w, replaceveh_d).list[i];
182
EngList_RemoveAll(list);
184
FOR_ALL_ENGINEIDS_OF_TYPE(e, type) {
185
if (type == VEH_TRAIN && !GenerateReplaceRailList(e, draw_left, WP(w, replaceveh_d).wagon_btnstate)) continue; // special rules for trains
188
const GroupID selected_group = WP(w, replaceveh_d).sel_group;
189
const uint num_engines = GetGroupNumEngines(_local_player, selected_group, e);
191
/* Skip drawing the engines we don't have any of and haven't set for replacement */
192
if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), e, selected_group) == INVALID_ENGINE) continue;
194
/* This is for engines we can replace to and they should depend on what we selected to replace from */
195
if (!IsEngineBuildable(e, type, _local_player)) continue; // we need to be able to build the engine
196
if (!EnginesGotCargoInCommon(e, WP(w, replaceveh_d).sel_engine[0])) continue; // the engines needs to be able to carry the same cargo
198
/* Road vehicles can't be replaced by trams and vice-versa */
199
if (type == VEH_ROAD && HasBit(EngInfo(WP(w, replaceveh_d).sel_engine[0])->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(e)->misc_flags, EF_ROAD_TRAM)) continue;
200
if (e == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
203
EngList_Add(list, e);
204
if (e == WP(w, replaceveh_d).sel_engine[i]) selected_engine = e; // The selected engine is still in the list
206
WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
207
if (type == VEH_TRAIN) EngList_Sort(list, &TrainEngineNumberSorter);
210
/** Generate the lists
211
* @param w Window containing the lists
213
static void GenerateLists(Window *w)
215
EngineID e = WP(w, replaceveh_d).sel_engine[0];
217
if (WP(w, replaceveh_d).update_left == true) {
218
/* We need to rebuild the left list */
219
GenerateReplaceVehList(w, true);
220
SetVScrollCount(w, EngList_Count(&WP(w, replaceveh_d).list[0]));
221
if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[0]) != 0) {
222
WP(w, replaceveh_d).sel_engine[0] = WP(w, replaceveh_d).list[0][0];
226
if (WP(w, replaceveh_d).update_right || e != WP(w, replaceveh_d).sel_engine[0]) {
227
/* Either we got a request to rebuild the right list or the left list selected a different engine */
228
if (WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE) {
229
/* Always empty the right list when nothing is selected in the left list */
230
EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
231
WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
233
GenerateReplaceVehList(w, false);
234
SetVScroll2Count(w, EngList_Count(&WP(w, replaceveh_d).list[1]));
235
if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[1] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[1]) != 0) {
236
WP(w, replaceveh_d).sel_engine[1] = WP(w, replaceveh_d).list[1][0];
240
/* Reset the flags about needed updates */
241
WP(w, replaceveh_d).update_left = false;
242
WP(w, replaceveh_d).update_right = false;
243
WP(w, replaceveh_d).init_lists = false;
247
void DrawEngineList(VehicleType type, int x, int y, const EngineList eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
249
static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
251
/* Strings for the pulldown menu */
252
static const StringID _vehicle_type_names[] = {
254
STR_019C_ROAD_VEHICLE,
261
WP(w, replaceveh_d).wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool)
262
EngList_Create(&WP(w, replaceveh_d).list[0]);
263
EngList_Create(&WP(w, replaceveh_d).list[1]);
264
WP(w, replaceveh_d).update_left = true;
265
WP(w, replaceveh_d).update_right = true;
266
WP(w, replaceveh_d).init_lists = true;
267
WP(w, replaceveh_d).sel_engine[0] = INVALID_ENGINE;
268
WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
272
if (WP(w, replaceveh_d).update_left || WP(w, replaceveh_d).update_right) GenerateLists(w);
274
Player *p = GetPlayer(_local_player);
275
EngineID selected_id[2];
276
const GroupID selected_group = WP(w, replaceveh_d).sel_group;
278
selected_id[0] = WP(w, replaceveh_d).sel_engine[0];
279
selected_id[1] = WP(w, replaceveh_d).sel_engine[1];
281
/* Disable the "Start Replacing" button if:
282
* Either list is empty
283
* or The selected replacement engine has a replacement (to prevent loops)
284
* or The right list (new replacement) has the existing replacement vehicle selected */
285
w->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE,
286
selected_id[0] == INVALID_ENGINE ||
287
selected_id[1] == INVALID_ENGINE ||
288
EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE ||
289
EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]);
291
/* Disable the "Stop Replacing" button if:
292
* The left list (existing vehicle) is empty
293
* or The selected vehicle has no replacement set up */
294
w->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE,
295
selected_id[0] == INVALID_ENGINE ||
296
!EngineHasReplacementForPlayer(p, selected_id[0], selected_group));
298
/* now the actual drawing of the window itself takes place */
299
SetDParam(0, _vehicle_type_names[w->window_number]);
301
if (w->window_number == VEH_TRAIN) {
302
/* set on/off for renew_keep_length */
303
SetDParam(1, p->renew_keep_length ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF);
305
/* set wagon/engine button */
306
SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
308
/* sets the colour of that art thing */
309
w->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].color = _player_colors[_local_player];
310
w->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].color = _player_colors[_local_player];
313
if (w->window_number == VEH_TRAIN) {
314
/* Show the selected railtype in the pulldown menu */
315
RailType railtype = _railtype_selected_in_replace_gui;
316
w->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = _rail_types_list[railtype];
319
DrawWindowWidgets(w);
321
/* sets up the string for the vehicle that is being replaced to */
322
if (selected_id[0] != INVALID_ENGINE) {
323
if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) {
324
SetDParam(0, STR_NOT_REPLACING);
326
SetDParam(0, STR_ENGINE_NAME);
327
SetDParam(1, EngineReplacementForPlayer(p, selected_id[0], selected_group));
330
SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
333
DrawString(145, w->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK);
336
for(byte i = 0; i < 2; i++) {
337
uint widget = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
338
EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
339
EngineID start = i == 0 ? w->vscroll.pos : w->vscroll2.pos; // what is the offset for the start (scrolling)
340
EngineID end = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list));
342
/* Do the actual drawing */
343
DrawEngineList((VehicleType)w->window_number, w->widget[widget].left + 2, w->widget[widget].top + 1, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0, selected_group);
345
/* Also draw the details if an engine is selected */
346
if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) {
347
const Widget *wi = &w->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS];
348
int text_end = DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, WP(w, replaceveh_d).sel_engine[i]);
350
if (text_end > wi->bottom) {
352
ResizeWindowForWidget(w, i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS, 0, text_end - wi->bottom);
358
} break; // end of paint
361
switch (e->we.click.widget) {
362
case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE:
363
WP(w, replaceveh_d).wagon_btnstate = !(WP(w, replaceveh_d).wagon_btnstate);
364
WP(w, replaceveh_d).update_left = true;
365
WP(w, replaceveh_d).init_lists = true;
369
case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: /* Railtype selection dropdown menu */
370
ShowDropDownMenu(w, _rail_types_list, _railtype_selected_in_replace_gui, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, 0, ~GetPlayer(_local_player)->avail_railtypes);
373
case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: /* toggle renew_keep_length */
374
DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE);
377
case RVW_WIDGET_START_REPLACE: { /* Start replacing */
378
EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
379
EngineID veh_to = WP(w, replaceveh_d).sel_engine[1];
380
DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
383
case RVW_WIDGET_STOP_REPLACE: { /* Stop replacing */
384
EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
385
DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
388
case RVW_WIDGET_LEFT_MATRIX:
389
case RVW_WIDGET_RIGHT_MATRIX: {
390
uint i = (e->we.click.pt.y - 14) / w->resize.step_height;
391
uint16 click_scroll_pos = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.pos : w->vscroll2.pos;
392
uint16 click_scroll_cap = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.cap : w->vscroll2.cap;
393
byte click_side = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
394
uint16 engine_count = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
396
if (i < click_scroll_cap) {
397
i += click_scroll_pos;
398
EngineID e = engine_count > i ? WP(w, replaceveh_d).list[click_side][i] : INVALID_ENGINE;
399
if (e == WP(w, replaceveh_d).sel_engine[click_side]) break; // we clicked the one we already selected
400
WP(w, replaceveh_d).sel_engine[click_side] = e;
401
if (click_side == 0) {
402
WP(w, replaceveh_d).update_right = true;
403
WP(w, replaceveh_d).init_lists = true;
413
case WE_DROPDOWN_SELECT: { /* we have selected a dropdown item in the list */
414
RailType temp = (RailType)e->we.dropdown.index;
415
if (temp == _railtype_selected_in_replace_gui) break; // we didn't select a new one. No need to change anything
416
_railtype_selected_in_replace_gui = temp;
417
/* Reset scrollbar positions */
420
/* Rebuild the lists */
421
WP(w, replaceveh_d).update_left = true;
422
WP(w, replaceveh_d).update_right = true;
423
WP(w, replaceveh_d).init_lists = true;
428
w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
429
w->vscroll2.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
431
w->widget[RVW_WIDGET_LEFT_MATRIX].data = (w->vscroll.cap << 8) + 1;
432
w->widget[RVW_WIDGET_RIGHT_MATRIX].data = (w->vscroll2.cap << 8) + 1;
435
case WE_INVALIDATE_DATA:
436
if (_rebuild_left_list) WP(w, replaceveh_d).update_left = true;
437
if (_rebuild_right_list) WP(w, replaceveh_d).update_right = true;
442
EngList_RemoveAll(&WP(w, replaceveh_d).list[0]);
443
EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
448
static const Widget _replace_rail_vehicle_widgets[] = {
449
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
450
{ WWT_CAPTION, RESIZE_NONE, 14, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS},
451
{ WWT_STICKYBOX, RESIZE_NONE, 14, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON},
452
{ WWT_PANEL, RESIZE_TB, 14, 0, 227, 126, 227, 0x0, STR_NULL},
453
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 240, 251, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON},
454
{ WWT_PANEL, RESIZE_TB, 14, 139, 316, 228, 239, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB},
455
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 306, 443, 240, 251, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON},
456
{ WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 14, 125, 0x801, STR_REPLACE_HELP_LEFT_ARRAY},
457
{ WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
458
{ WWT_MATRIX, RESIZE_BOTTOM, 14, 228, 443, 14, 125, 0x801, STR_REPLACE_HELP_RIGHT_ARRAY},
459
{ WWT_SCROLL2BAR, RESIZE_BOTTOM, 14, 444, 455, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
460
{ WWT_PANEL, RESIZE_TB, 14, 228, 455, 126, 227, 0x0, STR_NULL},
461
// train specific stuff
462
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 228, 239, STR_REPLACE_ENGINE_WAGON_SELECT, STR_REPLACE_ENGINE_WAGON_SELECT_HELP}, // widget 12
463
{ WWT_PANEL, RESIZE_TB, 14, 139, 153, 240, 251, 0x0, STR_NULL},
464
{ WWT_DROPDOWN, RESIZE_TB, 14, 154, 289, 240, 251, 0x0, STR_REPLACE_HELP_RAILTYPE},
465
{ WWT_PANEL, RESIZE_TB, 14, 290, 305, 240, 251, 0x0, STR_NULL},
466
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 317, 455, 228, 239, STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP},
467
// end of train specific stuff
468
{ WWT_RESIZEBOX, RESIZE_TB, 14, 444, 455, 240, 251, STR_NULL, STR_RESIZE_BUTTON},
472
static const Widget _replace_road_vehicle_widgets[] = {
473
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
474
{ WWT_CAPTION, RESIZE_NONE, 14, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS},
475
{ WWT_STICKYBOX, RESIZE_NONE, 14, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON},
476
{ WWT_PANEL, RESIZE_TB, 14, 0, 227, 126, 217, 0x0, STR_NULL},
477
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 218, 229, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON},
478
{ WWT_PANEL, RESIZE_TB, 14, 139, 305, 218, 229, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB},
479
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 306, 443, 218, 229, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON},
480
{ WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 14, 125, 0x801, STR_REPLACE_HELP_LEFT_ARRAY},
481
{ WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
482
{ WWT_MATRIX, RESIZE_BOTTOM, 14, 228, 443, 14, 125, 0x801, STR_REPLACE_HELP_RIGHT_ARRAY},
483
{ WWT_SCROLL2BAR, RESIZE_BOTTOM, 14, 444, 455, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
484
{ WWT_PANEL, RESIZE_TB, 14, 228, 455, 126, 217, 0x0, STR_NULL},
485
{ WWT_RESIZEBOX, RESIZE_TB, 14, 444, 455, 218, 229, STR_NULL, STR_RESIZE_BUTTON},
489
static const Widget _replace_ship_aircraft_vehicle_widgets[] = {
490
{ WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
491
{ WWT_CAPTION, RESIZE_NONE, 14, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS},
492
{ WWT_STICKYBOX, RESIZE_NONE, 14, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON},
493
{ WWT_PANEL, RESIZE_TB, 14, 0, 227, 110, 201, 0x0, STR_NULL},
494
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 202, 213, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON},
495
{ WWT_PANEL, RESIZE_TB, 14, 139, 305, 202, 213, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB},
496
{ WWT_PUSHTXTBTN, RESIZE_TB, 14, 306, 443, 202, 213, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON},
497
{ WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 14, 109, 0x401, STR_REPLACE_HELP_LEFT_ARRAY},
498
{ WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 14, 109, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
499
{ WWT_MATRIX, RESIZE_BOTTOM, 14, 228, 443, 14, 109, 0x401, STR_REPLACE_HELP_RIGHT_ARRAY},
500
{ WWT_SCROLL2BAR, RESIZE_BOTTOM, 14, 444, 455, 14, 109, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
501
{ WWT_PANEL, RESIZE_TB, 14, 228, 455, 110, 201, 0x0, STR_NULL},
502
{ WWT_RESIZEBOX, RESIZE_TB, 14, 444, 455, 202, 213, STR_NULL, STR_RESIZE_BUTTON},
506
static const WindowDesc _replace_rail_vehicle_desc = {
507
WDP_AUTO, WDP_AUTO, 456, 252, 456, 252,
508
WC_REPLACE_VEHICLE, WC_NONE,
509
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
510
_replace_rail_vehicle_widgets,
511
ReplaceVehicleWndProc
514
static const WindowDesc _replace_road_vehicle_desc = {
515
WDP_AUTO, WDP_AUTO, 456, 230, 456, 230,
516
WC_REPLACE_VEHICLE, WC_NONE,
517
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
518
_replace_road_vehicle_widgets,
519
ReplaceVehicleWndProc
522
static const WindowDesc _replace_ship_aircraft_vehicle_desc = {
523
WDP_AUTO, WDP_AUTO, 456, 214, 456, 214,
524
WC_REPLACE_VEHICLE, WC_NONE,
525
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
526
_replace_ship_aircraft_vehicle_widgets,
527
ReplaceVehicleWndProc
87
* Window for the autoreplacing of vehicles.
89
class ReplaceVehicleWindow : public Window {
91
EngineID sel_engine[2];
93
bool wagon_btnstate; ///< true means engine is selected
94
GUIEngineList list[2];
99
static RailType sel_railtype;
101
/** Figure out if an engine should be added to a list
102
* @param e The EngineID
103
* @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected)
104
* @param show_engines if truem then locomotives are drawn, else wagons (never both)
105
* @return true if the engine should be in the list (based on this check)
107
bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
109
const RailVehicleInfo *rvi = RailVehInfo(e);
111
/* Ensure that the wagon/engine selection fits the engine. */
112
if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
114
if (draw_left && show_engines) {
115
/* Ensure that the railtype is specific to the selected one */
116
if (rvi->railtype != this->sel_railtype) return false;
123
* @param w Window, that contains the list
124
* @param draw_left true if generating the left list, otherwise false
126
void GenerateReplaceVehList(Window *w, bool draw_left)
128
EngineID selected_engine = INVALID_ENGINE;
129
VehicleType type = (VehicleType)this->window_number;
130
byte i = draw_left ? 0 : 1;
132
GUIEngineList *list = &this->list[i];
136
FOR_ALL_ENGINES_OF_TYPE(e, type) {
137
EngineID eid = e->index;
138
if (type == VEH_TRAIN && !GenerateReplaceRailList(eid, draw_left, this->wagon_btnstate)) continue; // special rules for trains
141
const GroupID selected_group = this->sel_group;
142
const uint num_engines = GetGroupNumEngines(_local_company, selected_group, eid);
144
/* Skip drawing the engines we don't have any of and haven't set for replacement */
145
if (num_engines == 0 && EngineReplacementForCompany(GetCompany(_local_company), eid, selected_group) == INVALID_ENGINE) continue;
147
if (!CheckAutoreplaceValidity(this->sel_engine[0], eid, _local_company)) continue;
150
*list->Append() = eid;
151
if (eid == this->sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
153
this->sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
154
EngList_Sort(list, &EngineNumberSorter);
157
/** Generate the lists */
160
EngineID e = this->sel_engine[0];
162
if (this->update_left == true) {
163
/* We need to rebuild the left list */
164
GenerateReplaceVehList(this, true);
165
SetVScrollCount(this, this->list[0].Length());
166
if (this->init_lists && this->sel_engine[0] == INVALID_ENGINE && this->list[0].Length() != 0) {
167
this->sel_engine[0] = this->list[0][0];
171
if (this->update_right || e != this->sel_engine[0]) {
172
/* Either we got a request to rebuild the right list or the left list selected a different engine */
173
if (this->sel_engine[0] == INVALID_ENGINE) {
174
/* Always empty the right list when nothing is selected in the left list */
175
this->list[1].Clear();
176
this->sel_engine[1] = INVALID_ENGINE;
178
GenerateReplaceVehList(this, false);
179
SetVScroll2Count(this, this->list[1].Length());
180
if (this->init_lists && this->sel_engine[1] == INVALID_ENGINE && this->list[1].Length() != 0) {
181
this->sel_engine[1] = this->list[1][0];
185
/* Reset the flags about needed updates */
186
this->update_left = false;
187
this->update_right = false;
188
this->init_lists = false;
192
ReplaceVehicleWindow(const WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc, vehicletype)
194
this->wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool)
195
this->update_left = true;
196
this->update_right = true;
197
this->init_lists = true;
198
this->sel_engine[0] = INVALID_ENGINE;
199
this->sel_engine[1] = INVALID_ENGINE;
201
this->resize.step_height = GetVehicleListHeight(vehicletype);
202
this->vscroll.cap = this->resize.step_height == 14 ? 8 : 4;
204
Widget *widget = this->widget;
205
widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll.cap << 8) + 1;
207
if (vehicletype == VEH_TRAIN) {
208
this->wagon_btnstate = true;
209
/* The train window is bigger so we will move some of the widgets to fit the new size.
210
* We will start by moving the resize button to the lower right corner. */
211
widget[RVW_WIDGET_RESIZE].top = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].top;
212
widget[RVW_WIDGET_RESIZE].bottom = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].bottom;
213
widget[RVW_WIDGET_STOP_REPLACE].right = widget[RVW_WIDGET_RESIZE].right;
215
/* The detail panel is one line taller for trains so we will move some of the widgets one line (10 pixels) down. */
216
widget[RVW_WIDGET_LEFT_DETAILS].bottom += 10;
217
widget[RVW_WIDGET_RIGHT_DETAILS].bottom += 10;
218
for (int i = RVW_WIDGET_START_REPLACE; i < RVW_WIDGET_RESIZE; i++) {
220
widget[i].bottom += 10;
223
/* Since it's not a train we will hide the train only widgets. */
224
this->SetWidgetsHiddenState(true,
225
RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE,
226
RVW_WIDGET_TRAIN_FLUFF_LEFT,
227
RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN,
228
RVW_WIDGET_TRAIN_FLUFF_RIGHT,
229
RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE,
233
ResizeWindow(this, 0, this->resize.step_height * this->vscroll.cap);
235
/* Set the minimum window size to the current window size */
236
this->resize.width = this->width;
237
this->resize.height = this->height;
239
this->owner = _local_company;
240
this->sel_group = id_g;
241
this->vscroll2.cap = this->vscroll.cap; // these two are always the same
243
this->FindWindowPlacementAndResize(desc);
246
virtual void OnPaint()
248
if (this->update_left || this->update_right) this->GenerateLists();
250
Company *c = GetCompany(_local_company);
251
EngineID selected_id[2];
252
const GroupID selected_group = this->sel_group;
254
selected_id[0] = this->sel_engine[0];
255
selected_id[1] = this->sel_engine[1];
257
/* Disable the "Start Replacing" button if:
258
* Either list is empty
259
* or The selected replacement engine has a replacement (to prevent loops)
260
* or The right list (new replacement) has the existing replacement vehicle selected */
261
this->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE,
262
selected_id[0] == INVALID_ENGINE ||
263
selected_id[1] == INVALID_ENGINE ||
264
EngineReplacementForCompany(c, selected_id[1], selected_group) != INVALID_ENGINE ||
265
EngineReplacementForCompany(c, selected_id[0], selected_group) == selected_id[1]);
267
/* Disable the "Stop Replacing" button if:
268
* The left list (existing vehicle) is empty
269
* or The selected vehicle has no replacement set up */
270
this->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE,
271
selected_id[0] == INVALID_ENGINE ||
272
!EngineHasReplacementForCompany(c, selected_id[0], selected_group));
274
/* now the actual drawing of the window itself takes place */
275
SetDParam(0, STR_019F_TRAIN + this->window_number);
277
if (this->window_number == VEH_TRAIN) {
278
/* set on/off for renew_keep_length */
279
SetDParam(1, c->renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
281
/* set wagon/engine button */
282
SetDParam(2, this->wagon_btnstate ? STR_ENGINES : STR_WAGONS);
284
/* sets the colour of that art thing */
285
this->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].colour = _company_colours[_local_company];
286
this->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].colour = _company_colours[_local_company];
289
if (this->window_number == VEH_TRAIN) {
290
/* Show the selected railtype in the pulldown menu */
291
const RailtypeInfo *rti = GetRailTypeInfo(sel_railtype);
292
this->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = rti->strings.replace_text;
297
/* sets up the string for the vehicle that is being replaced to */
298
if (selected_id[0] != INVALID_ENGINE) {
299
if (!EngineHasReplacementForCompany(c, selected_id[0], selected_group)) {
300
SetDParam(0, STR_NOT_REPLACING);
302
SetDParam(0, STR_ENGINE_NAME);
303
SetDParam(1, EngineReplacementForCompany(c, selected_id[0], selected_group));
306
SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
309
DrawStringTruncated(this->widget[RVW_WIDGET_INFO_TAB].left + 6, this->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK, this->GetWidgetWidth(RVW_WIDGET_INFO_TAB) - 12);
312
for (byte i = 0; i < 2; i++) {
313
uint widget = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
314
GUIEngineList *list = &this->list[i]; // which list to draw
315
EngineID start = i == 0 ? this->vscroll.pos : this->vscroll2.pos; // what is the offset for the start (scrolling)
316
EngineID end = min((i == 0 ? this->vscroll.cap : this->vscroll2.cap) + start, list->Length());
318
/* Do the actual drawing */
319
DrawEngineList((VehicleType)this->window_number, this->widget[widget].left + 2, this->widget[widget].right, this->widget[widget].top + 1, list, start, end, this->sel_engine[i], i == 0 ? this->widget[RVW_WIDGET_LEFT_MATRIX].right - 2 : 0, selected_group);
321
/* Also draw the details if an engine is selected */
322
if (this->sel_engine[i] != INVALID_ENGINE) {
323
const Widget *wi = &this->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS];
324
int text_end = DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, this->sel_engine[i]);
326
if (text_end > wi->bottom) {
328
ResizeWindowForWidget(this, i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS, 0, text_end - wi->bottom);
335
virtual void OnClick(Point pt, int widget)
338
case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE:
339
this->wagon_btnstate = !(this->wagon_btnstate);
340
this->update_left = true;
341
this->init_lists = true;
345
case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: { // Railtype selection dropdown menu
346
const Company *c = GetCompany(_local_company);
347
DropDownList *list = new DropDownList();
348
for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
349
const RailtypeInfo *rti = GetRailTypeInfo(rt);
350
/* Skip rail type if it has no label */
351
if (rti->label == 0) continue;
352
list->push_back(new DropDownListStringItem(rti->strings.replace_text, rt, !HasBit(c->avail_railtypes, rt)));
354
ShowDropDownList(this, list, sel_railtype, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN);
358
case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
359
DoCommandP(0, 5, GetCompany(_local_company)->renew_keep_length ? 0 : 1, CMD_SET_AUTOREPLACE);
362
case RVW_WIDGET_START_REPLACE: { // Start replacing
363
EngineID veh_from = this->sel_engine[0];
364
EngineID veh_to = this->sel_engine[1];
365
DoCommandP(0, 3 + (this->sel_group << 16) , veh_from + (veh_to << 16), CMD_SET_AUTOREPLACE);
369
case RVW_WIDGET_STOP_REPLACE: { // Stop replacing
370
EngineID veh_from = this->sel_engine[0];
371
DoCommandP(0, 3 + (this->sel_group << 16), veh_from + (INVALID_ENGINE << 16), CMD_SET_AUTOREPLACE);
375
case RVW_WIDGET_LEFT_MATRIX:
376
case RVW_WIDGET_RIGHT_MATRIX: {
377
uint i = (pt.y - 14) / this->resize.step_height;
378
uint16 click_scroll_pos = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.pos : this->vscroll2.pos;
379
uint16 click_scroll_cap = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.cap : this->vscroll2.cap;
380
byte click_side = widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
381
size_t engine_count = this->list[click_side].Length();
383
if (i < click_scroll_cap) {
384
i += click_scroll_pos;
385
EngineID e = engine_count > i ? this->list[click_side][i] : INVALID_ENGINE;
386
if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected
387
this->sel_engine[click_side] = e;
388
if (click_side == 0) {
389
this->update_right = true;
390
this->init_lists = true;
399
virtual void OnDropdownSelect(int widget, int index)
401
RailType temp = (RailType)index;
402
if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
404
/* Reset scrollbar positions */
405
this->vscroll.pos = 0;
406
this->vscroll2.pos = 0;
407
/* Rebuild the lists */
408
this->update_left = true;
409
this->update_right = true;
410
this->init_lists = true;
414
virtual void OnResize(Point new_size, Point delta) {
415
this->vscroll.cap += delta.y / (int)this->resize.step_height;
416
this->vscroll2.cap += delta.y / (int)this->resize.step_height;
418
Widget *widget = this->widget;
420
widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll2.cap << 8) + 1;
423
/* We changed the width of the window so we have to resize the lists.
424
* Because ResizeButtons() makes each widget the same size it can't be used on the lists
425
* because then the lists would have the same size as the scrollbars.
426
* Instead we use it on the detail panels.
427
* Afterwards we use the new location of the detail panels (the middle of the window)
428
* to place the lists.
429
* This way the lists will have equal size while keeping the width of the scrollbars unchanged. */
430
ResizeButtons(this, RVW_WIDGET_LEFT_DETAILS, RVW_WIDGET_RIGHT_DETAILS);
431
widget[RVW_WIDGET_RIGHT_MATRIX].left = widget[RVW_WIDGET_RIGHT_DETAILS].left;
432
widget[RVW_WIDGET_LEFT_SCROLLBAR].right = widget[RVW_WIDGET_LEFT_DETAILS].right;
433
widget[RVW_WIDGET_LEFT_SCROLLBAR].left = widget[RVW_WIDGET_LEFT_SCROLLBAR].right - 11;
434
widget[RVW_WIDGET_LEFT_MATRIX].right = widget[RVW_WIDGET_LEFT_SCROLLBAR].left - 1;
438
virtual void OnInvalidateData(int data)
441
this->update_left = true;
443
this->update_right = true;
448
static const Widget _replace_vehicle_widgets[] = {
449
{ WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW},
450
{ WWT_CAPTION, RESIZE_RIGHT, COLOUR_GREY, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS},
451
{ WWT_STICKYBOX, RESIZE_LR, COLOUR_GREY, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON},
453
{ WWT_MATRIX, RESIZE_BOTTOM, COLOUR_GREY, 0, 215, 14, 13, 0x1, STR_REPLACE_HELP_LEFT_ARRAY},
454
{ WWT_SCROLLBAR, RESIZE_BOTTOM, COLOUR_GREY, 216, 227, 14, 13, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
455
{ WWT_MATRIX, RESIZE_LRB, COLOUR_GREY, 228, 443, 14, 13, 0x1, STR_REPLACE_HELP_RIGHT_ARRAY},
456
{ WWT_SCROLL2BAR, RESIZE_LRB, COLOUR_GREY, 444, 455, 14, 13, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST},
457
{ WWT_PANEL, RESIZE_TB, COLOUR_GREY, 0, 227, 14, 105, 0x0, STR_NULL},
458
{ WWT_PANEL, RESIZE_RTB, COLOUR_GREY, 228, 455, 14, 105, 0x0, STR_NULL},
460
{ WWT_PUSHTXTBTN, RESIZE_TB, COLOUR_GREY, 0, 138, 106, 117, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON},
461
{ WWT_PANEL, RESIZE_RTB, COLOUR_GREY, 139, 305, 106, 117, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB},
462
{ WWT_PUSHTXTBTN, RESIZE_LRTB, COLOUR_GREY, 306, 443, 106, 117, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON},
463
{ WWT_RESIZEBOX, RESIZE_LRTB, COLOUR_GREY, 444, 455, 106, 117, STR_NULL, STR_RESIZE_BUTTON},
465
{ WWT_PUSHTXTBTN, RESIZE_TB, COLOUR_GREY, 0, 138, 128, 139, STR_REPLACE_ENGINE_WAGON_SELECT, STR_REPLACE_ENGINE_WAGON_SELECT_HELP},
466
{ WWT_PANEL, RESIZE_TB, COLOUR_GREY, 139, 153, 128, 139, 0x0, STR_NULL},
467
{ WWT_DROPDOWN, RESIZE_RTB, COLOUR_GREY, 154, 289, 128, 139, 0x0, STR_REPLACE_HELP_RAILTYPE},
468
{ WWT_PANEL, RESIZE_LRTB, COLOUR_GREY, 290, 305, 128, 139, 0x0, STR_NULL},
469
{ WWT_PUSHTXTBTN, RESIZE_LRTB, COLOUR_GREY, 306, 443, 128, 139, STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP},
473
static const WindowDesc _replace_rail_vehicle_desc(
474
WDP_AUTO, WDP_AUTO, 456, 140, 456, 140,
475
WC_REPLACE_VEHICLE, WC_NONE,
476
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE | WDF_CONSTRUCTION,
477
_replace_vehicle_widgets
480
static const WindowDesc _replace_vehicle_desc(
481
WDP_AUTO, WDP_AUTO, 456, 118, 456, 118,
482
WC_REPLACE_VEHICLE, WC_NONE,
483
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE | WDF_CONSTRUCTION,
484
_replace_vehicle_widgets
487
RailType ReplaceVehicleWindow::sel_railtype = RAILTYPE_RAIL;
531
489
void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
535
491
DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
537
switch (vehicletype) {
538
default: NOT_REACHED();
540
w = AllocateWindowDescFront(&_replace_rail_vehicle_desc, vehicletype);
542
w->resize.step_height = 14;
543
WP(w, replaceveh_d).wagon_btnstate = true;
546
w = AllocateWindowDescFront(&_replace_road_vehicle_desc, vehicletype);
548
w->resize.step_height = 14;
552
w = AllocateWindowDescFront(&_replace_ship_aircraft_vehicle_desc, vehicletype);
554
w->resize.step_height = 24;
558
w->caption_color = _local_player;
559
WP(w, replaceveh_d).sel_group = id_g;
560
w->vscroll2.cap = w->vscroll.cap; // these two are always the same
492
new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g);