src/vehicle_gui.cpp
changeset 10980 5281330a77da
parent 10970 c0341ebaf88f
child 11031 ff45fd174578
equal deleted inserted replaced
10979:13c4100d8757 10980:5281330a77da
    88 	INVALID_STRING_ID
    88 	INVALID_STRING_ID
    89 };
    89 };
    90 
    90 
    91 void BuildVehicleList(VehicleListBase *vl, PlayerID owner, uint16 index, uint16 window_type)
    91 void BuildVehicleList(VehicleListBase *vl, PlayerID owner, uint16 index, uint16 window_type)
    92 {
    92 {
    93 	if (!(vl->vehicles.flags & VL_REBUILD)) return;
    93 	if (!vl->vehicles.NeedRebuild()) return;
    94 
    94 
    95 	DEBUG(misc, 3, "Building vehicle list for player %d at station %d", owner, index);
    95 	DEBUG(misc, 3, "Building vehicle list for player %d at station %d", owner, index);
    96 
    96 
    97 	GenerateVehicleSortList(&vl->vehicles, vl->vehicle_type, owner, index, window_type);
    97 	GenerateVehicleSortList(&vl->vehicles, vl->vehicle_type, owner, index, window_type);
    98 
    98 
    99 	vl->vehicles.flags &= ~VL_REBUILD;
    99 	vl->vehicles.RebuildDone();
   100 	vl->vehicles.flags |= VL_RESORT;
       
   101 }
   100 }
   102 
   101 
   103 /* cached values for VehicleNameSorter to spare many GetString() calls */
   102 /* cached values for VehicleNameSorter to spare many GetString() calls */
   104 static const Vehicle *_last_vehicle[2] = { NULL, NULL };
   103 static const Vehicle *_last_vehicle[2] = { NULL, NULL };
   105 
   104 
   106 void SortVehicleList(VehicleListBase *vl)
   105 void SortVehicleList(VehicleListBase *vl)
   107 {
   106 {
   108 	if (!(vl->vehicles.flags & VL_RESORT)) return;
   107 	if (vl->vehicles.Sort(_vehicle_sorter[vl->vehicles.SortType()])) return;
   109 
   108 
   110 	/* invalidate cached values for name sorter - vehicle names could change */
   109 	/* invalidate cached values for name sorter - vehicle names could change */
   111 	_last_vehicle[0] = _last_vehicle[1] = NULL;
   110 	_last_vehicle[0] = _last_vehicle[1] = NULL;
   112 
       
   113 	QSortT(vl->vehicles.Begin(), vl->vehicles.Length(), _vehicle_sorter[vl->vehicles.sort_type], vl->vehicles.flags & VL_DESC);
       
   114 
       
   115 	vl->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
       
   116 	vl->vehicles.flags &= ~VL_RESORT;
       
   117 }
   111 }
   118 
   112 
   119 void DepotSortList(VehicleList *list)
   113 void DepotSortList(VehicleList *list)
   120 {
   114 {
   121 	if (list->Length() < 2) return;
   115 	if (list->Length() < 2) return;
   863 			case VEH_SHIP:     this->sorting = &_sorting.ship; break;
   857 			case VEH_SHIP:     this->sorting = &_sorting.ship; break;
   864 			case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
   858 			case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
   865 			default: NOT_REACHED(); break;
   859 			default: NOT_REACHED(); break;
   866 		}
   860 		}
   867 
   861 
   868 		this->vehicles.flags = VL_REBUILD | (this->sorting->order ? VL_DESC : VL_NONE);
   862 		this->vehicles.SetListing(*this->sorting);
   869 		this->vehicles.sort_type = this->sorting->criteria;
   863 		this->vehicles.ForceRebuild();
   870 		this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer
   864 		this->vehicles.NeedResort();
   871 
   865 
   872 		this->FindWindowPlacementAndResize(desc);
   866 		this->FindWindowPlacementAndResize(desc);
   873 	}
   867 	}
   874 
   868 
   875 	~VehicleListWindow()
   869 	~VehicleListWindow()
   876 	{
   870 	{
       
   871 		*this->sorting = this->vehicles.GetListing();
   877 	}
   872 	}
   878 
   873 
   879 	virtual void OnPaint()
   874 	virtual void OnPaint()
   880 	{
   875 	{
   881 		int x = 2;
   876 		int x = 2;
   936 			WIDGET_LIST_END);
   931 			WIDGET_LIST_END);
   937 
   932 
   938 		this->DrawWidgets();
   933 		this->DrawWidgets();
   939 
   934 
   940 		/* draw sorting criteria string */
   935 		/* draw sorting criteria string */
   941 		DrawString(85, 15, _vehicle_sort_listing[this->vehicles.sort_type], TC_BLACK);
   936 		DrawString(85, 15, _vehicle_sort_listing[this->vehicles.SortType()], TC_BLACK);
   942 		/* draw arrow pointing up/down for ascending/descending sorting */
   937 		/* draw arrow pointing up/down for ascending/descending sorting */
   943 		this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP);
   938 		this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
   944 
   939 
   945 		max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.Length());
   940 		max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.Length());
   946 		for (i = this->vscroll.pos; i < max; ++i) {
   941 		for (i = this->vscroll.pos; i < max; ++i) {
   947 			const Vehicle *v = this->vehicles[i];
   942 			const Vehicle *v = this->vehicles[i];
   948 			StringID str;
   943 			StringID str;
   978 
   973 
   979 	virtual void OnClick(Point pt, int widget)
   974 	virtual void OnClick(Point pt, int widget)
   980 	{
   975 	{
   981 		switch (widget) {
   976 		switch (widget) {
   982 			case VLW_WIDGET_SORT_ORDER: /* Flip sorting method ascending/descending */
   977 			case VLW_WIDGET_SORT_ORDER: /* Flip sorting method ascending/descending */
   983 				this->vehicles.flags ^= VL_DESC;
   978 				this->vehicles.ToggleSortOrder();
   984 				this->vehicles.flags |= VL_RESORT;
       
   985 
       
   986 				this->sorting->order = !!(this->vehicles.flags & VL_DESC);
       
   987 				this->SetDirty();
   979 				this->SetDirty();
   988 				break;
   980 				break;
   989 			case VLW_WIDGET_SORT_BY_PULLDOWN:/* Select sorting criteria dropdown menu */
   981 			case VLW_WIDGET_SORT_BY_PULLDOWN:/* Select sorting criteria dropdown menu */
   990 				ShowDropDownMenu(this, _vehicle_sort_listing, this->vehicles.sort_type, VLW_WIDGET_SORT_BY_PULLDOWN, 0, (this->vehicle_type == VEH_TRAIN || this->vehicle_type == VEH_ROAD) ? 0 : (1 << 10));
   982 				ShowDropDownMenu(this, _vehicle_sort_listing, this->vehicles.SortType(), VLW_WIDGET_SORT_BY_PULLDOWN, 0, (this->vehicle_type == VEH_TRAIN || this->vehicle_type == VEH_ROAD) ? 0 : (1 << 10));
   991 				return;
   983 				return;
   992 			case VLW_WIDGET_LIST: { /* Matrix to show vehicles */
   984 			case VLW_WIDGET_LIST: { /* Matrix to show vehicles */
   993 				uint32 id_v = (pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / this->resize.step_height;
   985 				uint32 id_v = (pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / this->resize.step_height;
   994 				const Vehicle *v;
   986 				const Vehicle *v;
   995 
   987 
  1038 
  1030 
  1039 	virtual void OnDropdownSelect(int widget, int index)
  1031 	virtual void OnDropdownSelect(int widget, int index)
  1040 	{
  1032 	{
  1041 		switch (widget) {
  1033 		switch (widget) {
  1042 			case VLW_WIDGET_SORT_BY_PULLDOWN:
  1034 			case VLW_WIDGET_SORT_BY_PULLDOWN:
  1043 				if (this->vehicles.sort_type != index) {
  1035 				this->vehicles.SetSortType(index);
  1044 					/* value has changed -> resort */
       
  1045 					this->vehicles.flags |= VL_RESORT;
       
  1046 					this->vehicles.sort_type = index;
       
  1047 					this->sorting->criteria = this->vehicles.sort_type;
       
  1048 				}
       
  1049 				break;
  1036 				break;
  1050 			case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
  1037 			case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
  1051 				assert(this->vehicles.Length() != 0);
  1038 				assert(this->vehicles.Length() != 0);
  1052 
  1039 
  1053 				switch (index) {
  1040 				switch (index) {
  1076 	}
  1063 	}
  1077 
  1064 
  1078 	virtual void OnTick()
  1065 	virtual void OnTick()
  1079 	{
  1066 	{
  1080 		if (_pause_game != 0) return;
  1067 		if (_pause_game != 0) return;
  1081 		if (--this->vehicles.resort_timer == 0) {
  1068 		if (this->vehicles.NeedResort()) {
  1082 			StationID station = ((this->window_number & VLW_MASK) == VLW_STATION_LIST) ? GB(this->window_number, 16, 16) : INVALID_STATION;
  1069 			StationID station = ((this->window_number & VLW_MASK) == VLW_STATION_LIST) ? GB(this->window_number, 16, 16) : INVALID_STATION;
  1083 			PlayerID owner = (PlayerID)this->caption_color;
  1070 			PlayerID owner = (PlayerID)this->caption_color;
  1084 
  1071 
  1085 			DEBUG(misc, 3, "Periodic resort %d list player %d at station %d", this->vehicle_type, owner, station);
  1072 			DEBUG(misc, 3, "Periodic resort %d list player %d at station %d", this->vehicle_type, owner, station);
  1086 			this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
       
  1087 			this->vehicles.flags |= VL_RESORT;
       
  1088 			this->SetDirty();
  1073 			this->SetDirty();
  1089 		}
  1074 		}
  1090 	}
  1075 	}
  1091 
  1076 
  1092 	virtual void OnResize(Point new_size, Point delta)
  1077 	virtual void OnResize(Point new_size, Point delta)
  1095 		this->widget[VLW_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
  1080 		this->widget[VLW_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
  1096 	}
  1081 	}
  1097 
  1082 
  1098 	virtual void OnInvalidateData(int data)
  1083 	virtual void OnInvalidateData(int data)
  1099 	{
  1084 	{
  1100 		this->vehicles.flags |= (data == 0 ? VL_REBUILD : VL_RESORT);
  1085 		if (data == 0) {
       
  1086 			this->vehicles.ForceRebuild();
       
  1087 		} else {
       
  1088 			this->vehicles.ForceResort();
       
  1089 		}
  1101 	}
  1090 	}
  1102 };
  1091 };
  1103 
  1092 
  1104 static const WindowDesc _player_vehicle_list_train_desc = {
  1093 static const WindowDesc _player_vehicle_list_train_desc = {
  1105 	WDP_AUTO, WDP_AUTO, 260, 182, 260, 182,
  1094 	WDP_AUTO, WDP_AUTO, 260, 182, 260, 182,