Skip to main content

yog_ui/
lib.rs

1//! yog-ui — retained-mode UI framework for Yog mods.
2//!
3//! Flexbox-inspired layout engine + GPU rendering via [`yog-gfx`].
4//! Use for custom inventories, guide books, tooltips, HUD overlays.
5//!
6//! # Quick start
7//! ```ignore
8//! use yog_ui::{UiRoot, widget, Align, FlexDir, Units};
9//!
10//! let ui = UiRoot::new("mymod:main_menu")
11//!     .style(|s| s.bg(0x88332211).padding(8.0, 8.0, 8.0, 8.0))
12//!     .child(
13//!         widget::panel(FlexDir::Column).gap(4.0)
14//!             .child(widget::label("Hello, World!").color(0xFF_DDAA00))
15//!             .child(widget::button("Click me").on_click("mymod:btn_click"))
16//!     );
17//! ```
18
19pub mod component;
20pub mod event;
21pub mod image;
22pub mod layout;
23mod render;
24pub mod scroll;
25#[cfg(test)]
26mod state_tests;
27pub mod text;
28pub mod textbox;
29pub mod widget;
30
31pub use event::UiEvent;
32pub use layout::{hit_path, node_at, set_focus, Align, FlexDir, LayoutNode, Rect};
33pub use scroll::ScrollState;
34pub use textbox::{TextBoxEvent, TextBoxState};
35pub use widget::{
36    Action, Dock, FocusStyle, StateFlags, StateStyles, Stretch, StylePatch, Units, Widget,
37};
38
39use yog_gfx::GfxContext;
40
41/// Top-level UI tree.  Build it, call [`layout`], then [`render`] each frame.
42pub struct UiRoot {
43    pub id: String,
44    pub root: Widget,
45    pub layout_root: LayoutNode,
46    pub needs_layout: bool,
47}
48
49impl UiRoot {
50    pub fn new(id: impl Into<String>, root: Widget) -> Self {
51        Self {
52            id: id.into(),
53            root,
54            layout_root: LayoutNode::default(),
55            needs_layout: true,
56        }
57    }
58
59    /// Recalculate layout. Call after changing the tree or on window resize.
60    pub fn layout(&mut self, screen_w: f32, screen_h: f32) {
61        self.layout_root = layout::compute(&self.root, screen_w, screen_h);
62        self.needs_layout = false;
63    }
64
65    /// Render the UI tree via `yog-gfx` draw2d.
66    /// Must be called from `on_hud_render`.
67    pub fn render(&self, ctx: &GfxContext) {
68        text::use_host_metrics(ctx);
69        image::use_host_images(ctx);
70        let d2d = ctx.draw2d();
71        render::render_node(&d2d, &self.root, &self.layout_root);
72    }
73
74    /// Find the deepest clickable widget at `(mx, my)` in screen coordinates.
75    pub fn hit_test(&self, mx: f32, my: f32) -> Option<&LayoutNode> {
76        layout::hit_test(&self.layout_root, mx, my)
77    }
78
79    /// Resolved rect of the (first) node tagged `id` — see [`layout::find_by_id`].
80    /// Call after [`Self::layout`].
81    pub fn find_rect(&self, id: &str) -> Option<Rect> {
82        layout::find_by_id(&self.layout_root, id)
83    }
84
85    /// Laid-out node of the (first) widget tagged `id` — rect plus scroll
86    /// state, see [`layout::find_node`]. Call after [`Self::layout`].
87    pub fn find_node(&self, id: &str) -> Option<&LayoutNode> {
88        layout::find_node(&self.layout_root, id)
89    }
90}