1use crate::layout::{Align, FlexDir};
4
5#[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 Panel(FlexDir),
22 Label(String),
24 Button(String),
26 ItemSlot(String),
28 McImage { id: String, img_w: f32, img_h: f32 },
30 Spacer,
32}
33
34#[derive(Debug, Clone)]
36pub struct Style {
37 pub w: f32, pub h: f32, pub min_w: f32,
40 pub min_h: f32,
41 pub flex: f32, pub gap: f32, pub pad: [f32; 4], pub margin: [f32; 4],
45 pub bg: u32, pub color: u32, pub align: Align,
48 pub font_scale: f32,
49}
50
51impl Default for Style {
52 fn default() -> Self {
53 Self {
54 w: 0.0, h: 0.0, min_w: 4.0, min_h: 4.0, flex: 0.0, gap: 2.0,
55 pad: [0.0; 4], margin: [0.0; 4], bg: 0, color: 0xFF_CCCCAA,
56 align: Align::Start, font_scale: 1.0,
57 }
58 }
59}
60
61impl Widget {
64 fn new(kind: WidgetKind) -> Self {
65 let flex_dir = if let WidgetKind::Panel(dir) = &kind { *dir } else { FlexDir::Column };
66 Self { kind, id: None, style: Style::default(), flex_dir,
67 children: Vec::new(), on_click: None, enabled: true, focused: false }
68 }
69
70 pub fn id(mut self, id: impl Into<String>) -> Self { self.id = Some(id.into()); self }
71 pub fn on_click(mut self, ev: impl Into<String>) -> Self { self.on_click = Some(ev.into()); self }
72 pub fn child(mut self, w: Widget) -> Self { self.children.push(w); self }
73
74 pub fn w(mut self, w: f32) -> Self { self.style.w = w; self }
75 pub fn h(mut self, h: f32) -> Self { self.style.h = h; self }
76 pub fn min_w(mut self, v: f32) -> Self { self.style.min_w = v; self }
77 pub fn min_h(mut self, v: f32) -> Self { self.style.min_h = v; self }
78 pub fn flex(mut self, v: f32) -> Self { self.style.flex = v; self }
79 pub fn flex_dir(mut self, v: FlexDir) -> Self { self.flex_dir = v; self }
80 pub fn gap(mut self, v: f32) -> Self { self.style.gap = v; self }
81 pub fn bg(mut self, v: u32) -> Self { self.style.bg = v; self }
82 pub fn color(mut self, v: u32) -> Self { self.style.color = v; self }
83 pub fn align(mut self, v: Align) -> Self { self.style.align = v; self }
84 pub fn font_scale(mut self, v: f32) -> Self { self.style.font_scale = v; self }
85 pub fn enabled(mut self, v: bool) -> Self { self.enabled = v; self }
86 pub fn focused(mut self, v: bool) -> Self { self.focused = v; self }
87 pub fn padding(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
88 self.style.pad = [top, right, bottom, left]; self
89 }
90 pub fn margin(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
91 self.style.margin = [top, right, bottom, left]; self
92 }
93}
94
95pub fn panel(dir: FlexDir) -> Widget { Widget::new(WidgetKind::Panel(dir)) }
98pub fn label(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Label(text.into())) }
99pub fn button(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Button(text.into())) }
100pub fn item_slot(item_id: impl Into<String>) -> Widget { Widget::new(WidgetKind::ItemSlot(item_id.into())) }
101pub fn mc_image(id: impl Into<String>, img_w: f32, img_h: f32) -> Widget {
102 Widget::new(WidgetKind::McImage { id: id.into(), img_w, img_h })
103 .w(img_w).h(img_h)
104}
105pub fn spacer() -> Widget { Widget::new(WidgetKind::Spacer) }