From ea98b7e7d6141ddace491500376bd9e92228979f Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sat, 1 Aug 2020 22:51:44 -0700 Subject: [PATCH] slider rendering --- src/canvas/canvas_frame.rs | 4 +- src/drawables/rect.rs | 15 +++--- src/main.rs | 100 +++++++++++++++++++++---------------- 3 files changed, 67 insertions(+), 52 deletions(-) diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index 86a2f9c0..8754dabf 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -29,7 +29,9 @@ impl CanvasFrame { /// Push this drawable onto the back of the accumulator pub fn draw(&mut self, drawable: &dyn Drawable) { - drawable.get().iter().map(|x| {self.map.push(x.clone())}); + for i in drawable.get() { + self.map.push(i); + } } } diff --git a/src/drawables/rect.rs b/src/drawables/rect.rs index dc76274c..d949db13 100644 --- a/src/drawables/rect.rs +++ b/src/drawables/rect.rs @@ -17,29 +17,30 @@ impl Rect { /// pub fn new(position: (f32, f32), size: (f32, f32), - depth: u32) -> Rect { + 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: [0.0, 1.0, 1.0, 0.5] }, + color: [color.0, color.1, color.2, color.3] }, ColorVertex3D{ v_position: [position.0, position.1 + size.1, normalized_depth], // bottom left - color: [1.0, 1.0, 1.0, 1.0] }, + 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: [1.0, 1.0, 1.0, 1.0] }, + color: [color.0, color.1, color.2, color.3] }, ColorVertex3D{ v_position: [position.0, position.1, normalized_depth], // top left - color: [1.0, 1.0, 1.0, 1.0] }, + 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: [1.0, 1.0, 1.0, 1.0] }, + color: [color.0, color.1, color.2, color.3] }, ColorVertex3D{ v_position: [position.0 + size.0, position.1, normalized_depth], // top right - color: [1.0, 1.0, 1.0, 1.0] }, + color: [color.0, color.1, color.2, color.3] }, ]; Rect { diff --git a/src/main.rs b/src/main.rs index 300dae3b..87698ff4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,8 +50,9 @@ pub mod button_m { use crate::drawables::rect::Rect; use crate::drawables::sprite::Sprite; use std::collections::HashSet; - use crate::canvas::canvas_frame::Drawable; + use crate::canvas::canvas_frame::{Drawable, Eventable}; use crate::util::vertex::VertexType; + use winit::event::Event; pub struct Slider { handle : Rect, @@ -68,12 +69,17 @@ pub mod button_m { // render the guide first - let left_guide_bar = Rect::new((position.0 as f32, position.1 as f32), (0.1, 0.1), 1); - let right_guide_bar = Rect::new((position.0 + size.0 as f32, position.1 as f32), (0.1, 0.1), 1); - let line = Rect::new((position.0 as f32, position.1 - (size.1 / 2.0) as f32), (0.1, 0.1), 1); + 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.3, 0.3), 1); + let handle = Rect::new((position.0 + (size.0 * scale) as f32, position.1 as f32), (0.03, size.1), 1, rg); Slider { @@ -97,6 +103,12 @@ pub mod button_m { } } + impl Eventable for Slider { + fn notify(&mut self, event: &Event<'a, _>) -> () { + unimplemented!() + } + } + } pub fn main() { hprof::start_frame(); @@ -170,15 +182,15 @@ pub fn main() { let mut funky_sprite = Sprite::new((0.0, 0.5), (0.5, 0.5), 0, funky_handle.clone()); let sfml_sprite = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone()); - let rect = Rect::new((-0.5, -0.5), (0.5, 0.5), 1); - // let slider = button_m::Slider::new((0.5, 0.1), (0.1,0.1), 30000); + + let slider = button_m::Slider::new((0.3, 0.04), (-0.5,-0.3), 30000); // how do i register a sprite to get events... // explicit is much easier //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); + // let text_sprite = Text::new((-0.1, -0.1), (10.0, 10.0), 1); //let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0)); drop(q2); @@ -188,40 +200,40 @@ pub fn main() { let event_loop_proxy = events_loop.create_proxy(); - std::thread::spawn(move || { - - let mut gilrs = Gilrs::new().unwrap(); - // Iterate over all connected gamepads - let mut gamepad : Option = None; - for (_id, gamepad_) in gilrs.gamepads() { - if gamepad_.name() == "PS4" { - gamepad = Some(gamepad_); - } - println!("{} is {:?} {:?}", gamepad_.name(), gamepad_.power_info(), gamepad_.id()); - } - let mut active_gamepad = None; - - loop { - - while let Some(GilEvent { id, event, time }) = gilrs.next_event() { - println!("{:?} New event from {}: {:?}", time, id, event); - active_gamepad = Some(id); - event_loop_proxy.send_event(TrEvent::GamepadEvent { - gil_event: GilEvent {id, event, time} - }).ok(); - } - - // // You can also use cached gamepad state - // if let Some(gamepad) = active_gamepad.map(|id| gilrs.gamepad(id)) { - // if gamepad.is_pressed(Button::South) { - // println!("Button South is pressed (XBox - A, PS - X)"); - // } - // } - - std::thread::sleep(std::time::Duration::from_millis(50)); - - } - }); + // std::thread::spawn(move || { + // + // let mut gilrs = Gilrs::new().unwrap(); + // // Iterate over all connected gamepads + // let mut gamepad : Option = None; + // for (_id, gamepad_) in gilrs.gamepads() { + // if gamepad_.name() == "PS4" { + // gamepad = Some(gamepad_); + // } + // println!("{} is {:?} {:?}", gamepad_.name(), gamepad_.power_info(), gamepad_.id()); + // } + // let mut active_gamepad = None; + // + // loop { + // + // while let Some(GilEvent { id, event, time }) = gilrs.next_event() { + // println!("{:?} New event from {}: {:?}", time, id, event); + // active_gamepad = Some(id); + // event_loop_proxy.send_event(TrEvent::GamepadEvent { + // gil_event: GilEvent {id, event, time} + // }).ok(); + // } + // + // // // You can also use cached gamepad state + // // if let Some(gamepad) = active_gamepad.map(|id| gilrs.gamepad(id)) { + // // if gamepad.is_pressed(Button::South) { + // // println!("Button South is pressed (XBox - A, PS - X)"); + // // } + // // } + // + // std::thread::sleep(std::time::Duration::from_millis(50)); + // + // } + // }); // Events loop is borrowed from the surface events_loop.run(move |event, _, control_flow| { @@ -258,9 +270,9 @@ pub fn main() { let mut canvas_frame = CanvasFrame::default(); canvas_frame.draw(&funky_sprite); - canvas_frame.draw(&text_sprite); + // canvas_frame.draw(&text_sprite); canvas_frame.draw(&compu_sprite1); -// canvas_frame.draw(&slider); + canvas_frame.draw(&slider); let mut compu_frame = CompuFrame::new(); compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);