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
use crate::prelude::*;
use crate::ui::*;

#[derive(Debug)]
pub struct Label {
    text: Text,
}

impl Label {
    pub fn new(text: Text) -> Self {
        Self { text }
    }

    pub fn full(
        str: &str,
        pos: TextPos,
        color: Color,
        font: PixelFont,
        positioning: Positioning,
        wrapping: WrappingStrategy,
    ) -> Self {
        Self {
            text: Text::new(
                str,
                pos,
                TextFormat::new(wrapping, font, color, positioning),
            ),
        }
    }

    pub fn multiline<P: Into<Coord>>(
        str: &str,
        pos: P,
        color: Color,
        font: PixelFont,
        width_px: usize,
    ) -> Self {
        Self {
            text: Text::new(
                str,
                TextPos::px(pos.into()),
                TextFormat::new(
                    WrappingStrategy::SpaceBeforeCol(font.px_to_cols(width_px)),
                    font,
                    color,
                    Positioning::LeftTop,
                ),
            ),
        }
    }
}

impl UiElement for Label {
    fn set_position(&mut self, top_left: Coord) {
        self.text = self.text.with_pos(TextPos::px(top_left));
    }

    fn bounds(&self) -> &Rect {
        self.text.bounds()
    }

    fn render(&self, graphics: &mut Graphics, _: &MouseData) {
        self.text.render(graphics);
    }

    fn update(&mut self, _: &Timing) {}

    fn set_state(&mut self, _: ElementState) {
        unimplemented!("Label doesn't support state");
    }

    fn get_state(&self) -> ElementState {
        ElementState::Normal
    }
}