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
use algebr::Vec2;

use crate::{position::Rect, style::Style, Shape, ShapeType};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FontWeight {
    Regular,
    Bold,
    Italic,
    BoldItalic,
}

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

#[derive(Debug, Clone, PartialEq)]
pub struct Text {
    pub(crate) pos: Rect,
    pub(crate) text: String,
    pub(crate) style: Option<Style>,
    pub(crate) align: TextAlign,
    pub(crate) font_size: f32,
    pub(crate) font_weight: FontWeight,
}
crate::impl_pos!(Text);
crate::impl_style!(Text);
impl Text {
    pub const fn new(text: String) -> Self {
        Text {
            pos: Rect::new(),
            text,
            style: None,
            align: TextAlign::Left,
            font_size: 16.,
            font_weight: FontWeight::Regular,
        }
    }

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

    pub const fn with_align(mut self, align: TextAlign) -> Self {
        self.align = align;
        self
    }

    pub const fn with_font_size(mut self, font_size: f32) -> Self {
        self.font_size = font_size;
        self
    }

    pub const fn with_font_weight(mut self, font_weight: FontWeight) -> Self {
        self.font_weight = font_weight;
        self
    }
}

impl Into<Shape> for Text {
    fn into(self) -> Shape {
        Shape {
            pos: self.pos.with_size(Vec2::ones()),
            style: self.style,
            shape_type: ShapeType::Text {
                text: self.text,
                align: self.align,
                font_size: self.font_size,
                font_weight: self.font_weight,
            },
        }
    }
}