veloren/voxygen/src/hud/slots.rs

144 lines
4.0 KiB
Rust
Raw Normal View History

use super::item_imgs::{ItemImgs, ItemKey};
2020-04-06 15:25:45 +00:00
use crate::ui::slot::{self, SlotKey, SumSlot};
2020-04-04 17:51:41 +00:00
use common::comp::{item::ItemKind, Inventory, Loadout};
use conrod_core::image;
#[derive(Clone, Copy, PartialEq)]
2020-04-06 15:25:45 +00:00
pub enum SlotKind {
Inventory(InventorySlot),
Armor(ArmorSlot),
2020-04-06 15:25:45 +00:00
/*Hotbar(HotbarSlot),
*Spellbook(SpellbookSlot), TODO */
}
2020-04-06 15:25:45 +00:00
pub type SlotManager = slot::SlotManager<SlotKind>;
#[derive(Clone, Copy, PartialEq)]
pub struct InventorySlot(pub usize);
#[derive(Clone, Copy, PartialEq)]
pub enum ArmorSlot {
2020-04-04 17:51:41 +00:00
Head,
Neck,
Shoulders,
Chest,
Hands,
2020-04-06 21:16:24 +00:00
Ring,
Lantern,
Back,
Belt,
Legs,
Feet,
Mainhand,
Offhand,
Tabard,
}
2020-04-06 15:25:45 +00:00
/*#[derive(Clone, Copy, PartialEq)]
pub enum HotbarSlot {
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
2020-04-06 15:25:45 +00:00
}*/
2020-04-06 15:25:45 +00:00
impl SlotKey<Inventory, ItemImgs> for InventorySlot {
type ImageKey = ItemKey;
2020-04-06 15:25:45 +00:00
fn image_key(&self, source: &Inventory) -> Option<Self::ImageKey> {
source.get(self.0).map(Into::into)
}
2020-04-06 15:25:45 +00:00
fn amount(&self, source: &Inventory) -> Option<u32> {
source
.get(self.0)
.and_then(|item| match item.kind {
ItemKind::Tool { .. } | ItemKind::Armor { .. } => None,
ItemKind::Utility { amount, .. }
| ItemKind::Consumable { amount, .. }
| ItemKind::Ingredient { amount, .. } => Some(amount),
})
.filter(|amount| *amount > 1)
}
2020-04-06 15:25:45 +00:00
fn image_id(key: &Self::ImageKey, source: &ItemImgs) -> image::Id {
source.img_id_or_not_found_img(key.clone())
}
2020-04-04 17:51:41 +00:00
}
2020-04-06 15:25:45 +00:00
impl SlotKey<Loadout, ItemImgs> for ArmorSlot {
2020-04-04 17:51:41 +00:00
type ImageKey = ItemKey;
2020-04-06 15:25:45 +00:00
fn image_key(&self, source: &Loadout) -> Option<Self::ImageKey> {
2020-04-04 17:51:41 +00:00
let item = match self {
ArmorSlot::Shoulders => source.shoulder.as_ref(),
ArmorSlot::Chest => source.chest.as_ref(),
ArmorSlot::Belt => source.belt.as_ref(),
ArmorSlot::Hands => source.hand.as_ref(),
ArmorSlot::Legs => source.pants.as_ref(),
ArmorSlot::Feet => source.foot.as_ref(),
2020-04-06 21:16:24 +00:00
ArmorSlot::Back => source.back.as_ref(),
ArmorSlot::Ring => source.ring.as_ref(),
ArmorSlot::Neck => source.neck.as_ref(),
ArmorSlot::Head => source.head.as_ref(),
ArmorSlot::Lantern => source.lantern.as_ref(),
ArmorSlot::Tabard => source.tabard.as_ref(),
ArmorSlot::Mainhand => source.active_item.as_ref().map(|i| &i.item),
ArmorSlot::Offhand => source.second_item.as_ref().map(|i| &i.item),
2020-04-04 17:51:41 +00:00
_ => None,
};
2020-04-04 17:51:41 +00:00
item.map(Into::into)
}
2020-04-06 15:25:45 +00:00
fn amount(&self, _: &Loadout) -> Option<u32> { None }
2020-04-04 17:51:41 +00:00
2020-04-06 15:25:45 +00:00
fn image_id(key: &Self::ImageKey, source: &ItemImgs) -> image::Id {
2020-04-04 17:51:41 +00:00
source.img_id_or_not_found_img(key.clone())
}
}
2020-04-06 15:25:45 +00:00
/*impl SlotKey<Hotbar, ItemImgs> for HotbarSlot {
type ImageKey = ItemKey;
fn image_key(&self, source: &Inventory) -> Option<Self::ImageKey> {
source.get(self.0).map(Into::into)
}
fn amount(&self, source: &Inventory) -> Option<u32> {
source
.get(self.0)
.and_then(|item| match item.kind {
ItemKind::Tool { .. } | ItemKind::Armor { .. } => None,
ItemKind::Utility { amount, .. }
| ItemKind::Consumable { amount, .. }
| ItemKind::Ingredient { amount, .. } => Some(amount),
})
.filter(|amount| *amount > 1)
}
fn image_id(key: &Self::ImageKey, source: &ItemImgs) -> image::Id {
source.img_id_or_not_found_img(key.clone())
}
}*/
impl From<InventorySlot> for SlotKind {
fn from(inventory: InventorySlot) -> Self { Self::Inventory(inventory) }
}
2020-04-06 15:25:45 +00:00
impl From<ArmorSlot> for SlotKind {
fn from(armor: ArmorSlot) -> Self { Self::Armor(armor) }
}
2020-04-06 15:25:45 +00:00
//impl From<HotbarSlot> for SlotKind {
// fn from(hotbar: HotbarSlot) -> Self { Self::Hotbar(hotbar) }
//}
2020-04-06 15:25:45 +00:00
impl SumSlot for SlotKind {}