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 super::*;

/// A text label widget using a font resource to render the text.
pub struct Label {
    font_ref: FontRef,

    pos: (i32, i32),

    text: String
}

impl Label {
    /// Create a new label for rendering of static text.
    pub fn new(font_ref: FontRef) -> Self {
        Label { 
            font_ref,
            pos: (0, 0),
            text: String::new()
        }
    }

    /// Get the position.
    pub fn pos(&self) -> (i32, i32) {
        self.pos
    }

    /// Map a position.
    pub fn with_pos(mut self, x: i32, y: i32) -> Self {
        self.pos = (x, y);

        self
    }

    /// Change the position.
    pub fn set_pos(&mut self, x: i32, y: i32) {
        self.pos = (x, y);
    }

    /// Get the text.
    pub fn text(&self) -> &String {
        &self.text
    }

    /// Map the label text.
    pub fn with_text(mut self, text: &str) -> Self {
        self.text = String::from(text);

        self
    }

    /// Update the text of the label.
    pub fn set_text(&mut self, text: &str) {
        self.text = String::from(text);
    }
}

impl Control for Label {
    fn update(&mut self, _args: &ControlState, _res: &Resources) { }

    fn draw(&self, buffer: &mut Vec<u32>, buffer_width: usize, res: &Resources) {
        let font = res.get_font(self.font_ref).unwrap();

        font.draw_string(buffer, buffer_width, &self.text, self.pos);
    }

    fn control_type(&self) -> ControlType {
        ControlType::Label
    }

    fn as_any(&self) -> &Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut Any {
        self
    }
}