diff --git a/src/ecs_hooks.rs b/src/ecs_hooks.rs index 56c0dbd..f6e9e67 100644 --- a/src/ecs_hooks.rs +++ b/src/ecs_hooks.rs @@ -2,6 +2,7 @@ use crate::UiContext; use bevy::{ ecs::{ component::ComponentId, + event::{Event, EventIterator, Events, ManualEventReader}, query::{QueryState, ReadOnlyWorldQuery}, system::{Query, Resource}, world::{unsafe_world_cell::UnsafeWorldCell, World}, @@ -115,6 +116,16 @@ where } } +pub fn use_event_reader<'a, E: Event>(cx: &'a ScopeState) -> EventIterator<'a, E> { + let event_reader = cx.use_hook(|| ManualEventReader::default()); + let events = EcsContext::get_world(cx).resource::>(); + let new_events = event_reader.read(events); + if new_events.len() != 0 { + (cx.schedule_update())(); + } + new_events +} + pub struct UseQuery<'a, Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery> { query_state: QueryState, world_cell: UnsafeWorldCell<'a>, diff --git a/src/lib.rs b/src/lib.rs index 470900e..82ef08e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,9 @@ use dioxus::core::{Element, ElementId, Scope, VirtualDom}; pub mod prelude { pub use super::deferred_system::use_system_scheduler; - pub use super::ecs_hooks::{use_query, use_query_filtered, use_resource, use_world}; + pub use super::ecs_hooks::{ + use_event_reader, use_query, use_query_filtered, use_resource, use_world, + }; pub use super::elements::*; pub use super::use_state_send::*; pub use super::{DioxusUiBundle, DioxusUiPlugin, DioxusUiRoot};