(svn r13534) -Codechange: Replace the main part of VehiclesListBase sorting with GUIList function calls

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
skidd13 17 years ago
parent 6b8a758339
commit 0a19f738bc

@ -205,9 +205,9 @@ public:
case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break; case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
} }
this->vehicles.sort_type = this->sorting->criteria; this->vehicles.SetListing(*this->sorting);
this->vehicles.flags = VL_REBUILD | (this->sorting->order ? VL_DESC : VL_NONE); this->vehicles.ForceRebuild();
this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer this->vehicles.NeedResort();
this->groups.ForceRebuild(); this->groups.ForceRebuild();
this->groups.NeedResort(); this->groups.NeedResort();
@ -260,15 +260,16 @@ public:
~VehicleGroupWindow() ~VehicleGroupWindow()
{ {
*this->sorting = this->vehicles.GetListing();
} }
virtual void OnInvalidateData(int data) virtual void OnInvalidateData(int data)
{ {
this->vehicles.flags |= (data == 0 ? VL_REBUILD : VL_RESORT);
if (data == 0) { if (data == 0) {
this->vehicles.ForceRebuild();
this->groups.ForceRebuild(); this->groups.ForceRebuild();
} else { } else {
this->vehicles.ForceResort();
this->groups.ForceResort(); this->groups.ForceResort();
} }
@ -384,7 +385,7 @@ public:
} }
/* Set text of sort by dropdown */ /* Set text of sort by dropdown */
this->widget[GRP_WIDGET_SORT_BY_DROPDOWN].data = _vehicle_sort_listing[this->vehicles.sort_type]; this->widget[GRP_WIDGET_SORT_BY_DROPDOWN].data = _vehicle_sort_listing[this->vehicles.SortType()];
this->DrawWidgets(); this->DrawWidgets();
@ -434,7 +435,7 @@ public:
DrawStringRightAligned(187, y1 + 1, STR_GROUP_TINY_NUM, (this->group_sel == g->index) ? TC_WHITE : TC_BLACK); DrawStringRightAligned(187, y1 + 1, STR_GROUP_TINY_NUM, (this->group_sel == g->index) ? TC_WHITE : TC_BLACK);
} }
this->DrawSortButtonState(GRP_WIDGET_SORT_BY_ORDER, this->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP); this->DrawSortButtonState(GRP_WIDGET_SORT_BY_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
int list_width = this->widget[GRP_WIDGET_LIST_VEHICLE].right - this->widget[GRP_WIDGET_LIST_VEHICLE].left - 20; int list_width = this->widget[GRP_WIDGET_LIST_VEHICLE].right - this->widget[GRP_WIDGET_LIST_VEHICLE].left - 20;
@ -472,21 +473,18 @@ public:
switch(widget) { switch(widget) {
case GRP_WIDGET_SORT_BY_ORDER: // Flip sorting method ascending/descending case GRP_WIDGET_SORT_BY_ORDER: // Flip sorting method ascending/descending
this->vehicles.flags ^= VL_DESC; this->vehicles.ToggleSortOrder();
this->vehicles.flags |= VL_RESORT;
this->sorting->order = !!(this->vehicles.flags & VL_DESC);
this->SetDirty(); this->SetDirty();
break; break;
case GRP_WIDGET_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu case GRP_WIDGET_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
ShowDropDownMenu(this, _vehicle_sort_listing, this->vehicles.sort_type, GRP_WIDGET_SORT_BY_DROPDOWN, 0, (this->vehicle_type == VEH_TRAIN || this->vehicle_type == VEH_ROAD) ? 0 : (1 << 10)); ShowDropDownMenu(this, _vehicle_sort_listing, this->vehicles.SortType(), GRP_WIDGET_SORT_BY_DROPDOWN, 0, (this->vehicle_type == VEH_TRAIN || this->vehicle_type == VEH_ROAD) ? 0 : (1 << 10));
return; return;
case GRP_WIDGET_ALL_VEHICLES: // All vehicles button case GRP_WIDGET_ALL_VEHICLES: // All vehicles button
if (!IsAllGroupID(this->group_sel)) { if (!IsAllGroupID(this->group_sel)) {
this->group_sel = ALL_GROUP; this->group_sel = ALL_GROUP;
this->vehicles.flags |= VL_REBUILD; this->vehicles.ForceRebuild();
this->SetDirty(); this->SetDirty();
} }
break; break;
@ -494,7 +492,7 @@ public:
case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles button case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles button
if (!IsDefaultGroupID(this->group_sel)) { if (!IsDefaultGroupID(this->group_sel)) {
this->group_sel = DEFAULT_GROUP; this->group_sel = DEFAULT_GROUP;
this->vehicles.flags |= VL_REBUILD; this->vehicles.ForceRebuild();
this->SetDirty(); this->SetDirty();
} }
break; break;
@ -510,7 +508,7 @@ public:
this->group_sel = this->groups[id_g]->index;; this->group_sel = this->groups[id_g]->index;;
this->vehicles.flags |= VL_REBUILD; this->vehicles.ForceRebuild();
this->SetDirty(); this->SetDirty();
break; break;
} }
@ -669,11 +667,7 @@ public:
{ {
switch (widget) { switch (widget) {
case GRP_WIDGET_SORT_BY_DROPDOWN: case GRP_WIDGET_SORT_BY_DROPDOWN:
if (this->vehicles.sort_type != index) { this->vehicles.SetSortType(index);
this->vehicles.flags |= VL_RESORT;
this->vehicles.sort_type = index;
this->sorting->criteria = this->vehicles.sort_type;
}
break; break;
case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN: case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN:
@ -715,12 +709,7 @@ public:
virtual void OnTick() virtual void OnTick()
{ {
if (_pause_game != 0) return; if (_pause_game != 0) return;
if (--this->vehicles.resort_timer == 0) { if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
this->vehicles.flags |= VL_RESORT;
this->SetDirty();
}
if (this->groups.NeedResort()) {
this->SetDirty(); this->SetDirty();
} }
} }

@ -90,14 +90,13 @@ const StringID _vehicle_sort_listing[] = {
void BuildVehicleList(VehicleListBase *vl, PlayerID owner, uint16 index, uint16 window_type) void BuildVehicleList(VehicleListBase *vl, PlayerID owner, uint16 index, uint16 window_type)
{ {
if (!(vl->vehicles.flags & VL_REBUILD)) return; if (!vl->vehicles.NeedRebuild()) return;
DEBUG(misc, 3, "Building vehicle list for player %d at station %d", owner, index); DEBUG(misc, 3, "Building vehicle list for player %d at station %d", owner, index);
GenerateVehicleSortList(&vl->vehicles, vl->vehicle_type, owner, index, window_type); GenerateVehicleSortList(&vl->vehicles, vl->vehicle_type, owner, index, window_type);
vl->vehicles.flags &= ~VL_REBUILD; vl->vehicles.RebuildDone();
vl->vehicles.flags |= VL_RESORT;
} }
/* cached values for VehicleNameSorter to spare many GetString() calls */ /* cached values for VehicleNameSorter to spare many GetString() calls */
@ -105,15 +104,10 @@ static const Vehicle *_last_vehicle[2] = { NULL, NULL };
void SortVehicleList(VehicleListBase *vl) void SortVehicleList(VehicleListBase *vl)
{ {
if (!(vl->vehicles.flags & VL_RESORT)) return; if (vl->vehicles.Sort(_vehicle_sorter[vl->vehicles.SortType()])) return;
/* invalidate cached values for name sorter - vehicle names could change */ /* invalidate cached values for name sorter - vehicle names could change */
_last_vehicle[0] = _last_vehicle[1] = NULL; _last_vehicle[0] = _last_vehicle[1] = NULL;
QSortT(vl->vehicles.Begin(), vl->vehicles.Length(), _vehicle_sorter[vl->vehicles.sort_type], vl->vehicles.flags & VL_DESC);
vl->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
vl->vehicles.flags &= ~VL_RESORT;
} }
void DepotSortList(VehicleList *list) void DepotSortList(VehicleList *list)
@ -865,15 +859,16 @@ struct VehicleListWindow : public Window, public VehicleListBase {
default: NOT_REACHED(); break; default: NOT_REACHED(); break;
} }
this->vehicles.flags = VL_REBUILD | (this->sorting->order ? VL_DESC : VL_NONE); this->vehicles.SetListing(*this->sorting);
this->vehicles.sort_type = this->sorting->criteria; this->vehicles.ForceRebuild();
this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS; // Set up resort timer this->vehicles.NeedResort();
this->FindWindowPlacementAndResize(desc); this->FindWindowPlacementAndResize(desc);
} }
~VehicleListWindow() ~VehicleListWindow()
{ {
*this->sorting = this->vehicles.GetListing();
} }
virtual void OnPaint() virtual void OnPaint()
@ -938,9 +933,9 @@ struct VehicleListWindow : public Window, public VehicleListBase {
this->DrawWidgets(); this->DrawWidgets();
/* draw sorting criteria string */ /* draw sorting criteria string */
DrawString(85, 15, _vehicle_sort_listing[this->vehicles.sort_type], TC_BLACK); DrawString(85, 15, _vehicle_sort_listing[this->vehicles.SortType()], TC_BLACK);
/* draw arrow pointing up/down for ascending/descending sorting */ /* draw arrow pointing up/down for ascending/descending sorting */
this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.flags & VL_DESC ? SBS_DOWN : SBS_UP); this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.Length()); max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.Length());
for (i = this->vscroll.pos; i < max; ++i) { for (i = this->vscroll.pos; i < max; ++i) {
@ -980,14 +975,11 @@ struct VehicleListWindow : public Window, public VehicleListBase {
{ {
switch (widget) { switch (widget) {
case VLW_WIDGET_SORT_ORDER: /* Flip sorting method ascending/descending */ case VLW_WIDGET_SORT_ORDER: /* Flip sorting method ascending/descending */
this->vehicles.flags ^= VL_DESC; this->vehicles.ToggleSortOrder();
this->vehicles.flags |= VL_RESORT;
this->sorting->order = !!(this->vehicles.flags & VL_DESC);
this->SetDirty(); this->SetDirty();
break; break;
case VLW_WIDGET_SORT_BY_PULLDOWN:/* Select sorting criteria dropdown menu */ case VLW_WIDGET_SORT_BY_PULLDOWN:/* Select sorting criteria dropdown menu */
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)); 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));
return; return;
case VLW_WIDGET_LIST: { /* Matrix to show vehicles */ case VLW_WIDGET_LIST: { /* Matrix to show vehicles */
uint32 id_v = (pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / this->resize.step_height; uint32 id_v = (pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / this->resize.step_height;
@ -1040,12 +1032,7 @@ struct VehicleListWindow : public Window, public VehicleListBase {
{ {
switch (widget) { switch (widget) {
case VLW_WIDGET_SORT_BY_PULLDOWN: case VLW_WIDGET_SORT_BY_PULLDOWN:
if (this->vehicles.sort_type != index) { this->vehicles.SetSortType(index);
/* value has changed -> resort */
this->vehicles.flags |= VL_RESORT;
this->vehicles.sort_type = index;
this->sorting->criteria = this->vehicles.sort_type;
}
break; break;
case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN: case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
assert(this->vehicles.Length() != 0); assert(this->vehicles.Length() != 0);
@ -1078,13 +1065,11 @@ struct VehicleListWindow : public Window, public VehicleListBase {
virtual void OnTick() virtual void OnTick()
{ {
if (_pause_game != 0) return; if (_pause_game != 0) return;
if (--this->vehicles.resort_timer == 0) { if (this->vehicles.NeedResort()) {
StationID station = ((this->window_number & VLW_MASK) == VLW_STATION_LIST) ? GB(this->window_number, 16, 16) : INVALID_STATION; StationID station = ((this->window_number & VLW_MASK) == VLW_STATION_LIST) ? GB(this->window_number, 16, 16) : INVALID_STATION;
PlayerID owner = (PlayerID)this->caption_color; PlayerID owner = (PlayerID)this->caption_color;
DEBUG(misc, 3, "Periodic resort %d list player %d at station %d", this->vehicle_type, owner, station); DEBUG(misc, 3, "Periodic resort %d list player %d at station %d", this->vehicle_type, owner, station);
this->vehicles.resort_timer = DAY_TICKS * PERIODIC_RESORT_DAYS;
this->vehicles.flags |= VL_RESORT;
this->SetDirty(); this->SetDirty();
} }
} }
@ -1097,7 +1082,11 @@ struct VehicleListWindow : public Window, public VehicleListBase {
virtual void OnInvalidateData(int data) virtual void OnInvalidateData(int data)
{ {
this->vehicles.flags |= (data == 0 ? VL_REBUILD : VL_RESORT); if (data == 0) {
this->vehicles.ForceRebuild();
} else {
this->vehicles.ForceResort();
}
} }
}; };

Loading…
Cancel
Save