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

/// The skin of the button is a sprite.
#[derive(Debug)]
pub struct Sprite {
    pub sprite_ref: SpriteRef,
    pos: (i32, i32),
}

impl Sprite {
    pub fn new_with_sprite(sprite_ref: SpriteRef) -> Self {
        Sprite {
            sprite_ref,
            pos: (0, 0),
        }
    }

    /// Retrieve 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);
    }
}

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

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

        let draw_size = sprite.size();

        sprite.blit_rect(buffer, buffer_width, self.pos, (0, 0, draw_size.0, draw_size.1));
    }

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

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

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