From 8da810f23a7ec4b670c70e302ca5f8d4fedbe4d4 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Fri, 31 Jul 2020 23:39:25 -0700 Subject: [PATCH] event work + mulling over sprite containers and event notification --- src/canvas/canvas_frame.rs | 4 +- src/compute/compu_frame.rs | 8 +- src/drawables/compu_sprite.rs | 4 +- src/drawables/polygon.rs | 4 +- src/drawables/rect.rs | 4 +- src/drawables/sprite.rs | 19 ++--- src/drawables/text.rs | 4 +- src/main.rs | 152 +++++++++++++++++++++++++--------- src/util/mod.rs | 3 +- src/util/tr_event.rs | 22 ++--- 10 files changed, 149 insertions(+), 75 deletions(-) diff --git a/src/canvas/canvas_frame.rs b/src/canvas/canvas_frame.rs index a3f96777..ad9b5a52 100644 --- a/src/canvas/canvas_frame.rs +++ b/src/canvas/canvas_frame.rs @@ -11,7 +11,7 @@ use winit::event::Event; /// Trait which may be inherited by objects that wish to be drawn to the screen pub trait Drawable { - fn get(&self) -> VertexTypes; + fn get(&self) -> Vec; } /// Trait which may be inherited by objects that wish to receive events @@ -29,7 +29,7 @@ impl CanvasFrame { /// Push this drawable onto the back of the accumulator pub fn draw(&mut self, drawable: &dyn Drawable) { - self.map.push(drawable.get()); + drawable.get().iter().map(|x| {self.map.push(x.clone())}); } } diff --git a/src/compute/compu_frame.rs b/src/compute/compu_frame.rs index 42452db7..9620dcd0 100644 --- a/src/compute/compu_frame.rs +++ b/src/compute/compu_frame.rs @@ -54,8 +54,12 @@ impl CompuFrame { kernel: Arc, sprite: &CompuSprite) { - if let VertexTypes::ImageType(a, b) = sprite.get() { - self.swapped_to_image.push((buffer, b, kernel)) + let compu_sprites = sprite.get(); + + if compu_sprites.len() == 1 { + if let VertexTypes::ImageType(a, b) = compu_sprites.first().unwrap() { + self.swapped_to_image.push((buffer, b.clone(), kernel)) + }; } } diff --git a/src/drawables/compu_sprite.rs b/src/drawables/compu_sprite.rs index 0ea58855..3ba684b4 100644 --- a/src/drawables/compu_sprite.rs +++ b/src/drawables/compu_sprite.rs @@ -53,7 +53,7 @@ impl CompuSprite { } impl Drawable for CompuSprite { - fn get(&self) -> VertexTypes { - self.verts.clone() + fn get(&self) -> Vec { + vec![self.verts.clone()] } } \ No newline at end of file diff --git a/src/drawables/polygon.rs b/src/drawables/polygon.rs index dc326a12..6535478a 100644 --- a/src/drawables/polygon.rs +++ b/src/drawables/polygon.rs @@ -61,8 +61,8 @@ impl Polygon { } } impl Drawable for Polygon { - fn get(&self) -> VertexTypes { - self.verts.clone() + fn get(&self) -> Vec { + vec![self.verts.clone()] } } diff --git a/src/drawables/rect.rs b/src/drawables/rect.rs index d867cb00..54043751 100644 --- a/src/drawables/rect.rs +++ b/src/drawables/rect.rs @@ -50,8 +50,8 @@ impl Rect { } } impl Drawable for Rect { - fn get(&self) -> VertexTypes { - self.verts.clone() + fn get(&self) -> Vec { + vec![self.verts.clone()] } } diff --git a/src/drawables/sprite.rs b/src/drawables/sprite.rs index a1df1c89..27a40067 100644 --- a/src/drawables/sprite.rs +++ b/src/drawables/sprite.rs @@ -18,9 +18,7 @@ pub struct Sprite { /// Container class which implements drawable. impl Sprite { - - - fn generate_verts(position: (f32, f32), size: (f32, f32), depth: f32,) -> Vec { + fn generate_verts(position: (f32, f32), size: (f32, f32), depth: f32) -> Vec { vec![ TextureVertex3D { v_position: [position.0, position.1, depth], // top left @@ -54,7 +52,6 @@ impl Sprite { size: (f32, f32), depth: u32, texture_handle: Arc) -> Sprite { - let normalized_depth = (depth as f32 / 255.0); Sprite { @@ -68,24 +65,26 @@ impl Sprite { } impl Drawable for Sprite { - fn get(&self) -> VertexTypes { - VertexTypes::TextureType(Sprite::generate_verts(self.position, self.size, self.depth), self.texture_handle.clone()) + fn get(&self) -> Vec { + vec![ + VertexTypes::TextureType( + Sprite::generate_verts(self.position, self.size, self.depth), + self.texture_handle.clone()) + ] } } impl Eventable for Sprite { fn notify(&mut self, event: &Event) -> () { match event { - Event::WindowEvent { event: WindowEvent::MouseInput{ device_id, state, button, modifiers }, .. } => { + Event::WindowEvent { event: WindowEvent::MouseInput { device_id, state, button, modifiers }, .. } => { match button { MouseButton::Left => { if *state == ElementState::Pressed { self.position = (self.position.0, self.position.1 + 0.1); } - }, - _ => { - } + _ => {} } } _ => {} diff --git a/src/drawables/text.rs b/src/drawables/text.rs index 7e53359a..b6a26f11 100644 --- a/src/drawables/text.rs +++ b/src/drawables/text.rs @@ -137,7 +137,7 @@ impl Text { } impl Drawable for Text { - fn get(&self) -> VertexTypes { - self.verts.clone() + fn get(&self) -> Vec { + vec![self.verts.clone()] } } diff --git a/src/main.rs b/src/main.rs index 6b4375a0..be334e9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,8 @@ use crate::util::timer::Timer; use crate::util::vertex::{TextureVertex3D, VertexTypes}; use crate::vkprocessor::VkProcessor; use std::path::Path; -use gilrs::{Gilrs, Button, Event as GilEvent}; +use gilrs::{Gilrs, Button, Event as GilEvent, GamepadId, Gamepad}; +use crate::util::tr_event::TrEvent; pub mod util; pub mod vkprocessor; @@ -44,6 +45,36 @@ pub mod drawables; pub mod canvas; pub mod compute; +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::util::vertex::VertexTypes; + + // Should I force these to be drawables + // or better, how do I force these to be drawables + enum GuiElements { + HANDLE(Rect), + GUIDE(Sprite) + } + + pub struct Slider { + sprites: HashSet, + } + + impl Drawable for Slider { + fn get(&self) -> Vec { + + self.sprites.into() + // self.sprites.iter().map(|v| { + // + // }) + + } + } + +} pub fn main() { hprof::start_frame(); @@ -58,7 +89,7 @@ pub fn main() { println!("Debug callback: {:?}", msg.description); }).ok(); - let mut events_loop = EventLoop::new(); + let mut events_loop = EventLoop::::with_user_event(); let mut surface = WindowBuilder::new() .with_inner_size(LogicalSize::new(800, 800)) @@ -69,7 +100,6 @@ pub fn main() { { let g = hprof::enter("vulkan preload"); processor.create_swapchain(instance.clone(), surface.clone()); - processor.preload_kernels(); processor.preload_shaders(); processor.preload_textures(); @@ -119,6 +149,8 @@ pub fn main() { 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); +// 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); @@ -129,33 +161,47 @@ pub fn main() { let l = hprof::enter("Loop"); - // while let true = processor.is_open() { - // - // // Take care of our timing - // { - // elapsed_time = timer.elap_time(); - // delta_time = elapsed_time - current_time; - // current_time = elapsed_time; - // if delta_time > 0.02 { - // delta_time = 0.02; - // } - // accumulator_time += delta_time; - // } - // - // while (accumulator_time - step_size) >= step_size { - // accumulator_time -= step_size; - // } - - let mut gilrs = Gilrs::new().unwrap(); - // Iterate over all connected gamepads - for (_id, gamepad) in gilrs.gamepads() { - println!("{} is {:?}", gamepad.name(), gamepad.power_info()); - } + 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 { - let mut active_gamepad = None; + 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| { + *control_flow = ControlFlow::Poll; + match event { Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { @@ -164,7 +210,27 @@ pub fn main() { Event::WindowEvent { event: WindowEvent::Resized(_), .. } => { processor.swapchain_recreate_needed = true; } + Event::UserEvent(TrEvent::KeyHeldEvent { }) => { + } Event::MainEventsCleared => { + + // while let true = processor.is_open() { + // + // // Take care of our timing + // { + // elapsed_time = timer.elap_time(); + // delta_time = elapsed_time - current_time; + // current_time = elapsed_time; + // if delta_time > 0.02 { + // delta_time = 0.02; + // } + // accumulator_time += delta_time; + // } + // + // while (accumulator_time - step_size) >= step_size { + // accumulator_time -= step_size; + // } + let mut canvas_frame = CanvasFrame::default(); canvas_frame.draw(&funky_sprite); canvas_frame.draw(&text_sprite); @@ -181,18 +247,6 @@ pub fn main() { compu_frame); } - while let Some(GilEvent { id, event, time }) = gilrs.next_event() { - println!("{:?} New event from {}: {:?}", time, id, event); - active_gamepad = Some(id); - } - - // 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)"); - } - } - } Event::DeviceEvent { event: DeviceEvent::Key(keyboard_input), .. } => { match keyboard_input.virtual_keycode.unwrap() { @@ -210,13 +264,29 @@ pub fn main() { _ => () } } + Event::UserEvent(TrEvent::GamepadEvent { gil_event }) => { + } _ => () } + // bucket the events out, but not really match event { - _ => { - funky_sprite.notify(&event); - } + Event::NewEvents(_) => {}, + Event::WindowEvent { window_id, event } => { + + }, + Event::DeviceEvent { device_id, event } => { + + }, + Event::UserEvent(tr_event) => { + + }, + Event::Suspended => {}, + Event::Resumed => {}, + Event::MainEventsCleared => {}, + Event::RedrawRequested(_) => {}, + Event::RedrawEventsCleared => {}, + Event::LoopDestroyed => {}, } }); diff --git a/src/util/mod.rs b/src/util/mod.rs index 95019bf9..1281ea71 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -40,4 +40,5 @@ pub fn load_raw(filename: String) -> (Vec, (u32,u32)) { } (image_buffer, xy) -} \ No newline at end of file +} + diff --git a/src/util/tr_event.rs b/src/util/tr_event.rs index 7b4a6e85..63f3cd69 100644 --- a/src/util/tr_event.rs +++ b/src/util/tr_event.rs @@ -1,18 +1,18 @@ use winit::window::WindowId; -use winit::event::{WindowEvent, DeviceId, DeviceEvent}; +use winit::event::{WindowEvent, DeviceId, DeviceEvent, KeyboardInput, ModifiersState, MouseScrollDelta, TouchPhase, ElementState, MouseButton, AxisId, Touch}; +use std::path::PathBuf; +use winit::dpi::{PhysicalPosition, PhysicalSize}; +use gilrs::Event as GilEvent; -enum TrEvent { +pub enum TrEvent { + MouseHeldEvent { - // WindowEvent { - // window_id: WindowId, - // event: WindowEvent<'a>, - // }, - - /// Emitted when the OS sends an event to a device. - DeviceEvent { - device_id: DeviceId, - event: DeviceEvent, }, + KeyHeldEvent { + }, + GamepadEvent { + gil_event: GilEvent, + } } \ No newline at end of file