Sort items in inventory by quantity

This commit is contained in:
Hugo Famechon 2023-07-05 19:07:16 +00:00 committed by Isse
parent 32c7994cca
commit a648c68233
5 changed files with 9 additions and 1 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Esperanto translation
- Item quantity sort in player inventory.
### Changed

View File

@ -40,3 +40,4 @@ hud-bag-sort_by_name = Sort by Name
hud-bag-sort_by_quality = Sort by Quality
hud-bag-sort_by_category = Sort by Category
hud-bag-sort_by_tag = Sort by Tag
hud-bag-sort_by_quantity = Sort by Quantity

View File

@ -40,3 +40,4 @@ hud-bag-sort_by_name = Trier par Nom
hud-bag-sort_by_quality = Trier par Qualité
hud-bag-sort_by_category = Trier par Catégorie
hud-bag-sort_by_tag = Trier par Tag
hud-bag-sort_by_quantity = Trier par Quantité

View File

@ -61,6 +61,7 @@ pub enum InventorySortOrder {
Quality,
Category,
Tag,
Amount,
}
impl InventorySortOrder {
@ -69,7 +70,8 @@ impl InventorySortOrder {
InventorySortOrder::Name => InventorySortOrder::Quality,
InventorySortOrder::Quality => InventorySortOrder::Tag,
InventorySortOrder::Tag => InventorySortOrder::Category,
InventorySortOrder::Category => InventorySortOrder::Name,
InventorySortOrder::Category => InventorySortOrder::Amount,
InventorySortOrder::Amount => InventorySortOrder::Name,
}
}
}
@ -217,6 +219,8 @@ impl Inventory {
&a.tags().first().map_or("", |tag| tag.name()),
&b.tags().first().map_or("", |tag| tag.name()),
),
// Amount is sorted in reverse since we want high amounts items first
InventorySortOrder::Amount => Ord::cmp(&b.amount(), &a.amount()),
});
self.push_all(items.into_iter()).expect(

View File

@ -915,6 +915,7 @@ impl<'a> Widget for Bag<'a> {
InventorySortOrder::Quality => i18n.get_msg("hud-bag-sort_by_quality"),
InventorySortOrder::Category => i18n.get_msg("hud-bag-sort_by_category"),
InventorySortOrder::Tag => i18n.get_msg("hud-bag-sort_by_tag"),
InventorySortOrder::Amount => i18n.get_msg("hud-bag-sort_by_quantity"),
}),
"",
&tooltip,