From 8c1406f7a42822d1b020cc672ed99d02eb34d22f Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:22 +0100 Subject: feat(render-bsp): process into inputmanager --- stockton-render/src/window.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'stockton-render/src') diff --git a/stockton-render/src/window.rs b/stockton-render/src/window.rs index 6c4cecd..fa22e1d 100644 --- a/stockton-render/src/window.rs +++ b/stockton-render/src/window.rs @@ -16,15 +16,18 @@ */ use crate::Renderer; +use legion::systems::Runnable; -use winit::event::Event as WinitEvent; -use winit::event::WindowEvent as WinitWindowEvent; +use stockton_input::{Action as KBAction, InputManager}; + +use winit::event::{ElementState, Event as WinitEvent, WindowEvent as WinitWindowEvent}; use winit::event_loop::ControlFlow; #[derive(Debug, Clone, Copy)] pub enum WindowEvent { SizeChanged, CloseRequested, + KeyboardAction(KBAction), } impl WindowEvent { @@ -34,6 +37,14 @@ impl WindowEvent { WinitEvent::WindowEvent { event, .. } => match event { WinitWindowEvent::CloseRequested => Some(WindowEvent::CloseRequested), WinitWindowEvent::Resized(_) => Some(WindowEvent::SizeChanged), + WinitWindowEvent::KeyboardInput { input, .. } => match input.state { + ElementState::Pressed => Some(WindowEvent::KeyboardAction(KBAction::KeyPress( + input.scancode, + ))), + ElementState::Released => Some(WindowEvent::KeyboardAction( + KBAction::KeyRelease(input.scancode), + )), + }, _ => None, }, _ => None, @@ -43,7 +54,13 @@ impl WindowEvent { #[system] /// A system to process the window events sent to renderer by the winit event loop. -pub fn process_window_events(#[resource] renderer: &mut Renderer<'static>) { +pub fn _process_window_events( + #[resource] renderer: &mut Renderer<'static>, + #[resource] manager: &mut T, + #[state] actions_buf: &mut Vec, +) { + let mut actions_buf_cursor = 0; + while let Ok(event) = renderer.window_events.try_recv() { match event { WindowEvent::SizeChanged => renderer.resize(), @@ -52,6 +69,20 @@ pub fn process_window_events(#[resource] renderer: &mut Renderer<'static>) { // TODO: Let everything know this is our last frame *flow = ControlFlow::Exit; } + WindowEvent::KeyboardAction(action) => { + if actions_buf_cursor >= actions_buf.len() { + actions_buf.push(action); + } else { + actions_buf[actions_buf_cursor] = action; + } + actions_buf_cursor += 1; + } }; } + + manager.handle_frame(&actions_buf[0..actions_buf_cursor]); +} + +pub fn process_window_events_system() -> impl Runnable { + _process_window_events_system::(Vec::with_capacity(4)) } -- cgit v1.2.3