bevy_dioxus/src/hooks.rs

71 lines
1.7 KiB
Rust
Raw Normal View History

2023-12-07 20:30:33 +00:00
use crate::{
deferred_system::{new_deferred_system, DeferredSystem},
tick::EcsContext,
};
2023-12-07 02:46:50 +00:00
use bevy::ecs::{
2023-12-10 23:44:27 +00:00
query::{QueryState, ReadOnlyWorldQuery},
2023-12-07 21:51:55 +00:00
system::{IntoSystem, Query, Resource},
world::{unsafe_world_cell::UnsafeWorldCell, World},
2023-12-07 02:46:50 +00:00
};
use dioxus::core::ScopeState;
2023-12-07 02:46:50 +00:00
2023-12-10 23:53:50 +00:00
// TODO: Hooks need to schedule future updates
2023-12-07 21:51:55 +00:00
2023-12-10 23:53:50 +00:00
pub fn use_world<'a>(cx: &'a ScopeState) -> &'a World {
EcsContext::get_world(cx)
2023-12-07 02:46:50 +00:00
}
2023-12-10 23:53:50 +00:00
pub fn use_resource<'a, T: Resource>(cx: &'a ScopeState) -> &'a T {
EcsContext::get_world(cx).resource()
}
2023-12-07 02:46:50 +00:00
2023-12-10 23:53:50 +00:00
pub fn use_query<'a, Q>(cx: &'a ScopeState) -> DioxusUiQuery<'a, Q, ()>
where
Q: ReadOnlyWorldQuery,
{
use_query_filtered(cx)
}
2023-12-07 21:51:55 +00:00
2023-12-10 23:53:50 +00:00
pub fn use_query_filtered<'a, Q, F>(cx: &'a ScopeState) -> DioxusUiQuery<'a, Q, F>
where
Q: ReadOnlyWorldQuery,
F: ReadOnlyWorldQuery,
{
let world = EcsContext::get_world(cx);
DioxusUiQuery {
query_state: QueryState::new(world),
world_cell: world.as_unsafe_world_cell(),
2023-12-07 21:51:55 +00:00
}
2023-12-10 23:53:50 +00:00
}
2023-12-07 21:51:55 +00:00
2023-12-10 23:53:50 +00:00
pub fn use_system<S>(cx: &ScopeState, system: S) -> DeferredSystem
where
S: IntoSystem<(), (), ()> + 'static,
{
cx.use_hook(|| new_deferred_system(system, EcsContext::get_world(cx)))
.0
2023-12-07 02:46:50 +00:00
}
2023-12-10 23:44:27 +00:00
pub struct DioxusUiQuery<'a, Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery> {
2023-12-07 21:51:55 +00:00
query_state: QueryState<Q, F>,
world_cell: UnsafeWorldCell<'a>,
}
impl<'a, Q, F> DioxusUiQuery<'a, Q, F>
where
2023-12-10 23:44:27 +00:00
Q: ReadOnlyWorldQuery,
F: ReadOnlyWorldQuery,
2023-12-07 21:51:55 +00:00
{
pub fn query(&self) -> Query<Q, F> {
unsafe {
Query::new(
self.world_cell,
&self.query_state,
self.world_cell.last_change_tick(),
self.world_cell.change_tick(),
true,
)
}
}
}