amberwindow/window/widgets/
checkbox.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use macroquad::prelude::*;

/// Widget > Checkbox (Toggled bool value).
#[derive(Clone, Debug)]
pub struct Checkbox {
    pub text: String,
    pub rect: Rect,
    pub color: Color,
    pub bg_color: Color,
    pub font: Option<Font>,
    pub queue_free: bool,
    pub uuid: &'static str,
    pub box_rect: Rect,
    pub value: bool,
    pub hovering: bool,
    pub pressed: bool,
    pub is_just_pressed: bool,
}
impl Checkbox {
    pub fn new(
        text: &str,
        font: Option<Font>,
        ticked: Option<bool>,
        color: Option<Color>,
        uuid: Option<&'static str>,
    ) -> Self {
        let mut x = Self {
            text: text.to_owned(),
            uuid: uuid.unwrap_or(""),
            rect: Rect::new(0., 0., 0., 0.),
            color: color.unwrap_or(WHITE),
            font: font.clone(),
            queue_free: false,
            box_rect: Rect::new(0., 0., 15., 15.),
            value: ticked.unwrap_or(false),
            hovering: false,
            pressed: false,
            bg_color: Color::new(1.0, 0.7, 0., 1.0),
            is_just_pressed: false,
        };

        let dim = measure_text(&x.text, None, 16, 1f32);
        x.rect.w = dim.width * 1.2 + 7.0 + x.box_rect.w;
        x.rect.h = x.box_rect.h + 3.0;

        x
    }

    pub fn set_uuid(&mut self, uuid: &'static str) -> &mut Self {
        self.uuid = uuid;
        self
    }

    pub fn set_text(&mut self, text: String) -> &mut Self {
        self.text = text;
        self
    }

    pub fn update(&mut self, selected: bool, mouse_position: Vec2, mouse_released: bool) {
        let dim = measure_text(&self.text, None, 16, 1f32);
        self.rect.w = dim.width * 1.2 + 7.0 + self.box_rect.w;
        self.rect.h = self.box_rect.h + 3.0;
        self.rect.y -= self.box_rect.h;

        self.is_just_pressed = false;

        if mouse_released && self.hovering && self.pressed && selected {
            self.value = !self.value;
            self.is_just_pressed = true;
        }

        if !is_mouse_button_down(MouseButton::Left) {
            self.pressed = false;
        }

        if self.rect.contains(mouse_position) {
            self.hovering = true;
            if is_mouse_button_pressed(MouseButton::Left) {
                self.pressed = true;
            }
        } else {
            self.hovering = false;
        }
    }

    pub fn render(&mut self) {
        let dim = measure_text(&self.text.to_string(), None, 16, 1f32);
        let dim_some = measure_text(&self.text.to_string(), self.font.as_ref(), 16, 1f32);

        let height_diff = dim.height / dim_some.height;
        self.rect.w = dim.width * 1.2 + 7.0 + self.box_rect.w;
        self.rect.h = self.box_rect.h + 3.0;

        let bg_color = match self.value {
            true => self.bg_color,
            false => Color::from_vec(self.bg_color.to_vec() - vec4(0., 0., 0., 0.4)),
        };

        // RECTANGLE
        draw_rectangle(
            self.rect.x,
            self.rect.y,
            self.box_rect.w,
            self.box_rect.h,
            match (self.hovering, self.pressed) {
                (false, false) => match self.value {
                    true => self.bg_color,
                    false => Color::from_vec(bg_color.to_vec() - vec4(0., 0., 0., 0.5)),
                },
                (_, true) => Color::from_vec(bg_color.to_vec() - vec4(0., 0., 0., 0.5)),
                (true, _) => Color::from_vec(bg_color.to_vec() - vec4(0., 0., 0., 0.4)),
            },
        );

        // ICON
        if self.value {
            draw_line(
                self.rect.x + 3.,
                self.rect.y + 7.,
                self.rect.x + self.box_rect.w / 2.,
                self.rect.y + 5. + self.box_rect.h / 2.,
                2.,
                WHITE
            );
            draw_line(
                self.rect.x + self.box_rect.w / 2.,
                self.rect.y + 4. + self.box_rect.h / 2.,
                self.rect.x + self.box_rect.w - 3.,
                self.rect.y + 2.,
                2.,
                WHITE
            );
        }

        // TEXT
        draw_text_ex(
            self.text.as_str(),
            self.rect.x + self.box_rect.w + 5.0,
            self.rect.y + self.rect.h / 1.5,
            TextParams {
                font: self.font.as_ref(),
                font_size: 16,
                color: self.color,
                font_scale: height_diff,
                ..Default::default()
            },
        );
    }
}