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}
15
16#[derive(Debug, Clone)]
17pub enum WidgetKind {
18 Panel,
20 Label(String),
22 Button(String),
24 ItemSlot(String),
26 McImage { id: String, img_w: f32, img_h: f32 },
28 Spacer,
30}
31
32#[derive(Debug, Clone)]
34pub struct Style {
35 pub w: f32, pub h: f32, pub min_w: f32,
38 pub min_h: f32,
39 pub flex: f32, pub gap: f32, pub pad: [f32; 4], pub margin: [f32; 4],
43 pub bg: u32, pub color: u32, pub align: Align,
46 pub font_scale: f32,
47}
48
49impl Default for Style {
50 fn default() -> Self {
51 Self {
52 w: 0.0, h: 0.0, min_w: 4.0, min_h: 4.0, flex: 0.0, gap: 2.0,
53 pad: [0.0; 4], margin: [0.0; 4], bg: 0, color: 0xFF_CCCCAA,
54 align: Align::Start, font_scale: 1.0,
55 }
56 }
57}
58
59impl Widget {
62 fn new(kind: WidgetKind) -> Self {
63 Self { kind, id: None, style: Style::default(), flex_dir: FlexDir::Column,
64 children: Vec::new(), on_click: None }
65 }
66
67 pub fn id(mut self, id: impl Into<String>) -> Self { self.id = Some(id.into()); self }
68 pub fn on_click(mut self, ev: impl Into<String>) -> Self { self.on_click = Some(ev.into()); self }
69 pub fn child(mut self, w: Widget) -> Self { self.children.push(w); self }
70
71 pub fn w(mut self, w: f32) -> Self { self.style.w = w; self }
72 pub fn h(mut self, h: f32) -> Self { self.style.h = h; self }
73 pub fn min_w(mut self, v: f32) -> Self { self.style.min_w = v; self }
74 pub fn min_h(mut self, v: f32) -> Self { self.style.min_h = v; self }
75 pub fn flex(mut self, v: f32) -> Self { self.style.flex = v; self }
76 pub fn flex_dir(mut self, v: FlexDir) -> Self { self.flex_dir = v; self }
77 pub fn gap(mut self, v: f32) -> Self { self.style.gap = v; self }
78 pub fn bg(mut self, v: u32) -> Self { self.style.bg = v; self }
79 pub fn color(mut self, v: u32) -> Self { self.style.color = v; self }
80 pub fn align(mut self, v: Align) -> Self { self.style.align = v; self }
81 pub fn font_scale(mut self, v: f32) -> Self { self.style.font_scale = v; self }
82 pub fn padding(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
83 self.style.pad = [top, right, bottom, left]; self
84 }
85 pub fn margin(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
86 self.style.margin = [top, right, bottom, left]; self
87 }
88}
89
90pub fn panel(dir: FlexDir) -> Widget { Widget::new(WidgetKind::Panel).flex_dir(dir) }
93pub fn label(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Label(text.into())) }
94pub fn button(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Button(text.into())) }
95pub fn item_slot(item_id: impl Into<String>) -> Widget { Widget::new(WidgetKind::ItemSlot(item_id.into())) }
96pub fn mc_image(id: impl Into<String>, img_w: f32, img_h: f32) -> Widget {
97 Widget::new(WidgetKind::McImage { id: id.into(), img_w, img_h })
98 .w(img_w).h(img_h)
99}
100pub fn spacer() -> Widget { Widget::new(WidgetKind::Spacer) }