Skip to content

Commit 2dc9111

Browse files
committed
[fixing] KeyboardState, MouseState: .._do method can be nested
1 parent 2917113 commit 2dc9111

File tree

6 files changed

+68
-40
lines changed

6 files changed

+68
-40
lines changed

examples/keyboard_state.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@ fn logic(game_state: &mut Engine, _: &mut ()) {
3535
let ks = &mut game_state.keyboard_state;
3636

3737
ks //
38-
.pressed_any_do(&[KeyCode::A, KeyCode::Left], || {
38+
.pressed_any_do(&[KeyCode::A, KeyCode::Left], |_| {
3939
race_car.translation.x -= move_amount;
4040
})
41-
.pressed_any_do(&[KeyCode::D, KeyCode::Right, KeyCode::E], || {
41+
.pressed_any_do(&[KeyCode::D, KeyCode::Right, KeyCode::E], |_| {
4242
race_car.translation.x += move_amount;
4343
})
44-
.pressed_any_do(&[KeyCode::O, KeyCode::Down, KeyCode::S], || {
44+
.pressed_any_do(&[KeyCode::O, KeyCode::Down, KeyCode::S], |_| {
4545
race_car.translation.y -= move_amount;
4646
})
47-
.pressed_any_do(&[KeyCode::W, KeyCode::Up, KeyCode::Comma], || {
47+
.pressed_any_do(&[KeyCode::W, KeyCode::Up, KeyCode::Comma], |_| {
4848
race_car.translation.y += move_amount;
4949
})
50-
.pressed_any_do(&[KeyCode::Z, KeyCode::Semicolon], || {
50+
.pressed_any_do(&[KeyCode::Z, KeyCode::Semicolon], |_| {
5151
race_car.rotation += rotation_amount;
5252
})
53-
.pressed_any_do(&[KeyCode::C, KeyCode::J], || {
53+
.pressed_any_do(&[KeyCode::C, KeyCode::J], |_| {
5454
race_car.rotation -= rotation_amount;
5555
})
56-
.pressed_any_do(&[KeyCode::Plus, KeyCode::Equals], || {
56+
.pressed_any_do(&[KeyCode::Plus, KeyCode::Equals], |_| {
5757
race_car.scale *= 1.0 + scale_amount;
5858
})
59-
.pressed_any_do(&[KeyCode::Minus, KeyCode::Underline], || {
59+
.pressed_any_do(&[KeyCode::Minus, KeyCode::Underline], |_| {
6060
race_car.scale *= 1.0 - scale_amount;
6161
});
6262

examples/mouse_state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ fn logic(engine: &mut Engine, _: &mut ()) {
4646

4747
engine
4848
.mouse_state
49-
.pressed_do(MouseButton::Left, || {
49+
.pressed_do(MouseButton::Left, |_| {
5050
rotation_amount += ROTATION_SPEED * engine.delta_f32;
5151
})
52-
.pressed_do(MouseButton::Right, || {
52+
.pressed_do(MouseButton::Right, |_| {
5353
rotation_amount -= ROTATION_SPEED * engine.delta_f32;
5454
});
5555

examples/scenarios/extreme_drivers_ed.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -862,17 +862,17 @@ fn logic(engine: &mut Engine, game_state: &mut GameState) {
862862
// Acceleration input
863863
engine
864864
.keyboard_state
865-
.pressed_any_do(&[W, Up, Comma], || {
865+
.pressed_any_do(&[W, Up, Comma], |_| {
866866
acceleration += 1.0;
867867
})
868-
.pressed_any_do(&[S, Down, O], || {
868+
.pressed_any_do(&[S, Down, O], |_| {
869869
acceleration -= 1.0;
870870
})
871871
// Rotation/Turning input
872-
.pressed_any_do(&[A, Left], || {
872+
.pressed_any_do(&[A, Left], |_| {
873873
rotation += 1.0;
874874
})
875-
.pressed_any_do(&[D, Right, E], || {
875+
.pressed_any_do(&[D, Right, E], |_| {
876876
rotation -= 1.0;
877877
});
878878
}

examples/scenarios/road_race.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ fn game_logic(engine: &mut Engine, game_state: &mut GameState) {
7474
let mut direction = 0.0;
7575
engine
7676
.keyboard_state
77-
.pressed_any_do(&[KeyCode::Up, KeyCode::W, KeyCode::Comma], || {
77+
.pressed_any_do(&[KeyCode::Up, KeyCode::W, KeyCode::Comma], |_| {
7878
direction += 1.0;
7979
})
80-
.pressed_any_do(&[KeyCode::Down, KeyCode::S, KeyCode::O], || {
80+
.pressed_any_do(&[KeyCode::Down, KeyCode::S, KeyCode::O], |_| {
8181
direction -= 1.0;
8282
});
8383

src/keyboard.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,49 +68,61 @@ impl KeyboardState {
6868
// -----------------------------------
6969
/// Calls the closure if a key is currently pressed
7070
#[inline]
71-
pub fn pressed_do(&self, key: KeyCode, mut f: impl FnMut()) -> &Self {
71+
pub fn pressed_do(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
7272
if self.pressed(key) {
73-
f();
73+
f(self);
7474
}
7575
self
7676
}
7777
/// Calls the closure if any of the keys are currently pressed
7878
#[inline]
79-
pub fn pressed_any_do(&self, key_codes: &[KeyCode], mut f: impl FnMut()) -> &Self {
79+
pub fn pressed_any_do(
80+
&self,
81+
key_codes: &[KeyCode],
82+
mut f: impl FnMut(&KeyboardState),
83+
) -> &Self {
8084
if self.pressed_any(key_codes) {
81-
f();
85+
f(self);
8286
}
8387
self
8488
}
8589
/// Calls the closure if a key started being pressed this frame
8690
#[inline]
87-
pub fn just_pressed_do(&self, key: KeyCode, mut f: impl FnMut()) -> &Self {
91+
pub fn just_pressed_do(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
8892
if self.just_pressed(key) {
89-
f();
93+
f(self);
9094
}
9195
self
9296
}
9397
/// Calls the closure if any of the indicated keys started being pressed this frame
9498
#[inline]
95-
pub fn just_pressed_any_do(&self, key_codes: &[KeyCode], mut f: impl FnMut()) -> &Self {
99+
pub fn just_pressed_any_do(
100+
&self,
101+
key_codes: &[KeyCode],
102+
mut f: impl FnMut(&KeyboardState),
103+
) -> &Self {
96104
if self.just_pressed_any(key_codes) {
97-
f();
105+
f(self);
98106
}
99107
self
100108
}
101109
/// Calls the closure if a key started being released this frame
102110
#[inline]
103-
pub fn just_released_do(&self, key: KeyCode, mut f: impl FnMut()) -> &Self {
111+
pub fn just_released_do(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
104112
if self.just_released(key) {
105-
f();
113+
f(self);
106114
}
107115
self
108116
}
109117
/// Calls the closure if any of the indicated keys started being released this frame
110118
#[inline]
111-
pub fn just_released_any_do(&self, key_codes: &[KeyCode], mut f: impl FnMut()) -> &Self {
119+
pub fn just_released_any_do(
120+
&self,
121+
key_codes: &[KeyCode],
122+
mut f: impl FnMut(&KeyboardState),
123+
) -> &Self {
112124
if self.just_released_any(key_codes) {
113-
f();
125+
f(self);
114126
}
115127
self
116128
}

src/mouse.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,41 +102,57 @@ impl MouseState {
102102
// -------------------------
103103
/// Calls the closure if the mouse button was pressed
104104
#[inline]
105-
pub fn pressed_do(&self, mouse_button: MouseButton, mut f: impl FnMut()) -> &Self {
105+
pub fn pressed_do(&self, mouse_button: MouseButton, mut f: impl FnMut(&MouseState)) -> &Self {
106106
if self.pressed(mouse_button) {
107-
f();
107+
f(self);
108108
}
109109
self
110110
}
111111
/// Calls the closure if any of the indicated mouse buttons were pressed
112112
#[inline]
113-
pub fn pressed_any_do(&self, mouse_buttons: &[MouseButton], mut f: impl FnMut()) -> &Self {
113+
pub fn pressed_any_do(
114+
&self,
115+
mouse_buttons: &[MouseButton],
116+
mut f: impl FnMut(&MouseState),
117+
) -> &Self {
114118
if self.pressed_any(mouse_buttons) {
115-
f();
119+
f(self);
116120
}
117121
self
118122
}
119123
/// Calls the closure if the mouse button started being pressed during the last frame
120124
#[inline]
121-
pub fn just_pressed_do(&self, mouse_button: MouseButton, mut f: impl FnMut()) -> &Self {
125+
pub fn just_pressed_do(
126+
&self,
127+
mouse_button: MouseButton,
128+
mut f: impl FnMut(&MouseState),
129+
) -> &Self {
122130
if self.just_pressed(mouse_button) {
123-
f();
131+
f(self);
124132
}
125133
self
126134
}
127135
/// Calls the closure if any of the indicated mouse buttons were just pressed this frame
128136
#[inline]
129-
pub fn just_pressed_any_do(&self, mouse_buttons: &[MouseButton], mut f: impl FnMut()) -> &Self {
137+
pub fn just_pressed_any_do(
138+
&self,
139+
mouse_buttons: &[MouseButton],
140+
mut f: impl FnMut(&MouseState),
141+
) -> &Self {
130142
if self.just_pressed_any(mouse_buttons) {
131-
f();
143+
f(self);
132144
}
133145
self
134146
}
135147
/// Calls the closure if the mouse button started being released during the last frame
136148
#[inline]
137-
pub fn just_released_do(&self, mouse_button: MouseButton, mut f: impl FnMut()) -> &Self {
149+
pub fn just_released_do(
150+
&self,
151+
mouse_button: MouseButton,
152+
mut f: impl FnMut(&MouseState),
153+
) -> &Self {
138154
if self.just_released(mouse_button) {
139-
f();
155+
f(self);
140156
}
141157
self
142158
}
@@ -145,10 +161,10 @@ impl MouseState {
145161
pub fn just_released_any_do(
146162
&self,
147163
mouse_buttons: &[MouseButton],
148-
mut f: impl FnMut(),
164+
mut f: impl FnMut(&MouseState),
149165
) -> &Self {
150166
if self.just_released_any(mouse_buttons) {
151-
f();
167+
f(self);
152168
}
153169
self
154170
}

0 commit comments

Comments
 (0)