use crate::element::ElementId;
use bevy::prelude::{Entity, Event};
use std::fmt::{Debug, Formatter};
use std::hash::Hash;
#[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)
}
}
#[derive(Clone, PartialEq, Eq, Debug, Hash, Event)]
pub struct ElementToggle {
pub entity: Entity,
pub id: Option<ElementId>,
pub state: bool,
}
impl ElementToggle {
pub fn matches_id(&self, id: impl Into<ElementId>) -> bool {
self.id.as_ref().map(|i| *i == id.into()).unwrap_or(false)
}
}
#[derive(Event)]
pub struct ElementSet<T> {
pub entity: Entity,
pub id: Option<ElementId>,
pub value: T,
pub delta: Option<T>,
}
impl<T> ElementSet<T> {
pub fn matches_id(&self, id: impl Into<ElementId>) -> bool {
self.id.as_ref().map(|i| *i == id.into()).unwrap_or(false)
}
}
impl<T: Clone> Clone for ElementSet<T> {
fn clone(&self) -> Self {
Self {
entity: self.entity,
id: self.id.clone(),
value: self.value.clone(),
delta: self.delta.clone(),
}
}
}
impl<T: Debug> Debug for ElementSet<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ElementSet")
.field("entity", &self.entity)
.field("id", &self.id)
.field("value", &self.value)
.field("delta", &self.delta)
.finish()
}
}
impl<T: Hash> Hash for ElementSet<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.entity.hash(state);
self.id.hash(state);
self.value.hash(state);
self.delta.hash(state);
}
}
impl<T: PartialEq> PartialEq for ElementSet<T> {
fn eq(&self, other: &Self) -> bool {
self.entity == other.entity
&& self.id == other.id
&& self.value == other.value
&& self.delta == other.delta
}
}
impl<T: Eq> Eq for ElementSet<T> {}