Skip to content

Commit 2917113

Browse files
committed
easy to use MouseState chaining function caller
1 parent 164a77b commit 2917113

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

examples/mouse_state.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ fn logic(engine: &mut Engine, _: &mut ()) {
4343
if let Some(sprite) = engine.sprites.get_mut("Race Car") {
4444
// Use the latest state of the mouse buttons to rotate the sprite
4545
let mut rotation_amount = 0.0;
46-
if engine.mouse_state.pressed(MouseButton::Left) {
47-
rotation_amount += ROTATION_SPEED * engine.delta_f32;
48-
}
49-
if engine.mouse_state.pressed(MouseButton::Right) {
50-
rotation_amount -= ROTATION_SPEED * engine.delta_f32;
51-
}
46+
47+
engine
48+
.mouse_state
49+
.pressed_do(MouseButton::Left, || {
50+
rotation_amount += ROTATION_SPEED * engine.delta_f32;
51+
})
52+
.pressed_do(MouseButton::Right, || {
53+
rotation_amount -= ROTATION_SPEED * engine.delta_f32;
54+
});
55+
5256
sprite.rotation += rotation_amount;
5357

5458
// Use the latest state of the mouse wheel to scale the sprite

src/mouse.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,59 @@ impl MouseState {
9999
pub fn just_released_any(&self, mouse_buttons: &[MouseButton]) -> bool {
100100
mouse_buttons.iter().any(|k| self.just_released(*k))
101101
}
102+
// -------------------------
103+
/// Calls the closure if the mouse button was pressed
104+
#[inline]
105+
pub fn pressed_do(&self, mouse_button: MouseButton, mut f: impl FnMut()) -> &Self {
106+
if self.pressed(mouse_button) {
107+
f();
108+
}
109+
self
110+
}
111+
/// Calls the closure if any of the indicated mouse buttons were pressed
112+
#[inline]
113+
pub fn pressed_any_do(&self, mouse_buttons: &[MouseButton], mut f: impl FnMut()) -> &Self {
114+
if self.pressed_any(mouse_buttons) {
115+
f();
116+
}
117+
self
118+
}
119+
/// Calls the closure if the mouse button started being pressed during the last frame
120+
#[inline]
121+
pub fn just_pressed_do(&self, mouse_button: MouseButton, mut f: impl FnMut()) -> &Self {
122+
if self.just_pressed(mouse_button) {
123+
f();
124+
}
125+
self
126+
}
127+
/// Calls the closure if any of the indicated mouse buttons were just pressed this frame
128+
#[inline]
129+
pub fn just_pressed_any_do(&self, mouse_buttons: &[MouseButton], mut f: impl FnMut()) -> &Self {
130+
if self.just_pressed_any(mouse_buttons) {
131+
f();
132+
}
133+
self
134+
}
135+
/// Calls the closure if the mouse button started being released during the last frame
136+
#[inline]
137+
pub fn just_released_do(&self, mouse_button: MouseButton, mut f: impl FnMut()) -> &Self {
138+
if self.just_released(mouse_button) {
139+
f();
140+
}
141+
self
142+
}
143+
/// Calls the closure if any of the indicated mouse buttons were just released this frame
144+
#[inline]
145+
pub fn just_released_any_do(
146+
&self,
147+
mouse_buttons: &[MouseButton],
148+
mut f: impl FnMut(),
149+
) -> &Self {
150+
if self.just_released_any(mouse_buttons) {
151+
f();
152+
}
153+
self
154+
}
102155
}
103156

104157
/// Gather the mouse events from bevy and store them for our own use

0 commit comments

Comments
 (0)