From d193aa46ec80a58a6a6ad619c38619e434a6137a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 5 Apr 2026 18:12:51 +0200 Subject: feat(engine): add position to mouse component --- engine/src/camera/fly.rs | 16 +++++++--------- engine/src/input/mouse.rs | 2 +- engine/src/windowing.rs | 28 ++++++++++++++++++++-------- engine/src/windowing/mouse.rs | 12 ++++++++++-- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/engine/src/camera/fly.rs b/engine/src/camera/fly.rs index 7996b4d..2042c0c 100644 --- a/engine/src/camera/fly.rs +++ b/engine/src/camera/fly.rs @@ -1,15 +1,15 @@ use ecs::component::local::Local; use ecs::phase::UPDATE as UPDATE_PHASE; use ecs::sole::Single; -use ecs::system::initializable::Initializable; use ecs::system::Into; +use ecs::system::initializable::Initializable; use ecs::{Component, Query}; use crate::builder; use crate::camera::{Active as ActiveCamera, Camera}; use crate::delta_time::DeltaTime; use crate::input::keyboard::{Key, Keyboard}; -use crate::input::mouse::Motion as MouseMotion; +use crate::input::mouse::Mouse; use crate::transform::WorldPosition; use crate::vector::{Vec2, Vec3}; @@ -74,7 +74,7 @@ pub struct Options fn update( camera_query: Query<(&mut Camera, &mut WorldPosition, &mut Fly, &ActiveCamera)>, keyboard: Single, - mouse_motion: Single, + mouse: Single, delta_time: Single, options: Local, ) @@ -82,14 +82,12 @@ fn update( for (mut camera, mut camera_world_pos, mut fly_camera, _) in &camera_query { let delta_time = delta_time.duration; - // tracing::info!("Mouse motion: {:?}", mouse_motion.position_delta); - - if mouse_motion.position_delta != (Vec2 { x: 0.0, y: 0.0 }) { + if mouse.curr_tick_position_delta != (Vec2 { x: 0.0, y: 0.0 }) { let x_offset = - mouse_motion.position_delta.x * f64::from(options.mouse_sensitivity); + mouse.curr_tick_position_delta.x * f64::from(options.mouse_sensitivity); - let y_offset = - (-mouse_motion.position_delta.y) * f64::from(options.mouse_sensitivity); + let y_offset = (-mouse.curr_tick_position_delta.y) + * f64::from(options.mouse_sensitivity); fly_camera.current_yaw += x_offset; fly_camera.current_pitch += y_offset; diff --git a/engine/src/input/mouse.rs b/engine/src/input/mouse.rs index 90091f3..c10aaf2 100644 --- a/engine/src/input/mouse.rs +++ b/engine/src/input/mouse.rs @@ -1,6 +1,6 @@ mod reexports { - pub use crate::windowing::mouse::{Button, ButtonState, Buttons, Motion}; + pub use crate::windowing::mouse::{Button, ButtonState, Buttons, Mouse}; } pub use reexports::*; diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index 2bfdb31..d717785 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -1,12 +1,12 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Weak}; -use std::thread::{spawn, JoinHandle as ThreadJoinHandle}; +use std::thread::{JoinHandle as ThreadJoinHandle, spawn}; use crossbeam_channel::{ - bounded as bounded_channel, Receiver as ChannelReceiver, Sender as ChannelSender, TrySendError, + bounded as bounded_channel, }; use ecs::actions::Actions; use ecs::component::Component; @@ -17,7 +17,7 @@ use ecs::phase::{Phase, UPDATE as UPDATE_PHASE}; use ecs::sole::Single; use ecs::system::observer::Observe; use ecs::uid::Uid; -use ecs::{declare_entity, Query, Sole}; +use ecs::{Query, Sole, declare_entity}; use raw_window_handle::{DisplayHandle, HandleError, HasDisplayHandle, WindowHandle}; use winit::application::ApplicationHandler; use winit::dpi::PhysicalPosition; @@ -40,7 +40,7 @@ use crate::windowing::mouse::{ Button as MouseButton, ButtonState as MouseButtonState, Buttons as MouseButtons, - Motion as MouseMotion, + Mouse, }; use crate::windowing::window::{ Closed as WindowClosed, @@ -80,7 +80,7 @@ impl ecs::extension::Extension for Extension { collector.add_sole(Context::default()).ok(); collector.add_sole(Keyboard::default()).ok(); - collector.add_sole(MouseMotion::default()).ok(); + collector.add_sole(Mouse::default()).ok(); collector.add_sole(MouseButtons::default()).ok(); collector.add_declared_entity(&PHASE); @@ -122,7 +122,7 @@ fn handle_window_creation_ready( fn update_stuff( mut context: Single, mut keyboard: Single, - mut mouse_motion: Single, + mut mouse: Single, mut mouse_buttons: Single, mut actions: Actions, entity_obtainer: EntityObtainer, @@ -130,7 +130,7 @@ fn update_stuff( { keyboard.make_key_states_previous(); mouse_buttons.make_states_previous(); - mouse_motion.position_delta = Vec2::default(); + mouse.curr_tick_position_delta = Vec2::default(); let Context { ref message_from_app_receiver, @@ -221,8 +221,11 @@ fn update_stuff( MessageFromApp::KeyboardKeyStateChanged(key, key_state) => { keyboard.set_key_state(key, key_state); } + MessageFromApp::MouseMovedTo { position } => { + mouse.position = position; + } MessageFromApp::MouseMoved { position_delta } => { - mouse_motion.position_delta += position_delta; + mouse.curr_tick_position_delta += position_delta; } MessageFromApp::MouseButtonStateChanged(mouse_button, mouse_button_state) => { mouse_buttons.set(mouse_button, mouse_button_state); @@ -430,6 +433,10 @@ enum MessageFromApp WindowCloseRequested(WindowId), WindowScaleFactorChanged(WindowId, f64), KeyboardKeyStateChanged(Key, KeyState), + MouseMovedTo + { + position: Vec2, + }, MouseMoved { position_delta: Vec2, @@ -616,6 +623,11 @@ impl ApplicationHandler for App keyboard_event.state.into(), )); } + WindowEvent::CursorMoved { device_id: _, position } => { + self.send_message(MessageFromApp::MouseMovedTo { + position: Vec2 { x: position.x, y: position.y }, + }); + } WindowEvent::MouseInput { device_id: _, state, button } => { self.send_message(MessageFromApp::MouseButtonStateChanged( button.into(), diff --git a/engine/src/windowing/mouse.rs b/engine/src/windowing/mouse.rs index 1afe594..3b08443 100644 --- a/engine/src/windowing/mouse.rs +++ b/engine/src/windowing/mouse.rs @@ -6,9 +6,17 @@ use crate::vector::Vec2; #[derive(Debug, Default, Clone, Sole)] #[non_exhaustive] -pub struct Motion +pub struct Mouse { - pub position_delta: Vec2, + /// Change in coordinates this tick. Unit is unspecified and platforms may use + /// different units. Updated automatically by the [`windowing extension`]. + /// + /// [`windowing extension`]: crate::windowing + pub curr_tick_position_delta: Vec2, + + /// Coordinates in pixels relative to the top-left corner of the window. May have + /// been affected by cursor acceleration + pub position: Vec2, } /// Mouse buttons. -- cgit v1.2.3-18-g5258