veloren_voxygen/ui/ice/renderer/style/
button.rs

1use super::super::super::widget::image;
2use iced::Color;
3use vek::Rgba;
4
5#[derive(Clone, Copy)]
6struct Background {
7    default: image::Handle,
8    hover: image::Handle,
9    press: image::Handle,
10    color: Rgba<u8>,
11}
12
13impl Background {
14    fn new(image: image::Handle) -> Self {
15        Self {
16            default: image,
17            hover: image,
18            press: image,
19            color: Rgba::white(),
20        }
21    }
22}
23// TODO: consider a different place for this
24// Note: for now all buttons have an image background
25#[derive(Clone, Copy)]
26pub struct Style {
27    background: Option<Background>,
28    enabled_text: Color,
29    disabled_text: Color,
30}
31
32impl Style {
33    pub fn new(image: image::Handle) -> Self {
34        Self {
35            background: Some(Background::new(image)),
36            ..Default::default()
37        }
38    }
39
40    #[must_use]
41    pub fn hover_image(mut self, image: image::Handle) -> Self {
42        self.background = Some(match self.background {
43            Some(mut background) => {
44                background.hover = image;
45                background
46            },
47            None => Background::new(image),
48        });
49        self
50    }
51
52    #[must_use]
53    pub fn press_image(mut self, image: image::Handle) -> Self {
54        self.background = Some(match self.background {
55            Some(mut background) => {
56                background.press = image;
57                background
58            },
59            None => Background::new(image),
60        });
61        self
62    }
63
64    // TODO: this needs to be refactored since the color isn't used if there is no
65    // background
66    #[must_use]
67    pub fn image_color(mut self, color: Rgba<u8>) -> Self {
68        if let Some(background) = &mut self.background {
69            background.color = color;
70        }
71        self
72    }
73
74    #[must_use]
75    pub fn text_color(mut self, color: Color) -> Self {
76        self.enabled_text = color;
77        self
78    }
79
80    #[must_use]
81    pub fn disabled_text_color(mut self, color: Color) -> Self {
82        self.disabled_text = color;
83        self
84    }
85
86    pub fn disabled(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
87        (
88            self.background.as_ref().map(|b| (b.default, b.color)),
89            self.disabled_text,
90        )
91    }
92
93    pub fn pressed(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
94        (
95            self.background.as_ref().map(|b| (b.press, b.color)),
96            self.enabled_text,
97        )
98    }
99
100    pub fn hovered(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
101        (
102            self.background.as_ref().map(|b| (b.hover, b.color)),
103            self.enabled_text,
104        )
105    }
106
107    pub fn active(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
108        (
109            self.background.as_ref().map(|b| (b.default, b.color)),
110            self.enabled_text,
111        )
112    }
113}
114
115impl Default for Style {
116    fn default() -> Self {
117        Self {
118            background: None,
119            enabled_text: Color::WHITE,
120            disabled_text: Color::from_rgb(0.5, 0.5, 0.5),
121        }
122    }
123}