tron@2186: /* $Id$ */ tron@2186: belugas@6443: /** @file autoreplace_gui.cpp */ belugas@6443: darkvater@164: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" bjarni@842: #include "gui.h" rubidium@8612: #include "command_func.h" tron@2159: #include "variables.h" tron@2159: #include "vehicle_gui.h" peter1138@2962: #include "newgrf_engine.h" rubidium@7139: #include "group.h" rubidium@8599: #include "rail.h" rubidium@8610: #include "strings_func.h" rubidium@8627: #include "window_func.h" rubidium@8640: #include "vehicle_func.h" rubidium@8708: #include "autoreplace_func.h" rubidium@8720: #include "gfx_func.h" rubidium@8750: #include "player_func.h" peter1138@8780: #include "widgets/dropdown_func.h" rubidium@9282: #include "engine_func.h" bjarni@6059: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8760: rubidium@9266: struct replaceveh_d { rubidium@9266: byte sel_index[2]; rubidium@9266: EngineID sel_engine[2]; rubidium@9266: uint16 count[2]; rubidium@9266: bool wagon_btnstate; ///< true means engine is selected rubidium@9266: EngineList list[2]; rubidium@9266: bool update_left; rubidium@9266: bool update_right; rubidium@9266: bool init_lists; rubidium@9266: GroupID sel_group; rubidium@9266: }; rubidium@9266: assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(replaceveh_d)); rubidium@9266: bjarni@6060: static RailType _railtype_selected_in_replace_gui; tron@505: bjarni@6195: static bool _rebuild_left_list; bjarni@6195: static bool _rebuild_right_list; bjarni@6195: bjarni@6059: static const StringID _rail_types_list[] = { bjarni@6059: STR_RAIL_VEHICLES, bjarni@6059: STR_ELRAIL_VEHICLES, bjarni@6059: STR_MONORAIL_VEHICLES, bjarni@6059: STR_MAGLEV_VEHICLES, bjarni@6059: INVALID_STRING_ID bjarni@6059: }; bjarni@6059: peter1138@8841: enum ReplaceVehicleWindowWidgets { peter1138@8841: RVW_WIDGET_LEFT_DETAILS = 3, peter1138@8841: RVW_WIDGET_START_REPLACE, peter1138@8841: RVW_WIDGET_INFO_TAB, peter1138@8841: RVW_WIDGET_STOP_REPLACE, peter1138@8841: RVW_WIDGET_LEFT_MATRIX, peter1138@8841: RVW_WIDGET_LEFT_SCROLLBAR, peter1138@8841: RVW_WIDGET_RIGHT_MATRIX, peter1138@8841: RVW_WIDGET_RIGHT_SCROLLBAR, peter1138@8841: RVW_WIDGET_RIGHT_DETAILS, peter1138@8841: peter1138@8841: RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE, peter1138@8841: RVW_WIDGET_TRAIN_FLUFF_LEFT, peter1138@8841: RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, peter1138@8841: RVW_WIDGET_TRAIN_FLUFF_RIGHT, peter1138@8841: RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE, peter1138@8841: }; peter1138@8841: peter1138@8670: static int CDECL TrainEngineNumberSorter(const void *a, const void *b) peter1138@8670: { peter1138@8670: const EngineID va = *(const EngineID*)a; peter1138@8670: const EngineID vb = *(const EngineID*)b; peter1138@8670: int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb); peter1138@8670: peter1138@8670: return r; peter1138@8670: } peter1138@8670: bjarni@6059: /* General Vehicle GUI based procedures that are independent of vehicle types */ rubidium@6573: void InitializeVehiclesGuiList() bjarni@6059: { bjarni@6059: _railtype_selected_in_replace_gui = RAILTYPE_RAIL; bjarni@6059: } bjarni@6059: bjarni@6195: /** Rebuild the left autoreplace list if an engine is removed or added bjarni@6195: * @param e Engine to check if it is removed or added peter1138@7592: * @param id_g The group the engine belongs to bjarni@6195: * Note: this function only works if it is called either bjarni@6195: * - when a new vehicle is build, but before it's counted in num_engines bjarni@6195: * - when a vehicle is deleted and after it's substracted from num_engines bjarni@6195: * - when not changing the count (used when changing replace orders) bjarni@6059: */ peter1138@7592: void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g) bjarni@6059: { bjarni@6195: Player *p = GetPlayer(_local_player); peter1138@8975: VehicleType type = GetEngine(e)->type; rubidium@7977: uint num_engines = GetGroupNumEngines(_local_player, id_g, e); bjarni@6059: peter1138@7592: if (num_engines == 0 || p->num_engines[e] == 0) { bjarni@6195: /* We don't have any of this engine type. bjarni@6195: * Either we just sold the last one, we build a new one or we stopped replacing it. bjarni@6195: * In all cases, we need to update the left list */ bjarni@6195: _rebuild_left_list = true; bjarni@6195: } else { bjarni@6195: _rebuild_left_list = false; bjarni@6195: } bjarni@6195: _rebuild_right_list = false; bjarni@6195: InvalidateWindowData(WC_REPLACE_VEHICLE, type); bjarni@6195: } bjarni@6059: bjarni@6195: /** When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists bjarni@6195: * @param type The type of engine bjarni@6195: */ rubidium@7134: void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type) bjarni@6195: { bjarni@6195: _rebuild_left_list = false; // left list is only for the vehicles the player owns and is not related to being buildable bjarni@6195: _rebuild_right_list = true; bjarni@6195: InvalidateWindowData(WC_REPLACE_VEHICLE, type); // Update the autoreplace window bjarni@6195: InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well bjarni@6195: } bjarni@6195: bjarni@6195: /** Get the default cargo type for an engine bjarni@6195: * @param engine the EngineID to get the cargo for bjarni@6195: * @return the cargo type carried by the engine (CT_INVALID if engine got no cargo capacity) bjarni@6195: */ bjarni@6195: static CargoID EngineCargo(EngineID engine) bjarni@6195: { bjarni@6195: if (engine == INVALID_ENGINE) return CT_INVALID; // surely INVALID_ENGINE can't carry anything but CT_INVALID bjarni@6195: bjarni@6195: switch (GetEngine(engine)->type) { bjarni@6195: default: NOT_REACHED(); rubidium@6585: case VEH_TRAIN: bjarni@6195: if (RailVehInfo(engine)->capacity == 0) return CT_INVALID; // no capacity -> can't carry cargo bjarni@6195: return RailVehInfo(engine)->cargo_type; rubidium@6585: case VEH_ROAD: return RoadVehInfo(engine)->cargo_type; rubidium@6585: case VEH_SHIP: return ShipVehInfo(engine)->cargo_type; rubidium@6585: case VEH_AIRCRAFT: return CT_PASSENGERS; // all planes are build with passengers by default bjarni@6195: } bjarni@6195: } bjarni@6195: bjarni@6195: /** Figure out if an engine should be added to a list bjarni@6195: * @param e The EngineID bjarni@6195: * @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected) bjarni@6195: * @param show_engines if truem then locomotives are drawn, else wagons (never both) bjarni@6195: * @return true if the engine should be in the list (based on this check) bjarni@6195: */ bjarni@6195: static bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines) bjarni@6195: { bjarni@6195: const RailVehicleInfo *rvi = RailVehInfo(e); bjarni@6195: bjarni@6195: /* Ensure that the wagon/engine selection fits the engine. */ bjarni@6195: if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false; bjarni@6195: bjarni@6195: if (draw_left && show_engines) { bjarni@6195: /* Ensure that the railtype is specific to the selected one */ bjarni@6195: if (rvi->railtype != _railtype_selected_in_replace_gui) return false; bjarni@6195: } else { bjarni@6195: /* Ensure that it's a compatible railtype to the selected one (like electric <-> diesel) bjarni@6195: * The vehicle do not have to have power on the railtype in question, only able to drive (pulled if needed) */ bjarni@6195: if (!IsCompatibleRail(rvi->railtype, _railtype_selected_in_replace_gui)) return false; bjarni@6195: } bjarni@6195: return true; bjarni@6195: } bjarni@6195: bjarni@6195: /** Figure out if two engines got at least one type of cargo in common (refitting if needed) bjarni@6195: * @param engine_a one of the EngineIDs bjarni@6195: * @param engine_b the other EngineID bjarni@6195: * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all) bjarni@6195: */ bjarni@6195: static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b) bjarni@6195: { bjarni@6195: CargoID a = EngineCargo(engine_a); bjarni@6195: CargoID b = EngineCargo(engine_b); bjarni@6195: bjarni@6195: /* we should always be able to refit to/from locomotives without capacity bjarni@6195: * Because of that, CT_INVALID shoudl always return true */ bjarni@6195: if (a == CT_INVALID || b == CT_INVALID || a == b) return true; // they carry no ro the same type by default bjarni@6195: if (EngInfo(engine_a)->refit_mask & EngInfo(engine_b)->refit_mask) return true; // both can refit to the same bjarni@6195: if (CanRefitTo(engine_a, b) || CanRefitTo(engine_b, a)) return true; // one can refit to what the other one carries bjarni@6195: return false; bjarni@6195: } bjarni@6195: bjarni@6195: /** Generate a list bjarni@6195: * @param w Window, that contains the list bjarni@6195: * @param draw_left true if generating the left list, otherwise false bjarni@6195: */ bjarni@6195: static void GenerateReplaceVehList(Window *w, bool draw_left) bjarni@6195: { bjarni@6195: EngineID e; bjarni@6195: EngineID selected_engine = INVALID_ENGINE; peter1138@8975: VehicleType type = (VehicleType)w->window_number; bjarni@6195: byte i = draw_left ? 0 : 1; bjarni@6195: bjarni@6195: EngineList *list = &WP(w, replaceveh_d).list[i]; bjarni@6195: EngList_RemoveAll(list); bjarni@6195: bjarni@6195: FOR_ALL_ENGINEIDS_OF_TYPE(e, type) { rubidium@6585: if (type == VEH_TRAIN && !GenerateReplaceRailList(e, draw_left, WP(w, replaceveh_d).wagon_btnstate)) continue; // special rules for trains bjarni@6195: bjarni@6195: if (draw_left) { rubidium@7139: const GroupID selected_group = WP(w, replaceveh_d).sel_group; rubidium@7977: const uint num_engines = GetGroupNumEngines(_local_player, selected_group, e); rubidium@7139: bjarni@6195: /* Skip drawing the engines we don't have any of and haven't set for replacement */ rubidium@7139: if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), e, selected_group) == INVALID_ENGINE) continue; bjarni@6059: } else { bjarni@6195: /* This is for engines we can replace to and they should depend on what we selected to replace from */ bjarni@6195: if (!IsEngineBuildable(e, type, _local_player)) continue; // we need to be able to build the engine bjarni@6195: if (!EnginesGotCargoInCommon(e, WP(w, replaceveh_d).sel_engine[0])) continue; // the engines needs to be able to carry the same cargo rubidium@7183: rubidium@7183: /* Road vehicles can't be replaced by trams and vice-versa */ skidd13@8424: 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; bjarni@6195: if (e == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew) bjarni@6059: } bjarni@6059: bjarni@6195: EngList_Add(list, e); bjarni@6195: if (e == WP(w, replaceveh_d).sel_engine[i]) selected_engine = e; // The selected engine is still in the list bjarni@6195: } bjarni@6195: 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) peter1138@8670: if (type == VEH_TRAIN) EngList_Sort(list, &TrainEngineNumberSorter); bjarni@6195: } bjarni@6195: bjarni@6195: /** Generate the lists bjarni@6195: * @param w Window containing the lists bjarni@6195: */ bjarni@6195: static void GenerateLists(Window *w) bjarni@6195: { bjarni@6195: EngineID e = WP(w, replaceveh_d).sel_engine[0]; bjarni@6195: bjarni@6195: if (WP(w, replaceveh_d).update_left == true) { bjarni@6195: /* We need to rebuild the left list */ bjarni@6195: GenerateReplaceVehList(w, true); bjarni@6195: SetVScrollCount(w, EngList_Count(&WP(w, replaceveh_d).list[0])); bjarni@6195: 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) { bjarni@6195: WP(w, replaceveh_d).sel_engine[0] = WP(w, replaceveh_d).list[0][0]; bjarni@6195: } bjarni@6195: } bjarni@6195: bjarni@6195: if (WP(w, replaceveh_d).update_right || e != WP(w, replaceveh_d).sel_engine[0]) { bjarni@6195: /* Either we got a request to rebuild the right list or the left list selected a different engine */ bjarni@6195: if (WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE) { bjarni@6195: /* Always empty the right list when nothing is selected in the left list */ bjarni@6195: EngList_RemoveAll(&WP(w, replaceveh_d).list[1]); bjarni@6195: WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE; bjarni@6195: } else { bjarni@6195: GenerateReplaceVehList(w, false); bjarni@6195: SetVScroll2Count(w, EngList_Count(&WP(w, replaceveh_d).list[1])); bjarni@6195: 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) { bjarni@6195: WP(w, replaceveh_d).sel_engine[1] = WP(w, replaceveh_d).list[1][0]; bjarni@6059: } bjarni@6059: } bjarni@6059: } bjarni@6195: /* Reset the flags about needed updates */ bjarni@6195: WP(w, replaceveh_d).update_left = false; bjarni@6195: WP(w, replaceveh_d).update_right = false; bjarni@6195: WP(w, replaceveh_d).init_lists = false; bjarni@6059: } bjarni@6059: bjarni@6059: rubidium@7139: 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); bjarni@6059: bjarni@6059: static void ReplaceVehicleWndProc(Window *w, WindowEvent *e) bjarni@6059: { bjarni@6195: /* Strings for the pulldown menu */ bjarni@6059: static const StringID _vehicle_type_names[] = { bjarni@6059: STR_019F_TRAIN, bjarni@6059: STR_019C_ROAD_VEHICLE, bjarni@6059: STR_019E_SHIP, bjarni@6059: STR_019D_AIRCRAFT bjarni@6059: }; bjarni@6059: bjarni@6059: switch (e->event) { bjarni@6195: case WE_CREATE: bjarni@6195: WP(w, replaceveh_d).wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool) bjarni@6195: EngList_Create(&WP(w, replaceveh_d).list[0]); bjarni@6195: EngList_Create(&WP(w, replaceveh_d).list[1]); bjarni@6195: WP(w, replaceveh_d).update_left = true; bjarni@6195: WP(w, replaceveh_d).update_right = true; bjarni@6195: WP(w, replaceveh_d).init_lists = true; bjarni@6195: WP(w, replaceveh_d).sel_engine[0] = INVALID_ENGINE; bjarni@6195: WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE; bjarni@6195: break; bjarni@6059: bjarni@6195: case WE_PAINT: { bjarni@6195: if (WP(w, replaceveh_d).update_left || WP(w, replaceveh_d).update_right) GenerateLists(w); bjarni@6059: bjarni@6195: Player *p = GetPlayer(_local_player); bjarni@6195: EngineID selected_id[2]; rubidium@8578: const GroupID selected_group = WP(w, replaceveh_d).sel_group; bjarni@6059: bjarni@6195: selected_id[0] = WP(w, replaceveh_d).sel_engine[0]; bjarni@6195: selected_id[1] = WP(w, replaceveh_d).sel_engine[1]; bjarni@6059: bjarni@6195: /* Disable the "Start Replacing" button if: bjarni@6195: * Either list is empty bjarni@6195: * or The selected replacement engine has a replacement (to prevent loops) bjarni@6195: * or The right list (new replacement) has the existing replacement vehicle selected */ peter1138@8841: w->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE, bjarni@6195: selected_id[0] == INVALID_ENGINE || bjarni@6195: selected_id[1] == INVALID_ENGINE || rubidium@7139: EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE || rubidium@7139: EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]); bjarni@6195: bjarni@6195: /* Disable the "Stop Replacing" button if: bjarni@6195: * The left list (existing vehicle) is empty bjarni@6195: * or The selected vehicle has no replacement set up */ peter1138@8841: w->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE, bjarni@6195: selected_id[0] == INVALID_ENGINE || rubidium@7139: !EngineHasReplacementForPlayer(p, selected_id[0], selected_group)); bjarni@6195: bjarni@6195: /* now the actual drawing of the window itself takes place */ bjarni@6206: SetDParam(0, _vehicle_type_names[w->window_number]); bjarni@6195: rubidium@6585: if (w->window_number == VEH_TRAIN) { bjarni@6195: /* set on/off for renew_keep_length */ bjarni@6195: SetDParam(1, p->renew_keep_length ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF); bjarni@6195: bjarni@6195: /* set wagon/engine button */ bjarni@6195: SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS); glx@6894: glx@6894: /* sets the colour of that art thing */ peter1138@8841: w->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].color = _player_colors[_local_player]; peter1138@8841: w->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].color = _player_colors[_local_player]; bjarni@6195: } bjarni@6195: rubidium@6585: if (w->window_number == VEH_TRAIN) { peter1138@8842: /* Show the selected railtype in the pulldown menu */ bjarni@6195: RailType railtype = _railtype_selected_in_replace_gui; peter1138@8842: w->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = _rail_types_list[railtype]; bjarni@6195: } bjarni@6195: peter1138@8842: DrawWindowWidgets(w); peter1138@8842: bjarni@6195: /* sets up the string for the vehicle that is being replaced to */ bjarni@6195: if (selected_id[0] != INVALID_ENGINE) { rubidium@7139: if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) { bjarni@6195: SetDParam(0, STR_NOT_REPLACING); bjarni@6195: } else { peter1138@7555: SetDParam(0, STR_ENGINE_NAME); peter1138@7555: SetDParam(1, EngineReplacementForPlayer(p, selected_id[0], selected_group)); bjarni@6059: } bjarni@6195: } else { bjarni@6195: SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED); bjarni@6195: } bjarni@6195: peter1138@8841: DrawString(145, w->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK); bjarni@6195: bjarni@6195: /* Draw the lists */ bjarni@6195: for(byte i = 0; i < 2; i++) { peter1138@8848: uint widget = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX; bjarni@6195: EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw bjarni@6195: EngineID start = i == 0 ? w->vscroll.pos : w->vscroll2.pos; // what is the offset for the start (scrolling) bjarni@6195: EngineID end = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list)); bjarni@6195: bjarni@6195: /* Do the actual drawing */ peter1138@8848: 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); bjarni@6195: bjarni@6195: /* Also draw the details if an engine is selected */ bjarni@6195: if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) { peter1138@8841: const Widget *wi = &w->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS]; peter1138@8915: int text_end = DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, WP(w, replaceveh_d).sel_engine[i]); peter1138@8915: peter1138@8915: if (text_end > wi->bottom) { peter1138@8915: SetWindowDirty(w); peter1138@8915: ResizeWindowForWidget(w, i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS, 0, text_end - wi->bottom); peter1138@8915: SetWindowDirty(w); peter1138@8915: } bjarni@6195: } bjarni@6195: } bjarni@6195: bjarni@6195: } break; // end of paint bjarni@6059: bjarni@6059: case WE_CLICK: { bjarni@6059: switch (e->we.click.widget) { peter1138@8841: case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE: bjarni@6059: WP(w, replaceveh_d).wagon_btnstate = !(WP(w, replaceveh_d).wagon_btnstate); bjarni@6195: WP(w, replaceveh_d).update_left = true; bjarni@6195: WP(w, replaceveh_d).init_lists = true; bjarni@6059: SetWindowDirty(w); bjarni@6059: break; bjarni@6059: peter1138@8841: case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: /* Railtype selection dropdown menu */ peter1138@8841: ShowDropDownMenu(w, _rail_types_list, _railtype_selected_in_replace_gui, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, 0, ~GetPlayer(_local_player)->avail_railtypes); bjarni@6059: break; bjarni@6059: peter1138@8841: case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: /* toggle renew_keep_length */ bjarni@6059: DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE); bjarni@6059: break; bjarni@6059: peter1138@8841: case RVW_WIDGET_START_REPLACE: { /* Start replacing */ bjarni@6059: EngineID veh_from = WP(w, replaceveh_d).sel_engine[0]; bjarni@6059: EngineID veh_to = WP(w, replaceveh_d).sel_engine[1]; rubidium@7139: DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE); bjarni@6195: } break; bjarni@6059: peter1138@8841: case RVW_WIDGET_STOP_REPLACE: { /* Stop replacing */ bjarni@6059: EngineID veh_from = WP(w, replaceveh_d).sel_engine[0]; rubidium@7139: DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE); bjarni@6195: } break; bjarni@6059: peter1138@8841: case RVW_WIDGET_LEFT_MATRIX: peter1138@8841: case RVW_WIDGET_RIGHT_MATRIX: { bjarni@6059: uint i = (e->we.click.pt.y - 14) / w->resize.step_height; peter1138@8841: uint16 click_scroll_pos = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.pos : w->vscroll2.pos; peter1138@8841: uint16 click_scroll_cap = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.cap : w->vscroll2.cap; peter1138@8841: byte click_side = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1; bjarni@6195: uint16 engine_count = EngList_Count(&WP(w, replaceveh_d).list[click_side]); bjarni@6195: bjarni@6059: if (i < click_scroll_cap) { bjarni@6195: i += click_scroll_pos; bjarni@6195: EngineID e = engine_count > i ? WP(w, replaceveh_d).list[click_side][i] : INVALID_ENGINE; bjarni@6195: if (e == WP(w, replaceveh_d).sel_engine[click_side]) break; // we clicked the one we already selected bjarni@6195: WP(w, replaceveh_d).sel_engine[click_side] = e; bjarni@6195: if (click_side == 0) { bjarni@6195: WP(w, replaceveh_d).update_right = true; bjarni@6195: WP(w, replaceveh_d).init_lists = true; bjarni@6195: } bjarni@6059: SetWindowDirty(w); bjarni@6195: } bjarni@6195: break; bjarni@6059: } bjarni@6059: } bjarni@6059: break; bjarni@6059: } bjarni@6059: bjarni@6195: case WE_DROPDOWN_SELECT: { /* we have selected a dropdown item in the list */ bjarni@6195: RailType temp = (RailType)e->we.dropdown.index; bjarni@6195: if (temp == _railtype_selected_in_replace_gui) break; // we didn't select a new one. No need to change anything bjarni@6195: _railtype_selected_in_replace_gui = temp; bjarni@6059: /* Reset scrollbar positions */ bjarni@6059: w->vscroll.pos = 0; bjarni@6059: w->vscroll2.pos = 0; bjarni@6195: /* Rebuild the lists */ bjarni@6195: WP(w, replaceveh_d).update_left = true; bjarni@6195: WP(w, replaceveh_d).update_right = true; bjarni@6195: WP(w, replaceveh_d).init_lists = true; bjarni@6059: SetWindowDirty(w); bjarni@6195: } break; bjarni@6059: bjarni@6059: case WE_RESIZE: bjarni@6059: w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height; bjarni@6059: w->vscroll2.cap += e->we.sizing.diff.y / (int)w->resize.step_height; bjarni@6059: peter1138@8841: w->widget[RVW_WIDGET_LEFT_MATRIX].data = (w->vscroll.cap << 8) + 1; peter1138@8841: w->widget[RVW_WIDGET_RIGHT_MATRIX].data = (w->vscroll2.cap << 8) + 1; bjarni@6059: break; bjarni@6195: bjarni@6195: case WE_INVALIDATE_DATA: bjarni@6195: if (_rebuild_left_list) WP(w, replaceveh_d).update_left = true; bjarni@6195: if (_rebuild_right_list) WP(w, replaceveh_d).update_right = true; bjarni@6195: SetWindowDirty(w); bjarni@6195: break; bjarni@6195: bjarni@6195: case WE_DESTROY: bjarni@6195: EngList_RemoveAll(&WP(w, replaceveh_d).list[0]); bjarni@6195: EngList_RemoveAll(&WP(w, replaceveh_d).list[1]); bjarni@6195: break; bjarni@6059: } bjarni@6059: } bjarni@6059: bjarni@6059: static const Widget _replace_rail_vehicle_widgets[] = { bjarni@6059: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, bjarni@6059: { WWT_CAPTION, RESIZE_NONE, 14, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS}, bjarni@6059: { WWT_STICKYBOX, RESIZE_NONE, 14, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 0, 227, 126, 227, 0x0, STR_NULL}, bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 240, 251, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 139, 316, 228, 239, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB}, bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 306, 443, 240, 251, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON}, bjarni@6059: { WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 14, 125, 0x801, STR_REPLACE_HELP_LEFT_ARRAY}, bjarni@6059: { WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@6059: { WWT_MATRIX, RESIZE_BOTTOM, 14, 228, 443, 14, 125, 0x801, STR_REPLACE_HELP_RIGHT_ARRAY}, bjarni@6059: { WWT_SCROLL2BAR, RESIZE_BOTTOM, 14, 444, 455, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 228, 455, 126, 227, 0x0, STR_NULL}, bjarni@6059: // train specific stuff bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 228, 239, STR_REPLACE_ENGINE_WAGON_SELECT, STR_REPLACE_ENGINE_WAGON_SELECT_HELP}, // widget 12 bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 139, 153, 240, 251, 0x0, STR_NULL}, peter1138@8842: { WWT_DROPDOWN, RESIZE_TB, 14, 154, 289, 240, 251, 0x0, STR_REPLACE_HELP_RAILTYPE}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 290, 305, 240, 251, 0x0, STR_NULL}, bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 317, 455, 228, 239, STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP}, bjarni@6059: // end of train specific stuff bjarni@6059: { WWT_RESIZEBOX, RESIZE_TB, 14, 444, 455, 240, 251, STR_NULL, STR_RESIZE_BUTTON}, bjarni@6059: { WIDGETS_END}, bjarni@6059: }; bjarni@6059: bjarni@6059: static const Widget _replace_road_vehicle_widgets[] = { bjarni@6059: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, bjarni@6059: { WWT_CAPTION, RESIZE_NONE, 14, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS}, bjarni@6059: { WWT_STICKYBOX, RESIZE_NONE, 14, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 0, 227, 126, 217, 0x0, STR_NULL}, bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 218, 229, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 139, 305, 218, 229, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB}, bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 306, 443, 218, 229, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON}, bjarni@6059: { WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 14, 125, 0x801, STR_REPLACE_HELP_LEFT_ARRAY}, bjarni@6059: { WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@6059: { WWT_MATRIX, RESIZE_BOTTOM, 14, 228, 443, 14, 125, 0x801, STR_REPLACE_HELP_RIGHT_ARRAY}, bjarni@6059: { WWT_SCROLL2BAR, RESIZE_BOTTOM, 14, 444, 455, 14, 125, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 228, 455, 126, 217, 0x0, STR_NULL}, bjarni@6059: { WWT_RESIZEBOX, RESIZE_TB, 14, 444, 455, 218, 229, STR_NULL, STR_RESIZE_BUTTON}, bjarni@6059: { WIDGETS_END}, bjarni@6059: }; bjarni@6059: bjarni@6059: static const Widget _replace_ship_aircraft_vehicle_widgets[] = { bjarni@6059: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, bjarni@6059: { WWT_CAPTION, RESIZE_NONE, 14, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS}, bjarni@6059: { WWT_STICKYBOX, RESIZE_NONE, 14, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 0, 227, 110, 201, 0x0, STR_NULL}, bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 202, 213, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 139, 305, 202, 213, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB}, bjarni@6059: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 306, 443, 202, 213, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON}, bjarni@6059: { WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 14, 109, 0x401, STR_REPLACE_HELP_LEFT_ARRAY}, bjarni@6059: { WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 14, 109, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@6059: { WWT_MATRIX, RESIZE_BOTTOM, 14, 228, 443, 14, 109, 0x401, STR_REPLACE_HELP_RIGHT_ARRAY}, bjarni@6059: { WWT_SCROLL2BAR, RESIZE_BOTTOM, 14, 444, 455, 14, 109, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@6059: { WWT_PANEL, RESIZE_TB, 14, 228, 455, 110, 201, 0x0, STR_NULL}, bjarni@6059: { WWT_RESIZEBOX, RESIZE_TB, 14, 444, 455, 202, 213, STR_NULL, STR_RESIZE_BUTTON}, bjarni@6059: { WIDGETS_END}, bjarni@6059: }; bjarni@6059: bjarni@6059: static const WindowDesc _replace_rail_vehicle_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 456, 252, 456, 252, rubidium@6144: WC_REPLACE_VEHICLE, WC_NONE, bjarni@6059: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE, bjarni@6059: _replace_rail_vehicle_widgets, bjarni@6059: ReplaceVehicleWndProc bjarni@6059: }; bjarni@6059: bjarni@6059: static const WindowDesc _replace_road_vehicle_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 456, 230, 456, 230, rubidium@6144: WC_REPLACE_VEHICLE, WC_NONE, bjarni@6059: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE, bjarni@6059: _replace_road_vehicle_widgets, bjarni@6059: ReplaceVehicleWndProc bjarni@6059: }; bjarni@6059: bjarni@6059: static const WindowDesc _replace_ship_aircraft_vehicle_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 456, 214, 456, 214, rubidium@6144: WC_REPLACE_VEHICLE, WC_NONE, bjarni@6059: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE, bjarni@6059: _replace_ship_aircraft_vehicle_widgets, bjarni@6059: ReplaceVehicleWndProc bjarni@6059: }; bjarni@6059: bjarni@6059: rubidium@7139: void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype) rubidium@7139: { rubidium@7139: Window *w; rubidium@7139: rubidium@7139: DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype); rubidium@7139: rubidium@7139: switch (vehicletype) { rubidium@7139: default: NOT_REACHED(); rubidium@7139: case VEH_TRAIN: rubidium@7139: w = AllocateWindowDescFront(&_replace_rail_vehicle_desc, vehicletype); rubidium@7139: w->vscroll.cap = 8; rubidium@7139: w->resize.step_height = 14; rubidium@7139: WP(w, replaceveh_d).wagon_btnstate = true; rubidium@7139: break; rubidium@7139: case VEH_ROAD: rubidium@7139: w = AllocateWindowDescFront(&_replace_road_vehicle_desc, vehicletype); rubidium@7139: w->vscroll.cap = 8; rubidium@7139: w->resize.step_height = 14; rubidium@7139: break; rubidium@7139: case VEH_SHIP: rubidium@7139: case VEH_AIRCRAFT: rubidium@7139: w = AllocateWindowDescFront(&_replace_ship_aircraft_vehicle_desc, vehicletype); rubidium@7139: w->vscroll.cap = 4; rubidium@7139: w->resize.step_height = 24; rubidium@7139: break; rubidium@7139: } rubidium@7139: rubidium@7139: w->caption_color = _local_player; rubidium@7139: WP(w, replaceveh_d).sel_group = id_g; rubidium@7139: w->vscroll2.cap = w->vscroll.cap; // these two are always the same bjarni@6059: }