Skip to main content

ursus_ecs/components/
ui.rs

1use glam::Vec2;
2
3#[derive(Debug, Clone)]
4pub struct UiLayout {
5    pub anchor: Vec2,
6    pub pivot: Vec2,
7    pub offset: Vec2,
8}
9
10impl UiLayout {
11    pub fn top_left(offset: Vec2) -> Self {
12        Self { anchor: Vec2::ZERO, pivot: Vec2::ZERO, offset }
13    }
14    pub fn center() -> Self {
15        Self { anchor: Vec2::splat(0.5), pivot: Vec2::splat(0.5), offset: Vec2::ZERO }
16    }
17    pub fn top_right(offset: Vec2) -> Self {
18        Self { anchor: Vec2::new(1.0, 0.0), pivot: Vec2::new(1.0, 0.0), offset }
19    }
20}
21
22impl Default for UiLayout {
23    fn default() -> Self {
24        UiLayout::center()
25    }
26}
27
28#[derive(Debug, Clone)]
29pub struct UiText {
30    pub text: String,
31    pub font_size: f32,
32    pub color: [f32; 4],
33}
34
35impl UiText {
36    pub fn new(text: impl Into<String>) -> Self {
37        Self { text: text.into(), font_size: 14.0, color: [1.0; 4] }
38    }
39    pub fn with_size(mut self, size: f32) -> Self {
40        self.font_size = size;
41        self
42    }
43    pub fn with_color(mut self, color: [f32; 4]) -> Self {
44        self.color = color;
45        self
46    }
47}
48
49impl Default for UiText {
50    fn default() -> Self {
51        UiText::new("NoText")
52    }
53}
54
55#[derive(Debug, Clone)]
56pub struct UiRect {
57    pub size: Vec2,
58    pub color: [f32; 4],
59    pub border_radius: f32,
60}
61
62impl Default for UiRect {
63    fn default() -> Self {
64        UiRect { size: Vec2::ZERO, color: [1.0; 4], border_radius: 1.0 }
65    }
66}