Skip to main content

yog_ui/
lib.rs

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