From 848e7fae43e9f0a271c8ef6f2d8079a1b2c26bf1 Mon Sep 17 00:00:00 2001 From: Rich Churcher Date: Tue, 30 Jul 2024 11:38:59 +1200 Subject: [PATCH] Use AccumulatedMouseMotion, AccumulatedMouseScroll in examples (#14488) # Objective Use the new `AccumulatedMouseMotion` and `AccumulatedMouseScroll` resources in place of mouse event handling. I left the `mouse_input_events` example alone, since by its nature it demonstrates event detection. Fixes #14066 ## Testing Ran each example locally before and after changes. --- examples/camera/first_person_view_model.rs | 12 +++++--- examples/helpers/camera_controller.rs | 36 ++++++++-------------- examples/math/random_sampling.rs | 14 +++++---- examples/math/sampling_primitives.rs | 36 +++++++++++----------- examples/transforms/align.rs | 14 ++++----- 5 files changed, 53 insertions(+), 59 deletions(-) diff --git a/examples/camera/first_person_view_model.rs b/examples/camera/first_person_view_model.rs index d2e6c24373..9a91eb324b 100644 --- a/examples/camera/first_person_view_model.rs +++ b/examples/camera/first_person_view_model.rs @@ -43,7 +43,7 @@ //! | arrow down | Increase FOV | use bevy::color::palettes::tailwind; -use bevy::input::mouse::MouseMotion; +use bevy::input::mouse::AccumulatedMouseMotion; use bevy::pbr::NotShadowCaster; use bevy::prelude::*; use bevy::render::view::RenderLayers; @@ -219,13 +219,15 @@ fn spawn_text(mut commands: Commands) { } fn move_player( - mut mouse_motion: EventReader, + accumulated_mouse_motion: Res, mut player: Query<&mut Transform, With>, ) { let mut transform = player.single_mut(); - for motion in mouse_motion.read() { - let yaw = -motion.delta.x * 0.003; - let pitch = -motion.delta.y * 0.002; + let delta = accumulated_mouse_motion.delta; + + if delta != Vec2::ZERO { + let yaw = -delta.x * 0.003; + let pitch = -delta.y * 0.002; // Order of rotations is important, see transform.rotate_y(yaw); transform.rotate_local_x(pitch); diff --git a/examples/helpers/camera_controller.rs b/examples/helpers/camera_controller.rs index 75e428aa3e..86defefb90 100644 --- a/examples/helpers/camera_controller.rs +++ b/examples/helpers/camera_controller.rs @@ -3,7 +3,7 @@ //! - Copy the code for the [`CameraControllerPlugin`] and add the plugin to your App. //! - Attach the [`CameraController`] component to an entity with a [`Camera3dBundle`]. -use bevy::input::mouse::{MouseMotion, MouseScrollUnit, MouseWheel}; +use bevy::input::mouse::{AccumulatedMouseMotion, AccumulatedMouseScroll, MouseScrollUnit}; use bevy::prelude::*; use bevy::window::CursorGrabMode; use std::{f32::consts::*, fmt}; @@ -101,8 +101,8 @@ Freecam Controls: fn run_camera_controller( time: Res