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
|
|
|
};
|
2023-12-10 20:35:19 +00:00
|
|
|
use dioxus::core::ScopeState;
|
2023-12-07 02:46:50 +00:00
|
|
|
|
|
|
|
pub trait DioxusUiHooks {
|
|
|
|
fn use_world<'a>(&'a self) -> &'a World;
|
2023-12-07 21:51:55 +00:00
|
|
|
|
2023-12-07 02:46:50 +00:00
|
|
|
fn use_resource<'a, T: Resource>(&'a self) -> &'a T;
|
2023-12-07 21:51:55 +00:00
|
|
|
|
2023-12-07 21:54:11 +00:00
|
|
|
fn use_query<'a, Q>(&'a self) -> DioxusUiQuery<'a, Q, ()>
|
2023-12-07 21:51:55 +00:00
|
|
|
where
|
2023-12-10 23:44:27 +00:00
|
|
|
Q: ReadOnlyWorldQuery;
|
2023-12-07 21:51:55 +00:00
|
|
|
|
2023-12-07 21:54:11 +00:00
|
|
|
fn use_query_filtered<'a, Q, F>(&'a self) -> DioxusUiQuery<'a, Q, F>
|
2023-12-07 21:51:55 +00:00
|
|
|
where
|
2023-12-10 23:44:27 +00:00
|
|
|
Q: ReadOnlyWorldQuery,
|
|
|
|
F: ReadOnlyWorldQuery;
|
2023-12-07 21:51:55 +00:00
|
|
|
|
2023-12-07 02:46:50 +00:00
|
|
|
fn use_system<S>(&self, system: S) -> DeferredSystem
|
|
|
|
where
|
|
|
|
S: IntoSystem<(), (), ()> + 'static;
|
|
|
|
}
|
|
|
|
|
2023-12-09 18:58:59 +00:00
|
|
|
// TODO: Hooks need to schedule future updates
|
2023-12-07 02:46:50 +00:00
|
|
|
impl DioxusUiHooks for ScopeState {
|
|
|
|
fn use_world<'a>(&'a self) -> &'a World {
|
|
|
|
EcsContext::get_world(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn use_resource<'a, T: Resource>(&'a self) -> &'a T {
|
|
|
|
EcsContext::get_world(self).resource()
|
|
|
|
}
|
|
|
|
|
2023-12-07 21:54:11 +00:00
|
|
|
fn use_query<'a, Q>(&'a self) -> DioxusUiQuery<'a, Q, ()>
|
2023-12-07 21:51:55 +00:00
|
|
|
where
|
2023-12-10 23:44:27 +00:00
|
|
|
Q: ReadOnlyWorldQuery,
|
2023-12-07 21:51:55 +00:00
|
|
|
{
|
|
|
|
Self::use_query_filtered(self)
|
|
|
|
}
|
|
|
|
|
2023-12-07 21:54:11 +00:00
|
|
|
fn use_query_filtered<'a, Q, F>(&'a self) -> DioxusUiQuery<'a, Q, F>
|
2023-12-07 21:51:55 +00:00
|
|
|
where
|
2023-12-10 23:44:27 +00:00
|
|
|
Q: ReadOnlyWorldQuery,
|
|
|
|
F: ReadOnlyWorldQuery,
|
2023-12-07 21:51:55 +00:00
|
|
|
{
|
|
|
|
let world = EcsContext::get_world(self);
|
|
|
|
DioxusUiQuery {
|
|
|
|
query_state: QueryState::new(world),
|
|
|
|
world_cell: world.as_unsafe_world_cell(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 02:46:50 +00:00
|
|
|
fn use_system<S>(&self, system: S) -> DeferredSystem
|
|
|
|
where
|
|
|
|
S: IntoSystem<(), (), ()> + 'static,
|
|
|
|
{
|
2023-12-07 20:30:33 +00:00
|
|
|
self.use_hook(|| new_deferred_system(system, EcsContext::get_world(self)))
|
2023-12-07 20:25:02 +00:00
|
|
|
.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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|