moved over to the enum method of drawing. Not flexible, but type safe
This commit is contained in:
283
src/sprite.rs
283
src/sprite.rs
@@ -1,306 +1,45 @@
|
||||
use std::sync::Arc;
|
||||
use crate::util::vertex_3d::Vertex3D;
|
||||
use crate::canvas::*;
|
||||
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, CanvasTextureHandle, Handle};
|
||||
use crate::canvas::managed::shader::text_shader::GlyphInstance;
|
||||
use crate::canvas::canvas_frame::{DrawableTest, Drawable};
|
||||
use crate::{VertexTypes, ImplVertexData1};
|
||||
use crate::canvas::canvas_frame::dynhash::DynHash;
|
||||
use crate::util::vertex::{VertexTypes, TextureVertex2D, Vertex3D};
|
||||
|
||||
///
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Sprite {
|
||||
|
||||
pub verts: VertexTypes::TexturedType(vec![]),
|
||||
|
||||
pub vertices: [(f32, f32, f32); 6],
|
||||
pub ti_position: [(f32, f32); 6],
|
||||
pub verts: VertexTypes,
|
||||
|
||||
position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
color: (f32, f32, f32, f32),
|
||||
|
||||
textured: bool,
|
||||
texture_handle: Option<Arc<CanvasTextureHandle>>,
|
||||
|
||||
value: GlyphInstance,
|
||||
}
|
||||
|
||||
/// Container class which implements drawable.
|
||||
impl Sprite {
|
||||
pub fn new(position: (f32, f32), size: (f32, f32)) -> Sprite {
|
||||
Sprite::new_with_color(position, size, 0, (0., 0., 0., 0.))
|
||||
}
|
||||
|
||||
pub fn new_with_color(position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
depth: u32,
|
||||
color: (f32, f32, f32, f32)) -> Sprite {
|
||||
let normalized_depth = (depth as f32 / 255.0);
|
||||
|
||||
Sprite {
|
||||
vertices: [
|
||||
(position.0, position.1, normalized_depth), // top left
|
||||
(position.0, position.1 + size.1, normalized_depth), // bottom left
|
||||
(position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right
|
||||
(position.0, position.1, normalized_depth), // top left
|
||||
(position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right
|
||||
(position.0 + size.0, position.1, normalized_depth), // top right
|
||||
],
|
||||
|
||||
position: position,
|
||||
ti_position: [
|
||||
(-0.0, -0.0), // top left
|
||||
(-0.0, 1.0), // bottom left
|
||||
(1.0, 1.0), // bottom right
|
||||
(-0.0, -0.0), // top left
|
||||
(1.0, 1.0), // bottom right
|
||||
(1.0, -0.0), // top right
|
||||
],
|
||||
size: size,
|
||||
color: color,
|
||||
textured: false,
|
||||
texture_handle: None,
|
||||
value: GlyphInstance {
|
||||
screen_position: (0.0, 0.0),
|
||||
atlas_position: (0.0, 0.0),
|
||||
atlas_size: (0.0, 0.0),
|
||||
scale: 0.0,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub fn new_with_texture(position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
depth: u32,
|
||||
texture_handle: Arc<CanvasTextureHandle>) -> Sprite {
|
||||
pub fn new(position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
depth: u32,
|
||||
texture_handle: Arc<CanvasTextureHandle>) -> Sprite {
|
||||
|
||||
let normalized_depth = (depth as f32 / 255.0);
|
||||
|
||||
Sprite {
|
||||
vertices: [
|
||||
(position.0, position.1, normalized_depth), // top left
|
||||
(position.0, position.1 + size.1, normalized_depth), // bottom left
|
||||
(position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right
|
||||
(position.0, position.1, normalized_depth), // top left
|
||||
(position.0 + size.0, position.1 + size.1, normalized_depth), // bottom right
|
||||
(position.0 + size.0, position.1, normalized_depth), // top right
|
||||
],
|
||||
verts: VertexTypes::TextureType(Vec::new(), texture_handle),
|
||||
position: position,
|
||||
ti_position: [
|
||||
(-0.0, -0.0), // top left
|
||||
(-0.0, 1.0), // bottom left
|
||||
(1.0, 1.0), // bottom right
|
||||
(-0.0, -0.0), // top left
|
||||
(1.0, 1.0), // bottom right
|
||||
(1.0, -0.0), // top right
|
||||
],
|
||||
size: size,
|
||||
color: (1.0, 0.0, 0.0, 1.0),
|
||||
textured: true,
|
||||
texture_handle: Some(texture_handle.clone()),
|
||||
value: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: Into<VertexTypes>, H: Handle + DynHash> DrawableTest<V, H> for Sprite{
|
||||
|
||||
fn get_vertices(&self) -> V {
|
||||
VertexTypes::TexturedType(vec![ImplVertexData1{ x: 0, y: 0 }])
|
||||
impl DrawableTest for Sprite{
|
||||
fn get(&self) -> VertexTypes {
|
||||
self.verts.clone()
|
||||
}
|
||||
|
||||
fn get_handle(&self) -> H {
|
||||
self.texture_handle.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for Sprite {
|
||||
fn get_vertices(&self) -> Vec<(f32, f32, f32)> {
|
||||
self.vertices.to_vec()
|
||||
}
|
||||
|
||||
fn get_color(&self) -> (f32, f32, f32, f32) {
|
||||
self.color.clone()
|
||||
}
|
||||
|
||||
fn get_ti_coords(&self) -> Vec<(f32, f32)> {
|
||||
self.ti_position.to_vec()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Poly {
|
||||
pub vertices: Vec<(f32, f32, f32)>,
|
||||
pub ti_position: Vec<(f32, f32)>,
|
||||
|
||||
position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
color: (f32, f32, f32, f32),
|
||||
|
||||
textured: bool,
|
||||
texture_handle: Option<Arc<CanvasTextureHandle>>,
|
||||
|
||||
// ==================================
|
||||
}
|
||||
|
||||
/// Container class which implements drawable.
|
||||
impl Poly {
|
||||
pub fn new(position: (f32, f32), size: (f32, f32)) -> Poly {
|
||||
Poly::new_with_color(position, size, 0, (0., 0., 0., 0.))
|
||||
}
|
||||
|
||||
pub fn new_with_color(position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
depth: u32,
|
||||
color: (f32, f32, f32, f32)) -> Poly {
|
||||
let normalized_depth = (depth as f32 / 255.0);
|
||||
|
||||
Poly {
|
||||
vertices: vec![
|
||||
(-0.5, -0.5, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(-0.25, 0.0, normalized_depth),
|
||||
(-0.25, 0.0, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(0.0, 0.5, normalized_depth),
|
||||
(0.25, 0.0, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(0.0, 0.5, normalized_depth),
|
||||
(0.5, -0.5, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(0.25, 0.0, normalized_depth),
|
||||
(0.25, -0.5, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(0.5, -0.5, normalized_depth),
|
||||
(0.25, -0.5, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(0.0, -0.1, normalized_depth),
|
||||
(-0.25, -0.5, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(0.0, -0.1, normalized_depth),
|
||||
(-0.5, -0.5, normalized_depth),
|
||||
(-1.0, 1.0, normalized_depth),
|
||||
(-0.25, -0.5, normalized_depth),
|
||||
],
|
||||
|
||||
position: position,
|
||||
ti_position: vec![
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
(0.0, 0.0),
|
||||
],
|
||||
size: size,
|
||||
color: color,
|
||||
textured: false,
|
||||
texture_handle: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for Poly {
|
||||
fn get_vertices(&self) -> Vec<(f32, f32, f32)> {
|
||||
self.vertices.to_vec()
|
||||
}
|
||||
|
||||
fn get_color(&self) -> (f32, f32, f32, f32) {
|
||||
self.color.clone()
|
||||
}
|
||||
|
||||
fn get_ti_coords(&self) -> Vec<(f32, f32)> {
|
||||
self.ti_position.to_vec()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Text {
|
||||
position: (f32, f32, f32),
|
||||
scale: f32,
|
||||
color: (f32, f32, f32, f32),
|
||||
|
||||
text_handle: Arc<CanvasFontHandle>,
|
||||
|
||||
}
|
||||
|
||||
/// Container class which implements drawable.
|
||||
impl Text {
|
||||
pub fn new(position: (f32, f32), size: (f32, f32), font_handle: Arc<CanvasFontHandle>) -> Text {
|
||||
Text::new_with_color(position, size, 0, (0., 0., 0., 0.), font_handle)
|
||||
}
|
||||
|
||||
pub fn new_with_color(position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
depth: u32,
|
||||
color: (f32, f32, f32, f32),
|
||||
handle: Arc<CanvasFontHandle>) -> Text {
|
||||
let normalized_depth = (depth as f32 / 255.0);
|
||||
|
||||
Text {
|
||||
position: (position.0, position.1, normalized_depth),
|
||||
scale: 0.0,
|
||||
color,
|
||||
text_handle: handle,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TextHandle {
|
||||
fn do_nothing() -> u32;
|
||||
}
|
||||
|
||||
pub trait TextInstance {
|
||||
fn get_thing() -> Vec<(u32, u32, u32)>;
|
||||
}
|
||||
|
||||
pub trait TextVertex {
|
||||
fn get_vertices() -> Vec<(u32, u32, u32)>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user