|
|
|
use crate::canvas::{Drawable, CanvasTextureHandle, CanvasImageHandle};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Sprite {
|
|
|
|
pub vertices: [(f32, f32); 6],
|
|
|
|
|
|
|
|
position: (f32, f32),
|
|
|
|
size: (f32, f32),
|
|
|
|
color: (f32, f32, f32, f32),
|
|
|
|
|
|
|
|
textured: bool,
|
|
|
|
texture_handle: Option<Arc<CanvasTextureHandle>>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sprite {
|
|
|
|
|
|
|
|
pub fn new(position: (f32, f32), size: (f32, f32)) -> Sprite {
|
|
|
|
Sprite::new_with_color(position, size, (0.,0.,0.,0.))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_color(position: (f32, f32),
|
|
|
|
size: (f32, f32),
|
|
|
|
color: (f32, f32, f32, f32)) -> Sprite {
|
|
|
|
|
|
|
|
let fsize = (size.0 as f32, size.1 as f32);
|
|
|
|
|
|
|
|
Sprite {
|
|
|
|
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: color,
|
|
|
|
textured: false,
|
|
|
|
texture_handle: None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_texture(position: (f32, f32), size: (f32, f32), texture_handle: Arc<CanvasTextureHandle>) -> Sprite {
|
|
|
|
|
|
|
|
let fsize = (size.0 as f32, size.1 as f32);
|
|
|
|
|
|
|
|
Sprite {
|
|
|
|
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),
|
|
|
|
textured: true,
|
|
|
|
texture_handle: Some(texture_handle.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drawable for Sprite {
|
|
|
|
fn get_vertices(&self) -> Vec<(f32,f32)> {
|
|
|
|
self.vertices.to_vec()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_color(&self) -> (f32, f32, f32, f32) {
|
|
|
|
self.color.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_texture_handle(&self) -> Option<Arc<CanvasTextureHandle>> {
|
|
|
|
match self.textured {
|
|
|
|
true => {
|
|
|
|
self.texture_handle.clone()
|
|
|
|
},
|
|
|
|
false => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_image_handle(&self) -> Option<Arc<CanvasImageHandle>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
let vertex_buffer = {
|
|
|
|
|
|
|
|
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), [
|
|
|
|
ColoredVertex2D { position: [ 1.0, 1.0 ], color },
|
|
|
|
ColoredVertex2D { position: [ 1.0, 0.5 ], color },
|
|
|
|
ColoredVertex2D { position: [ 0.5, 0.5 ], color },
|
|
|
|
ColoredVertex2D { position: [ 0.5, 1.0 ], color },
|
|
|
|
].iter().cloned()).unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
*/
|