2023-12-07 02:46:50 +00:00
|
|
|
use crate::{
|
2023-12-16 00:20:05 +00:00
|
|
|
apply_mutations::{apply_mutations, BevyTemplate},
|
2023-12-15 20:36:53 +00:00
|
|
|
deferred_system::DeferredSystemRegistry,
|
|
|
|
|
events::EventReaders,
|
|
|
|
|
hooks::{EcsContext, EcsSubscriptions},
|
|
|
|
|
DioxusUiRoot,
|
2023-12-07 02:46:50 +00:00
|
|
|
};
|
|
|
|
|
use bevy::{
|
2023-12-07 20:25:02 +00:00
|
|
|
ecs::{
|
|
|
|
|
entity::Entity,
|
|
|
|
|
world::{Mut, World},
|
|
|
|
|
},
|
2023-12-16 00:20:05 +00:00
|
|
|
hierarchy::Parent,
|
2023-12-07 02:46:50 +00:00
|
|
|
prelude::{Deref, DerefMut},
|
2023-12-16 00:20:05 +00:00
|
|
|
utils::{hashbrown::HashMap, synccell::SyncCell, EntityHashMap},
|
2023-12-07 02:46:50 +00:00
|
|
|
};
|
2023-12-16 00:20:05 +00:00
|
|
|
use dioxus::core::{Element, ElementId, Scope, VirtualDom};
|
|
|
|
|
use std::{any::Any, mem, rc::Rc, sync::Arc};
|
2023-12-07 02:46:50 +00:00
|
|
|
|
|
|
|
|
pub fn tick_dioxus_ui(world: &mut World) {
|
2023-12-16 00:20:05 +00:00
|
|
|
run_deferred_systems(world);
|
|
|
|
|
|
2023-12-07 02:46:50 +00:00
|
|
|
let world_ptr: *mut World = world;
|
|
|
|
|
let world_cell = world.as_unsafe_world_cell();
|
2023-12-16 00:20:05 +00:00
|
|
|
|
|
|
|
|
let ui_events = unsafe {
|
2023-12-12 08:12:06 +00:00
|
|
|
world_cell
|
|
|
|
|
.get_resource_mut::<EventReaders>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.get_dioxus_events(world_cell.get_resource().unwrap())
|
|
|
|
|
};
|
2023-12-07 02:46:50 +00:00
|
|
|
|
|
|
|
|
for (root_entity, mut dioxus_ui_root) in unsafe {
|
|
|
|
|
world_cell
|
|
|
|
|
.world_mut()
|
|
|
|
|
.query::<(Entity, &mut DioxusUiRoot)>()
|
|
|
|
|
.iter_mut(world_cell.world_mut())
|
|
|
|
|
} {
|
|
|
|
|
let DioxusUiRoot {
|
|
|
|
|
virtual_dom,
|
2023-12-07 19:17:42 +00:00
|
|
|
element_id_to_bevy_ui_entity,
|
2023-12-12 08:12:06 +00:00
|
|
|
bevy_ui_entity_to_element_id,
|
2023-12-07 19:17:42 +00:00
|
|
|
templates,
|
2023-12-10 21:50:10 +00:00
|
|
|
needs_rebuild,
|
2023-12-07 02:46:50 +00:00
|
|
|
} = &mut *dioxus_ui_root;
|
|
|
|
|
let virtual_dom = virtual_dom.get();
|
|
|
|
|
|
2023-12-16 00:20:05 +00:00
|
|
|
dispatch_ui_events(
|
|
|
|
|
&ui_events,
|
|
|
|
|
bevy_ui_entity_to_element_id,
|
|
|
|
|
virtual_dom,
|
|
|
|
|
unsafe { world_cell.world() },
|
|
|
|
|
);
|
2023-12-07 20:57:34 +00:00
|
|
|
|
2023-12-16 00:20:05 +00:00
|
|
|
schedule_ui_renders_from_ecs_subscriptions(
|
|
|
|
|
virtual_dom,
|
|
|
|
|
unsafe { world_cell.get_resource::<EcsSubscriptions>().unwrap() },
|
|
|
|
|
unsafe { world_cell.world() },
|
|
|
|
|
);
|
2023-12-07 02:46:50 +00:00
|
|
|
|
2023-12-16 00:20:05 +00:00
|
|
|
render_ui(
|
|
|
|
|
virtual_dom,
|
|
|
|
|
needs_rebuild,
|
2023-12-07 19:17:42 +00:00
|
|
|
element_id_to_bevy_ui_entity,
|
2023-12-12 08:12:06 +00:00
|
|
|
bevy_ui_entity_to_element_id,
|
2023-12-07 19:17:42 +00:00
|
|
|
templates,
|
|
|
|
|
root_entity,
|
2023-12-16 00:20:05 +00:00
|
|
|
unsafe { world_cell.world_mut() },
|
|
|
|
|
world_ptr,
|
2023-12-07 19:17:42 +00:00
|
|
|
);
|
2023-12-07 02:46:50 +00:00
|
|
|
}
|
2023-12-16 00:20:05 +00:00
|
|
|
}
|
2023-12-07 02:46:50 +00:00
|
|
|
|
2023-12-16 00:20:05 +00:00
|
|
|
fn run_deferred_systems(world: &mut World) {
|
2023-12-07 20:25:02 +00:00
|
|
|
for system_id in mem::take(&mut *world.resource_mut::<DeferredSystemRegistry>().run_queue) {
|
|
|
|
|
let _ = world.run_system(system_id);
|
2023-12-07 02:46:50 +00:00
|
|
|
}
|
2023-12-07 20:25:02 +00:00
|
|
|
|
|
|
|
|
world.resource_scope(|world, mut system_registry: Mut<DeferredSystemRegistry>| {
|
|
|
|
|
system_registry.ref_counts.retain(|system_id, ref_count| {
|
|
|
|
|
let cleanup = Arc::strong_count(ref_count) == 1;
|
|
|
|
|
if cleanup {
|
|
|
|
|
world.remove_system(*system_id).unwrap();
|
|
|
|
|
}
|
|
|
|
|
!cleanup
|
|
|
|
|
});
|
|
|
|
|
});
|
2023-12-07 02:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 00:20:05 +00:00
|
|
|
fn schedule_ui_renders_from_ecs_subscriptions(
|
|
|
|
|
virtual_dom: &mut VirtualDom,
|
|
|
|
|
ecs_subscriptions: &EcsSubscriptions,
|
|
|
|
|
world: &World,
|
|
|
|
|
) {
|
|
|
|
|
let schedule_update = virtual_dom.base_scope().schedule_update_any();
|
|
|
|
|
for scope_id in &*ecs_subscriptions.world_and_queries {
|
|
|
|
|
schedule_update(*scope_id);
|
|
|
|
|
}
|
|
|
|
|
for (resource_id, scope_ids) in &*ecs_subscriptions.resources {
|
|
|
|
|
if world.is_resource_changed_by_id(*resource_id) {
|
|
|
|
|
for scope_id in scope_ids {
|
|
|
|
|
schedule_update(*scope_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dispatch_ui_events(
|
|
|
|
|
events: &Vec<(Entity, &str, Rc<dyn Any>)>,
|
|
|
|
|
bevy_ui_entity_to_element_id: &mut EntityHashMap<Entity, ElementId>,
|
|
|
|
|
virtual_dom: &mut VirtualDom,
|
|
|
|
|
world: &World,
|
|
|
|
|
) {
|
|
|
|
|
for (mut target, name, data) in events {
|
|
|
|
|
let mut target_element_id = bevy_ui_entity_to_element_id.get(&target);
|
|
|
|
|
while target_element_id.is_none() {
|
|
|
|
|
target = world.entity(target).get::<Parent>().unwrap().get();
|
|
|
|
|
target_element_id = bevy_ui_entity_to_element_id.get(&target);
|
|
|
|
|
}
|
|
|
|
|
virtual_dom.handle_event(name, Rc::clone(data), *target_element_id.unwrap(), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn render_ui(
|
|
|
|
|
virtual_dom: &mut VirtualDom,
|
|
|
|
|
needs_rebuild: &mut bool,
|
|
|
|
|
element_id_to_bevy_ui_entity: &mut HashMap<ElementId, Entity>,
|
|
|
|
|
bevy_ui_entity_to_element_id: &mut EntityHashMap<Entity, ElementId>,
|
|
|
|
|
templates: &mut HashMap<String, BevyTemplate>,
|
|
|
|
|
root_entity: Entity,
|
|
|
|
|
world: &mut World,
|
|
|
|
|
world_ptr: *mut World,
|
|
|
|
|
) {
|
|
|
|
|
virtual_dom
|
|
|
|
|
.base_scope()
|
|
|
|
|
.provide_context(EcsContext { world: world_ptr });
|
|
|
|
|
|
|
|
|
|
if *needs_rebuild {
|
|
|
|
|
apply_mutations(
|
|
|
|
|
virtual_dom.rebuild(),
|
|
|
|
|
element_id_to_bevy_ui_entity,
|
|
|
|
|
bevy_ui_entity_to_element_id,
|
|
|
|
|
templates,
|
|
|
|
|
root_entity,
|
|
|
|
|
world,
|
|
|
|
|
);
|
|
|
|
|
*needs_rebuild = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apply_mutations(
|
|
|
|
|
virtual_dom.render_immediate(),
|
|
|
|
|
element_id_to_bevy_ui_entity,
|
|
|
|
|
bevy_ui_entity_to_element_id,
|
|
|
|
|
templates,
|
|
|
|
|
root_entity,
|
|
|
|
|
world,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 02:46:50 +00:00
|
|
|
#[derive(Deref, DerefMut)]
|
|
|
|
|
pub struct VirtualDomUnsafe(pub SyncCell<VirtualDom>);
|
|
|
|
|
unsafe impl Send for VirtualDomUnsafe {}
|
|
|
|
|
|
|
|
|
|
impl VirtualDomUnsafe {
|
|
|
|
|
pub fn new(root_component: fn(Scope) -> Element) -> Self {
|
|
|
|
|
Self(SyncCell::new(VirtualDom::new(root_component)))
|
|
|
|
|
}
|
|
|
|
|
}
|