tron@2186: /* $Id$ */ tron@2186: rubidium@10429: /** @file autoreplace_gui.cpp GUI for autoreplace handling. */ 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" peter1138@10382: #include "engine_base.h" rubidium@10596: #include "window_gui.h" peter1138@10737: #include "engine_gui.h" bjarni@6059: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8760: peter1138@10764: void DrawEngineList(VehicleType type, int x, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group); tron@505: 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 { bjarni@10302: RVW_WIDGET_LEFT_MATRIX = 3, bjarni@10302: RVW_WIDGET_LEFT_SCROLLBAR, bjarni@10302: RVW_WIDGET_RIGHT_MATRIX, bjarni@10302: RVW_WIDGET_RIGHT_SCROLLBAR, bjarni@10302: RVW_WIDGET_LEFT_DETAILS, bjarni@10302: RVW_WIDGET_RIGHT_DETAILS, bjarni@10302: bjarni@10302: /* Button row */ peter1138@8841: RVW_WIDGET_START_REPLACE, peter1138@8841: RVW_WIDGET_INFO_TAB, peter1138@8841: RVW_WIDGET_STOP_REPLACE, bjarni@10302: RVW_WIDGET_RESIZE, peter1138@8841: bjarni@10302: /* Train only widgets */ 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@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); 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 */ rubidium@10492: InvalidateWindowData(WC_REPLACE_VEHICLE, GetEngine(e)->type, true); bjarni@6195: } 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: { rubidium@10492: InvalidateWindowData(WC_REPLACE_VEHICLE, type, false); // 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 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: rubidium@10494: /** rubidium@10494: * Window for the autoreplacing of vehicles. bjarni@6195: */ rubidium@10494: class ReplaceVehicleWindow : public Window { rubidium@10494: byte sel_index[2]; rubidium@10494: EngineID sel_engine[2]; rubidium@10494: uint16 count[2]; rubidium@10494: bool wagon_btnstate; ///< true means engine is selected peter1138@10764: GUIEngineList list[2]; rubidium@10494: bool update_left; rubidium@10494: bool update_right; rubidium@10494: bool init_lists; rubidium@10494: GroupID sel_group; rubidium@10494: static RailType sel_railtype; bjarni@6195: rubidium@10494: /** Figure out if an engine should be added to a list rubidium@10494: * @param e The EngineID rubidium@10494: * @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected) rubidium@10494: * @param show_engines if truem then locomotives are drawn, else wagons (never both) rubidium@10494: * @return true if the engine should be in the list (based on this check) rubidium@10494: */ rubidium@10494: bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines) rubidium@10494: { rubidium@10494: const RailVehicleInfo *rvi = RailVehInfo(e); rubidium@7139: rubidium@10494: /* Ensure that the wagon/engine selection fits the engine. */ rubidium@10494: if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false; rubidium@10494: rubidium@10494: if (draw_left && show_engines) { rubidium@10494: /* Ensure that the railtype is specific to the selected one */ rubidium@10494: if (rvi->railtype != this->sel_railtype) return false; bjarni@6059: } else { rubidium@10494: /* Ensure that it's a compatible railtype to the selected one (like electric <-> diesel) rubidium@10494: * The vehicle do not have to have power on the railtype in question, only able to drive (pulled if needed) */ rubidium@10494: if (!IsCompatibleRail(rvi->railtype, this->sel_railtype)) return false; rubidium@10494: } rubidium@10494: return true; rubidium@10494: } rubidium@7183: rubidium@10494: rubidium@10494: /** Generate a list rubidium@10494: * @param w Window, that contains the list rubidium@10494: * @param draw_left true if generating the left list, otherwise false rubidium@10494: */ rubidium@10494: void GenerateReplaceVehList(Window *w, bool draw_left) rubidium@10494: { rubidium@10494: EngineID selected_engine = INVALID_ENGINE; rubidium@10494: VehicleType type = (VehicleType)this->window_number; rubidium@10494: byte i = draw_left ? 0 : 1; rubidium@10494: peter1138@10764: GUIEngineList *list = &this->list[i]; peter1138@10764: list->Clear(); rubidium@10494: rubidium@10494: const Engine *e; rubidium@10494: FOR_ALL_ENGINES_OF_TYPE(e, type) { rubidium@10494: EngineID eid = e->index; rubidium@10494: if (type == VEH_TRAIN && !GenerateReplaceRailList(eid, draw_left, this->wagon_btnstate)) continue; // special rules for trains rubidium@10494: rubidium@10494: if (draw_left) { rubidium@10494: const GroupID selected_group = this->sel_group; rubidium@10494: const uint num_engines = GetGroupNumEngines(_local_player, selected_group, eid); rubidium@10494: rubidium@10494: /* Skip drawing the engines we don't have any of and haven't set for replacement */ rubidium@10494: if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), eid, selected_group) == INVALID_ENGINE) continue; rubidium@10494: } else { rubidium@10494: /* This is for engines we can replace to and they should depend on what we selected to replace from */ rubidium@10494: if (!IsEngineBuildable(eid, type, _local_player)) continue; // we need to be able to build the engine rubidium@10494: if (!EnginesGotCargoInCommon(eid, this->sel_engine[0])) continue; // the engines needs to be able to carry the same cargo rubidium@10494: rubidium@10494: /* Road vehicles can't be replaced by trams and vice-versa */ rubidium@10494: if (type == VEH_ROAD && HasBit(EngInfo(this->sel_engine[0])->misc_flags, EF_ROAD_TRAM) != HasBit(e->info.misc_flags, EF_ROAD_TRAM)) continue; rubidium@10494: if (eid == this->sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew) rubidium@10494: } rubidium@10494: peter1138@10764: *list->Append() = eid; rubidium@10494: if (eid == this->sel_engine[i]) selected_engine = eid; // The selected engine is still in the list rubidium@10494: } rubidium@10494: this->sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore) rubidium@10494: if (type == VEH_TRAIN) EngList_Sort(list, &TrainEngineNumberSorter); rubidium@10494: } rubidium@10494: rubidium@10494: /** Generate the lists */ rubidium@10494: void GenerateLists() rubidium@10494: { rubidium@10494: EngineID e = this->sel_engine[0]; rubidium@10494: rubidium@10494: if (this->update_left == true) { rubidium@10494: /* We need to rebuild the left list */ rubidium@10494: GenerateReplaceVehList(this, true); peter1138@10764: SetVScrollCount(this, this->list[0].Length()); peter1138@10764: if (this->init_lists && this->sel_engine[0] == INVALID_ENGINE && this->list[0].Length() != 0) { rubidium@10494: this->sel_engine[0] = this->list[0][0]; rubidium@10494: } bjarni@6059: } bjarni@6059: rubidium@10494: if (this->update_right || e != this->sel_engine[0]) { rubidium@10494: /* Either we got a request to rebuild the right list or the left list selected a different engine */ rubidium@10494: if (this->sel_engine[0] == INVALID_ENGINE) { rubidium@10494: /* Always empty the right list when nothing is selected in the left list */ peter1138@10764: this->list[1].Clear(); rubidium@10494: this->sel_engine[1] = INVALID_ENGINE; rubidium@10494: } else { rubidium@10494: GenerateReplaceVehList(this, false); peter1138@10764: SetVScroll2Count(this, this->list[1].Length()); peter1138@10764: if (this->init_lists && this->sel_engine[1] == INVALID_ENGINE && this->list[1].Length() != 0) { rubidium@10494: this->sel_engine[1] = this->list[1][0]; rubidium@10494: } rubidium@10494: } bjarni@6195: } rubidium@10494: /* Reset the flags about needed updates */ rubidium@10494: this->update_left = false; rubidium@10494: this->update_right = false; rubidium@10494: this->init_lists = false; bjarni@6195: } bjarni@6195: rubidium@10494: public: rubidium@10532: ReplaceVehicleWindow(const WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc, vehicletype) rubidium@10494: { rubidium@10494: this->wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool) rubidium@10494: this->update_left = true; rubidium@10494: this->update_right = true; rubidium@10494: this->init_lists = true; rubidium@10494: this->sel_engine[0] = INVALID_ENGINE; rubidium@10494: this->sel_engine[1] = INVALID_ENGINE; rubidium@10494: rubidium@10494: this->resize.step_height = GetVehicleListHeight(vehicletype); rubidium@10494: this->vscroll.cap = this->resize.step_height == 14 ? 8 : 4; rubidium@10494: rubidium@10494: Widget *widget = this->widget; rubidium@10494: widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll.cap << 8) + 1; rubidium@10494: rubidium@10494: if (vehicletype == VEH_TRAIN) { rubidium@10494: this->wagon_btnstate = true; rubidium@10494: /* The train window is bigger so we will move some of the widgets to fit the new size. rubidium@10494: * We will start by moving the resize button to the lower right corner. */ rubidium@10494: widget[RVW_WIDGET_RESIZE].top = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].top; rubidium@10494: widget[RVW_WIDGET_RESIZE].bottom = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].bottom; rubidium@10494: widget[RVW_WIDGET_STOP_REPLACE].right = widget[RVW_WIDGET_RESIZE].right; rubidium@10494: rubidium@10494: /* The detail panel is one line taller for trains so we will move some of the widgets one line (10 pixels) down. */ rubidium@10494: widget[RVW_WIDGET_LEFT_DETAILS].bottom += 10; rubidium@10494: widget[RVW_WIDGET_RIGHT_DETAILS].bottom += 10; rubidium@10494: for (int i = RVW_WIDGET_START_REPLACE; i < RVW_WIDGET_RESIZE; i++) { rubidium@10494: widget[i].top += 10; rubidium@10494: widget[i].bottom += 10; rubidium@10494: } bjarni@6195: } else { rubidium@10494: /* Since it's not a train we will hide the train only widgets. */ rubidium@10494: this->SetWidgetsHiddenState(true, rubidium@10494: RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE, rubidium@10494: RVW_WIDGET_TRAIN_FLUFF_LEFT, rubidium@10494: RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, rubidium@10494: RVW_WIDGET_TRAIN_FLUFF_RIGHT, rubidium@10494: RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE, rubidium@10494: WIDGET_LIST_END); rubidium@10494: } rubidium@10494: rubidium@10494: ResizeWindow(this, 0, this->resize.step_height * this->vscroll.cap); rubidium@10494: rubidium@10494: /* Set the minimum window size to the current window size */ rubidium@10494: this->resize.width = this->width; rubidium@10494: this->resize.height = this->height; rubidium@10494: rubidium@10494: this->caption_color = _local_player; rubidium@10494: this->sel_group = id_g; rubidium@10494: this->vscroll2.cap = this->vscroll.cap; // these two are always the same rubidium@10498: rubidium@10498: this->FindWindowPlacementAndResize(desc); rubidium@10494: } rubidium@10494: rubidium@10494: virtual void OnPaint() rubidium@10494: { rubidium@10494: static const StringID _vehicle_type_names[] = { rubidium@10494: STR_019F_TRAIN, rubidium@10494: STR_019C_ROAD_VEHICLE, rubidium@10494: STR_019E_SHIP, rubidium@10494: STR_019D_AIRCRAFT rubidium@10494: }; rubidium@10494: rubidium@10494: if (this->update_left || this->update_right) this->GenerateLists(); rubidium@10494: rubidium@10494: Player *p = GetPlayer(_local_player); rubidium@10494: EngineID selected_id[2]; rubidium@10494: const GroupID selected_group = this->sel_group; rubidium@10494: rubidium@10494: selected_id[0] = this->sel_engine[0]; rubidium@10494: selected_id[1] = this->sel_engine[1]; rubidium@10494: rubidium@10494: /* Disable the "Start Replacing" button if: rubidium@10494: * Either list is empty rubidium@10494: * or The selected replacement engine has a replacement (to prevent loops) rubidium@10494: * or The right list (new replacement) has the existing replacement vehicle selected */ rubidium@10494: this->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE, rubidium@10494: selected_id[0] == INVALID_ENGINE || rubidium@10494: selected_id[1] == INVALID_ENGINE || rubidium@10494: EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE || rubidium@10494: EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]); rubidium@10494: rubidium@10494: /* Disable the "Stop Replacing" button if: rubidium@10494: * The left list (existing vehicle) is empty rubidium@10494: * or The selected vehicle has no replacement set up */ rubidium@10494: this->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE, rubidium@10494: selected_id[0] == INVALID_ENGINE || rubidium@10494: !EngineHasReplacementForPlayer(p, selected_id[0], selected_group)); rubidium@10494: rubidium@10494: /* now the actual drawing of the window itself takes place */ rubidium@10494: SetDParam(0, _vehicle_type_names[this->window_number]); rubidium@10494: rubidium@10494: if (this->window_number == VEH_TRAIN) { rubidium@10494: /* set on/off for renew_keep_length */ rubidium@10494: SetDParam(1, p->renew_keep_length ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF); rubidium@10494: rubidium@10494: /* set wagon/engine button */ rubidium@10494: SetDParam(2, this->wagon_btnstate ? STR_ENGINES : STR_WAGONS); rubidium@10494: rubidium@10494: /* sets the colour of that art thing */ rubidium@10494: this->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].color = _player_colors[_local_player]; rubidium@10494: this->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].color = _player_colors[_local_player]; rubidium@10494: } rubidium@10494: rubidium@10494: if (this->window_number == VEH_TRAIN) { rubidium@10494: /* Show the selected railtype in the pulldown menu */ rubidium@10494: this->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = _rail_types_list[sel_railtype]; rubidium@10494: } rubidium@10494: rubidium@10595: this->DrawWidgets(); rubidium@10494: rubidium@10494: /* sets up the string for the vehicle that is being replaced to */ rubidium@10494: if (selected_id[0] != INVALID_ENGINE) { rubidium@10494: if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) { rubidium@10494: SetDParam(0, STR_NOT_REPLACING); rubidium@10494: } else { rubidium@10494: SetDParam(0, STR_ENGINE_NAME); rubidium@10494: SetDParam(1, EngineReplacementForPlayer(p, selected_id[0], selected_group)); rubidium@10494: } rubidium@10494: } else { rubidium@10494: SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED); rubidium@10494: } rubidium@10494: rubidium@10494: DrawString(145, this->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK); rubidium@10494: rubidium@10494: /* Draw the lists */ rubidium@10494: for (byte i = 0; i < 2; i++) { rubidium@10494: uint widget = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX; peter1138@10764: GUIEngineList *list = &this->list[i]; // which list to draw rubidium@10494: EngineID start = i == 0 ? this->vscroll.pos : this->vscroll2.pos; // what is the offset for the start (scrolling) peter1138@10764: EngineID end = min((i == 0 ? this->vscroll.cap : this->vscroll2.cap) + start, list->Length()); rubidium@10494: rubidium@10494: /* Do the actual drawing */ peter1138@10763: DrawEngineList((VehicleType)this->window_number, this->widget[widget].left + 2, 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); rubidium@10494: rubidium@10494: /* Also draw the details if an engine is selected */ rubidium@10494: if (this->sel_engine[i] != INVALID_ENGINE) { rubidium@10494: const Widget *wi = &this->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS]; rubidium@10494: int text_end = DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, this->sel_engine[i]); rubidium@10494: rubidium@10494: if (text_end > wi->bottom) { rubidium@10494: this->SetDirty(); rubidium@10494: ResizeWindowForWidget(this, i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS, 0, text_end - wi->bottom); rubidium@10494: this->SetDirty(); rubidium@10494: } bjarni@6059: } bjarni@6059: } bjarni@6059: } peter1138@8915: rubidium@10494: virtual void OnClick(Point pt, int widget) rubidium@10494: { rubidium@10494: switch (widget) { rubidium@10494: case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE: rubidium@10494: this->wagon_btnstate = !(this->wagon_btnstate); rubidium@10494: this->update_left = true; rubidium@10494: this->init_lists = true; rubidium@10494: this->SetDirty(); rubidium@10494: break; bjarni@6195: rubidium@10494: case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: /* Railtype selection dropdown menu */ rubidium@10494: ShowDropDownMenu(this, _rail_types_list, sel_railtype, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, 0, ~GetPlayer(_local_player)->avail_railtypes); rubidium@10494: break; bjarni@6059: rubidium@10494: case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: /* toggle renew_keep_length */ rubidium@10494: DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE); rubidium@10494: break; bjarni@10302: rubidium@10494: case RVW_WIDGET_START_REPLACE: { /* Start replacing */ rubidium@10494: EngineID veh_from = this->sel_engine[0]; rubidium@10494: EngineID veh_to = this->sel_engine[1]; rubidium@10494: DoCommandP(0, 3 + (this->sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE); rubidium@10494: } break; bjarni@6195: rubidium@10494: case RVW_WIDGET_STOP_REPLACE: { /* Stop replacing */ rubidium@10494: EngineID veh_from = this->sel_engine[0]; rubidium@10494: DoCommandP(0, 3 + (this->sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE); rubidium@10494: } break; bjarni@6195: rubidium@10494: case RVW_WIDGET_LEFT_MATRIX: rubidium@10494: case RVW_WIDGET_RIGHT_MATRIX: { rubidium@10494: uint i = (pt.y - 14) / this->resize.step_height; rubidium@10494: uint16 click_scroll_pos = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.pos : this->vscroll2.pos; rubidium@10494: uint16 click_scroll_cap = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.cap : this->vscroll2.cap; rubidium@10494: byte click_side = widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1; peter1138@10764: size_t engine_count = this->list[click_side].Length(); rubidium@10494: rubidium@10494: if (i < click_scroll_cap) { rubidium@10494: i += click_scroll_pos; rubidium@10494: EngineID e = engine_count > i ? this->list[click_side][i] : INVALID_ENGINE; rubidium@10494: if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected rubidium@10494: this->sel_engine[click_side] = e; rubidium@10494: if (click_side == 0) { rubidium@10494: this->update_right = true; rubidium@10494: this->init_lists = true; rubidium@10494: } rubidium@10494: this->SetDirty(); rubidium@10494: } rubidium@10494: break; rubidium@10494: } rubidium@10494: } bjarni@6059: } rubidium@10494: rubidium@10494: virtual void OnDropdownSelect(int widget, int index) rubidium@10494: { rubidium@10494: RailType temp = (RailType)index; rubidium@10494: if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything rubidium@10494: sel_railtype = temp; rubidium@10494: /* Reset scrollbar positions */ rubidium@10494: this->vscroll.pos = 0; rubidium@10494: this->vscroll2.pos = 0; rubidium@10494: /* Rebuild the lists */ rubidium@10494: this->update_left = true; rubidium@10494: this->update_right = true; rubidium@10494: this->init_lists = true; rubidium@10494: this->SetDirty(); rubidium@10494: } rubidium@10494: rubidium@10494: virtual void OnResize(Point new_size, Point delta) { rubidium@10494: this->vscroll.cap += delta.y / (int)this->resize.step_height; rubidium@10494: this->vscroll2.cap += delta.y / (int)this->resize.step_height; rubidium@10494: rubidium@10494: Widget *widget = this->widget; rubidium@10494: rubidium@10494: widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll2.cap << 8) + 1; rubidium@10494: rubidium@10494: if (delta.x != 0) { rubidium@10494: /* We changed the width of the window so we have to resize the lists. rubidium@10494: * Because ResizeButtons() makes each widget the same size it can't be used on the lists rubidium@10494: * because then the lists would have the same size as the scrollbars. rubidium@10494: * Instead we use it on the detail panels. rubidium@10494: * Afterwards we use the new location of the detail panels (the middle of the window) rubidium@10494: * to place the lists. rubidium@10494: * This way the lists will have equal size while keeping the width of the scrollbars unchanged. */ rubidium@10494: ResizeButtons(this, RVW_WIDGET_LEFT_DETAILS, RVW_WIDGET_RIGHT_DETAILS); rubidium@10494: widget[RVW_WIDGET_RIGHT_MATRIX].left = widget[RVW_WIDGET_RIGHT_DETAILS].left; rubidium@10494: widget[RVW_WIDGET_LEFT_SCROLLBAR].right = widget[RVW_WIDGET_LEFT_DETAILS].right; rubidium@10494: widget[RVW_WIDGET_LEFT_SCROLLBAR].left = widget[RVW_WIDGET_LEFT_SCROLLBAR].right - 11; rubidium@10494: widget[RVW_WIDGET_LEFT_MATRIX].right = widget[RVW_WIDGET_LEFT_SCROLLBAR].left - 1; rubidium@10494: } rubidium@10494: } rubidium@10494: rubidium@10494: virtual void OnInvalidateData(int data) rubidium@10494: { rubidium@10494: if (data != 0) { rubidium@10494: this->update_left = true; rubidium@10494: } else { rubidium@10494: this->update_right = true; rubidium@10494: } rubidium@10494: } rubidium@10494: }; bjarni@6059: bjarni@10302: static const Widget _replace_vehicle_widgets[] = { belugas@10296: { WWT_CLOSEBOX, RESIZE_NONE, 14, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, bjarni@10302: { WWT_CAPTION, RESIZE_RIGHT, 14, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS}, bjarni@10302: { WWT_STICKYBOX, RESIZE_LR, 14, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON}, bjarni@6059: bjarni@10302: { WWT_MATRIX, RESIZE_BOTTOM, 14, 0, 215, 14, 13, 0x1, STR_REPLACE_HELP_LEFT_ARRAY}, bjarni@10302: { WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 216, 227, 14, 13, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@10302: { WWT_MATRIX, RESIZE_LRB, 14, 228, 443, 14, 13, 0x1, STR_REPLACE_HELP_RIGHT_ARRAY}, bjarni@10302: { WWT_SCROLL2BAR, RESIZE_LRB, 14, 444, 455, 14, 13, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, bjarni@10302: { WWT_PANEL, RESIZE_TB, 14, 0, 227, 14, 105, 0x0, STR_NULL}, bjarni@10302: { WWT_PANEL, RESIZE_RTB, 14, 228, 455, 14, 105, 0x0, STR_NULL}, bjarni@6059: bjarni@10302: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 106, 117, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON}, bjarni@10302: { WWT_PANEL, RESIZE_RTB, 14, 139, 305, 106, 117, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB}, bjarni@10302: { WWT_PUSHTXTBTN, RESIZE_LRTB, 14, 306, 443, 106, 117, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON}, bjarni@10302: { WWT_RESIZEBOX, RESIZE_LRTB, 14, 444, 455, 106, 117, STR_NULL, STR_RESIZE_BUTTON}, bjarni@10302: bjarni@10302: { WWT_PUSHTXTBTN, RESIZE_TB, 14, 0, 138, 128, 139, STR_REPLACE_ENGINE_WAGON_SELECT, STR_REPLACE_ENGINE_WAGON_SELECT_HELP}, bjarni@10302: { WWT_PANEL, RESIZE_TB, 14, 139, 153, 128, 139, 0x0, STR_NULL}, bjarni@10302: { WWT_DROPDOWN, RESIZE_RTB, 14, 154, 289, 128, 139, 0x0, STR_REPLACE_HELP_RAILTYPE}, bjarni@10302: { WWT_PANEL, RESIZE_LRTB, 14, 290, 305, 128, 139, 0x0, STR_NULL}, bjarni@10302: { WWT_PUSHTXTBTN, RESIZE_LRTB, 14, 306, 443, 128, 139, STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP}, bjarni@6059: { WIDGETS_END}, bjarni@6059: }; bjarni@6059: bjarni@6059: static const WindowDesc _replace_rail_vehicle_desc = { bjarni@10302: WDP_AUTO, WDP_AUTO, 456, 140, 456, 140, 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@10302: _replace_vehicle_widgets, bjarni@6059: }; bjarni@6059: bjarni@10302: static const WindowDesc _replace_vehicle_desc = { bjarni@10302: WDP_AUTO, WDP_AUTO, 456, 118, 456, 118, 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@10302: _replace_vehicle_widgets, bjarni@6059: }; bjarni@6059: rubidium@10494: RailType ReplaceVehicleWindow::sel_railtype = RAILTYPE_RAIL; bjarni@6059: rubidium@7139: void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype) rubidium@7139: { rubidium@7139: DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype); rubidium@10494: new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g); bjarni@6059: }