veloren/voxygen/src/hud/hotbar.rs

92 lines
2.7 KiB
Rust
Raw Normal View History

2021-11-10 02:20:41 +00:00
use common::comp::slot::InvSlotId;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq)]
2020-04-11 06:33:06 +00:00
pub enum Slot {
One = 0,
Two = 1,
Three = 2,
Four = 3,
Five = 4,
Six = 5,
Seven = 6,
Eight = 7,
Nine = 8,
Ten = 9,
}
#[derive(Clone, Copy, PartialEq, Debug, Serialize, Deserialize)]
2020-04-11 06:33:06 +00:00
pub enum SlotContents {
Inventory(InvSlotId),
2021-11-10 02:20:41 +00:00
Ability(usize),
2020-04-11 06:33:06 +00:00
}
#[derive(Clone, Copy, Default)]
2020-04-11 06:33:06 +00:00
pub struct State {
pub slots: [Option<SlotContents>; 10],
2020-04-11 06:33:06 +00:00
inputs: [bool; 10],
}
impl State {
pub fn new(slots: [Option<SlotContents>; 10]) -> Self {
Self {
slots,
inputs: [false; 10],
}
}
2020-04-11 06:33:06 +00:00
2020-04-12 19:45:01 +00:00
/// Returns true if the button was just pressed
2020-04-11 06:33:06 +00:00
pub fn process_input(&mut self, slot: Slot, state: bool) -> bool {
let slot = slot as usize;
let just_pressed = !self.inputs[slot] && state;
self.inputs[slot] = state;
just_pressed
}
pub fn get(&self, slot: Slot) -> Option<SlotContents> { self.slots[slot as usize] }
pub fn swap(&mut self, a: Slot, b: Slot) { self.slots.swap(a as usize, b as usize); }
pub fn clear_slot(&mut self, slot: Slot) { self.slots[slot as usize] = None; }
pub fn add_inventory_link(&mut self, slot: Slot, inventory_pos: InvSlotId) {
self.slots[slot as usize] = Some(SlotContents::Inventory(inventory_pos));
2020-04-11 06:33:06 +00:00
}
2021-11-10 02:20:41 +00:00
// TODO: remove pending UI
// Adds ability slots if missing and should be present
// Removes ability slots if not there and shouldn't be present
pub fn maintain_abilities(&mut self, client: &client::Client) {
2020-04-11 06:33:06 +00:00
use specs::WorldExt;
2021-11-12 03:37:37 +00:00
if let Some(active_abilities) = client
.state()
.ecs()
2021-11-12 03:37:37 +00:00
.read_storage::<common::comp::ActiveAbilities>()
2021-11-10 02:20:41 +00:00
.get(client.entity())
{
2021-11-11 22:55:14 +00:00
use common::comp::ability::AuxiliaryAbility;
2021-11-12 03:37:37 +00:00
for ((i, ability), hotbar_slot) in active_abilities
2021-11-11 22:55:14 +00:00
.abilities
.iter()
.enumerate()
.zip(self.slots.iter_mut())
{
if matches!(ability, AuxiliaryAbility::Empty) {
if matches!(hotbar_slot, Some(SlotContents::Ability(_))) {
// If ability is empty but hotbar shows an ability, clear it
*hotbar_slot = None;
}
} else {
// If an ability is not empty show it on the hotbar
*hotbar_slot = Some(SlotContents::Ability(i));
2021-11-10 02:20:41 +00:00
}
}
2021-11-11 22:55:14 +00:00
} else {
2021-11-12 03:37:37 +00:00
self.slots
.iter_mut()
.filter(|slot| matches!(slot, Some(SlotContents::Ability(_))))
.for_each(|slot| *slot = None)
}
}
2020-04-11 06:33:06 +00:00
}