aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:22 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:22 +0100
commit8c1406f7a42822d1b020cc672ed99d02eb34d22f (patch)
treee9b3c21520786d2cb9819e46c504e759e4d2d695 /stockton-render/src
parentb380ad94647c9bc446d5a76bb16dcc286077275a (diff)
feat(render-bsp): process into inputmanager
Diffstat (limited to 'stockton-render/src')
-rw-r--r--stockton-render/src/window.rs37
1 files changed, 34 insertions, 3 deletions
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<T: 'static + InputManager>(
+ #[resource] renderer: &mut Renderer<'static>,
+ #[resource] manager: &mut T,
+ #[state] actions_buf: &mut Vec<KBAction>,
+) {
+ 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<T: 'static + InputManager>() -> impl Runnable {
+ _process_window_events_system::<T>(Vec::with_capacity(4))
}