slider scales sorta

master
mitchellhansen 4 years ago
parent 0619b79d34
commit 799e337e37

@ -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)
)
]
}
}

@ -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!()
}
}

@ -25,7 +25,6 @@ use winit::event_loop::{ControlFlow, EventLoop, EventLoopProxy};
use winit::platform::unix::WindowBuilderExtUnix;
use winit::window::WindowBuilder;
use crate::button_m::Slider;
use crate::canvas::canvas_frame::{CanvasFrame, Drawable, Eventable, Updatable};
use crate::canvas::canvas_state::CanvasState;
use crate::canvas::managed::handles::{CanvasFontHandle, CanvasTextureHandle, Handle};
@ -40,6 +39,7 @@ use crate::util::timer::Timer;
use crate::util::tr_event::TrEvent;
use crate::util::vertex::{TextureVertex3D, VertexType};
use crate::vkprocessor::VkProcessor;
use crate::drawables::slider::Slider;
pub mod util;
pub mod vkprocessor;
@ -47,70 +47,6 @@ pub mod drawables;
pub mod canvas;
pub mod compute;
pub mod button_m {
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 as f32, position.1 as f32), (0.01, size.1), 1, red);
let right_guide_bar = Rect::new((position.0 + size.0 as f32, position.1 as f32), (0.01, size.1), 1, blue);
let line = Rect::new((position.0 as f32, position.1 - (size.1 / 2.0) as f32), (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) as f32, position.1 as f32), (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 = vec![self.handle.verts.clone()];
vertices.extend_from_slice(self.guide.iter()
.map(|x| x.clone().verts).collect::<Vec<VertexType>>().as_slice()
);
vertices
}
}
impl<T> Eventable<T> for Slider {
fn notify(&mut self, event: &Event<T>) -> () {
unimplemented!()
}
}
}
pub fn main() {
hprof::start_frame();
@ -188,7 +124,7 @@ pub fn main() {
(100.0, 150.0), 10, funky_handle.clone());
let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
let slider = button_m::Slider::new((0.3, 0.04), (-0.5, -0.3), 30000);
let slider = Slider::new((300.0, 50.0), (550.0, 100.0), 30000);
//let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
//let text_sprite = Text::new((-0.1, -0.1), (10.0, 10.0), 1);
@ -308,12 +244,12 @@ pub fn main() {
canvas_frame = CanvasFrame::new(window_size);
canvas_frame.draw(&funky_sprite);
// canvas_frame.draw(&compu_sprite1);
// canvas_frame.draw(&compu_sprite1);
canvas_frame.draw(&slider);
compu_frame = CompuFrame::new(window_size);
// compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
// compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);
// compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
{
let g = hprof::enter("Run");

Loading…
Cancel
Save