tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file autoreplace_gui.cpp GUI for autoreplace handling. */ belugas@6117: darkvater@164: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" bjarni@842: #include "gui.h" rubidium@8116: #include "command_func.h" tron@2159: #include "variables.h" tron@2159: #include "vehicle_gui.h" peter1138@2962: #include "newgrf_engine.h" rubidium@6643: #include "group.h" rubidium@8103: #include "rail.h" rubidium@8114: #include "strings_func.h" rubidium@8131: #include "window_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8212: #include "autoreplace_func.h" rubidium@8224: #include "gfx_func.h" rubidium@8254: #include "player_func.h" peter1138@8284: #include "widgets/dropdown_func.h" rubidium@8786: #include "engine_func.h" peter1138@9070: #include "engine_base.h" rubidium@9274: #include "window_gui.h" peter1138@9380: #include "engine_gui.h" frosch@9725: #include "articulated_vehicles.h" bjarni@5808: rubidium@8264: #include "table/sprites.h" rubidium@8264: #include "table/strings.h" rubidium@8264: peter1138@9679: void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group); tron@505: bjarni@5808: static const StringID _rail_types_list[] = { bjarni@5808: STR_RAIL_VEHICLES, bjarni@5808: STR_ELRAIL_VEHICLES, bjarni@5808: STR_MONORAIL_VEHICLES, bjarni@5808: STR_MAGLEV_VEHICLES, bjarni@5808: INVALID_STRING_ID bjarni@5808: }; bjarni@5808: peter1138@8345: enum ReplaceVehicleWindowWidgets { bjarni@9027: RVW_WIDGET_LEFT_MATRIX = 3, bjarni@9027: RVW_WIDGET_LEFT_SCROLLBAR, bjarni@9027: RVW_WIDGET_RIGHT_MATRIX, bjarni@9027: RVW_WIDGET_RIGHT_SCROLLBAR, bjarni@9027: RVW_WIDGET_LEFT_DETAILS, bjarni@9027: RVW_WIDGET_RIGHT_DETAILS, bjarni@9027: bjarni@9027: /* Button row */ peter1138@8345: RVW_WIDGET_START_REPLACE, peter1138@8345: RVW_WIDGET_INFO_TAB, peter1138@8345: RVW_WIDGET_STOP_REPLACE, bjarni@9027: RVW_WIDGET_RESIZE, peter1138@8345: bjarni@9027: /* Train only widgets */ peter1138@8345: RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE, peter1138@8345: RVW_WIDGET_TRAIN_FLUFF_LEFT, peter1138@8345: RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, peter1138@8345: RVW_WIDGET_TRAIN_FLUFF_RIGHT, peter1138@8345: RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE, peter1138@8345: }; peter1138@8345: peter1138@9671: static int CDECL EngineNumberSorter(const void *a, const void *b) peter1138@8174: { peter1138@8174: const EngineID va = *(const EngineID*)a; peter1138@8174: const EngineID vb = *(const EngineID*)b; peter1138@8174: int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb); peter1138@8174: peter1138@8174: return r; peter1138@8174: } peter1138@8174: bjarni@5944: /** Rebuild the left autoreplace list if an engine is removed or added bjarni@5944: * @param e Engine to check if it is removed or added peter1138@7096: * @param id_g The group the engine belongs to bjarni@5944: * Note: this function only works if it is called either bjarni@5944: * - when a new vehicle is build, but before it's counted in num_engines bjarni@5944: * - when a vehicle is deleted and after it's substracted from num_engines bjarni@5944: * - when not changing the count (used when changing replace orders) bjarni@5808: */ peter1138@7096: void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g) bjarni@5808: { bjarni@5944: Player *p = GetPlayer(_local_player); rubidium@7481: uint num_engines = GetGroupNumEngines(_local_player, id_g, e); bjarni@5808: peter1138@7096: if (num_engines == 0 || p->num_engines[e] == 0) { bjarni@5944: /* We don't have any of this engine type. bjarni@5944: * Either we just sold the last one, we build a new one or we stopped replacing it. bjarni@5944: * In all cases, we need to update the left list */ rubidium@9172: InvalidateWindowData(WC_REPLACE_VEHICLE, GetEngine(e)->type, true); bjarni@5944: } bjarni@5944: } bjarni@5808: bjarni@5944: /** When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists bjarni@5944: * @param type The type of engine bjarni@5944: */ rubidium@6638: void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type) bjarni@5944: { rubidium@9172: InvalidateWindowData(WC_REPLACE_VEHICLE, type, false); // Update the autoreplace window bjarni@5944: InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well bjarni@5944: } bjarni@5944: bjarni@5944: /** Figure out if two engines got at least one type of cargo in common (refitting if needed) bjarni@5944: * @param engine_a one of the EngineIDs bjarni@5944: * @param engine_b the other EngineID frosch@9725: * @param type the type of the engines bjarni@5944: * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all) bjarni@5944: */ frosch@9725: static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b, VehicleType type) bjarni@5944: { frosch@9725: uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, type, true); frosch@9725: uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, type, true); frosch@9725: return (available_cargos_a == 0 || available_cargos_b == 0 || (available_cargos_a & available_cargos_b) != 0); bjarni@5944: } bjarni@5944: rubidium@9174: /** rubidium@9174: * Window for the autoreplacing of vehicles. bjarni@5944: */ rubidium@9174: class ReplaceVehicleWindow : public Window { rubidium@9174: byte sel_index[2]; rubidium@9174: EngineID sel_engine[2]; rubidium@9174: uint16 count[2]; rubidium@9174: bool wagon_btnstate; ///< true means engine is selected peter1138@9403: GUIEngineList list[2]; rubidium@9174: bool update_left; rubidium@9174: bool update_right; rubidium@9174: bool init_lists; rubidium@9174: GroupID sel_group; rubidium@9174: static RailType sel_railtype; bjarni@5944: rubidium@9174: /** Figure out if an engine should be added to a list rubidium@9174: * @param e The EngineID rubidium@9174: * @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected) rubidium@9174: * @param show_engines if truem then locomotives are drawn, else wagons (never both) rubidium@9174: * @return true if the engine should be in the list (based on this check) rubidium@9174: */ rubidium@9174: bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines) rubidium@9174: { rubidium@9174: const RailVehicleInfo *rvi = RailVehInfo(e); rubidium@6643: rubidium@9174: /* Ensure that the wagon/engine selection fits the engine. */ rubidium@9174: if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false; rubidium@9174: rubidium@9174: if (draw_left && show_engines) { rubidium@9174: /* Ensure that the railtype is specific to the selected one */ rubidium@9174: if (rvi->railtype != this->sel_railtype) return false; bjarni@5808: } else { rubidium@9174: /* Ensure that it's a compatible railtype to the selected one (like electric <-> diesel) rubidium@9174: * The vehicle do not have to have power on the railtype in question, only able to drive (pulled if needed) */ rubidium@9174: if (!IsCompatibleRail(rvi->railtype, this->sel_railtype)) return false; rubidium@9174: } rubidium@9174: return true; rubidium@9174: } rubidium@6687: rubidium@9174: rubidium@9174: /** Generate a list rubidium@9174: * @param w Window, that contains the list rubidium@9174: * @param draw_left true if generating the left list, otherwise false rubidium@9174: */ rubidium@9174: void GenerateReplaceVehList(Window *w, bool draw_left) rubidium@9174: { rubidium@9174: EngineID selected_engine = INVALID_ENGINE; rubidium@9174: VehicleType type = (VehicleType)this->window_number; rubidium@9174: byte i = draw_left ? 0 : 1; rubidium@9174: peter1138@9403: GUIEngineList *list = &this->list[i]; peter1138@9403: list->Clear(); rubidium@9174: rubidium@9174: const Engine *e; rubidium@9174: FOR_ALL_ENGINES_OF_TYPE(e, type) { rubidium@9174: EngineID eid = e->index; rubidium@9174: if (type == VEH_TRAIN && !GenerateReplaceRailList(eid, draw_left, this->wagon_btnstate)) continue; // special rules for trains rubidium@9174: rubidium@9174: if (draw_left) { rubidium@9174: const GroupID selected_group = this->sel_group; rubidium@9174: const uint num_engines = GetGroupNumEngines(_local_player, selected_group, eid); rubidium@9174: rubidium@9174: /* Skip drawing the engines we don't have any of and haven't set for replacement */ rubidium@9174: if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), eid, selected_group) == INVALID_ENGINE) continue; rubidium@9174: } else { rubidium@9174: /* This is for engines we can replace to and they should depend on what we selected to replace from */ rubidium@9174: if (!IsEngineBuildable(eid, type, _local_player)) continue; // we need to be able to build the engine frosch@9725: if (!EnginesGotCargoInCommon(eid, this->sel_engine[0], type)) continue; // the engines needs to be able to carry the same cargo rubidium@9174: rubidium@9174: /* Road vehicles can't be replaced by trams and vice-versa */ rubidium@9174: 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@9174: if (eid == this->sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew) rubidium@9174: } rubidium@9174: peter1138@9403: *list->Append() = eid; rubidium@9174: if (eid == this->sel_engine[i]) selected_engine = eid; // The selected engine is still in the list rubidium@9174: } rubidium@9174: this->sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore) peter1138@9671: EngList_Sort(list, &EngineNumberSorter); rubidium@9174: } rubidium@9174: rubidium@9174: /** Generate the lists */ rubidium@9174: void GenerateLists() rubidium@9174: { rubidium@9174: EngineID e = this->sel_engine[0]; rubidium@9174: rubidium@9174: if (this->update_left == true) { rubidium@9174: /* We need to rebuild the left list */ rubidium@9174: GenerateReplaceVehList(this, true); peter1138@9403: SetVScrollCount(this, this->list[0].Length()); peter1138@9403: if (this->init_lists && this->sel_engine[0] == INVALID_ENGINE && this->list[0].Length() != 0) { rubidium@9174: this->sel_engine[0] = this->list[0][0]; rubidium@9174: } bjarni@5808: } bjarni@5808: rubidium@9174: if (this->update_right || e != this->sel_engine[0]) { rubidium@9174: /* Either we got a request to rebuild the right list or the left list selected a different engine */ rubidium@9174: if (this->sel_engine[0] == INVALID_ENGINE) { rubidium@9174: /* Always empty the right list when nothing is selected in the left list */ peter1138@9403: this->list[1].Clear(); rubidium@9174: this->sel_engine[1] = INVALID_ENGINE; rubidium@9174: } else { rubidium@9174: GenerateReplaceVehList(this, false); peter1138@9403: SetVScroll2Count(this, this->list[1].Length()); peter1138@9403: if (this->init_lists && this->sel_engine[1] == INVALID_ENGINE && this->list[1].Length() != 0) { rubidium@9174: this->sel_engine[1] = this->list[1][0]; rubidium@9174: } rubidium@9174: } bjarni@5944: } rubidium@9174: /* Reset the flags about needed updates */ rubidium@9174: this->update_left = false; rubidium@9174: this->update_right = false; rubidium@9174: this->init_lists = false; bjarni@5944: } bjarni@5944: rubidium@9174: public: rubidium@9210: ReplaceVehicleWindow(const WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc, vehicletype) rubidium@9174: { rubidium@9174: this->wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool) rubidium@9174: this->update_left = true; rubidium@9174: this->update_right = true; rubidium@9174: this->init_lists = true; rubidium@9174: this->sel_engine[0] = INVALID_ENGINE; rubidium@9174: this->sel_engine[1] = INVALID_ENGINE; rubidium@9174: rubidium@9174: this->resize.step_height = GetVehicleListHeight(vehicletype); rubidium@9174: this->vscroll.cap = this->resize.step_height == 14 ? 8 : 4; rubidium@9174: rubidium@9174: Widget *widget = this->widget; rubidium@9174: widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll.cap << 8) + 1; rubidium@9174: rubidium@9174: if (vehicletype == VEH_TRAIN) { rubidium@9174: this->wagon_btnstate = true; rubidium@9174: /* The train window is bigger so we will move some of the widgets to fit the new size. rubidium@9174: * We will start by moving the resize button to the lower right corner. */ rubidium@9174: widget[RVW_WIDGET_RESIZE].top = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].top; rubidium@9174: widget[RVW_WIDGET_RESIZE].bottom = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].bottom; rubidium@9174: widget[RVW_WIDGET_STOP_REPLACE].right = widget[RVW_WIDGET_RESIZE].right; rubidium@9174: rubidium@9174: /* The detail panel is one line taller for trains so we will move some of the widgets one line (10 pixels) down. */ rubidium@9174: widget[RVW_WIDGET_LEFT_DETAILS].bottom += 10; rubidium@9174: widget[RVW_WIDGET_RIGHT_DETAILS].bottom += 10; rubidium@9174: for (int i = RVW_WIDGET_START_REPLACE; i < RVW_WIDGET_RESIZE; i++) { rubidium@9174: widget[i].top += 10; rubidium@9174: widget[i].bottom += 10; rubidium@9174: } bjarni@5944: } else { rubidium@9174: /* Since it's not a train we will hide the train only widgets. */ rubidium@9174: this->SetWidgetsHiddenState(true, rubidium@9174: RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE, rubidium@9174: RVW_WIDGET_TRAIN_FLUFF_LEFT, rubidium@9174: RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, rubidium@9174: RVW_WIDGET_TRAIN_FLUFF_RIGHT, rubidium@9174: RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE, rubidium@9174: WIDGET_LIST_END); rubidium@9174: } rubidium@9174: rubidium@9174: ResizeWindow(this, 0, this->resize.step_height * this->vscroll.cap); rubidium@9174: rubidium@9174: /* Set the minimum window size to the current window size */ rubidium@9174: this->resize.width = this->width; rubidium@9174: this->resize.height = this->height; rubidium@9174: rubidium@9174: this->caption_color = _local_player; rubidium@9174: this->sel_group = id_g; rubidium@9174: this->vscroll2.cap = this->vscroll.cap; // these two are always the same rubidium@9178: rubidium@9178: this->FindWindowPlacementAndResize(desc); rubidium@9174: } rubidium@9174: rubidium@9174: virtual void OnPaint() rubidium@9174: { rubidium@9174: static const StringID _vehicle_type_names[] = { rubidium@9174: STR_019F_TRAIN, rubidium@9174: STR_019C_ROAD_VEHICLE, rubidium@9174: STR_019E_SHIP, rubidium@9174: STR_019D_AIRCRAFT rubidium@9174: }; rubidium@9174: rubidium@9174: if (this->update_left || this->update_right) this->GenerateLists(); rubidium@9174: rubidium@9174: Player *p = GetPlayer(_local_player); rubidium@9174: EngineID selected_id[2]; rubidium@9174: const GroupID selected_group = this->sel_group; rubidium@9174: rubidium@9174: selected_id[0] = this->sel_engine[0]; rubidium@9174: selected_id[1] = this->sel_engine[1]; rubidium@9174: rubidium@9174: /* Disable the "Start Replacing" button if: rubidium@9174: * Either list is empty rubidium@9174: * or The selected replacement engine has a replacement (to prevent loops) rubidium@9174: * or The right list (new replacement) has the existing replacement vehicle selected */ rubidium@9174: this->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE, rubidium@9174: selected_id[0] == INVALID_ENGINE || rubidium@9174: selected_id[1] == INVALID_ENGINE || rubidium@9174: EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE || rubidium@9174: EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]); rubidium@9174: rubidium@9174: /* Disable the "Stop Replacing" button if: rubidium@9174: * The left list (existing vehicle) is empty rubidium@9174: * or The selected vehicle has no replacement set up */ rubidium@9174: this->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE, rubidium@9174: selected_id[0] == INVALID_ENGINE || rubidium@9174: !EngineHasReplacementForPlayer(p, selected_id[0], selected_group)); rubidium@9174: rubidium@9174: /* now the actual drawing of the window itself takes place */ rubidium@9174: SetDParam(0, _vehicle_type_names[this->window_number]); rubidium@9174: rubidium@9174: if (this->window_number == VEH_TRAIN) { rubidium@9174: /* set on/off for renew_keep_length */ rubidium@9174: SetDParam(1, p->renew_keep_length ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF); rubidium@9174: rubidium@9174: /* set wagon/engine button */ rubidium@9174: SetDParam(2, this->wagon_btnstate ? STR_ENGINES : STR_WAGONS); rubidium@9174: rubidium@9174: /* sets the colour of that art thing */ rubidium@9174: this->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].color = _player_colors[_local_player]; rubidium@9174: this->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].color = _player_colors[_local_player]; rubidium@9174: } rubidium@9174: rubidium@9174: if (this->window_number == VEH_TRAIN) { rubidium@9174: /* Show the selected railtype in the pulldown menu */ rubidium@9174: this->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = _rail_types_list[sel_railtype]; rubidium@9174: } rubidium@9174: rubidium@9273: this->DrawWidgets(); rubidium@9174: rubidium@9174: /* sets up the string for the vehicle that is being replaced to */ rubidium@9174: if (selected_id[0] != INVALID_ENGINE) { rubidium@9174: if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) { rubidium@9174: SetDParam(0, STR_NOT_REPLACING); rubidium@9174: } else { rubidium@9174: SetDParam(0, STR_ENGINE_NAME); rubidium@9174: SetDParam(1, EngineReplacementForPlayer(p, selected_id[0], selected_group)); rubidium@9174: } rubidium@9174: } else { rubidium@9174: SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED); rubidium@9174: } rubidium@9174: frosch@9719: 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); rubidium@9174: rubidium@9174: /* Draw the lists */ rubidium@9174: for (byte i = 0; i < 2; i++) { rubidium@9174: uint widget = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX; peter1138@9403: GUIEngineList *list = &this->list[i]; // which list to draw rubidium@9174: EngineID start = i == 0 ? this->vscroll.pos : this->vscroll2.pos; // what is the offset for the start (scrolling) peter1138@9403: EngineID end = min((i == 0 ? this->vscroll.cap : this->vscroll2.cap) + start, list->Length()); rubidium@9174: rubidium@9174: /* Do the actual drawing */ peter1138@9679: 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); rubidium@9174: rubidium@9174: /* Also draw the details if an engine is selected */ rubidium@9174: if (this->sel_engine[i] != INVALID_ENGINE) { rubidium@9174: const Widget *wi = &this->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS]; rubidium@9174: int text_end = DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, this->sel_engine[i]); rubidium@9174: rubidium@9174: if (text_end > wi->bottom) { rubidium@9174: this->SetDirty(); rubidium@9174: ResizeWindowForWidget(this, i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS, 0, text_end - wi->bottom); rubidium@9174: this->SetDirty(); rubidium@9174: } bjarni@5808: } bjarni@5808: } bjarni@5808: } peter1138@8419: rubidium@9174: virtual void OnClick(Point pt, int widget) rubidium@9174: { rubidium@9174: switch (widget) { rubidium@9174: case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE: rubidium@9174: this->wagon_btnstate = !(this->wagon_btnstate); rubidium@9174: this->update_left = true; rubidium@9174: this->init_lists = true; rubidium@9174: this->SetDirty(); rubidium@9174: break; bjarni@5944: rubidium@9174: case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: /* Railtype selection dropdown menu */ rubidium@9174: ShowDropDownMenu(this, _rail_types_list, sel_railtype, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, 0, ~GetPlayer(_local_player)->avail_railtypes); rubidium@9174: break; bjarni@5808: rubidium@9174: case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: /* toggle renew_keep_length */ rubidium@9174: DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE); rubidium@9174: break; bjarni@9027: rubidium@9174: case RVW_WIDGET_START_REPLACE: { /* Start replacing */ rubidium@9174: EngineID veh_from = this->sel_engine[0]; rubidium@9174: EngineID veh_to = this->sel_engine[1]; rubidium@9174: DoCommandP(0, 3 + (this->sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE); frosch@9719: this->SetDirty(); rubidium@9174: } break; bjarni@5944: rubidium@9174: case RVW_WIDGET_STOP_REPLACE: { /* Stop replacing */ rubidium@9174: EngineID veh_from = this->sel_engine[0]; rubidium@9174: DoCommandP(0, 3 + (this->sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE); frosch@9719: this->SetDirty(); rubidium@9174: } break; bjarni@5944: rubidium@9174: case RVW_WIDGET_LEFT_MATRIX: rubidium@9174: case RVW_WIDGET_RIGHT_MATRIX: { rubidium@9174: uint i = (pt.y - 14) / this->resize.step_height; rubidium@9174: uint16 click_scroll_pos = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.pos : this->vscroll2.pos; rubidium@9174: uint16 click_scroll_cap = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.cap : this->vscroll2.cap; rubidium@9174: byte click_side = widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1; peter1138@9403: size_t engine_count = this->list[click_side].Length(); rubidium@9174: rubidium@9174: if (i < click_scroll_cap) { rubidium@9174: i += click_scroll_pos; rubidium@9174: EngineID e = engine_count > i ? this->list[click_side][i] : INVALID_ENGINE; rubidium@9174: if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected rubidium@9174: this->sel_engine[click_side] = e; rubidium@9174: if (click_side == 0) { rubidium@9174: this->update_right = true; rubidium@9174: this->init_lists = true; rubidium@9174: } rubidium@9174: this->SetDirty(); rubidium@9174: } rubidium@9174: break; rubidium@9174: } rubidium@9174: } bjarni@5808: } rubidium@9174: rubidium@9174: virtual void OnDropdownSelect(int widget, int index) rubidium@9174: { rubidium@9174: RailType temp = (RailType)index; rubidium@9174: if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything rubidium@9174: sel_railtype = temp; rubidium@9174: /* Reset scrollbar positions */ rubidium@9174: this->vscroll.pos = 0; rubidium@9174: this->vscroll2.pos = 0; rubidium@9174: /* Rebuild the lists */ rubidium@9174: this->update_left = true; rubidium@9174: this->update_right = true; rubidium@9174: this->init_lists = true; rubidium@9174: this->SetDirty(); rubidium@9174: } rubidium@9174: rubidium@9174: virtual void OnResize(Point new_size, Point delta) { rubidium@9174: this->vscroll.cap += delta.y / (int)this->resize.step_height; rubidium@9174: this->vscroll2.cap += delta.y / (int)this->resize.step_height; rubidium@9174: rubidium@9174: Widget *widget = this->widget; rubidium@9174: rubidium@9174: widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll2.cap << 8) + 1; rubidium@9174: rubidium@9174: if (delta.x != 0) { rubidium@9174: /* We changed the width of the window so we have to resize the lists. rubidium@9174: * Because ResizeButtons() makes each widget the same size it can't be used on the lists rubidium@9174: * because then the lists would have the same size as the scrollbars. rubidium@9174: * Instead we use it on the detail panels. rubidium@9174: * Afterwards we use the new location of the detail panels (the middle of the window) rubidium@9174: * to place the lists. rubidium@9174: * This way the lists will have equal size while keeping the width of the scrollbars unchanged. */ rubidium@9174: ResizeButtons(this, RVW_WIDGET_LEFT_DETAILS, RVW_WIDGET_RIGHT_DETAILS); rubidium@9174: widget[RVW_WIDGET_RIGHT_MATRIX].left = widget[RVW_WIDGET_RIGHT_DETAILS].left; rubidium@9174: widget[RVW_WIDGET_LEFT_SCROLLBAR].right = widget[RVW_WIDGET_LEFT_DETAILS].right; rubidium@9174: widget[RVW_WIDGET_LEFT_SCROLLBAR].left = widget[RVW_WIDGET_LEFT_SCROLLBAR].right - 11; rubidium@9174: widget[RVW_WIDGET_LEFT_MATRIX].right = widget[RVW_WIDGET_LEFT_SCROLLBAR].left - 1; rubidium@9174: } rubidium@9174: } rubidium@9174: rubidium@9174: virtual void OnInvalidateData(int data) rubidium@9174: { rubidium@9174: if (data != 0) { rubidium@9174: this->update_left = true; rubidium@9174: } else { rubidium@9174: this->update_right = true; rubidium@9174: } rubidium@9174: } rubidium@9174: }; bjarni@5808: bjarni@9027: static const Widget _replace_vehicle_widgets[] = { belugas@9751: { WWT_CLOSEBOX, RESIZE_NONE, COLOUR_GREY, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, belugas@9751: { WWT_CAPTION, RESIZE_RIGHT, COLOUR_GREY, 11, 443, 0, 13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS}, belugas@9751: { WWT_STICKYBOX, RESIZE_LR, COLOUR_GREY, 444, 455, 0, 13, STR_NULL, STR_STICKY_BUTTON}, bjarni@5808: belugas@9751: { WWT_MATRIX, RESIZE_BOTTOM, COLOUR_GREY, 0, 215, 14, 13, 0x1, STR_REPLACE_HELP_LEFT_ARRAY}, belugas@9751: { WWT_SCROLLBAR, RESIZE_BOTTOM, COLOUR_GREY, 216, 227, 14, 13, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, belugas@9751: { WWT_MATRIX, RESIZE_LRB, COLOUR_GREY, 228, 443, 14, 13, 0x1, STR_REPLACE_HELP_RIGHT_ARRAY}, belugas@9751: { WWT_SCROLL2BAR, RESIZE_LRB, COLOUR_GREY, 444, 455, 14, 13, STR_NULL, STR_0190_SCROLL_BAR_SCROLLS_LIST}, belugas@9751: { WWT_PANEL, RESIZE_TB, COLOUR_GREY, 0, 227, 14, 105, 0x0, STR_NULL}, belugas@9751: { WWT_PANEL, RESIZE_RTB, COLOUR_GREY, 228, 455, 14, 105, 0x0, STR_NULL}, bjarni@5808: belugas@9751: { WWT_PUSHTXTBTN, RESIZE_TB, COLOUR_GREY, 0, 138, 106, 117, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON}, belugas@9751: { WWT_PANEL, RESIZE_RTB, COLOUR_GREY, 139, 305, 106, 117, 0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB}, belugas@9751: { WWT_PUSHTXTBTN, RESIZE_LRTB, COLOUR_GREY, 306, 443, 106, 117, STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON}, belugas@9751: { WWT_RESIZEBOX, RESIZE_LRTB, COLOUR_GREY, 444, 455, 106, 117, STR_NULL, STR_RESIZE_BUTTON}, bjarni@9027: belugas@9751: { WWT_PUSHTXTBTN, RESIZE_TB, COLOUR_GREY, 0, 138, 128, 139, STR_REPLACE_ENGINE_WAGON_SELECT, STR_REPLACE_ENGINE_WAGON_SELECT_HELP}, belugas@9751: { WWT_PANEL, RESIZE_TB, COLOUR_GREY, 139, 153, 128, 139, 0x0, STR_NULL}, belugas@9751: { WWT_DROPDOWN, RESIZE_RTB, COLOUR_GREY, 154, 289, 128, 139, 0x0, STR_REPLACE_HELP_RAILTYPE}, belugas@9751: { WWT_PANEL, RESIZE_LRTB, COLOUR_GREY, 290, 305, 128, 139, 0x0, STR_NULL}, belugas@9751: { WWT_PUSHTXTBTN, RESIZE_LRTB, COLOUR_GREY, 306, 443, 128, 139, STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP}, bjarni@5808: { WIDGETS_END}, bjarni@5808: }; bjarni@5808: bjarni@5808: static const WindowDesc _replace_rail_vehicle_desc = { bjarni@9027: WDP_AUTO, WDP_AUTO, 456, 140, 456, 140, rubidium@5893: WC_REPLACE_VEHICLE, WC_NONE, bjarni@5808: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE, bjarni@9027: _replace_vehicle_widgets, bjarni@5808: }; bjarni@5808: bjarni@9027: static const WindowDesc _replace_vehicle_desc = { bjarni@9027: WDP_AUTO, WDP_AUTO, 456, 118, 456, 118, rubidium@5893: WC_REPLACE_VEHICLE, WC_NONE, bjarni@5808: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE, bjarni@9027: _replace_vehicle_widgets, bjarni@5808: }; bjarni@5808: rubidium@9174: RailType ReplaceVehicleWindow::sel_railtype = RAILTYPE_RAIL; bjarni@5808: rubidium@6643: void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype) rubidium@6643: { rubidium@6643: DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype); rubidium@9174: new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g); bjarni@5808: }