From aaef25f710b96f8406ea8711e51fc97e14c63fe8 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sat, 19 Sep 2020 01:24:00 -0700 Subject: [PATCH] how do I make eventing extensible without cluttering this file with standalone functions --- src/event_system.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 35 ++++--------------------- 2 files changed, 69 insertions(+), 30 deletions(-) create mode 100644 src/event_system.rs diff --git a/src/event_system.rs b/src/event_system.rs new file mode 100644 index 00000000..e56b59d0 --- /dev/null +++ b/src/event_system.rs @@ -0,0 +1,64 @@ +use std::sync::Arc; + +use specs::{Component, Entities, Join, System, VecStorage, Write, WriteStorage}; +use vulkano::swapchain::Surface; +use winit::window::Window; + +use crate::canvas::canvas_frame::CanvasFrame; +use crate::canvas::compu_frame::CompuFrame; +use crate::PersistentState; +use crate::render_system::Position; +use crate::util::tr_event::{TrEvent, TrEventExtension, TrWindowEvent}; +use crate::vkprocessor::VkProcessor; +use winit::event::ElementState; + +#[derive(Debug, Clone)] +pub struct Evented { + pub subscribed: fn(event: TrEvent) -> bool, +} + +impl Component for Evented { + type Storage = VecStorage; +} + +pub struct EventSystem; + +impl<'a> System<'a> for EventSystem { + type SystemData = ( + Entities<'a>, + WriteStorage<'a, Position>, + WriteStorage<'a, Evented>, + Write<'a, PersistentState>, + Write<'a, VkProcessor>, + Write<'a, Vec>> + ); + + fn run(&mut self, ( + entity, + mut position_list, + mut evented_list, + mut state, + mut vk_processor, + event_stack + ): Self::SystemData) { + + for (position, evented) in (&mut position_list, &evented_list).join() { + for event in &*event_stack { + match event { + TrEvent::WindowEvent { window_id, event } => { + match event { + TrWindowEvent::MouseInput { device_id, state, button, modifiers } => { + if *state == ElementState::Pressed { + position.x += 100.0; + } + }, + _ => {} + } + } + _ => {} + } + } + } + for (entity, evented) in (&*entity, &mut evented_list).join() {} + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 604a2d20..b20bbc35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,6 +46,7 @@ use crate::util::tr_event::{TrEvent, TrEventExtension}; use crate::util::vertex::{TextureVertex3D, VertexTypeContainer}; use crate::vkprocessor::VkProcessor; use crate::compu_system::{CompuSystem, Compu}; +use crate::event_system::{EventSystem, Evented}; pub mod util; pub mod vkprocessor; @@ -53,15 +54,7 @@ pub mod drawables; pub mod canvas; pub mod render_system; pub mod compu_system; - -#[derive(Debug, Clone)] -pub struct Evented { - subscribed: fn(event: TrEvent), -} - -impl Component for Evented { - type Storage = VecStorage; -} +pub mod event_system; #[derive(Default)] pub struct PersistentState { @@ -72,25 +65,6 @@ pub struct PersistentState { compu_frame: CompuFrame, } -struct EventSystem; - -impl<'a> System<'a> for EventSystem { - type SystemData = ( - Entities<'a>, - WriteStorage<'a, Evented>, - Write<'a, PersistentState>, - Write<'a, VkProcessor>, - Write<'a, Vec>> - ); - - fn run(&mut self, (entity, mut evented_list, mut state, mut vk_processor, event_stack): Self::SystemData) { - - for (entity, evented) in (&*entity, &mut evented_list).join() { - - } - } -} - pub fn main() { //hprof::start_frame(); //let g = hprof::enter("vulkan preload"); @@ -152,6 +126,7 @@ pub fn main() { processor.get_texture_handle(String::from("sfml.png")).unwrap(); let mut world = World::new(); + world.register::(); world.register::(); world.register::(); world.register::(); @@ -175,14 +150,14 @@ pub fn main() { .with(Images { images: vec![compu_image], image_resolutions: vec![image_dimensions_u] }) .build(); - world.create_entity()// just a drawable + world.create_entity() + .with(Evented { subscribed: |event| {true} }) .with(Position { x: 0.0, y: 0.0, z: 0 }) .with(Geometry { size_x: 300.0, size_y: 300.0, rotation: 0.0 }) .with(Textures { textures: vec![funky_handle] }) .build(); - // call the run method for the following systems & deps let mut dispatcher = DispatcherBuilder::new() // .with(SysA, "sys_a", &[])