-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathmouse_events.rs
More file actions
91 lines (77 loc) · 3.63 KB
/
mouse_events.rs
File metadata and controls
91 lines (77 loc) · 3.63 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! To run this code, clone the rusty_engine repository and run the command:
//!
//! cargo run --release --example mouse_events
use rusty_engine::prelude::*;
const ORIGIN_LOCATION: (f32, f32) = (0.0, -200.0);
fn main() {
let mut game = Game::new();
let race_car = game.add_sprite("Race Car", SpritePreset::RacingCarGreen);
race_car.translation = Vec2::new(0.0, 0.0);
race_car.rotation = UP;
race_car.scale = 1.0;
race_car.layer = 2.0;
let mover = game.add_sprite("move indicator", SpritePreset::RollingHoleStart);
mover.translation = ORIGIN_LOCATION.into();
mover.layer = 1.0;
let anchor = game.add_sprite("move indicator origin", SpritePreset::RollingHoleEnd);
anchor.translation = ORIGIN_LOCATION.into();
anchor.layer = 0.0;
let msg = game.add_text("relative message", "Relative Mouse Motion Indicator");
msg.translation.y = -300.0;
msg.font_size = 20.0;
let msg2 = game.add_text(
"instructions",
"Discrete Movement with Mouse Events\n==============================\nMove the car around with your mouse.\nRotate it by clicking left/right mouse buttons.\nScale it with the mousewheel.",
);
msg2.font_size = 30.0;
msg2.translation.y = 275.0;
game.add_logic(logic);
game.run(());
}
fn logic(engine_state: &mut EngineState, _: &mut ()) -> bool {
if let Some(sprite) = engine_state.sprites.get_mut("Race Car") {
// Use mouse button events to rotate. Every click rotates the sprite by a fixed amount
for mouse_button_input in &engine_state.mouse_button_events {
if mouse_button_input.state != ElementState::Pressed {
break;
}
match mouse_button_input.button {
MouseButton::Left => sprite.rotation += std::f32::consts::FRAC_PI_4,
MouseButton::Right => sprite.rotation -= std::f32::consts::FRAC_PI_4,
_ => {}
}
}
// Use mouse location events to set the location of the sprite. This loop is effectively
// discarding all but the last location. If that is what you want, you should use
// EngineState::mouse_state instead. See the mouse_state example for more details.
for cursor_moved in &engine_state.mouse_location_events {
sprite.translation = cursor_moved.position;
}
// Use the mouse wheel events to scale the sprite. Events are typically the best way to deal
// with the mouse wheel, because then you can handle quick spins by processing each event
// individually.
for mouse_wheel in &engine_state.mouse_wheel_events {
sprite.scale *= 1.0 + (0.05 * mouse_wheel.y);
sprite.scale = sprite.scale.clamp(0.1, 4.0);
}
}
// Offset the move indicator sprite from the move indicator origin to visually represent the
// relative mouse motion for the frame
if let Some(sprite) = engine_state.sprites.get_mut("move indicator") {
// let motion = game_state.mouse_state.motion();
// if motion != Vec2::ZERO {
// sprite.translation = motion + ORIGIN_LOCATION.into();
// }
let mut cumulative_motion = Vec2::ZERO;
for mouse_motion in &engine_state.mouse_motion_events {
cumulative_motion += mouse_motion.delta
}
// There seems to be a Bevy 0.6 bug where every other frame we don't receive any mouse
// motion events, so ignore those frames.
// TODO: Follow up on this bug in upstream Bevy
if cumulative_motion != Vec2::ZERO {
sprite.translation = cumulative_motion + Vec2::from(ORIGIN_LOCATION);
}
}
true
}