Add use_event_reader()

This commit is contained in:
JMS55 2023-12-27 11:56:36 -08:00
parent 01eed81bf1
commit 85709a3a92
2 changed files with 14 additions and 1 deletions

View file

@ -2,6 +2,7 @@ use crate::UiContext;
use bevy::{ use bevy::{
ecs::{ ecs::{
component::ComponentId, component::ComponentId,
event::{Event, EventIterator, Events, ManualEventReader},
query::{QueryState, ReadOnlyWorldQuery}, query::{QueryState, ReadOnlyWorldQuery},
system::{Query, Resource}, system::{Query, Resource},
world::{unsafe_world_cell::UnsafeWorldCell, World}, 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::<Events<E>>();
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> { pub struct UseQuery<'a, Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery> {
query_state: QueryState<Q, F>, query_state: QueryState<Q, F>,
world_cell: UnsafeWorldCell<'a>, world_cell: UnsafeWorldCell<'a>,

View file

@ -29,7 +29,9 @@ use dioxus::core::{Element, ElementId, Scope, VirtualDom};
pub mod prelude { pub mod prelude {
pub use super::deferred_system::use_system_scheduler; 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::elements::*;
pub use super::use_state_send::*; pub use super::use_state_send::*;
pub use super::{DioxusUiBundle, DioxusUiPlugin, DioxusUiRoot}; pub use super::{DioxusUiBundle, DioxusUiPlugin, DioxusUiRoot};