1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
}
}