aboutsummaryrefslogtreecommitdiff
path: root/src/widget.rs
blob: 5749ae28ca0cee1ce26a562038451a1f8b755157 (plain)
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
use crate::rect;

use sdl2::{rect::Rect, render::Canvas, video::Window};

#[derive(Debug, Clone)]
pub struct Container {
    pub start: (i32, i32),
    pub width: u32,
    pub height: u32,
}

pub enum Size {
    Max,
    Absolute(u32),
}

#[derive(Debug, Copy, PartialEq, Clone)]
pub enum Offset {
    Top(u32),
    Left(u32),
    Bottom(u32),
    Right(u32),
}

#[derive(Debug, Copy, PartialEq, Clone)]
pub enum HorAlign {
    Left,
    Center,
    Right,
}

#[derive(Debug, Copy, PartialEq, Clone)]
pub enum VertAlign {
    Top,
    Center,
    Bottom,
}

impl Offset {
    fn apply(self, size: u32) -> u32 {
        match self {
            Self::Top(t) => t,
            Self::Left(l) => l,
            Self::Bottom(b) => size - b,
            Self::Right(r) => size - r,
        }
    }
}

impl Container {
    pub fn uninit() -> Self {
        Self {
            start: (0, 0),
            width: 0,
            height: 0,
        }
    }

    pub fn new(start_x: Offset, start_y: Offset, canvas: &Canvas<Window>) -> Self {
        let (winsize_x, winsize_y) = canvas.window().size();
        Self {
            start: (
                start_x.apply(winsize_x) as i32,
                start_y.apply(winsize_y) as i32,
            ),
            width: 0,
            height: 0,
        }
    }

    pub fn width(mut self, size: Size, canvas: &Canvas<Window>) -> Self {
        match size {
            Size::Max => {
                let (winsize_x, _) = canvas.window().size();
                self.width = winsize_x;
            }
            Size::Absolute(x) => {
                self.width = x;
            }
        }
        self
    }

    pub fn height(mut self, size: Size, canvas: &Canvas<Window>) -> Self {
        match size {
            Size::Max => {
                let (_, winsize_y) = canvas.window().size();
                self.height = winsize_y;
            }
            Size::Absolute(y) => {
                self.height = y;
            }
        }
        self
    }

    pub fn place(&self, widget: &mut Container, hor_align: HorAlign, vert_align: VertAlign) {
        let x = self.start.0
            + match hor_align {
                HorAlign::Left => 0,
                HorAlign::Center => ((self.width / 2).saturating_sub(widget.width / 2)) as i32,
                HorAlign::Right => (self.width - widget.width) as i32,
            };
        let y = self.start.1
            + match vert_align {
                VertAlign::Top => 0,
                VertAlign::Center => ((self.height / 2).saturating_sub(widget.height / 2)) as i32,
                VertAlign::Bottom => (self.height - widget.height) as i32,
            };
        widget.start = (x, y);
    }

    pub fn area(&self) -> Rect {
        rect!(self.start.0, self.start.1, self.width, self.height)
    }
}

#[derive(Debug, Copy, Clone)]
pub struct Widget {
    width: u32,
    height: u32,
}

impl Widget {
    pub fn linear_layout(widgets: &[Widget], padding: u32) -> Widget {
        Widget {
            width: widgets.iter().fold(0, |acc, x| acc + x.width + padding),
            height: widgets.iter().map(|x| x.height).max().unwrap_or(0),
        }
    }
}