Skip to main content

yog_ui/
widget.rs

1//! Widget types and styling.
2
3use crate::layout::{Align, FlexDir};
4
5/// A single widget in the UI tree.
6#[derive(Debug, Clone)]
7pub struct Widget {
8    pub kind:     WidgetKind,
9    pub id:       Option<String>,
10    pub style:    Style,
11    pub flex_dir: FlexDir,
12    pub children: Vec<Widget>,
13    pub on_click: Option<String>,
14    pub enabled:  bool,
15    pub focused:  bool,
16}
17
18#[derive(Debug, Clone)]
19pub enum WidgetKind {
20    /// Container — arranges children according to `flex_dir`.
21    Panel(FlexDir),
22    /// Static or dynamic text label.
23    Label(String),
24    /// Clickable button with text.
25    Button(String),
26    /// Minecraft item icon.
27    ItemSlot(String),
28    /// Minecraft texture blit (via `draw2d_mc_tex`).
29    McImage { id: String, img_w: f32, img_h: f32 },
30    /// Invisible spacer.
31    Spacer,
32}
33
34/// How a focused widget shows its focus indicator.
35#[derive(Debug, Clone, Copy, PartialEq)]
36pub enum FocusStyle {
37    /// 1px outline using `focus_color` (default, amber).
38    Outline,
39    /// Solid background fill using `focus_color`.
40    Fill,
41    /// No visual indicator.
42    None,
43}
44
45impl Default for FocusStyle { fn default() -> Self { FocusStyle::Outline } }
46
47/// Visual and layout style for a widget.
48#[derive(Debug, Clone)]
49pub struct Style {
50    pub w:      f32,   // explicit width; 0 = auto
51    pub h:      f32,   // explicit height; 0 = auto
52    pub min_w:  f32,
53    pub min_h:  f32,
54    pub flex:   f32,   // grow factor inside flex container
55    pub gap:    f32,   // spacing between children
56    pub pad:    [f32; 4],  // top, right, bottom, left
57    pub margin: [f32; 4],
58    pub bg:     u32,   // background colour 0xAARRGGBB; 0 = transparent
59    pub color:  u32,   // text colour
60    pub align:  Align,
61    pub font_scale:  f32,
62    pub focus_style: FocusStyle,
63    pub focus_color: u32,  // 0 = default amber 0xFF_FFE040
64}
65
66impl Default for Style {
67    fn default() -> Self {
68        Self {
69            w: 0.0, h: 0.0, min_w: 4.0, min_h: 4.0, flex: 0.0, gap: 2.0,
70            pad: [0.0; 4], margin: [0.0; 4], bg: 0, color: 0xFF_CCCCAA,
71            align: Align::Start, font_scale: 1.0,
72            focus_style: FocusStyle::default(), focus_color: 0,
73        }
74    }
75}
76
77// ── Builder API ───────────────────────────────────────────────────────────────
78
79impl Widget {
80    fn new(kind: WidgetKind) -> Self {
81        let flex_dir = if let WidgetKind::Panel(dir) = &kind { *dir } else { FlexDir::Column };
82        Self { kind, id: None, style: Style::default(), flex_dir,
83               children: Vec::new(), on_click: None, enabled: true, focused: false }
84    }
85
86    pub fn id(mut self, id: impl Into<String>) -> Self { self.id = Some(id.into()); self }
87    pub fn on_click(mut self, ev: impl Into<String>) -> Self { self.on_click = Some(ev.into()); self }
88    pub fn child(mut self, w: Widget) -> Self { self.children.push(w); self }
89
90    pub fn w(mut self, w: f32) -> Self { self.style.w = w; self }
91    pub fn h(mut self, h: f32) -> Self { self.style.h = h; self }
92    pub fn min_w(mut self, v: f32) -> Self { self.style.min_w = v; self }
93    pub fn min_h(mut self, v: f32) -> Self { self.style.min_h = v; self }
94    pub fn flex(mut self, v: f32) -> Self { self.style.flex = v; self }
95    pub fn flex_dir(mut self, v: FlexDir) -> Self { self.flex_dir = v; self }
96    pub fn gap(mut self, v: f32) -> Self { self.style.gap = v; self }
97    pub fn bg(mut self, v: u32) -> Self { self.style.bg = v; self }
98    pub fn color(mut self, v: u32) -> Self { self.style.color = v; self }
99    pub fn align(mut self, v: Align) -> Self { self.style.align = v; self }
100    pub fn font_scale(mut self, v: f32) -> Self { self.style.font_scale = v; self }
101    pub fn focus_style(mut self, v: FocusStyle) -> Self { self.style.focus_style = v; self }
102    pub fn focus_color(mut self, v: u32) -> Self { self.style.focus_color = v; self }
103    pub fn enabled(mut self, v: bool) -> Self { self.enabled = v; self }
104    pub fn focused(mut self, v: bool) -> Self { self.focused = v; self }
105    pub fn padding(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
106        self.style.pad = [top, right, bottom, left]; self
107    }
108    pub fn margin(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
109        self.style.margin = [top, right, bottom, left]; self
110    }
111}
112
113// ── Widget constructors ───────────────────────────────────────────────────────
114
115pub fn panel(dir: FlexDir) -> Widget { Widget::new(WidgetKind::Panel(dir)) }
116pub fn label(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Label(text.into())) }
117pub fn button(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Button(text.into())) }
118pub fn item_slot(item_id: impl Into<String>) -> Widget { Widget::new(WidgetKind::ItemSlot(item_id.into())) }
119pub fn mc_image(id: impl Into<String>, img_w: f32, img_h: f32) -> Widget {
120    Widget::new(WidgetKind::McImage { id: id.into(), img_w, img_h })
121        .w(img_w).h(img_h)
122}
123pub fn spacer() -> Widget { Widget::new(WidgetKind::Spacer) }