Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ecs/src/event/event_cursor.rs
6600 views
1
use bevy_ecs::event::{
2
BufferedEvent, EventIterator, EventIteratorWithId, EventMutIterator, EventMutIteratorWithId,
3
Events,
4
};
5
#[cfg(feature = "multi_threaded")]
6
use bevy_ecs::event::{EventMutParIter, EventParIter};
7
use core::marker::PhantomData;
8
9
/// Stores the state for an [`EventReader`] or [`EventMutator`].
10
///
11
/// Access to the [`Events<E>`] resource is required to read any incoming events.
12
///
13
/// In almost all cases, you should just use an [`EventReader`] or [`EventMutator`],
14
/// which will automatically manage the state for you.
15
///
16
/// However, this type can be useful if you need to manually track events,
17
/// such as when you're attempting to send and receive events of the same type in the same system.
18
///
19
/// # Example
20
///
21
/// ```
22
/// use bevy_ecs::prelude::*;
23
/// use bevy_ecs::event::{BufferedEvent, Events, EventCursor};
24
///
25
/// #[derive(BufferedEvent, Clone, Debug)]
26
/// struct MyEvent;
27
///
28
/// /// A system that both sends and receives events using a [`Local`] [`EventCursor`].
29
/// fn send_and_receive_events(
30
/// // The `Local` `SystemParam` stores state inside the system itself, rather than in the world.
31
/// // `EventCursor<T>` is the internal state of `EventMutator<T>`, which tracks which events have been seen.
32
/// mut local_event_reader: Local<EventCursor<MyEvent>>,
33
/// // We can access the `Events` resource mutably, allowing us to both read and write its contents.
34
/// mut events: ResMut<Events<MyEvent>>,
35
/// ) {
36
/// // We must collect the events to resend, because we can't mutate events while we're iterating over the events.
37
/// let mut events_to_resend = Vec::new();
38
///
39
/// for event in local_event_reader.read(&mut events) {
40
/// events_to_resend.push(event.clone());
41
/// }
42
///
43
/// for event in events_to_resend {
44
/// events.write(MyEvent);
45
/// }
46
/// }
47
///
48
/// # bevy_ecs::system::assert_is_system(send_and_receive_events);
49
/// ```
50
///
51
/// [`EventReader`]: super::EventReader
52
/// [`EventMutator`]: super::EventMutator
53
#[derive(Debug)]
54
pub struct EventCursor<E: BufferedEvent> {
55
pub(super) last_event_count: usize,
56
pub(super) _marker: PhantomData<E>,
57
}
58
59
impl<E: BufferedEvent> Default for EventCursor<E> {
60
fn default() -> Self {
61
EventCursor {
62
last_event_count: 0,
63
_marker: Default::default(),
64
}
65
}
66
}
67
68
impl<E: BufferedEvent> Clone for EventCursor<E> {
69
fn clone(&self) -> Self {
70
EventCursor {
71
last_event_count: self.last_event_count,
72
_marker: PhantomData,
73
}
74
}
75
}
76
77
impl<E: BufferedEvent> EventCursor<E> {
78
/// See [`EventReader::read`](super::EventReader::read)
79
pub fn read<'a>(&'a mut self, events: &'a Events<E>) -> EventIterator<'a, E> {
80
self.read_with_id(events).without_id()
81
}
82
83
/// See [`EventMutator::read`](super::EventMutator::read)
84
pub fn read_mut<'a>(&'a mut self, events: &'a mut Events<E>) -> EventMutIterator<'a, E> {
85
self.read_mut_with_id(events).without_id()
86
}
87
88
/// See [`EventReader::read_with_id`](super::EventReader::read_with_id)
89
pub fn read_with_id<'a>(&'a mut self, events: &'a Events<E>) -> EventIteratorWithId<'a, E> {
90
EventIteratorWithId::new(self, events)
91
}
92
93
/// See [`EventMutator::read_with_id`](super::EventMutator::read_with_id)
94
pub fn read_mut_with_id<'a>(
95
&'a mut self,
96
events: &'a mut Events<E>,
97
) -> EventMutIteratorWithId<'a, E> {
98
EventMutIteratorWithId::new(self, events)
99
}
100
101
/// See [`EventReader::par_read`](super::EventReader::par_read)
102
#[cfg(feature = "multi_threaded")]
103
pub fn par_read<'a>(&'a mut self, events: &'a Events<E>) -> EventParIter<'a, E> {
104
EventParIter::new(self, events)
105
}
106
107
/// See [`EventMutator::par_read`](super::EventMutator::par_read)
108
#[cfg(feature = "multi_threaded")]
109
pub fn par_read_mut<'a>(&'a mut self, events: &'a mut Events<E>) -> EventMutParIter<'a, E> {
110
EventMutParIter::new(self, events)
111
}
112
113
/// See [`EventReader::len`](super::EventReader::len)
114
pub fn len(&self, events: &Events<E>) -> usize {
115
// The number of events in this reader is the difference between the most recent event
116
// and the last event seen by it. This will be at most the number of events contained
117
// with the events (any others have already been dropped)
118
// TODO: Warn when there are dropped events, or return e.g. a `Result<usize, (usize, usize)>`
119
events
120
.event_count
121
.saturating_sub(self.last_event_count)
122
.min(events.len())
123
}
124
125
/// Amount of events we missed.
126
pub fn missed_events(&self, events: &Events<E>) -> usize {
127
events
128
.oldest_event_count()
129
.saturating_sub(self.last_event_count)
130
}
131
132
/// See [`EventReader::is_empty()`](super::EventReader::is_empty)
133
pub fn is_empty(&self, events: &Events<E>) -> bool {
134
self.len(events) == 0
135
}
136
137
/// See [`EventReader::clear()`](super::EventReader::clear)
138
pub fn clear(&mut self, events: &Events<E>) {
139
self.last_event_count = events.event_count;
140
}
141
}
142
143