From f4c0e91851c5aff09e4e1e45fa23b57f4e0af25c Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 20 Jan 2024 09:40:00 -0800 Subject: [PATCH] Added mouse input --- crates/galactica/src/main.rs | 9 ++++++--- crates/playeragent/src/inputstatus.rs | 20 ++++++++++++++++++-- crates/playeragent/src/playeragent.rs | 13 ------------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/crates/galactica/src/main.rs b/crates/galactica/src/main.rs index b9fab3e..8b24857 100644 --- a/crates/galactica/src/main.rs +++ b/crates/galactica/src/main.rs @@ -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); diff --git a/crates/playeragent/src/inputstatus.rs b/crates/playeragent/src/inputstatus.rs index 3eb5ca8..cfa42cb 100644 --- a/crates/playeragent/src/inputstatus.rs +++ b/crates/playeragent/src/inputstatus.rs @@ -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, + // 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) { + 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, _ => {} } } diff --git a/crates/playeragent/src/playeragent.rs b/crates/playeragent/src/playeragent.rs index 380f578..bc89b7c 100644 --- a/crates/playeragent/src/playeragent.rs +++ b/crates/playeragent/src/playeragent.rs @@ -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())