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 layout;
20mod render;
21pub mod text;
22pub mod widget;
23
24pub use layout::{Align, FlexDir, LayoutNode, Rect, set_focus};
25pub use widget::{Dock, FocusStyle, Widget};
26
27use yog_gfx::GfxContext;
28
29/// Top-level UI tree. Build it, call [`layout`], then [`render`] each frame.
30pub struct UiRoot {
31 pub id: String,
32 pub root: Widget,
33 pub layout_root: LayoutNode,
34 pub needs_layout: bool,
35}
36
37impl UiRoot {
38 pub fn new(id: impl Into<String>, root: Widget) -> Self {
39 Self { id: id.into(), root, layout_root: LayoutNode::default(), needs_layout: true }
40 }
41
42 /// Recalculate layout. Call after changing the tree or on window resize.
43 pub fn layout(&mut self, screen_w: f32, screen_h: f32) {
44 self.layout_root = layout::compute(&self.root, screen_w, screen_h);
45 self.needs_layout = false;
46 }
47
48 /// Render the UI tree via `yog-gfx` draw2d.
49 /// Must be called from `on_hud_render`.
50 pub fn render(&self, ctx: &GfxContext) {
51 let d2d = ctx.draw2d();
52 render::render_node(&d2d, &self.root, &self.layout_root);
53 }
54
55 /// Find the deepest clickable widget at `(mx, my)` in screen coordinates.
56 pub fn hit_test(&self, mx: f32, my: f32) -> Option<&LayoutNode> {
57 layout::hit_test(&self.layout_root, mx, my)
58 }
59}