Begin implement input management
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m4s
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m4s
This commit is contained in:
parent
2c3392c3ea
commit
c4c691c4dd
3 changed files with 99 additions and 1 deletions
|
@ -16,6 +16,7 @@ use winit::event::WindowEvent;
|
||||||
use winit::event_loop::ActiveEventLoop;
|
use winit::event_loop::ActiveEventLoop;
|
||||||
use winit::window::WindowId;
|
use winit::window::WindowId;
|
||||||
|
|
||||||
|
use super::input::InputState;
|
||||||
use super::vulkan_context::VulkanContext;
|
use super::vulkan_context::VulkanContext;
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
|
@ -24,6 +25,7 @@ pub struct App {
|
||||||
gui: HashMap<WindowId, Gui>,
|
gui: HashMap<WindowId, Gui>,
|
||||||
scene: Option<Scene>,
|
scene: Option<Scene>,
|
||||||
clear_color: [f32; 3],
|
clear_color: [f32; 3],
|
||||||
|
input_state: InputState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<VulkanoContext> for App {
|
impl From<VulkanoContext> for App {
|
||||||
|
@ -34,6 +36,7 @@ impl From<VulkanoContext> for App {
|
||||||
gui: HashMap::new(),
|
gui: HashMap::new(),
|
||||||
scene: None,
|
scene: None,
|
||||||
clear_color: [0.0, 0.0, 0.0],
|
clear_color: [0.0, 0.0, 0.0],
|
||||||
|
input_state: InputState::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,7 +83,10 @@ impl ApplicationHandler for App {
|
||||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
|
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
|
||||||
let renderer = self.vulkano_windows.get_renderer_mut(id).unwrap();
|
let renderer = self.vulkano_windows.get_renderer_mut(id).unwrap();
|
||||||
let gui = self.gui.get_mut(&id).unwrap();
|
let gui = self.gui.get_mut(&id).unwrap();
|
||||||
gui.update(&event);
|
|
||||||
|
if !gui.update(&event) {
|
||||||
|
self.input_state.process_event(&event);
|
||||||
|
}
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested => {
|
WindowEvent::CloseRequested => {
|
||||||
|
@ -94,6 +100,8 @@ impl ApplicationHandler for App {
|
||||||
renderer.resize();
|
renderer.resize();
|
||||||
}
|
}
|
||||||
WindowEvent::RedrawRequested => {
|
WindowEvent::RedrawRequested => {
|
||||||
|
self.input_state.update();
|
||||||
|
|
||||||
let acquire_future = renderer.acquire(None, |_| {}).unwrap();
|
let acquire_future = renderer.acquire(None, |_| {}).unwrap();
|
||||||
|
|
||||||
let mut builder = AutoCommandBufferBuilder::primary(
|
let mut builder = AutoCommandBufferBuilder::primary(
|
||||||
|
|
89
src/render/input.rs
Normal file
89
src/render/input.rs
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use winit::{
|
||||||
|
dpi::PhysicalPosition,
|
||||||
|
event::{ElementState, KeyEvent, WindowEvent},
|
||||||
|
keyboard::PhysicalKey,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub enum KeyState {
|
||||||
|
#[default]
|
||||||
|
Pressed,
|
||||||
|
Released,
|
||||||
|
Held,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct MouseState {
|
||||||
|
old_position: glam::Vec2,
|
||||||
|
pub position: glam::Vec2,
|
||||||
|
pub delta: glam::Vec2,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct InputState {
|
||||||
|
pub key_states: HashMap<PhysicalKey, KeyState>,
|
||||||
|
pub mouse_state: MouseState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InputState {
|
||||||
|
pub fn process_event(&mut self, event: &WindowEvent) {
|
||||||
|
match event {
|
||||||
|
WindowEvent::KeyboardInput { event, .. } => {
|
||||||
|
self.update_key_state(event.physical_key, event);
|
||||||
|
}
|
||||||
|
WindowEvent::CursorMoved { position, .. } => {
|
||||||
|
self.update_mouse_position(position);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates deltas before running update
|
||||||
|
pub fn update(&mut self) {
|
||||||
|
self.mouse_state.delta = self.mouse_state.position - self.mouse_state.old_position;
|
||||||
|
self.mouse_state.old_position = self.mouse_state.position;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_key_state(&self, key: PhysicalKey) -> &KeyState {
|
||||||
|
self.key_states.get(&key).unwrap_or(&KeyState::Released)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_mouse_state(&self) -> &MouseState {
|
||||||
|
&self.mouse_state
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_key_state(&mut self, key: PhysicalKey, event: &KeyEvent) {
|
||||||
|
let key_state = self.key_states.get(&event.physical_key);
|
||||||
|
let new_key_state = match key_state {
|
||||||
|
Some(key_state) => match event.state {
|
||||||
|
ElementState::Pressed => match key_state {
|
||||||
|
KeyState::Released => Some(KeyState::Pressed),
|
||||||
|
KeyState::Pressed => Some(KeyState::Held),
|
||||||
|
KeyState::Held => None,
|
||||||
|
},
|
||||||
|
ElementState::Released => match key_state {
|
||||||
|
KeyState::Released => None,
|
||||||
|
_ => Some(KeyState::Released),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
None => match event.state {
|
||||||
|
ElementState::Pressed => Some(KeyState::Pressed),
|
||||||
|
ElementState::Released => Some(KeyState::Released),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if let Some(new_key_state) = new_key_state {
|
||||||
|
log::trace!(
|
||||||
|
"New key state {:?} for key {:?}",
|
||||||
|
new_key_state,
|
||||||
|
event.physical_key
|
||||||
|
);
|
||||||
|
self.key_states.insert(key, new_key_state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_mouse_position(&mut self, position: &PhysicalPosition<f64>) {
|
||||||
|
self.mouse_state.position = glam::Vec2::new(position.x as f32, position.y as f32);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod app;
|
pub mod app;
|
||||||
|
pub mod input;
|
||||||
pub mod pipelines;
|
pub mod pipelines;
|
||||||
pub mod scene;
|
pub mod scene;
|
||||||
pub mod vertex;
|
pub mod vertex;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue