bevy_dioxus/src/lib.rs

100 lines
2.9 KiB
Rust
Raw Normal View History

2024-03-25 02:38:08 +00:00
#![feature(trait_alias)]
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-23 05:36:05 +00:00
mod ecs_hooks;
2024-04-08 22:30:42 +00:00
pub mod elements;
2023-12-19 05:24:06 +00:00
#[macro_use]
2023-12-12 08:12:06 +00:00
mod events;
2023-12-21 08:45:28 +00:00
#[cfg(feature = "hot_reload")]
mod hot_reload;
2023-12-21 06:53:55 +00:00
mod parse_attributes;
2023-12-07 02:46:50 +00:00
mod tick;
2023-12-27 20:56:17 +00:00
mod use_state_sendable;
2023-12-06 05:24:51 +00:00
2023-12-07 02:46:50 +00:00
use self::{
apply_mutations::BevyTemplate,
2023-12-27 19:19:02 +00:00
deferred_system::DeferredSystemRunQueue,
2023-12-23 05:36:05 +00:00
ecs_hooks::EcsSubscriptions,
events::{generate_mouse_enter_leave_events, EventReaders, MouseEnter, MouseExit},
tick::tick_dioxus_ui,
2023-12-07 02:46:50 +00:00
};
2023-12-06 05:24:51 +00:00
use bevy::{
app::{App, Last, Plugin, PreUpdate},
2024-03-25 02:38:08 +00:00
ecs::{bundle::Bundle, component::Component, entity::{Entity, EntityHashMap}, schedule::IntoSystemConfigs},
2023-12-17 04:19:41 +00:00
prelude::Deref,
ui::{node_bundles::NodeBundle, ui_focus_system},
2024-03-25 02:38:08 +00:00
utils::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-19 05:24:06 +00:00
pub mod prelude {
2023-12-27 19:24:36 +00:00
pub use super::deferred_system::use_system_scheduler;
2023-12-27 19:56:36 +00:00
pub use super::ecs_hooks::{
use_event_reader, use_query, use_query_filtered, use_resource, use_world,
};
2024-03-25 02:38:08 +00:00
pub use super::elements::dioxus_elements;
2023-12-27 20:56:17 +00:00
pub use super::use_state_sendable::*;
2023-12-19 05:24:06 +00:00
pub use super::{DioxusUiBundle, DioxusUiPlugin, DioxusUiRoot};
2023-12-24 21:51:05 +00:00
pub use bevy_mod_picking::pointer::PointerButton;
2023-12-19 05:24:06 +00:00
pub use dioxus;
2024-01-05 08:04:24 +00:00
pub use dioxus::prelude::{Event as DioxusEvent, *};
2023-12-19 05:24:06 +00:00
}
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-21 08:45:28 +00:00
#[cfg(feature = "hot_reload")]
dioxus_hot_reload::hot_reload_init!(dioxus_hot_reload::Config::<
hot_reload::HotReloadContext,
>::default());
2023-12-17 04:19:41 +00:00
app.init_non_send_resource::<UiContext>()
2023-12-27 19:19:02 +00:00
.init_resource::<DeferredSystemRunQueue>()
2023-12-12 08:12:06 +00:00
.init_resource::<EventReaders>()
.add_event::<MouseEnter>()
.add_event::<MouseExit>()
.add_systems(
PreUpdate,
generate_mouse_enter_leave_events.after(ui_focus_system),
)
.add_systems(Last, 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
2023-12-17 04:19:41 +00:00
#[derive(Default)]
struct UiContext {
roots: HashMap<(Entity, DioxusUiRoot), UiRoot>,
subscriptions: EcsSubscriptions,
}
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>,
2024-03-25 02:38:08 +00:00
bevy_ui_entity_to_element_id: EntityHashMap<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
}
}
}