pixels_graphics_lib/ui/
button.rs

1use crate::prelude::*;
2use crate::ui::layout::LayoutView;
3use crate::ui::styles::ButtonStyle;
4use crate::ui::{PixelView, ViewState};
5
6#[derive(Debug)]
7pub struct Button {
8    label: String, //needed for relayout
9    text: Text,
10    bounds: Rect,
11    border: Polyline,
12    shadow: Polyline,
13    style: ButtonStyle,
14    state: ViewState,
15}
16
17impl Button {
18    pub fn new<P: Into<Coord>>(
19        xy: P,
20        text: &str,
21        min_width: Option<usize>,
22        style: &ButtonStyle,
23    ) -> Self {
24        let bounds = Self::calc_bounds(xy.into(), text, min_width, style.font);
25        let label = text.to_string();
26        let (text, border, shadow) = Self::layout(&bounds, style, text);
27        Self {
28            label,
29            text,
30            bounds,
31            border,
32            shadow,
33            style: style.clone(),
34            state: ViewState::Normal,
35        }
36    }
37
38    fn layout(bounds: &Rect, style: &ButtonStyle, text: &str) -> (Text, Polyline, Polyline) {
39        let border = Polyline::rounded_rect(
40            bounds.left(),
41            bounds.top(),
42            bounds.right(),
43            bounds.bottom(),
44            style.rounding,
45            WHITE,
46        )
47        .unwrap();
48        let shadow = Polyline::rounded_rect(
49            bounds.left() + 1,
50            bounds.top() + 1,
51            bounds.right() + 1,
52            bounds.bottom() + 1,
53            style.rounding,
54            WHITE,
55        )
56        .unwrap();
57        let text = Text::new(
58            text,
59            TextPos::px(bounds.center() + (0, 1)),
60            (
61                WHITE,
62                style.font,
63                WrappingStrategy::AtCol(style.font.px_to_cols(bounds.width())),
64                Positioning::Center,
65            ),
66        );
67        (text, border, shadow)
68    }
69
70    pub fn calc_bounds(xy: Coord, text: &str, min_width: Option<usize>, font: PixelFont) -> Rect {
71        let min_width = min_width.unwrap_or_default();
72        let (w, h) = font.measure(text);
73        Rect::new_with_size(
74            xy,
75            ((w as f32 * 1.2) as usize).max(min_width),
76            (h as f32 * 2.0) as usize,
77        )
78    }
79
80    #[must_use]
81    pub fn on_mouse_click(&mut self, down: Coord, up: Coord) -> bool {
82        if self.state != ViewState::Disabled {
83            self.bounds.contains(down) && self.bounds.contains(up)
84        } else {
85            false
86        }
87    }
88}
89
90impl PixelView for Button {
91    fn set_position(&mut self, top_left: Coord) {
92        self.bounds = self.bounds.move_to(top_left);
93        let (text, border, shadow) = Self::layout(&self.bounds, &self.style, &self.label);
94        self.text = text;
95        self.shadow = shadow;
96        self.border = border;
97    }
98
99    #[must_use]
100    fn bounds(&self) -> &Rect {
101        &self.bounds
102    }
103
104    fn render(&self, graphics: &mut Graphics, mouse: &MouseData) {
105        let (error, disabled) = self.state.get_err_dis();
106        let hovering = self.bounds.contains(mouse.xy);
107        if let Some(color) = self.style.shadow.get(hovering, error, disabled) {
108            self.shadow.with_color(color).render(graphics);
109        }
110        if let Some(color) = self.style.border.get(hovering, error, disabled) {
111            self.border.with_color(color).render(graphics);
112        }
113        if let Some(color) = self.style.text.get(hovering, error, disabled) {
114            self.text.with_color(color).render(graphics);
115        }
116    }
117
118    fn update(&mut self, _: &Timing) {}
119
120    #[inline]
121    fn set_state(&mut self, state: ViewState) {
122        self.state = state;
123    }
124
125    #[inline]
126    #[must_use]
127    fn get_state(&self) -> ViewState {
128        self.state
129    }
130}
131
132impl LayoutView for Button {
133    fn set_bounds(&mut self, bounds: Rect) {
134        self.bounds = bounds.clone();
135        self.set_position(bounds.top_left());
136    }
137}