diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index 25fb919b..49b4c279 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -8,6 +8,7 @@ use vulkano::pipeline::vertex::Vertex; use std::any::Any; use crate::{VertexTypeContainer, Move, Geom}; use winit::event::Event; +use crate::util::tr_event::TrEvent; /// Trait which may be inherited by objects that wish to be drawn to the screen pub trait Drawable { @@ -22,7 +23,7 @@ pub trait Drawable { /// Trait which may be inherited by objects that wish to receive events pub trait Eventable { - fn notify(&mut self, event: &Event) -> (); + fn notify(&mut self, event: &TrEvent) -> (); } /// Trait which may be inherited by objects that wish to be updated diff --git a/src/drawables/slider.rs b/src/drawables/slider.rs index eb1f2c2f..fcb6e552 100644 --- a/src/drawables/slider.rs +++ b/src/drawables/slider.rs @@ -1,11 +1,12 @@ use std::collections::HashSet; -use winit::event::Event; +use winit::event::{Event, ElementState, MouseButton}; use crate::canvas::canvas_frame::{Drawable, Eventable}; use crate::drawables::rect::Rect; use crate::drawables::sprite::Sprite; use crate::util::vertex::VertexTypeContainer; +use crate::util::tr_event::{TrEvent, TrWindowEvent}; #[derive(Debug, Clone)] pub struct Slider { @@ -43,6 +44,26 @@ impl Slider { value, } } + + pub fn update(&mut self) { + + // 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((self.position.0, self.position.1), (2.0, self.size.1), 1, red); + let right_guide_bar = Rect::new((self.position.0 + self.size.0, self.position.1), (2.0, self.size.1), 1, blue); + let line = Rect::new((self.position.0, self.position.1 - (self.size.1 / 2.0)), (self.size.0, 2.0), 1, green); + + let scale = self.value as f32 / u16::max_value() as f32; + let handle = Rect::new((self.position.0 + (self.size.0 * scale), self.position.1), (15.0, self.size.1), 1, rg); + + self.handle = handle; + self.guide = vec![left_guide_bar, right_guide_bar, line]; + self.scaler = 255; + } } impl Drawable for Slider { @@ -85,7 +106,24 @@ impl Drawable for Slider { } impl Eventable for Slider { - fn notify(&mut self, event: &Event) -> () { - unimplemented!() + fn notify(&mut self, event: &TrEvent) -> () { + match event { + TrEvent::WindowEvent { event: TrWindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { + + + match button { + MouseButton::Left => { + if *state == ElementState::Pressed { + self.position.0 += 30.0; + self.value += 10; + self.update() + } + } + _ => {} + } + } + _ => {} + } + } } diff --git a/src/drawables/sprite.rs b/src/drawables/sprite.rs index 9780cd19..f72f61fd 100644 --- a/src/drawables/sprite.rs +++ b/src/drawables/sprite.rs @@ -4,6 +4,7 @@ use crate::canvas::managed::handles::{CanvasFontHandle, CanvasImageHandle, Canva use crate::canvas::canvas_frame::{Drawable, Eventable, Updatable}; use crate::util::vertex::{VertexTypeContainer, TextureVertex3D, Vertex3D}; use winit::event::{DeviceEvent, MouseButton, ElementState, Event, WindowEvent}; +use crate::util::tr_event::{TrEvent, TrWindowEvent}; /// #[derive(Debug, Clone)] @@ -85,9 +86,9 @@ impl Drawable for Sprite { } impl Eventable for Sprite { - fn notify(&mut self, event: &Event) -> () { + fn notify(&mut self, event: &TrEvent) -> () { match event { - Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { + TrEvent::WindowEvent { event: TrWindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { match button { MouseButton::Left => { if *state == ElementState::Pressed { diff --git a/src/main.rs b/src/main.rs index 3b6ed65f..ff6a97af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -141,11 +141,14 @@ impl<'a> System<'a> for EventSystem { WriteStorage<'a, Draws>, Write<'a, PersistentState>, Write<'a, VkProcessor>, + Write<'a, Vec>> ); - fn run(&mut self, (mut draw, mut state, mut vk_processor): Self::SystemData) { - for draw_data in (&draw).join() { - //draw_data.1.notify() + fn run(&mut self, (mut draw, mut state, mut vk_processor, event_stack): Self::SystemData) { + for draw_data in (&mut draw).join() { + for event in event_stack.iter() { + draw_data.1.notify(event) + } } } } @@ -259,7 +262,8 @@ pub fn main() { // call the run method for the following systems & deps let mut dispatcher = DispatcherBuilder::new() // .with(SysA, "sys_a", &[]) - .with(RenderSystem, "render_s", &[]).build(); + .with(EventSystem, "event_s", &[]) + .with(RenderSystem, "render_s", &["event_s"]).build(); let event_loop_proxy = events_loop.create_proxy(); @@ -306,12 +310,22 @@ pub fn main() { world.write_resource::() .window_size = surface.window().inner_size().into(); } else { + // println!("{}", world.write_resource::>>().len()); world.write_resource::>>().clear(); } } Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { *control_flow = ControlFlow::Exit } + Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { + match button { + MouseButton::Left => { + if state == ElementState::Pressed { + } + } + _ => {} + } + } Event::WindowEvent { event: WindowEvent::Resized(new_size), .. } => { world.write_resource::() .swapchain_recreate_needed = true; diff --git a/src/util/tr_event.rs b/src/util/tr_event.rs index 42ba32d3..cb1a6723 100644 --- a/src/util/tr_event.rs +++ b/src/util/tr_event.rs @@ -16,7 +16,7 @@ pub enum TrEventExtension { }, } -#[derive(Clone)] +#[derive(Clone, Debug)] pub enum TrEvent { /// Custom events here