From 0505f940d0c4d407b0356a29d52f18aacb369f04 Mon Sep 17 00:00:00 2001 From: olekspickle <22867443+olekspickle@users.noreply.github.com> Date: Wed, 21 May 2025 19:14:12 +0200 Subject: [PATCH 1/7] address clippy --- src/gamepad.rs | 12 +++++------- src/lib.rs | 32 +++++++++++++------------------- src/mouse.rs | 7 ++----- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/gamepad.rs b/src/gamepad.rs index b2eb2a1..54882f0 100644 --- a/src/gamepad.rs +++ b/src/gamepad.rs @@ -86,13 +86,11 @@ pub fn orbit_gamepad( if rotation.length_squared() > 0.0 { let window = window_q.single().unwrap(); - let delta_x = { - let delta = rotation.x / window.width() - * std::f32::consts::PI - * 2.0 - * cam.gamepad_settings.sensitivity.x; - delta - }; + let delta_x = rotation.x / window.width() + * std::f32::consts::PI + * 2.0 + * cam.gamepad_settings.sensitivity.x; + let delta_y = -rotation.y / window.height() * PI * cam.gamepad_settings.sensitivity.y; let yaw = Quat::from_rotation_y(-delta_x); let pitch = Quat::from_rotation_x(-delta_y); diff --git a/src/lib.rs b/src/lib.rs index 34fbf33..c7015eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,9 +13,7 @@ use mouse::MousePlugin; /// ``` /// use bevy::prelude::*; /// use bevy_third_person_camera::ThirdPersonCameraPlugin; -/// fn main() { -/// App::new().add_plugins(ThirdPersonCameraPlugin); -/// } +/// App::new().add_plugins(ThirdPersonCameraPlugin); /// ``` pub struct ThirdPersonCameraPlugin; @@ -299,6 +297,7 @@ fn aim_condition(cam_q: Query<&ThirdPersonCamera, With>) -> b cam.aim_enabled } +#[allow(clippy::type_complexity)] fn aim( mut cam_q: Query< (&mut ThirdPersonCamera, &Transform), @@ -317,10 +316,7 @@ fn aim( return; }; - let gamepad = match gamepad_q.single() { - Ok(value) => Some(value), - Err(_) => None, - }; + let gamepad = gamepad_q.single().ok(); let is_gamepad_aiming = match gamepad { Some(gp) => gp.pressed(cam.gamepad_settings.aim_button), @@ -351,17 +347,15 @@ fn aim( } else { cam.zoom.radius -= zoom_factor; } - } else { - if let Some(radius_copy) = cam.zoom.radius_copy { - let zoom_factor = (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs(); - - // stop zooming out if current radius is greater than original radius - if cam.zoom.radius >= radius_copy || cam.zoom.radius + zoom_factor >= radius_copy { - cam.zoom.radius = radius_copy; - cam.zoom.radius_copy = None; - } else { - cam.zoom.radius += (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs(); - } + } else if let Some(radius_copy) = cam.zoom.radius_copy { + let zoom_factor = (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs(); + + // stop zooming out if current radius is greater than original radius + if cam.zoom.radius >= radius_copy || cam.zoom.radius + zoom_factor >= radius_copy { + cam.zoom.radius = radius_copy; + cam.zoom.radius_copy = None; + } else { + cam.zoom.radius += (radius_copy / cam.aim_zoom) * cam.aim_speed * time.delta_secs(); } } } @@ -370,7 +364,7 @@ pub fn zoom_condition(cam_q: Query<&ThirdPersonCamera, With>) let Ok(cam) = cam_q.single() else { return false; }; - return cam.zoom_enabled && cam.cursor_lock_active; + cam.zoom_enabled && cam.cursor_lock_active } // only run toggle_x_offset if `offset_toggle_enabled` is true diff --git a/src/mouse.rs b/src/mouse.rs index 02c1e53..55c8426 100644 --- a/src/mouse.rs +++ b/src/mouse.rs @@ -22,7 +22,7 @@ fn orbit_condition(cam_q: Query<&ThirdPersonCamera>) -> bool { let Ok(cam) = cam_q.single() else { return true; }; - return cam.cursor_lock_active; + cam.cursor_lock_active } // heavily referenced https://bevy-cheatbook.github.io/cookbook/pan-orbit-camera.html @@ -49,10 +49,7 @@ pub fn orbit_mouse( if rotation.length_squared() > 0.0 { let window = window_q.single().unwrap(); - let delta_x = { - let delta = rotation.x / window.width() * std::f32::consts::PI * cam.sensitivity.x; - delta - }; + let delta_x = rotation.x / window.width() * std::f32::consts::PI * cam.sensitivity.x; let delta_y = rotation.y / window.height() * PI * cam.sensitivity.y; let yaw = Quat::from_rotation_y(-delta_x); From a7328486532190a668e793ba4a090c450cfbe251 Mon Sep 17 00:00:00 2001 From: olekspickle <22867443+olekspickle@users.noreply.github.com> Date: Wed, 21 May 2025 19:58:46 +0200 Subject: [PATCH 2/7] rm redundant query filters; fix outdated docs --- src/gamepad.rs | 5 +---- src/lib.rs | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/gamepad.rs b/src/gamepad.rs index 54882f0..7f27217 100644 --- a/src/gamepad.rs +++ b/src/gamepad.rs @@ -73,13 +73,10 @@ pub fn orbit_gamepad( if cam.mouse_orbit_button_enabled && !gamepad.pressed(cam.gamepad_settings.mouse_orbit_button) { return; } - - let x_axis = gamepad.right_stick().x; - let y_axis = gamepad.right_stick().y; + let (x, y) = (gamepad.right_stick().x, gamepad.right_stick().y); let deadzone = 0.5; let mut rotation = Vec2::ZERO; - let (x, y) = (x_axis, y_axis); if x.abs() > deadzone || y.abs() > deadzone { rotation = Vec2::new(x, y); } diff --git a/src/lib.rs b/src/lib.rs index c7015eb..059310e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,6 +148,13 @@ impl Default for ThirdPersonCamera { } } +impl ThirdPersonCamera { + pub fn with_custom_settings(mut self, gamepad_settings: CustomGamepadSettings) -> Self { + self.gamepad_settings = gamepad_settings; + self + } +} + /// Sets the zoom bounds (min & max) pub struct Zoom { pub min: f32, @@ -193,19 +200,9 @@ impl Offset { /// use bevy::prelude::*; /// use bevy_third_person_camera::{CustomGamepadSettings, ThirdPersonCamera}; /// fn spawn_camera(mut commands: Commands) { -/// let gamepad = Gamepad::new(0); +/// let settings = CustomGamepadSettings::default() /// commands.spawn(( -/// ThirdPersonCamera { -/// gamepad_settings: CustomGamepadSettings { -/// aim_button: GamepadButton::new(gamepad, GamepadButtonType::LeftTrigger2), -/// mouse_orbit_button: GamepadButton::new(gamepad, GamepadButtonType::LeftTrigger), -/// offset_toggle_button: GamepadButton::new(gamepad, GamepadButtonType::DPadRight), -/// sensitivity: Vec2::new(7.0, 4.0), -/// zoom_in_button: GamepadButton::new(gamepad, GamepadButtonType::DPadUp), -/// zoom_out_button: GamepadButton::new(gamepad, GamepadButtonType::DPadDown), -/// }, -/// ..default() -/// }, +/// ThirdPersonCamera::default().with_custom_settings(settings) /// Camera3dBundle::default(), /// )); /// } @@ -360,7 +357,7 @@ fn aim( } } -pub fn zoom_condition(cam_q: Query<&ThirdPersonCamera, With>) -> bool { +pub fn zoom_condition(cam_q: Query<&ThirdPersonCamera>) -> bool { let Ok(cam) = cam_q.single() else { return false; }; @@ -368,7 +365,7 @@ pub fn zoom_condition(cam_q: Query<&ThirdPersonCamera, With>) } // only run toggle_x_offset if `offset_toggle_enabled` is true -fn toggle_x_offset_condition(cam_q: Query<&ThirdPersonCamera, With>) -> bool { +fn toggle_x_offset_condition(cam_q: Query<&ThirdPersonCamera>) -> bool { let Ok(cam) = cam_q.single() else { return false; }; @@ -377,7 +374,7 @@ fn toggle_x_offset_condition(cam_q: Query<&ThirdPersonCamera, With right shoulder view & vice versa fn toggle_x_offset( - mut cam_q: Query<&mut ThirdPersonCamera, With>, + mut cam_q: Query<&mut ThirdPersonCamera>, keys: Res>, time: Res