generated from Leafwing-Studios/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 120
/
mouse_wheel.rs
75 lines (64 loc) · 2.91 KB
/
mouse_wheel.rs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(InputManagerPlugin::<CameraMovement>::default())
.add_systems(Startup, setup)
.add_systems(Update, zoom_camera)
.add_systems(Update, pan_camera.after(zoom_camera))
.run()
}
#[derive(Actionlike, Clone, Debug, Copy, PartialEq, Eq, Hash, Reflect)]
enum CameraMovement {
Zoom,
PanLeft,
PanRight,
}
fn setup(mut commands: Commands) {
commands
.spawn(Camera2dBundle::default())
.insert(InputManagerBundle::<CameraMovement> {
input_map: InputMap::default()
// This will capture the total continuous value, for direct use.
.insert(CameraMovement::Zoom, SingleAxis::mouse_wheel_y())
// This will return a binary button-like output.
.insert(CameraMovement::PanLeft, MouseWheelDirection::Left)
.insert(CameraMovement::PanRight, MouseWheelDirection::Right)
// Alternatively, you could model this as a virtual Dpad,
// which is extremely useful when you want to model 4-directional buttonlike inputs using the mouse wheel
// .insert(VirtualDpad::mouse_wheel(), Pan)
// Or even a continuous `DualAxis`!
// .insert(DualAxis::mouse_wheel(), Pan)
.build(),
..default()
});
commands.spawn(SpriteBundle {
transform: Transform::from_scale(Vec3::new(100., 100., 1.)),
..default()
});
}
fn zoom_camera(
mut query: Query<(&mut OrthographicProjection, &ActionState<CameraMovement>), With<Camera2d>>,
) {
const CAMERA_ZOOM_RATE: f32 = 0.05;
let (mut camera_projection, action_state) = query.single_mut();
// Here, we use the `action_value` method to extract the total net amount that the mouse wheel has travelled
// Up and right axis movements are always positive by default
let zoom_delta = action_state.value(&CameraMovement::Zoom);
// We want to zoom in when we use mouse wheel up
// so we increase the scale proportionally
// Note that the projections scale should always be positive (or our images will flip)
camera_projection.scale *= 1. - zoom_delta * CAMERA_ZOOM_RATE;
}
fn pan_camera(mut query: Query<(&mut Transform, &ActionState<CameraMovement>), With<Camera2d>>) {
const CAMERA_PAN_RATE: f32 = 10.;
let (mut camera_transform, action_state) = query.single_mut();
// When using the `MouseWheelDirection` type, mouse wheel inputs can be treated like simple buttons
if action_state.pressed(&CameraMovement::PanLeft) {
camera_transform.translation.x -= CAMERA_PAN_RATE;
}
if action_state.pressed(&CameraMovement::PanRight) {
camera_transform.translation.x += CAMERA_PAN_RATE;
}
}