pub trait ButtonState {
fn is_hovered(&self, button_id: &str) -> bool;
fn is_pressed(&self, button_id: &str) -> bool;
fn is_focused(&self, button_id: &str) -> bool;
fn set_hovered(&mut self, button_id: Option<&str>);
fn set_pressed(&mut self, button_id: Option<&str>);
fn set_focused(&mut self, button_id: Option<&str>);
}
#[derive(Clone, Debug, Default)]
pub struct SimpleButtonState {
pub hovered: Option<String>,
pub pressed: Option<String>,
pub focused: Option<String>,
}
impl SimpleButtonState {
pub fn new() -> Self {
Self {
hovered: None,
pressed: None,
focused: None,
}
}
}
impl ButtonState for SimpleButtonState {
fn is_hovered(&self, button_id: &str) -> bool {
self.hovered.as_deref() == Some(button_id)
}
fn is_pressed(&self, button_id: &str) -> bool {
self.pressed.as_deref() == Some(button_id)
}
fn is_focused(&self, button_id: &str) -> bool {
self.focused.as_deref() == Some(button_id)
}
fn set_hovered(&mut self, button_id: Option<&str>) {
self.hovered = button_id.map(|s| s.to_string());
}
fn set_pressed(&mut self, button_id: Option<&str>) {
self.pressed = button_id.map(|s| s.to_string());
}
fn set_focused(&mut self, button_id: Option<&str>) {
self.focused = button_id.map(|s| s.to_string());
}
}