Skip to content

Commit 52dd58e

Browse files
Foundations for EventReader etc Query parameters
1 parent 64da9d4 commit 52dd58e

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

crates/bevy_ecs/src/query/event.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use super::{
2+
super::event::{EventReader, EventWriter, Events, ManualEventReader},
3+
Access, Fetch, FetchState, FilteredAccess, WorldQuery,
4+
};
5+
use crate::{
6+
archetype::{Archetype, ArchetypeComponentId},
7+
component::{Component, ComponentId},
8+
prelude::World,
9+
storage::{Table, Tables},
10+
};
11+
use std::marker::PhantomData;
12+
13+
impl<'a, T: Component> WorldQuery for EventWriter<'a, T> {
14+
type Fetch = EventWriterFetch<T>;
15+
type State = EventWriterState<T>;
16+
}
17+
18+
struct EventWriterFetch<T> {
19+
marker: PhantomData<T>,
20+
}
21+
22+
impl<'a, T: Component> Fetch<'a> for EventWriterFetch<T> {
23+
// EventWriter queries return an EventWriter<T> in each item
24+
type Item = EventWriter<'a, T>;
25+
// This is the corresponding S: FetchState type
26+
type State = EventWriterState<T>;
27+
28+
unsafe fn init(
29+
world: &World,
30+
state: &Self::State,
31+
last_change_tick: u32,
32+
change_tick: u32,
33+
) -> Self {
34+
}
35+
36+
fn is_dense(&self) -> bool {}
37+
38+
unsafe fn set_archetype(
39+
&mut self,
40+
state: &Self::State,
41+
archetype: &Archetype,
42+
tables: &Tables,
43+
) {
44+
}
45+
46+
unsafe fn set_table(&mut self, state: &Self::State, table: &Table) {}
47+
48+
unsafe fn archetype_fetch(&mut self, archetype_index: usize) -> Self::Item {}
49+
50+
unsafe fn table_fetch(&mut self, table_row: usize) -> Self::Item {}
51+
}
52+
struct EventWriterState<T> {
53+
marker: PhantomData<T>,
54+
}
55+
56+
unsafe impl<T: Component> FetchState for EventWriterState<T> {
57+
fn init(world: &mut World) -> Self {}
58+
59+
fn update_component_access(&self, access: &mut FilteredAccess<ComponentId>) {}
60+
61+
fn update_archetype_component_access(
62+
&self,
63+
archetype: &Archetype,
64+
access: &mut Access<ArchetypeComponentId>,
65+
) {
66+
}
67+
68+
fn matches_archetype(&self, archetype: &Archetype) -> bool {}
69+
70+
fn matches_table(&self, table: &Table) -> bool {}
71+
}

crates/bevy_ecs/src/query/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod access;
2+
mod event;
23
mod fetch;
34
mod filter;
45
mod iter;
@@ -9,6 +10,7 @@ pub use fetch::*;
910
pub use filter::*;
1011
pub use iter::*;
1112
pub use state::*;
13+
pub use state::*;
1214

1315
#[cfg(test)]
1416
mod tests {

examples/ecs/per_entity_events.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,14 @@ fn scale_selected(
194194
}
195195
}
196196

197-
// FIXME: make this work using `EventWriter<T>` syntax and specialized behavior
198197
/// Dispatches actions to entities based on the input
199198
/// Note that we can store several events at once!
200199
/// Try pressing both "1" and "3" to add 4 to the selected display
201200
fn input_dispatch(
202201
// You could also access the &Events<T> component directly
203202
// then send events to that component with `Events::send`
204203
mut query: Query<
205-
(&mut Events<CycleColorAction>, &mut Events<AddNumberAction>),
204+
(EventWriter<CycleColorAction>, EventWriter<AddNumberAction>),
206205
With<Selectable>,
207206
>,
208207
selected: Res<Selected>,
@@ -230,8 +229,7 @@ fn input_dispatch(
230229

231230
fn cycle_color(mut query: Query<(&mut ColorChoices, EventReader<CycleColorAction>)>) {
232231
for (mut color, action_queue) in query.iter_mut() {
233-
let mut reader = action_queue.get_reader();
234-
for _ in reader.iter(&action_queue) {
232+
for _ in action_queue.iter() {
235233
*color = color.next().unwrap();
236234
}
237235
}

0 commit comments

Comments
 (0)