aboutsummaryrefslogtreecommitdiff
path: root/stockton-render
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
parentb380ad94647c9bc446d5a76bb16dcc286077275a (diff)
feat(render-bsp): process into inputmanager
Diffstat (limited to 'stockton-render')
-rw-r--r--stockton-render/Cargo.toml1
-rw-r--r--stockton-render/src/window.rs37
2 files changed, 35 insertions, 3 deletions
diff --git a/stockton-render/Cargo.toml b/stockton-render/Cargo.toml
index eef6bc0..e4f2153 100644
--- a/stockton-render/Cargo.toml
+++ b/stockton-render/Cargo.toml
@@ -5,6 +5,7 @@ authors = ["Oscar <oscar.shrimpton.personal@gmail.com>"]
edition = "2018"
[dependencies]
+stockton-input = { path = "../stockton-input" }
stockton-levels = { path = "../stockton-levels" }
stockton-types = { path = "../stockton-types" }
winit = "^0.21"
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))
}