use std::ops::{Deref, DerefMut};
use shrev::{EventChannel, ReaderId};
use crate::{
join::Join,
storage::{MaskedStorage, Storage},
world::{Component, Index},
};
pub trait Tracked {
fn channel(&self) -> &EventChannel<ComponentEvent>;
fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent>;
#[cfg(feature = "storage-event-control")]
fn set_event_emission(&mut self, emit: bool);
#[cfg(feature = "storage-event-control")]
fn event_emission(&self) -> bool;
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ComponentEvent {
Inserted(Index),
Modified(Index),
Removed(Index),
}
impl<'e, T, D> Storage<'e, T, D>
where
T: Component,
T::Storage: Tracked,
D: Deref<Target = MaskedStorage<T>>,
{
pub fn channel(&self) -> &EventChannel<ComponentEvent> {
unsafe { self.open() }.1.channel()
}
#[cfg(feature = "storage-event-control")]
pub fn event_emission(&self) -> bool {
unsafe { self.open() }.1.event_emission()
}
}
impl<'e, T, D> Storage<'e, T, D>
where
T: Component,
T::Storage: Tracked,
D: DerefMut<Target = MaskedStorage<T>>,
{
pub fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent> {
unsafe { self.open() }.1.channel_mut()
}
pub fn register_reader(&mut self) -> ReaderId<ComponentEvent> {
self.channel_mut().register_reader()
}
pub fn flag(&mut self, event: ComponentEvent) {
self.channel_mut().single_write(event);
}
#[cfg(feature = "storage-event-control")]
pub fn set_event_emission(&mut self, emit: bool) {
unsafe { self.open() }.1.set_event_emission(emit);
}
}