Skip to main content

wde_ui/
lib.rs

1//! WaterDropEngine's `wde-ui` crate: an ECS-driven game UI, laid out with [taffy](https://github.com/DioxusLabs/taffy).
2//!
3//! A UI is a hierarchy of entities with a [`UiNode`](node::UiNode), whose [`UiStyle`](style::UiStyle) follows the CSS
4//! flexbox / grid / block model. Nodes are drawn as rounded rectangles on top of the scene with a
5//! [`UiBackground`](node::UiBackground), [`UiBorderColor`](node::UiBorderColor) and
6//! [`UiCornerRadius`](node::UiCornerRadius), and react to the pointer with a
7//! [`UiInteraction`](interaction::UiInteraction). A [`UiText`](text::UiText) node is sized to its text,
8//! shaped with cosmic-text and drawn with glyphon.
9//!
10//! # Example
11//! ```rust,ignore
12//! commands.spawn((
13//!     UiNode(UiStyle {
14//!         width: Val::Percent(100.0),
15//!         height: Val::Px(48.0),
16//!         padding: UiRect::all(Val::Px(8.0)),
17//!         ..default()
18//!     }),
19//!     UiBackground(Color::from_srgba(0.1, 0.1, 0.1, 0.8)),
20//!     children![(
21//!         UiNode(UiStyle { width: Val::Px(32.0), height: Val::Px(32.0), ..default() }),
22//!         UiBackground(Color::WHITE),
23//!         UiInteraction::default()
24//!     )]
25//! ));
26//! ```
27//!
28//! Each frame in `PostUpdate` ([`UiSystems`]), the hierarchy is laid out against the window and the
29//! visible nodes are uploaded to the GPU, then drawn by the [`UiRenderPass`].
30use bevy::{input::InputSystems, prelude::*};
31use wde_renderer::prelude::*;
32
33use crate::{
34    interaction::{UiClick, UiPointer, update_interactions},
35    layout::{UiLayoutTree, compute_layout, sync_layout_tree},
36    render::{
37        data::{UiQuadData, UiQuadsBinding},
38        extract::{UiDrawList, build_draw_list, update_ui_buffer},
39        image::{UiDrawBatches, UiImageBindGroups, UiImageLayout},
40        pass::SubRenderPassUi,
41        pipeline::UiRenderPipeline,
42        text::{SubRenderPassUiText, UiTextRenderer, build_text_draws, prepare_text}
43    },
44    text::UiFonts
45};
46
47pub mod interaction;
48mod layout;
49pub mod node;
50mod render;
51pub mod style;
52pub mod text;
53
54pub use render::pass::UiRenderPass;
55
56#[doc(hidden)]
57pub mod prelude {
58    pub use crate::interaction::{UiClick, UiInteraction, UiPointer};
59    pub use crate::node::{
60        ComputedUiNode, UiBackground, UiBorderColor, UiCornerRadius, UiImage, UiNode
61    };
62    pub use crate::style::*;
63    pub use crate::text::UiText;
64    pub use crate::{UiPlugin, UiSystems};
65}
66
67/// Lays out the UI and builds its draw list, in `PostUpdate`.
68#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
69pub struct UiSystems;
70
71pub struct UiPlugin;
72impl Plugin for UiPlugin {
73    fn build(&self, app: &mut App) {
74        let fonts = UiFonts::default();
75        app.insert_resource(fonts.clone())
76            .init_resource::<UiLayoutTree>()
77            .init_resource::<UiPointer>()
78            .init_resource::<UiDrawList>()
79            .add_message::<UiClick>()
80            .add_systems(PreUpdate, update_interactions.after(InputSystems))
81            .add_systems(
82                PostUpdate,
83                (
84                    sync_layout_tree,
85                    compute_layout,
86                    (build_draw_list, build_text_draws).chain()
87                )
88                    .chain()
89                    .in_set(UiSystems)
90            );
91
92        app.add_plugins((
93            ExtractResourcePlugin::<UiDrawList>::default(),
94            RenderDataRegisterPlugin::<UiQuadData>::default(),
95            RenderBindingRegisterPlugin::<UiQuadsBinding>::default(),
96            RenderPipelineRegisterPlugin::<UiRenderPipeline>::default()
97        ));
98
99        let render_app = app.get_sub_app_mut(RenderApp).unwrap();
100        render_app
101            .insert_resource(fonts)
102            .init_resource::<UiDrawList>()
103            .init_resource::<UiDrawBatches>()
104            .init_resource::<UiImageBindGroups>()
105            .init_resource::<UiImageLayout>()
106            .add_systems(
107                Render,
108                (update_ui_buffer, prepare_text).in_set(RenderSet::Prepare)
109            );
110        render_app
111            .world_mut()
112            .resource_mut::<RenderGraph>()
113            .add_pass::<UiRenderPass>()
114            .add_sub_pass::<SubRenderPassUi, UiRenderPass>()
115            .add_sub_pass::<SubRenderPassUiText, UiRenderPass>();
116    }
117
118    fn finish(&self, app: &mut App) {
119        let render_app = app.get_sub_app_mut(RenderApp).unwrap();
120        let text_renderer = UiTextRenderer::new(render_app.world());
121        render_app.insert_resource(text_renderer);
122    }
123}