forked from Leafwing-Studios/leafwing-input-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_motion.rs
More file actions
47 lines (39 loc) · 1.66 KB
/
Copy pathmouse_motion.rs
File metadata and controls
47 lines (39 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(InputManagerPlugin::<CameraMovement>::default())
.add_startup_system(setup)
.add_system(pan_camera)
.run()
}
#[derive(Actionlike, Clone, Debug, Copy, PartialEq, Eq)]
enum CameraMovement {
Pan,
}
fn setup(mut commands: Commands) {
commands
.spawn(Camera2dBundle::default())
.insert(InputManagerBundle::<CameraMovement> {
input_map: InputMap::default()
// This will capture the total continous value, for direct use
// Note that you can also use discrete gesture-like motion, via the `MouseMotionDirection` enum
.insert(DualAxis::mouse_motion(), CameraMovement::Pan)
.build(),
..default()
});
commands.spawn(SpriteBundle {
transform: Transform::from_scale(Vec3::new(100., 100., 1.)),
..default()
});
}
fn pan_camera(mut query: Query<(&mut Transform, &ActionState<CameraMovement>), With<Camera2d>>) {
const CAMERA_PAN_RATE: f32 = 0.5;
let (mut camera_transform, action_state) = query.single_mut();
let camera_pan_vector = action_state.axis_pair(CameraMovement::Pan).unwrap();
// Because we're moving the camera, not the object, we want to pan in the opposite direction
// However, UI cordinates are inverted on the y-axis, so we need to flip y a second time
camera_transform.translation.x -= CAMERA_PAN_RATE * camera_pan_vector.x();
camera_transform.translation.y += CAMERA_PAN_RATE * camera_pan_vector.y();
}