bevy_dioxus/src/lib.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2023-12-07 02:46:50 +00:00
mod apply_mutations;
2023-12-13 03:47:14 +00:00
pub mod colors;
2023-12-07 02:46:50 +00:00
mod deferred_system;
2023-12-12 08:12:06 +00:00
mod events;
2023-12-10 23:53:50 +00:00
pub mod hooks;
2023-12-07 02:46:50 +00:00
mod tick;
2023-12-06 05:24:51 +00:00
2023-12-07 02:46:50 +00:00
use self::{
2023-12-16 20:41:32 +00:00
apply_mutations::BevyTemplate, deferred_system::DeferredSystemRegistry, events::EventReaders,
hooks::EcsSubscriptions, tick::tick_dioxus_ui,
2023-12-07 02:46:50 +00:00
};
2023-12-06 05:24:51 +00:00
use bevy::{
app::{App, Plugin, Update},
ecs::{bundle::Bundle, component::Component, entity::Entity},
2023-12-16 20:41:32 +00:00
prelude::{Deref, DerefMut},
ui::node_bundles::NodeBundle,
2023-12-12 08:12:06 +00:00
utils::{EntityHashMap, HashMap},
2023-12-06 05:24:51 +00:00
};
2023-12-16 20:41:32 +00:00
use dioxus::core::{Element, ElementId, Scope, VirtualDom};
2023-12-06 05:24:51 +00:00
2023-12-10 23:44:27 +00:00
pub use bevy_mod_picking;
pub use dioxus;
2023-12-06 05:58:45 +00:00
2023-12-06 05:24:51 +00:00
pub struct DioxusUiPlugin;
impl Plugin for DioxusUiPlugin {
fn build(&self, app: &mut App) {
2023-12-17 01:48:42 +00:00
// TODO: I think UiRoots must be dropped only after EcsSubscriptions
2023-12-16 20:41:32 +00:00
app.init_non_send_resource::<UiRoots>()
.init_resource::<EcsSubscriptions>()
2023-12-13 00:22:54 +00:00
.init_resource::<DeferredSystemRegistry>()
2023-12-12 08:12:06 +00:00
.init_resource::<EventReaders>()
2023-12-07 02:46:50 +00:00
.add_systems(Update, tick_dioxus_ui);
2023-12-06 05:24:51 +00:00
}
}
#[derive(Bundle)]
pub struct DioxusUiBundle {
pub dioxus_ui_root: DioxusUiRoot,
pub node_bundle: NodeBundle,
}
#[derive(Component, Deref, Hash, PartialEq, Eq, Clone, Copy)]
pub struct DioxusUiRoot(pub fn(Scope) -> Element);
2023-12-16 20:41:32 +00:00
#[derive(Deref, DerefMut, Default)]
struct UiRoots(HashMap<(Entity, DioxusUiRoot), UiRoot>);
2023-12-16 20:41:32 +00:00
struct UiRoot {
virtual_dom: VirtualDom,
2023-12-07 19:17:42 +00:00
element_id_to_bevy_ui_entity: HashMap<ElementId, Entity>,
2023-12-12 08:12:06 +00:00
bevy_ui_entity_to_element_id: EntityHashMap<Entity, ElementId>,
templates: HashMap<String, BevyTemplate>,
2023-12-07 02:46:50 +00:00
needs_rebuild: bool,
2023-12-06 05:24:51 +00:00
}
2023-12-16 20:41:32 +00:00
impl UiRoot {
fn new(root_component: DioxusUiRoot) -> Self {
2023-12-06 05:24:51 +00:00
Self {
virtual_dom: VirtualDom::new(root_component.0),
2023-12-07 19:17:42 +00:00
element_id_to_bevy_ui_entity: HashMap::new(),
2023-12-12 08:12:06 +00:00
bevy_ui_entity_to_element_id: EntityHashMap::default(),
2023-12-07 19:17:42 +00:00
templates: HashMap::new(),
2023-12-07 02:46:50 +00:00
needs_rebuild: true,
2023-12-06 05:24:51 +00:00
}
}
}