Skip to content

Commit f047be1

Browse files
committed
*Chainer -> *Chain, if_() -> chain()
1 parent 41db2aa commit f047be1

File tree

4 files changed

+30
-38
lines changed

4 files changed

+30
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
## Added
55

6-
- Added `_do` variants to all of the `[just_]pressed[_any]` methods of `KeyboardState` and `MouseState`. These new variants accept closures to perform the logic, and can be chained. Contributed by [@just-do-halee](https://github.com/just-do-halee) in [#55].
6+
- Added `KeyboardStateChain` and `MouseStateChain` to provide a functional interface for dealing with user input. Call `.chain()` on `KeyboardState` or `MouseState` to access them. These new structs have methods with the same names as in their `Chain`-less variants which accept closures to perform the logic, and can be chained. Contributed by [@just-do-halee](https://github.com/just-do-halee) in [#55].
77

88
## Improved
99

examples/keyboard_state.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,42 +33,34 @@ fn logic(game_state: &mut Engine, _: &mut ()) {
3333

3434
// Handle keyboard input
3535
let ks = &mut game_state.keyboard_state;
36-
if ks.pressed_any(&[KeyCode::A, KeyCode::Left]) {
37-
race_car.translation.x -= move_amount;
38-
}
39-
if ks.pressed_any(&[KeyCode::D, KeyCode::Right, KeyCode::E]) {
40-
race_car.translation.x += move_amount;
41-
}
42-
if ks.pressed_any(&[KeyCode::O, KeyCode::Down, KeyCode::S]) {
43-
race_car.translation.y -= move_amount;
44-
}
4536
if ks.pressed_any(&[KeyCode::W, KeyCode::Up, KeyCode::Comma]) {
4637
race_car.translation.y += move_amount;
4738
}
48-
if ks.pressed_any(&[KeyCode::Z, KeyCode::Semicolon]) {
49-
race_car.rotation += rotation_amount;
50-
}
51-
if ks.pressed_any(&[KeyCode::C, KeyCode::J]) {
52-
race_car.rotation -= rotation_amount;
39+
if ks.pressed_any(&[KeyCode::A, KeyCode::Left]) {
40+
race_car.translation.x -= move_amount;
5341
}
54-
if ks.pressed_any(&[KeyCode::Plus, KeyCode::Equals]) {
55-
race_car.scale *= 1.0 + scale_amount;
42+
if ks.pressed_any(&[KeyCode::S, KeyCode::Down, KeyCode::O]) {
43+
race_car.translation.y -= move_amount;
5644
}
57-
if ks.pressed_any(&[KeyCode::Minus, KeyCode::Underline]) {
58-
race_car.scale *= 1.0 - scale_amount;
45+
if ks.pressed_any(&[KeyCode::D, KeyCode::Right, KeyCode::E]) {
46+
race_car.translation.x += move_amount;
5947
}
6048

61-
// If you prefer a more functional style, there are also method `if_` that accept a
62-
// closure to perform your logic with and are chainable, like this:
63-
//
64-
// ks.if_()
65-
// .pressed_any(&[KeyCode::A, KeyCode::Left], |_| {
66-
// race_car.translation.x -= move_amount;
67-
// })
68-
// .pressed_any(&[KeyCode::D, KeyCode::Right, KeyCode::E], |_| {
69-
// race_car.translation.x += move_amount;
70-
// });
71-
// ...etc
49+
// If you prefer a more functional style that is equivalent to the kind of logic above,
50+
// but takes closures to run if the buttons are pressed, you can call `.chain()`
51+
ks.chain()
52+
.pressed_any(&[KeyCode::Z, KeyCode::Semicolon], |_| {
53+
race_car.rotation += rotation_amount;
54+
})
55+
.pressed_any(&[KeyCode::C, KeyCode::J], |_| {
56+
race_car.rotation -= rotation_amount;
57+
})
58+
.pressed_any(&[KeyCode::Plus, KeyCode::Equals], |_| {
59+
race_car.scale *= 1.0 + scale_amount;
60+
})
61+
.pressed_any(&[KeyCode::Minus, KeyCode::Underline], |_| {
62+
race_car.scale *= 1.0 - scale_amount;
63+
});
7264

7365
// Clamp the scale to a certain range so the scaling is reasonable
7466
race_car.scale = race_car.scale.clamp(0.1, 3.0);

src/keyboard.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ fn sync_keyboard_events(
3030
}
3131
}
3232

33-
pub struct KeyboardStateChainer(KeyboardState);
33+
pub struct KeyboardStateChain(KeyboardState);
3434

35-
impl KeyboardStateChainer {
35+
impl KeyboardStateChain {
3636
/// Calls the closure if a key is currently pressed
3737
#[inline]
3838
pub fn pressed(&self, key: KeyCode, mut then: impl FnMut(&KeyboardState)) -> &Self {
@@ -130,8 +130,8 @@ impl KeyboardState {
130130
pub fn just_released_any(&self, key_codes: &[KeyCode]) -> bool {
131131
key_codes.iter().any(|k| self.just_released(*k))
132132
}
133-
pub fn if_(&self) -> KeyboardStateChainer {
134-
KeyboardStateChainer(self.clone())
133+
pub fn chain(&self) -> KeyboardStateChain {
134+
KeyboardStateChain(self.clone())
135135
}
136136
}
137137

src/mouse.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ impl Plugin for MousePlugin {
2222
}
2323
}
2424

25-
pub struct MouseStateChainer(MouseState);
25+
pub struct MouseStateChain(MouseState);
2626

27-
impl MouseStateChainer {
27+
impl MouseStateChain {
2828
/// Calls the closure if the mouse button was pressed
2929
#[inline]
3030
pub fn pressed(&self, mouse_button: MouseButton, mut then: impl FnMut(&MouseState)) -> &Self {
@@ -172,8 +172,8 @@ impl MouseState {
172172
pub fn just_released_any(&self, mouse_buttons: &[MouseButton]) -> bool {
173173
mouse_buttons.iter().any(|k| self.just_released(*k))
174174
}
175-
pub fn if_(&self) -> MouseStateChainer {
176-
MouseStateChainer(self.clone())
175+
pub fn chain(&self) -> MouseStateChain {
176+
MouseStateChain(self.clone())
177177
}
178178
}
179179

0 commit comments

Comments
 (0)