src/build_vehicle_gui.cpp
changeset 9403 8cfca59c11f1
parent 9395 003c5b8d2931
child 9413 7042a8ec3fa8
equal deleted inserted replaced
9402:4eb0f983b218 9403:8cfca59c11f1
   743  * @param min where to start in the list
   743  * @param min where to start in the list
   744  * @param max where in the list to end
   744  * @param max where in the list to end
   745  * @param selected_id what engine to highlight as selected, if any
   745  * @param selected_id what engine to highlight as selected, if any
   746  * @param count_location Offset to print the engine count (used by autoreplace). 0 means it's off
   746  * @param count_location Offset to print the engine count (used by autoreplace). 0 means it's off
   747  */
   747  */
   748 void DrawEngineList(VehicleType type, int x, int y, const EngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group)
   748 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)
   749 {
   749 {
   750 	byte step_size = GetVehicleListHeight(type);
   750 	byte step_size = GetVehicleListHeight(type);
   751 	byte x_offset = 0;
   751 	byte x_offset = 0;
   752 	byte y_offset = 0;
   752 	byte y_offset = 0;
   753 
   753 
   754 	assert(max <= eng_list->size());
   754 	assert(max <= eng_list->Length());
   755 
   755 
   756 	switch (type) {
   756 	switch (type) {
   757 		case VEH_TRAIN:
   757 		case VEH_TRAIN:
   758 			x++; // train and road vehicles use the same offset, except trains are one more pixel to the right
   758 			x++; // train and road vehicles use the same offset, except trains are one more pixel to the right
   759 			/* Fallthough */
   759 			/* Fallthough */
   804 	bool descending_sort_order;
   804 	bool descending_sort_order;
   805 	byte sort_criteria;
   805 	byte sort_criteria;
   806 	bool regenerate_list;
   806 	bool regenerate_list;
   807 	EngineID sel_engine;
   807 	EngineID sel_engine;
   808 	EngineID rename_engine;
   808 	EngineID rename_engine;
   809 	EngineList eng_list;
   809 	GUIEngineList eng_list;
   810 
   810 
   811 	BuildVehicleWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window(desc, tile == 0 ? (int)type : tile)
   811 	BuildVehicleWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window(desc, tile == 0 ? (int)type : tile)
   812 	{
   812 	{
   813 		this->vehicle_type = type;
   813 		this->vehicle_type = type;
   814 		int vlh = GetVehicleListHeight(this->vehicle_type);
   814 		int vlh = GetVehicleListHeight(this->vehicle_type);
   846 		this->SetupWindowStrings(type);
   846 		this->SetupWindowStrings(type);
   847 		ResizeButtons(this, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
   847 		ResizeButtons(this, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
   848 
   848 
   849 		this->GenerateBuildList(); // generate the list, since we need it in the next line
   849 		this->GenerateBuildList(); // generate the list, since we need it in the next line
   850 		/* Select the first engine in the list as default when opening the window */
   850 		/* Select the first engine in the list as default when opening the window */
   851 		if (this->eng_list.size() > 0) this->sel_engine = this->eng_list[0];
   851 		if (this->eng_list.Length() > 0) this->sel_engine = this->eng_list[0];
   852 
   852 
   853 		this->FindWindowPlacementAndResize(desc);
   853 		this->FindWindowPlacementAndResize(desc);
   854 	}
   854 	}
   855 
   855 
   856 	/* Setup widget strings to fit the different types of vehicles */
   856 	/* Setup widget strings to fit the different types of vehicles */
   904 		int num_engines = 0;
   904 		int num_engines = 0;
   905 		int num_wagons  = 0;
   905 		int num_wagons  = 0;
   906 
   906 
   907 		this->filter.railtype = (this->window_number <= VEH_END) ? RAILTYPE_END : GetRailType(this->window_number);
   907 		this->filter.railtype = (this->window_number <= VEH_END) ? RAILTYPE_END : GetRailType(this->window_number);
   908 
   908 
   909 		this->eng_list.clear();
   909 		this->eng_list.Clear();
   910 
   910 
   911 		/* Make list of all available train engines and wagons.
   911 		/* Make list of all available train engines and wagons.
   912 		* Also check to see if the previously selected engine is still available,
   912 		* Also check to see if the previously selected engine is still available,
   913 		* and if not, reset selection to INVALID_ENGINE. This could be the case
   913 		* and if not, reset selection to INVALID_ENGINE. This could be the case
   914 		* when engines become obsolete and are removed */
   914 		* when engines become obsolete and are removed */
   918 			const RailVehicleInfo *rvi = &e->u.rail;
   918 			const RailVehicleInfo *rvi = &e->u.rail;
   919 
   919 
   920 			if (this->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, this->filter.railtype)) continue;
   920 			if (this->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, this->filter.railtype)) continue;
   921 			if (!IsEngineBuildable(eid, VEH_TRAIN, _local_player)) continue;
   921 			if (!IsEngineBuildable(eid, VEH_TRAIN, _local_player)) continue;
   922 
   922 
   923 			this->eng_list.push_back(eid);
   923 			*this->eng_list.Append() = eid;
       
   924 
   924 			if (rvi->railveh_type != RAILVEH_WAGON) {
   925 			if (rvi->railveh_type != RAILVEH_WAGON) {
   925 				num_engines++;
   926 				num_engines++;
   926 			} else {
   927 			} else {
   927 				num_wagons++;
   928 				num_wagons++;
   928 			}
   929 			}
   947 	/* Figure out what road vehicle EngineIDs to put in the list */
   948 	/* Figure out what road vehicle EngineIDs to put in the list */
   948 	void GenerateBuildRoadVehList()
   949 	void GenerateBuildRoadVehList()
   949 	{
   950 	{
   950 		EngineID sel_id = INVALID_ENGINE;
   951 		EngineID sel_id = INVALID_ENGINE;
   951 
   952 
   952 		this->eng_list.clear();
   953 		this->eng_list.Clear();
   953 
   954 
   954 		const Engine *e;
   955 		const Engine *e;
   955 		FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
   956 		FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
   956 			EngineID eid = e->index;
   957 			EngineID eid = e->index;
   957 			if (!IsEngineBuildable(eid, VEH_ROAD, _local_player)) continue;
   958 			if (!IsEngineBuildable(eid, VEH_ROAD, _local_player)) continue;
   958 			if (!HasBit(this->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
   959 			if (!HasBit(this->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
   959 			this->eng_list.push_back(eid);
   960 			*this->eng_list.Append() = eid;
   960 
   961 
   961 			if (eid == this->sel_engine) sel_id = eid;
   962 			if (eid == this->sel_engine) sel_id = eid;
   962 		}
   963 		}
   963 		this->sel_engine = sel_id;
   964 		this->sel_engine = sel_id;
   964 	}
   965 	}
   965 
   966 
   966 	/* Figure out what ship EngineIDs to put in the list */
   967 	/* Figure out what ship EngineIDs to put in the list */
   967 	void GenerateBuildShipList()
   968 	void GenerateBuildShipList()
   968 	{
   969 	{
   969 		EngineID sel_id = INVALID_ENGINE;
   970 		EngineID sel_id = INVALID_ENGINE;
   970 		this->eng_list.clear();
   971 		this->eng_list.Clear();
   971 
   972 
   972 		const Engine *e;
   973 		const Engine *e;
   973 		FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) {
   974 		FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) {
   974 			EngineID eid = e->index;
   975 			EngineID eid = e->index;
   975 			if (!IsEngineBuildable(eid, VEH_SHIP, _local_player)) continue;
   976 			if (!IsEngineBuildable(eid, VEH_SHIP, _local_player)) continue;
   976 			this->eng_list.push_back(eid);
   977 			*this->eng_list.Append() = eid;
   977 
   978 
   978 			if (eid == this->sel_engine) sel_id = eid;
   979 			if (eid == this->sel_engine) sel_id = eid;
   979 		}
   980 		}
   980 		this->sel_engine = sel_id;
   981 		this->sel_engine = sel_id;
   981 	}
   982 	}
   983 	/* Figure out what aircraft EngineIDs to put in the list */
   984 	/* Figure out what aircraft EngineIDs to put in the list */
   984 	void GenerateBuildAircraftList()
   985 	void GenerateBuildAircraftList()
   985 	{
   986 	{
   986 		EngineID sel_id = INVALID_ENGINE;
   987 		EngineID sel_id = INVALID_ENGINE;
   987 
   988 
   988 		this->eng_list.clear();
   989 		this->eng_list.Clear();
   989 
   990 
   990 		/* Make list of all available planes.
   991 		/* Make list of all available planes.
   991 		* Also check to see if the previously selected plane is still available,
   992 		* Also check to see if the previously selected plane is still available,
   992 		* and if not, reset selection to INVALID_ENGINE. This could be the case
   993 		* and if not, reset selection to INVALID_ENGINE. This could be the case
   993 		* when planes become obsolete and are removed */
   994 		* when planes become obsolete and are removed */
   996 			EngineID eid = e->index;
   997 			EngineID eid = e->index;
   997 			if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_player)) continue;
   998 			if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_player)) continue;
   998 			/* First VEH_END window_numbers are fake to allow a window open for all different types at once */
   999 			/* First VEH_END window_numbers are fake to allow a window open for all different types at once */
   999 			if (this->window_number > VEH_END && !CanAircraftUseStation(eid, this->window_number)) continue;
  1000 			if (this->window_number > VEH_END && !CanAircraftUseStation(eid, this->window_number)) continue;
  1000 
  1001 
  1001 			this->eng_list.push_back(eid);
  1002 			*this->eng_list.Append() = eid;
  1002 			if (eid == this->sel_engine) sel_id = eid;
  1003 			if (eid == this->sel_engine) sel_id = eid;
  1003 		}
  1004 		}
  1004 
  1005 
  1005 		this->sel_engine = sel_id;
  1006 		this->sel_engine = sel_id;
  1006 	}
  1007 	}
  1037 				this->SetDirty();
  1038 				this->SetDirty();
  1038 				break;
  1039 				break;
  1039 
  1040 
  1040 			case BUILD_VEHICLE_WIDGET_LIST: {
  1041 			case BUILD_VEHICLE_WIDGET_LIST: {
  1041 				uint i = (pt.y - this->widget[BUILD_VEHICLE_WIDGET_LIST].top) / GetVehicleListHeight(this->vehicle_type) + this->vscroll.pos;
  1042 				uint i = (pt.y - this->widget[BUILD_VEHICLE_WIDGET_LIST].top) / GetVehicleListHeight(this->vehicle_type) + this->vscroll.pos;
  1042 				size_t num_items = this->eng_list.size();
  1043 				size_t num_items = this->eng_list.Length();
  1043 				this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
  1044 				this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
  1044 				this->SetDirty();
  1045 				this->SetDirty();
  1045 				break;
  1046 				break;
  1046 			}
  1047 			}
  1047 
  1048 
  1103 		if (this->regenerate_list) {
  1104 		if (this->regenerate_list) {
  1104 			this->regenerate_list = false;
  1105 			this->regenerate_list = false;
  1105 			this->GenerateBuildList();
  1106 			this->GenerateBuildList();
  1106 		}
  1107 		}
  1107 
  1108 
  1108 		uint max = min(this->vscroll.pos + this->vscroll.cap, this->eng_list.size());
  1109 		uint max = min(this->vscroll.pos + this->vscroll.cap, this->eng_list.Length());
  1109 
  1110 
  1110 		this->SetWidgetDisabledState(BUILD_VEHICLE_WIDGET_BUILD, this->window_number <= VEH_END);
  1111 		this->SetWidgetDisabledState(BUILD_VEHICLE_WIDGET_BUILD, this->window_number <= VEH_END);
  1111 
  1112 
  1112 		SetVScrollCount(this, this->eng_list.size());
  1113 		SetVScrollCount(this, this->eng_list.Length());
  1113 		SetDParam(0, this->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); // This should only affect rail vehicles
  1114 		SetDParam(0, this->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); // This should only affect rail vehicles
  1114 
  1115 
  1115 		/* Set text of sort by dropdown */
  1116 		/* Set text of sort by dropdown */
  1116 		this->widget[BUILD_VEHICLE_WIDGET_SORT_DROPDOWN].data = _sort_listing[this->vehicle_type][this->sort_criteria];
  1117 		this->widget[BUILD_VEHICLE_WIDGET_SORT_DROPDOWN].data = _sort_listing[this->vehicle_type][this->sort_criteria];
  1117 
  1118