use crate::element::ElementId;
use bevy::prelude::{Changed, Commands, Entity, Event, Query};
use bevy::ui::Interaction;
pub(crate) fn interactions(
mut commands: Commands,
entities: Query<(Entity, &Interaction, Option<&ElementId>), Changed<Interaction>>,
) {
for (e, i, id) in entities {
if matches!(i, Interaction::Pressed) {
commands.trigger(ElementClick {
entity: e,
id: id.cloned(),
});
} else if matches!(i, Interaction::Hovered) {
commands.trigger(ElementHover {
entity: e,
id: id.cloned(),
});
}
}
}
#[derive(Clone, PartialEq, Eq, Debug, Hash, Event)]
pub struct ElementClick {
pub entity: Entity,
pub id: Option<ElementId>,
}
impl ElementClick {
pub fn matches_id(&self, id: impl Into<ElementId>) -> bool {
self.id.as_ref().map(|i| *i == id.into()).unwrap_or(false)
}
}
#[derive(Clone, PartialEq, Eq, Debug, Hash, Event)]
pub struct ElementHover {
pub entity: Entity,
pub id: Option<ElementId>,
}
impl ElementHover {
pub fn matches_id(&self, id: impl Into<ElementId>) -> bool {
self.id.as_ref().map(|i| *i == id.into()).unwrap_or(false)
}
}
#[derive(Clone, PartialEq, Eq, Debug, Hash, Event)]
pub struct ElementSpawn {
pub entity: Entity,
pub id: Option<ElementId>,
}
impl ElementSpawn {
pub fn matches_id(&self, id: impl Into<ElementId>) -> bool {
self.id.as_ref().map(|i| *i == id.into()).unwrap_or(false)
}
}