Skip to main content

sge_ui/base/
align.rs

1use sge_vectors::vec2;
2use strum::Display;
3
4use super::*;
5
6#[derive(Debug, Display, Clone, Copy)]
7pub enum AlignType {
8    Start,
9    Center,
10    End,
11}
12
13impl AlignType {
14    fn align(&self, min: f32, max: f32, length: f32) -> f32 {
15        match self {
16            Self::Start => min,
17            Self::End => max - length,
18            Self::Center => (min + max - length) / 2.0,
19        }
20    }
21}
22
23#[derive(Debug)]
24pub struct Align {
25    x: AlignType,
26    y: AlignType,
27    child: Child,
28}
29
30impl Align {
31    pub fn new(x: AlignType, y: AlignType, child: Child) -> UiRef {
32        Self { x, y, child }.to_ref()
33    }
34
35    pub fn center(child: Child) -> UiRef {
36        Self {
37            x: AlignType::Center,
38            y: AlignType::Center,
39            child,
40        }
41        .to_ref()
42    }
43
44    pub fn top_right(child: Child) -> UiRef {
45        Self {
46            x: AlignType::End,
47            y: AlignType::Start,
48            child,
49        }
50        .to_ref()
51    }
52
53    pub fn top_left(child: Child) -> UiRef {
54        Self {
55            x: AlignType::Start,
56            y: AlignType::Start,
57            child,
58        }
59        .to_ref()
60    }
61
62    pub fn bottom_right(child: Child) -> UiRef {
63        Self {
64            x: AlignType::End,
65            y: AlignType::End,
66            child,
67        }
68        .to_ref()
69    }
70
71    pub fn bottom_left(child: Child) -> UiRef {
72        Self {
73            x: AlignType::Start,
74            y: AlignType::End,
75            child,
76        }
77        .to_ref()
78    }
79
80    pub fn top_center(child: Child) -> UiRef {
81        Self {
82            x: AlignType::Center,
83            y: AlignType::Start,
84            child,
85        }
86        .to_ref()
87    }
88
89    pub fn bottom_center(child: Child) -> UiRef {
90        Self {
91            x: AlignType::Center,
92            y: AlignType::End,
93            child,
94        }
95        .to_ref()
96    }
97
98    pub fn center_right(child: Child) -> UiRef {
99        Self {
100            x: AlignType::End,
101            y: AlignType::Center,
102            child,
103        }
104        .to_ref()
105    }
106
107    pub fn center_left(child: Child) -> UiRef {
108        Self {
109            x: AlignType::Start,
110            y: AlignType::Center,
111            child,
112        }
113        .to_ref()
114    }
115}
116
117impl UiNode for Align {
118    fn preferred_dimensions(&self) -> Vec2 {
119        self.child.node.preferred_dimensions()
120    }
121
122    fn draw(&self, area: Area, ui: &UiState) -> Vec2 {
123        let inner_area = self.child.node.preferred_dimensions();
124        let tl = area.top_left;
125        let br = area.bottom_right();
126
127        let x = self.x.align(tl.x, br.x, inner_area.x);
128        let y = self.y.align(tl.y, br.y, inner_area.y);
129
130        let child_area = Area::new(vec2(x, y), inner_area);
131        self.child.node.draw(child_area, ui)
132    }
133}