use crate::prelude::*;
use std::collections::HashSet;
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub(crate) struct KeyState {
pub(crate) entered: Option<Key>,
pub(crate) typed: Option<String>,
pub(crate) pressed: HashSet<Key>,
pub(crate) keymod: KeyMod,
}
impl KeyState {
#[inline]
pub(crate) fn is_pressed(&self) -> bool {
!self.pressed.is_empty()
}
#[inline]
pub(crate) const fn pressed(&self) -> &HashSet<Key> {
&self.pressed
}
#[inline]
pub(crate) fn is_down(&self, key: Key) -> bool {
self.pressed.contains(&key)
}
#[inline]
pub(crate) fn was_entered(&self, key: Key) -> bool {
matches!(self.entered, Some(k) if k == key)
}
#[inline]
pub(crate) const fn mod_down(&self, keymod: KeyMod) -> bool {
self.keymod.intersects(keymod)
}
#[inline]
pub(crate) const fn keymod(&self) -> &KeyMod {
&self.keymod
}
#[inline]
pub(crate) fn press(&mut self, key: Key, keymod: KeyMod) {
self.entered = Some(key);
self.pressed.insert(key);
self.keymod = keymod;
}
#[inline]
pub(crate) fn release(&mut self, key: Key, keymod: KeyMod) {
self.pressed.remove(&key);
self.keymod = keymod;
}
#[inline]
pub(crate) fn typed(&mut self, text: String) {
self.typed = Some(text);
}
}