slider scales sorta
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
pub mod slider;
|
||||
pub mod polygon;
|
||||
pub mod sprite;
|
||||
pub mod rect;
|
||||
|
||||
@@ -4,56 +4,80 @@ use crate::util::vertex::{VertexType, ColorVertex3D};
|
||||
///
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Rect {
|
||||
|
||||
pub verts: VertexType,
|
||||
|
||||
position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
color: (f32, f32, f32, f32),
|
||||
depth: f32,
|
||||
}
|
||||
|
||||
/// Container class which implements drawable.
|
||||
impl Rect {
|
||||
|
||||
///
|
||||
pub fn new(position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
depth: u32,
|
||||
color: (f32, f32, f32, f32)) -> Rect {
|
||||
|
||||
let normalized_depth = (depth as f32 / 255.0);
|
||||
|
||||
let verts = vec![
|
||||
ColorVertex3D{
|
||||
v_position: [position.0, position.1, normalized_depth], // top left
|
||||
color: [color.0, color.1, color.2, color.3] },
|
||||
ColorVertex3D{
|
||||
v_position: [position.0, position.1 + size.1, normalized_depth], // bottom left
|
||||
color: [color.0, color.1, color.2, color.3] },
|
||||
ColorVertex3D{
|
||||
v_position: [position.0 + size.0, position.1 + size.1, normalized_depth], // bottom right
|
||||
color: [color.0, color.1, color.2, color.3] },
|
||||
ColorVertex3D{
|
||||
v_position: [position.0, position.1, normalized_depth], // top left
|
||||
color: [color.0, color.1, color.2, color.3] },
|
||||
ColorVertex3D{
|
||||
v_position: [position.0 + size.0, position.1 + size.1, normalized_depth], // bottom right
|
||||
color: [color.0, color.1, color.2, color.3] },
|
||||
ColorVertex3D{
|
||||
v_position: [position.0 + size.0, position.1, normalized_depth], // top right
|
||||
color: [color.0, color.1, color.2, color.3] },
|
||||
];
|
||||
|
||||
Rect {
|
||||
verts: VertexType::ColorType(verts),
|
||||
position: position,
|
||||
size: size,
|
||||
color: color,
|
||||
depth: normalized_depth,
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_vertices(window_size: (u32, u32),
|
||||
position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
depth: f32,
|
||||
color: (f32, f32, f32, f32)) -> Vec<ColorVertex3D> {
|
||||
|
||||
let ss_position = (
|
||||
position.0 / window_size.0 as f32 - 1.0,
|
||||
position.1 / window_size.1 as f32 - 1.0
|
||||
);
|
||||
|
||||
let ss_size = (
|
||||
size.0 / window_size.0 as f32,
|
||||
size.1 / window_size.1 as f32
|
||||
);
|
||||
|
||||
vec![
|
||||
ColorVertex3D {
|
||||
v_position: [ss_position.0, ss_position.1, depth], // top left
|
||||
color: [color.0, color.1, color.2, color.3],
|
||||
},
|
||||
ColorVertex3D {
|
||||
v_position: [ss_position.0, ss_position.1 + ss_size.1, depth], // bottom left
|
||||
color: [color.0, color.1, color.2, color.3],
|
||||
},
|
||||
ColorVertex3D {
|
||||
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
|
||||
color: [color.0, color.1, color.2, color.3],
|
||||
},
|
||||
ColorVertex3D {
|
||||
v_position: [ss_position.0, ss_position.1, depth], // top left
|
||||
color: [color.0, color.1, color.2, color.3],
|
||||
},
|
||||
ColorVertex3D {
|
||||
v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
|
||||
color: [color.0, color.1, color.2, color.3],
|
||||
},
|
||||
ColorVertex3D {
|
||||
v_position: [ss_position.0 + ss_size.0, ss_position.1, depth], // top right
|
||||
color: [color.0, color.1, color.2, color.3],
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for Rect {
|
||||
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
|
||||
vec![self.verts.clone()]
|
||||
vec![
|
||||
VertexType::ColorType(
|
||||
Rect::generate_vertices(window_size, self.position, self.size, self.depth, self.color)
|
||||
)
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
68
src/drawables/slider.rs
Normal file
68
src/drawables/slider.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use winit::event::Event;
|
||||
|
||||
use crate::canvas::canvas_frame::{Drawable, Eventable};
|
||||
use crate::drawables::rect::Rect;
|
||||
use crate::drawables::sprite::Sprite;
|
||||
use crate::util::vertex::VertexType;
|
||||
|
||||
pub struct Slider {
|
||||
handle: Rect,
|
||||
guide: Vec<Rect>,
|
||||
|
||||
scaler: u32,
|
||||
position: (f32, f32),
|
||||
size: (f32, f32),
|
||||
value: u16,
|
||||
}
|
||||
|
||||
impl Slider {
|
||||
pub fn new(size: (f32, f32), position: (f32, f32), value: u16) -> Slider {
|
||||
|
||||
// render the guide first
|
||||
let red = (1.0, 0.0, 0.0, 0.0);
|
||||
let green = (0.0, 1.0, 0.0, 0.0);
|
||||
let blue = (0.0, 1.0, 1.0, 0.0);
|
||||
let rg = (1.0, 1.0, 0.0, 0.0);
|
||||
|
||||
let left_guide_bar = Rect::new((position.0, position.1), (0.01, size.1), 1, red);
|
||||
let right_guide_bar = Rect::new((position.0 + size.0, position.1), (0.01, size.1), 1, blue);
|
||||
let line = Rect::new((position.0, position.1 - (size.1 / 2.0) ), (size.0, 0.01), 1, green);
|
||||
|
||||
let scale = value as f32 / u16::max_value() as f32;
|
||||
let handle = Rect::new((position.0 + (size.0 * scale), position.1), (0.03, size.1), 1, rg);
|
||||
|
||||
Slider {
|
||||
handle: handle,
|
||||
guide: vec![left_guide_bar, right_guide_bar, line],
|
||||
scaler: 255,
|
||||
position,
|
||||
size,
|
||||
value,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for Slider {
|
||||
fn get(&self, window_size: (u32, u32)) -> Vec<VertexType> {
|
||||
let mut vertices = self.handle.get(window_size).clone();
|
||||
|
||||
vertices.extend_from_slice(
|
||||
self.guide.iter()
|
||||
.map(|x| x.get(window_size))
|
||||
.flatten()
|
||||
.collect::<Vec<VertexType>>()
|
||||
.as_slice()
|
||||
);
|
||||
|
||||
vertices.extend_from_slice(self.guide[0].get(window_size).as_slice());
|
||||
vertices
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Eventable<T> for Slider {
|
||||
fn notify(&mut self, event: &Event<T>) -> () {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user