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, Copy, PartialEq, Default)]
43pub enum Dock { #[default] None, Fill, Left, Right, Top, Bottom }
44
45#[derive(Debug, Clone, Copy, PartialEq)]
47pub enum FocusStyle {
48 Outline,
50 Fill,
52 None,
54}
55
56impl Default for FocusStyle { fn default() -> Self { FocusStyle::Outline } }
57
58#[derive(Debug, Clone)]
60pub struct Style {
61 pub w: f32, pub h: f32, pub min_w: f32,
64 pub min_h: f32,
65 pub flex: f32, pub dock: Dock, pub gap: f32, pub pad: [f32; 4], pub margin: [f32; 4], pub bg: u32, pub color: u32, pub align: Align,
73 pub font_scale: f32,
74 pub focus_style: FocusStyle,
75 pub focus_color: u32, }
77
78impl Default for Style {
79 fn default() -> Self {
80 Self {
81 w: 0.0, h: 0.0, min_w: 4.0, min_h: 4.0, flex: 0.0, dock: Dock::None, gap: 2.0,
82 pad: [0.0; 4], margin: [0.0; 4], bg: 0, color: 0xFF_CCCCAA,
83 align: Align::Start, font_scale: 1.0,
84 focus_style: FocusStyle::default(), focus_color: 0,
85 }
86 }
87}
88
89impl Widget {
92 fn new(kind: WidgetKind) -> Self {
93 let flex_dir = if let WidgetKind::Panel(dir) = &kind { *dir } else { FlexDir::Column };
94 Self { kind, id: None, style: Style::default(), flex_dir,
95 children: Vec::new(), on_click: None, enabled: true, focused: false }
96 }
97
98 pub fn id(mut self, id: impl Into<String>) -> Self { self.id = Some(id.into()); self }
99 pub fn on_click(mut self, ev: impl Into<String>) -> Self { self.on_click = Some(ev.into()); self }
100 pub fn child(mut self, w: Widget) -> Self { self.children.push(w); self }
101
102 pub fn w(mut self, w: f32) -> Self { self.style.w = w; self }
103 pub fn h(mut self, h: f32) -> Self { self.style.h = h; self }
104 pub fn min_w(mut self, v: f32) -> Self { self.style.min_w = v; self }
105 pub fn min_h(mut self, v: f32) -> Self { self.style.min_h = v; self }
106 pub fn flex(mut self, v: f32) -> Self { self.style.flex = v; self }
107 pub fn flex_dir(mut self, v: FlexDir) -> Self { self.flex_dir = v; self }
108 pub fn gap(mut self, v: f32) -> Self { self.style.gap = v; self }
109 pub fn bg(mut self, v: u32) -> Self { self.style.bg = v; self }
110 pub fn color(mut self, v: u32) -> Self { self.style.color = v; self }
111 pub fn align(mut self, v: Align) -> Self { self.style.align = v; self }
112 pub fn font_scale(mut self, v: f32) -> Self { self.style.font_scale = v; self }
113 pub fn focus_style(mut self, v: FocusStyle) -> Self { self.style.focus_style = v; self }
114 pub fn focus_color(mut self, v: u32) -> Self { self.style.focus_color = v; self }
115 pub fn dock(mut self, v: Dock) -> Self { self.style.dock = v; self }
116 pub fn enabled(mut self, v: bool) -> Self { self.enabled = v; self }
117 pub fn focused(mut self, v: bool) -> Self { self.focused = v; self }
118 pub fn padding(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
119 self.style.pad = [top, right, bottom, left]; self
120 }
121 pub fn margin(mut self, top: f32, right: f32, bottom: f32, left: f32) -> Self {
122 self.style.margin = [top, right, bottom, left]; self
123 }
124}
125
126pub fn panel(dir: FlexDir) -> Widget { Widget::new(WidgetKind::Panel(dir)) }
129pub fn label(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Label(text.into())) }
130pub fn button(text: impl Into<String>) -> Widget { Widget::new(WidgetKind::Button(text.into())) }
131pub fn item_slot(item_id: impl Into<String>) -> Widget { Widget::new(WidgetKind::ItemSlot(item_id.into())) }
132pub fn mc_image(id: impl Into<String>, img_w: f32, img_h: f32) -> Widget {
133 Widget::new(WidgetKind::McImage { id: id.into(), img_w, img_h })
134 .w(img_w).h(img_h)
135}
136pub fn spacer() -> Widget { Widget::new(WidgetKind::Spacer) }