Added mouse input

master
Mark 2024-01-20 09:40:00 -08:00
parent 70c9ec3b92
commit f4c0e91851
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
3 changed files with 24 additions and 18 deletions

View File

@ -137,13 +137,16 @@ fn main() -> Result<()> {
},
..
} => {
player.process_key(state, key);
player.input.process_key(state, key);
}
WindowEvent::CursorMoved { position, .. } => {
player.input.process_mouse(position);
}
WindowEvent::MouseInput { state, button, .. } => {
player.process_click(state, button);
player.input.process_click(state, button);
}
WindowEvent::MouseWheel { delta, phase, .. } => {
player.process_scroll(delta, phase);
player.input.process_scroll(delta, phase);
}
WindowEvent::Resized(_) => {
gpu.resize(&content);

View File

@ -1,14 +1,20 @@
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
use winit::{
dpi::PhysicalPosition,
event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode},
};
pub struct InputStatus {
// Parameters
scroll_speed: f32,
mouse_position: PhysicalPosition<f32>,
// Continuous keys
key_left: bool,
key_right: bool,
key_thrust: bool,
key_guns: bool,
key_leftclick: bool,
// One-shot keys (audomatically released at the end of each frame)
key_land: bool,
@ -23,6 +29,8 @@ impl InputStatus {
key_thrust: false,
key_guns: false,
key_land: false,
key_leftclick: false,
mouse_position: PhysicalPosition { x: 0.0, y: 0.0 },
v_scroll: 0.0,
scroll_speed: 10.0,
}
@ -65,9 +73,17 @@ impl InputStatus {
}
}
pub fn process_mouse(&mut self, position: &PhysicalPosition<f64>) {
self.mouse_position = PhysicalPosition {
x: position.x as f32,
y: position.y as f32,
};
}
pub fn process_click(&mut self, state: &ElementState, key: &MouseButton) {
let _down = state == &ElementState::Pressed;
let down = state == &ElementState::Pressed;
match key {
MouseButton::Left => self.key_leftclick = down,
_ => {}
}
}

View File

@ -1,6 +1,5 @@
use galactica_content::{Content, SystemHandle, SystemObjectHandle};
use rapier2d::geometry::ColliderHandle;
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
use crate::{camera::Camera, inputstatus::InputStatus, PlayerStatus};
@ -57,18 +56,6 @@ impl PlayerAgent {
self.camera.aspect = v
}
pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
self.input.process_key(state, key)
}
pub fn process_click(&mut self, state: &ElementState, key: &MouseButton) {
self.input.process_click(state, key)
}
pub fn process_scroll(&mut self, delta: &MouseScrollDelta, phase: &TouchPhase) {
self.input.process_scroll(delta, phase)
}
pub fn step(&mut self, ct: &Content, status: PlayerStatus) {
if self.input.get_v_scroll() != 0.0 {
self.camera.zoom = (self.camera.zoom + self.input.get_v_scroll())