event work + mulling over sprite containers and event notification

master
mitchellhansen 4 years ago
parent 55899f26ad
commit 8da810f23a

@ -11,7 +11,7 @@ use winit::event::Event;
/// Trait which may be inherited by objects that wish to be drawn to the screen /// Trait which may be inherited by objects that wish to be drawn to the screen
pub trait Drawable { pub trait Drawable {
fn get(&self) -> VertexTypes; fn get(&self) -> Vec<VertexTypes>;
} }
/// Trait which may be inherited by objects that wish to receive events /// 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 /// Push this drawable onto the back of the accumulator
pub fn draw(&mut self, drawable: &dyn Drawable) { pub fn draw(&mut self, drawable: &dyn Drawable) {
self.map.push(drawable.get()); drawable.get().iter().map(|x| {self.map.push(x.clone())});
} }
} }

@ -54,8 +54,12 @@ impl CompuFrame {
kernel: Arc<CompuKernelHandle>, kernel: Arc<CompuKernelHandle>,
sprite: &CompuSprite) { sprite: &CompuSprite) {
if let VertexTypes::ImageType(a, b) = sprite.get() { let compu_sprites = sprite.get();
self.swapped_to_image.push((buffer, b, kernel))
if compu_sprites.len() == 1 {
if let VertexTypes::ImageType(a, b) = compu_sprites.first().unwrap() {
self.swapped_to_image.push((buffer, b.clone(), kernel))
};
} }
} }

@ -53,7 +53,7 @@ impl CompuSprite {
} }
impl Drawable for CompuSprite { impl Drawable for CompuSprite {
fn get(&self) -> VertexTypes { fn get(&self) -> Vec<VertexTypes> {
self.verts.clone() vec![self.verts.clone()]
} }
} }

@ -61,8 +61,8 @@ impl Polygon {
} }
} }
impl Drawable for Polygon { impl Drawable for Polygon {
fn get(&self) -> VertexTypes { fn get(&self) -> Vec<VertexTypes> {
self.verts.clone() vec![self.verts.clone()]
} }
} }

@ -50,8 +50,8 @@ impl Rect {
} }
} }
impl Drawable for Rect { impl Drawable for Rect {
fn get(&self) -> VertexTypes { fn get(&self) -> Vec<VertexTypes> {
self.verts.clone() vec![self.verts.clone()]
} }
} }

@ -18,9 +18,7 @@ pub struct Sprite {
/// Container class which implements drawable. /// Container class which implements drawable.
impl Sprite { impl Sprite {
fn generate_verts(position: (f32, f32), size: (f32, f32), depth: f32) -> Vec<TextureVertex3D> {
fn generate_verts(position: (f32, f32), size: (f32, f32), depth: f32,) -> Vec<TextureVertex3D> {
vec![ vec![
TextureVertex3D { TextureVertex3D {
v_position: [position.0, position.1, depth], // top left v_position: [position.0, position.1, depth], // top left
@ -54,7 +52,6 @@ impl Sprite {
size: (f32, f32), size: (f32, f32),
depth: u32, depth: u32,
texture_handle: Arc<CanvasTextureHandle>) -> Sprite { texture_handle: Arc<CanvasTextureHandle>) -> Sprite {
let normalized_depth = (depth as f32 / 255.0); let normalized_depth = (depth as f32 / 255.0);
Sprite { Sprite {
@ -68,24 +65,26 @@ impl Sprite {
} }
impl Drawable for Sprite { impl Drawable for Sprite {
fn get(&self) -> VertexTypes { fn get(&self) -> Vec<VertexTypes> {
VertexTypes::TextureType(Sprite::generate_verts(self.position, self.size, self.depth), self.texture_handle.clone()) vec![
VertexTypes::TextureType(
Sprite::generate_verts(self.position, self.size, self.depth),
self.texture_handle.clone())
]
} }
} }
impl<T> Eventable<T> for Sprite { impl<T> Eventable<T> for Sprite {
fn notify(&mut self, event: &Event<T>) -> () { fn notify(&mut self, event: &Event<T>) -> () {
match 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 { match button {
MouseButton::Left => { MouseButton::Left => {
if *state == ElementState::Pressed { if *state == ElementState::Pressed {
self.position = (self.position.0, self.position.1 + 0.1); self.position = (self.position.0, self.position.1 + 0.1);
} }
},
_ => {
} }
_ => {}
} }
} }
_ => {} _ => {}

@ -137,7 +137,7 @@ impl Text {
} }
impl Drawable for Text { impl Drawable for Text {
fn get(&self) -> VertexTypes { fn get(&self) -> Vec<VertexTypes> {
self.verts.clone() vec![self.verts.clone()]
} }
} }

@ -36,7 +36,8 @@ use crate::util::timer::Timer;
use crate::util::vertex::{TextureVertex3D, VertexTypes}; use crate::util::vertex::{TextureVertex3D, VertexTypes};
use crate::vkprocessor::VkProcessor; use crate::vkprocessor::VkProcessor;
use std::path::Path; 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 util;
pub mod vkprocessor; pub mod vkprocessor;
@ -44,6 +45,36 @@ pub mod drawables;
pub mod canvas; pub mod canvas;
pub mod compute; 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<GuiElements>,
}
impl Drawable for Slider {
fn get(&self) -> Vec<VertexTypes> {
self.sprites.into()
// self.sprites.iter().map(|v| {
//
// })
}
}
}
pub fn main() { pub fn main() {
hprof::start_frame(); hprof::start_frame();
@ -58,7 +89,7 @@ pub fn main() {
println!("Debug callback: {:?}", msg.description); println!("Debug callback: {:?}", msg.description);
}).ok(); }).ok();
let mut events_loop = EventLoop::new(); let mut events_loop = EventLoop::<TrEvent>::with_user_event();
let mut surface = WindowBuilder::new() let mut surface = WindowBuilder::new()
.with_inner_size(LogicalSize::new(800, 800)) .with_inner_size(LogicalSize::new(800, 800))
@ -69,7 +100,6 @@ pub fn main() {
{ {
let g = hprof::enter("vulkan preload"); let g = hprof::enter("vulkan preload");
processor.create_swapchain(instance.clone(), surface.clone()); processor.create_swapchain(instance.clone(), surface.clone());
processor.preload_kernels(); processor.preload_kernels();
processor.preload_shaders(); processor.preload_shaders();
processor.preload_textures(); 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 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 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 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);
@ -129,33 +161,47 @@ pub fn main() {
let l = hprof::enter("Loop"); let l = hprof::enter("Loop");
// while let true = processor.is_open() { let event_loop_proxy = events_loop.create_proxy();
//
// // Take care of our timing std::thread::spawn(move || {
// {
// elapsed_time = timer.elap_time(); let mut gilrs = Gilrs::new().unwrap();
// delta_time = elapsed_time - current_time; // Iterate over all connected gamepads
// current_time = elapsed_time; let mut gamepad : Option<Gamepad> = None;
// if delta_time > 0.02 { for (_id, gamepad_) in gilrs.gamepads() {
// delta_time = 0.02; if gamepad_.name() == "PS4" {
// } gamepad = Some(gamepad_);
// accumulator_time += delta_time; }
// } println!("{} is {:?} {:?}", gamepad_.name(), gamepad_.power_info(), gamepad_.id());
// }
// while (accumulator_time - step_size) >= step_size { let mut active_gamepad = None;
// accumulator_time -= step_size;
// } loop {
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 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 is borrowed from the surface
events_loop.run(move |event, _, control_flow| { events_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event { match event {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => Event::WindowEvent { event: WindowEvent::CloseRequested, .. } =>
{ {
@ -164,7 +210,27 @@ pub fn main() {
Event::WindowEvent { event: WindowEvent::Resized(_), .. } => { Event::WindowEvent { event: WindowEvent::Resized(_), .. } => {
processor.swapchain_recreate_needed = true; processor.swapchain_recreate_needed = true;
} }
Event::UserEvent(TrEvent::KeyHeldEvent { }) => {
}
Event::MainEventsCleared => { 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(); let mut canvas_frame = CanvasFrame::default();
canvas_frame.draw(&funky_sprite); canvas_frame.draw(&funky_sprite);
canvas_frame.draw(&text_sprite); canvas_frame.draw(&text_sprite);
@ -181,18 +247,6 @@ pub fn main() {
compu_frame); 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), .. } => { Event::DeviceEvent { event: DeviceEvent::Key(keyboard_input), .. } => {
match keyboard_input.virtual_keycode.unwrap() { 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 { match event {
_ => { Event::NewEvents(_) => {},
funky_sprite.notify(&event); 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 => {},
} }
}); });

@ -40,4 +40,5 @@ pub fn load_raw(filename: String) -> (Vec<u8>, (u32,u32)) {
} }
(image_buffer, xy) (image_buffer, xy)
} }

@ -1,18 +1,18 @@
use winit::window::WindowId; 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,
}
} }
Loading…
Cancel
Save