use std::{any::Any, collections::VecDeque, mem::take};
use lotus_proc_macros::Resource;
use super::ecs::entity::Entity;
#[derive(Clone, PartialEq)]
pub(crate) enum EventType {
Transform(SubEventType),
Text(SubEventType)
}
#[derive(Clone, PartialEq)]
pub(crate) enum SubEventType {
UpdatePixelatedPosition,
UpdatePixelatedScale,
UpdateTextFont,
UpdateTextPosition,
UpdateTextContent,
UpdateTextColor
}
pub(crate) struct Event {
pub(crate) entity: Entity,
pub(crate) event_type: EventType,
pub(crate) value: Box<dyn Any + Send + Sync>
}
impl Event {
pub(crate) fn new<T: Any + Send + Sync>(entity: Entity, event_type: EventType, value: T) -> Self {
return Self {
entity,
event_type,
value: Box::new(value)
};
}
pub(crate) fn get<T: Any>(&self) -> Option<&T> {
self.value.downcast_ref::<T>()
}
}
#[derive(Resource)]
pub(crate) struct EventDispatcher {
pub(crate) events: VecDeque<Event>
}
impl EventDispatcher {
pub(crate) fn new() -> Self {
return Self {
events: VecDeque::new()
};
}
pub(crate) fn send(&mut self, event: Event) {
self.events.push_front(event);
}
pub(crate) fn drain(&mut self) -> VecDeque<Event> {
return take(&mut self.events);
}
}