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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
pub enum HorizontalAlignment {
    Centered,
    Left,
    Right,
}

pub enum VerticalAlignment {
    Centered,
    Top,
    Bottom,
}

pub mod color {

    pub enum ColorError {
        HueOutOfRange,
        SaturationOutOfRange,
        LightnessOutOfRange,
    }

    pub const BLACK: Color = Color::rgb(0, 0, 0);
    pub const WHITE: Color = Color::rgb(255, 255, 255);

    pub struct Color {
        r: u8,
        g: u8,
        b: u8,
    }

    impl Color {
        pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
            Self { r, g, b }
        }

        pub fn hsl(h: u16, s: f32, l: f32) -> Result<Self, ColorError> {
            if h > 360 {
                return Err(ColorError::HueOutOfRange);
            }

            if !(0. ..=1.).contains(&s) {
                return Err(ColorError::SaturationOutOfRange);
            }

            if !(0. ..=1.).contains(&l) {
                return Err(ColorError::LightnessOutOfRange);
            }

            let (r, g, b) = if s == 0. {
                (l, l, l)
            } else {
                let q = if l < 0.5 {
                    l * (1_f32 + s)
                } else {
                    l + s - l * s
                };
                let p = 2. * l - q;
                (
                    Color::hue_to_rgb(p, q, f32::from(h) + 1. / 3.),
                    Color::hue_to_rgb(p, q, f32::from(h)),
                    Color::hue_to_rgb(p, q, f32::from(h) - 1. / 3.),
                )
            };

            Ok(Self {
                r: (r * 255.) as u8,
                g: (g * 255.) as u8,
                b: (b * 255.) as u8,
            })
        }

        fn hue_to_rgb(p: f32, q: f32, mut t: f32) -> f32 {
            if t < 0. {
                t += 1.;
            }

            if t > 1. {
                t -= 1.;
            }

            if t < 1. / 6. {
                return p + (q - p) * 6. * t;
            }

            if t < 0.5 {
                return q;
            }

            if t < 2. / 3. {
                return p + (q - p) * (2. / 3. - t) * 6.;
            }

            p
        }

        pub fn red(&self) -> u8 {
            self.r
        }

        pub fn blue(&self) -> u8 {
            self.b
        }

        pub fn green(&self) -> u8 {
            self.g
        }
    }
}

pub struct Text {
    color: color::Color,
    text: String,
}

impl Text {
    pub fn new(text: &str) -> Self {
        Self {
            text: text.to_owned(),
            color: color::BLACK,
        }
    }

    pub const fn color(mut self, color: color::Color) -> Self {
        self.color = color;
        self
    }

    pub fn text(&self) -> &str {
        &self.text
    }
}

pub struct CellLayout {
    horizontal_alignment: HorizontalAlignment,
    vertical_alignment: VerticalAlignment,
    horizontal_offset: i32,
    vertical_offset: i32,
    text: Option<Text>,
}

impl CellLayout {
    pub fn centered() -> Self {
        Self {
            horizontal_alignment: HorizontalAlignment::Centered,
            vertical_alignment: VerticalAlignment::Centered,
            horizontal_offset: 0,
            vertical_offset: 0,
            text: None,
        }
    }

    pub fn text(mut self, text: Text) -> Self {
        self.text = Some(text);
        self
    }

    pub fn align_horizontally(mut self, horizontal_alignment: HorizontalAlignment) -> Self {
        self.horizontal_alignment = horizontal_alignment;
        self
    }

    pub fn align_vertically(mut self, vertical_alignment: VerticalAlignment) -> Self {
        self.vertical_alignment = vertical_alignment;
        self
    }

    pub fn offset_horizontally(mut self, horizontal_offset: i32) -> Self {
        self.horizontal_offset = horizontal_offset;
        self
    }

    pub fn offset_vertically(mut self, vertical_offset: i32) -> Self {
        self.vertical_offset = vertical_offset;
        self
    }
}

pub enum Layout {
    Column(Column),
    Row(Vec<Box<dyn Component>>),
    Grid(Vec<Vec<Box<dyn Component>>>),
    Cell(CellLayout),
}

pub struct Column {
    components: Vec<Box<dyn Component>>,
}

impl Column {
    pub fn add_to_bottom(&mut self, component: Box<dyn Component>) {
        self.components.push(component);
    }

    pub fn add_to_top(&mut self, component: Box<dyn Component>) {
        let mut components = vec![component];
        components.append(&mut self.components);
        self.components = components;
    }

    pub fn into_layout(self) -> Layout {
        self.into()
    }
}

impl From<Column> for Layout {
    fn from(value: Column) -> Self {
        Self::Column(value)
    }
}

pub trait Component {
    fn layout(&self) -> &Layout;

    // Event listeners

    fn update(&mut self) {}
    fn on_mouse_down(&mut self) {}
    fn on_mouse_up(&mut self) {}
    fn on_key_down(&mut self) {}
    fn on_key_up(&mut self) {}
    fn on_right_mouse_down(&mut self) {}
    fn on_right_mouse_up(&mut self) {}
}