WIP apply_mutations
This commit is contained in:
parent
4ff1563941
commit
939c12d78f
|
@ -1,6 +1,71 @@
|
||||||
use bevy::ecs::{entity::Entity, world::World};
|
use crate::bsn::Bsn;
|
||||||
use dioxus_core::Mutations;
|
use bevy::{
|
||||||
|
ecs::{entity::Entity, world::World},
|
||||||
|
hierarchy::{BuildWorldChildren, DespawnRecursiveExt},
|
||||||
|
text::Text,
|
||||||
|
ui::node_bundles::TextBundle,
|
||||||
|
utils::HashMap,
|
||||||
|
};
|
||||||
|
use dioxus_core::{ElementId, Mutation, Mutations};
|
||||||
|
|
||||||
pub fn apply_mutations(mutations: Mutations, root_entity: Entity, world: &mut World) {
|
pub fn apply_mutations(
|
||||||
todo!("Modify bevy_ui entities based on mutations");
|
mutations: Mutations,
|
||||||
|
element_id_to_bevy_ui_entity: &mut HashMap<ElementId, Entity>,
|
||||||
|
templates: &mut HashMap<String, Bsn>,
|
||||||
|
root_entity: Entity,
|
||||||
|
world: &mut World,
|
||||||
|
) {
|
||||||
|
for new_template in mutations.templates {
|
||||||
|
templates.insert(new_template.name.to_owned(), todo!());
|
||||||
|
}
|
||||||
|
|
||||||
|
let map = element_id_to_bevy_ui_entity;
|
||||||
|
map.insert(ElementId(0), root_entity);
|
||||||
|
|
||||||
|
let mut stack = vec![root_entity];
|
||||||
|
for edit in mutations.edits {
|
||||||
|
match edit {
|
||||||
|
Mutation::AppendChildren { id, m } => {
|
||||||
|
let mut parent = world.entity_mut(map[&id]);
|
||||||
|
for _ in 0..m {
|
||||||
|
parent.add_child(stack.pop().unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mutation::AssignId { path, id } => todo!(),
|
||||||
|
Mutation::CreatePlaceholder { id } => {
|
||||||
|
map.insert(id, world.spawn(()).id());
|
||||||
|
}
|
||||||
|
Mutation::CreateTextNode { value, id } => {
|
||||||
|
map.insert(id, world.spawn(TextBundle::from(value)).id());
|
||||||
|
}
|
||||||
|
Mutation::HydrateText { path, value, id } => todo!(),
|
||||||
|
Mutation::LoadTemplate { name, index, id } => todo!(),
|
||||||
|
Mutation::ReplaceWith { id, m } => todo!(),
|
||||||
|
Mutation::ReplacePlaceholder { path, m } => todo!(),
|
||||||
|
Mutation::InsertAfter { id, m } => todo!(),
|
||||||
|
Mutation::InsertBefore { id, m } => todo!(),
|
||||||
|
Mutation::SetAttribute {
|
||||||
|
name,
|
||||||
|
value,
|
||||||
|
id,
|
||||||
|
ns,
|
||||||
|
} => todo!(),
|
||||||
|
Mutation::SetText { value, id } => {
|
||||||
|
world
|
||||||
|
.entity_mut(map[&id])
|
||||||
|
.get_mut::<Text>()
|
||||||
|
.unwrap()
|
||||||
|
.sections[0]
|
||||||
|
.value = value.to_owned();
|
||||||
|
}
|
||||||
|
Mutation::NewEventListener { name, id } => todo!(),
|
||||||
|
Mutation::RemoveEventListener { name, id } => todo!(),
|
||||||
|
Mutation::Remove { id } => {
|
||||||
|
world
|
||||||
|
.entity_mut(map.remove(&id).unwrap())
|
||||||
|
.despawn_recursive();
|
||||||
|
}
|
||||||
|
Mutation::PushRoot { id } => stack.push(map[&id]),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
1
src/bsn.rs
Normal file
1
src/bsn.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub struct Bsn {}
|
10
src/lib.rs
10
src/lib.rs
|
@ -1,16 +1,20 @@
|
||||||
mod apply_mutations;
|
mod apply_mutations;
|
||||||
|
mod bsn;
|
||||||
mod deferred_system;
|
mod deferred_system;
|
||||||
mod hooks;
|
mod hooks;
|
||||||
mod tick;
|
mod tick;
|
||||||
|
|
||||||
use self::{
|
use self::{
|
||||||
|
bsn::Bsn,
|
||||||
deferred_system::DeferredSystemRunQueue,
|
deferred_system::DeferredSystemRunQueue,
|
||||||
tick::{tick_dioxus_ui, VirtualDomUnsafe},
|
tick::{tick_dioxus_ui, VirtualDomUnsafe},
|
||||||
};
|
};
|
||||||
use bevy::{
|
use bevy::{
|
||||||
app::{App, Plugin, Update},
|
app::{App, Plugin, Update},
|
||||||
ecs::component::Component,
|
ecs::{component::Component, entity::Entity},
|
||||||
|
utils::HashMap,
|
||||||
};
|
};
|
||||||
|
use dioxus_core::ElementId;
|
||||||
|
|
||||||
pub use self::hooks::DioxusUiHooks;
|
pub use self::hooks::DioxusUiHooks;
|
||||||
pub use dioxus_core::{Element, Scope};
|
pub use dioxus_core::{Element, Scope};
|
||||||
|
@ -27,6 +31,8 @@ impl Plugin for DioxusUiPlugin {
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct DioxusUiRoot {
|
pub struct DioxusUiRoot {
|
||||||
virtual_dom: VirtualDomUnsafe,
|
virtual_dom: VirtualDomUnsafe,
|
||||||
|
element_id_to_bevy_ui_entity: HashMap<ElementId, Entity>,
|
||||||
|
templates: HashMap<String, Bsn>,
|
||||||
needs_rebuild: bool,
|
needs_rebuild: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +40,8 @@ impl DioxusUiRoot {
|
||||||
pub fn new(root_component: fn(Scope) -> Element) -> Self {
|
pub fn new(root_component: fn(Scope) -> Element) -> Self {
|
||||||
Self {
|
Self {
|
||||||
virtual_dom: VirtualDomUnsafe::new(root_component),
|
virtual_dom: VirtualDomUnsafe::new(root_component),
|
||||||
|
element_id_to_bevy_ui_entity: HashMap::new(),
|
||||||
|
templates: HashMap::new(),
|
||||||
needs_rebuild: true,
|
needs_rebuild: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
22
src/tick.rs
22
src/tick.rs
|
@ -22,6 +22,8 @@ pub fn tick_dioxus_ui(world: &mut World) {
|
||||||
let DioxusUiRoot {
|
let DioxusUiRoot {
|
||||||
virtual_dom,
|
virtual_dom,
|
||||||
needs_rebuild,
|
needs_rebuild,
|
||||||
|
element_id_to_bevy_ui_entity,
|
||||||
|
templates,
|
||||||
} = &mut *dioxus_ui_root;
|
} = &mut *dioxus_ui_root;
|
||||||
let virtual_dom = virtual_dom.get();
|
let virtual_dom = virtual_dom.get();
|
||||||
|
|
||||||
|
@ -30,18 +32,26 @@ pub fn tick_dioxus_ui(world: &mut World) {
|
||||||
.provide_context(EcsContext { world: world_ptr });
|
.provide_context(EcsContext { world: world_ptr });
|
||||||
|
|
||||||
if *needs_rebuild {
|
if *needs_rebuild {
|
||||||
apply_mutations(virtual_dom.rebuild(), root_entity, unsafe {
|
apply_mutations(
|
||||||
world_cell.world_mut()
|
virtual_dom.rebuild(),
|
||||||
});
|
element_id_to_bevy_ui_entity,
|
||||||
|
templates,
|
||||||
|
root_entity,
|
||||||
|
unsafe { world_cell.world_mut() },
|
||||||
|
);
|
||||||
*needs_rebuild = false;
|
*needs_rebuild = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Handle events from winit
|
// TODO: Handle events from winit
|
||||||
// virtual_dom.handle_event(todo!(), todo!(), todo!(), todo!());
|
// virtual_dom.handle_event(todo!(), todo!(), todo!(), todo!());
|
||||||
|
|
||||||
apply_mutations(virtual_dom.render_immediate(), root_entity, unsafe {
|
apply_mutations(
|
||||||
world_cell.world_mut()
|
virtual_dom.render_immediate(),
|
||||||
});
|
element_id_to_bevy_ui_entity,
|
||||||
|
templates,
|
||||||
|
root_entity,
|
||||||
|
unsafe { world_cell.world_mut() },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for system_id in mem::take(&mut world.resource_mut::<DeferredSystemRunQueue>().0) {
|
for system_id in mem::take(&mut world.resource_mut::<DeferredSystemRunQueue>().0) {
|
||||||
|
|
Loading…
Reference in a new issue