Skip to content

Commit

Permalink
Fix MouseMotion resource (#40)
Browse files Browse the repository at this point in the history
Fixes #39.

The `MouseMotion` resource did not get properly updated due to a mixup between `bevy_input::MouseMotion` and `bevy_mouse_tracking::MouseMotion`.

This mixup has been fixed. One of the examples has been modified to make use of `MouseMotion`, to prevent future regressions.
  • Loading branch information
JoJoJet committed Jan 27, 2023
1 parent 30cd2fa commit fd118a6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bevy_mouse_tracking_plugin"
description = "A plugin for effortless mouse tracking in the bevy game engine."
version = "0.5.2"
version = "0.5.3"
authors = ["JoJoJet <joe102000@gmail.com>"]
edition = "2021"
license = "MIT"
Expand Down
14 changes: 11 additions & 3 deletions examples/screen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::prelude::*;

use bevy_mouse_tracking_plugin::{prelude::*, MainCamera, MousePos};
use bevy_mouse_tracking_plugin::{prelude::*, MainCamera, MouseMotion, MousePos};

#[derive(Component)]
struct Cursor;
Expand All @@ -13,6 +13,7 @@ fn main() {
.add_plugins(DefaultPlugins)
.insert_resource(ClearColor(Color::BLACK))
.add_plugin(MousePosPlugin)
.add_plugin(MouseMotionPlugin)
.add_startup_system(setup)
.add_system(bevy::window::close_on_esc)
.add_system(run)
Expand Down Expand Up @@ -61,8 +62,15 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, windows: Res<Wi
));
}

fn run(mouse_pos: Res<MousePos>, mut hud_text: Query<&mut Text, With<Hud>>) {
let hud_value = format!("Mouse: ({}, {})", mouse_pos.x, mouse_pos.y,);
fn run(
mouse_pos: Res<MousePos>,
mouse_motion: Res<MouseMotion>,
mut hud_text: Query<&mut Text, With<Hud>>,
) {
let hud_value = format!(
"Mouse: ({}, {})\nDelta: ({}, {})",
mouse_pos.x, mouse_pos.y, mouse_motion.delta.x, mouse_motion.delta.y,
);

if let Some(mut hud_text) = hud_text.iter_mut().next() {
hud_text.sections.first_mut().unwrap().value = hud_value;
Expand Down
4 changes: 3 additions & 1 deletion src/mouse_motion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;

use bevy::input::mouse::MouseMotion as BevyMouseMotion;

/// Plugin that tracks mouse motion.
pub struct MouseMotionPlugin;

Expand All @@ -19,7 +21,7 @@ impl bevy::app::Plugin for MouseMotionPlugin {
}
}

fn update_mouse_motion(mut events: EventReader<MouseMotion>, mut res: ResMut<MouseMotion>) {
fn update_mouse_motion(mut events: EventReader<BevyMouseMotion>, mut res: ResMut<MouseMotion>) {
let delta = events.iter().fold(Vec2::ZERO, |acc, e| acc + e.delta);
*res = MouseMotion { delta };
}

0 comments on commit fd118a6

Please sign in to comment.