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
use crate::canvas::{CanvasImageHandle, Drawable, CanvasTextureHandle};
use std::sync::Arc;

pub struct CompuSprite {
    vertices: [(f32, f32); 6],
    position: (f32, f32),
    size: (f32, f32),
    color: (f32, f32, f32, f32),
    image_handle: Arc<CanvasImageHandle>,
}

impl CompuSprite {
    pub fn new(position: (f32, f32),
               size: (f32, f32),
               image_handle: Arc<CanvasImageHandle>) -> CompuSprite {
        let fsize = (size.0 as f32, size.1 as f32);

        CompuSprite {
            vertices: [
                (position.0, position.1),                     // top left
                (position.0, position.1 + fsize.1),           // bottom left
                (position.0 + fsize.0, position.1 + fsize.1), // bottom right
                (position.0, position.1),                     // top left
                (position.0 + fsize.0, position.1 + fsize.1), // bottom right
                (position.0 + fsize.0, position.1),           // top right
            ],

            position: position,
            size: size,
            color: (0.0, 0.0, 0.0, 0.0),
            image_handle: image_handle.clone(),
        }
    }
}

impl Drawable for CompuSprite {
    fn get_vertices(&self) -> Vec<(f32, f32)> {
        self.vertices.to_vec()
    }

    fn get_color(&self) -> (f32, f32, f32, f32) {
        self.color
    }

    fn get_texture_handle(&self) -> Option<Arc<CanvasTextureHandle>> {
        None
    }

    fn get_image_handle(&self) -> Option<Arc<CanvasImageHandle>> {
        Some(self.image_handle.clone())
    }
}