Skip to content

Commit 164a77b

Browse files
committed
easy to use KeyboardState chaining function caller
1 parent 1666aeb commit 164a77b

File tree

4 files changed

+97
-48
lines changed

4 files changed

+97
-48
lines changed

examples/keyboard_state.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,32 @@ 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-
}
45-
if ks.pressed_any(&[KeyCode::W, KeyCode::Up, KeyCode::Comma]) {
46-
race_car.translation.y += move_amount;
47-
}
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;
53-
}
54-
if ks.pressed_any(&[KeyCode::Plus, KeyCode::Equals]) {
55-
race_car.scale *= 1.0 + scale_amount;
56-
}
57-
if ks.pressed_any(&[KeyCode::Minus, KeyCode::Underline]) {
58-
race_car.scale *= 1.0 - scale_amount;
59-
}
36+
37+
ks //
38+
.pressed_any_do(&[KeyCode::A, KeyCode::Left], || {
39+
race_car.translation.x -= move_amount;
40+
})
41+
.pressed_any_do(&[KeyCode::D, KeyCode::Right, KeyCode::E], || {
42+
race_car.translation.x += move_amount;
43+
})
44+
.pressed_any_do(&[KeyCode::O, KeyCode::Down, KeyCode::S], || {
45+
race_car.translation.y -= move_amount;
46+
})
47+
.pressed_any_do(&[KeyCode::W, KeyCode::Up, KeyCode::Comma], || {
48+
race_car.translation.y += move_amount;
49+
})
50+
.pressed_any_do(&[KeyCode::Z, KeyCode::Semicolon], || {
51+
race_car.rotation += rotation_amount;
52+
})
53+
.pressed_any_do(&[KeyCode::C, KeyCode::J], || {
54+
race_car.rotation -= rotation_amount;
55+
})
56+
.pressed_any_do(&[KeyCode::Plus, KeyCode::Equals], || {
57+
race_car.scale *= 1.0 + scale_amount;
58+
})
59+
.pressed_any_do(&[KeyCode::Minus, KeyCode::Underline], || {
60+
race_car.scale *= 1.0 - scale_amount;
61+
});
6062

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

examples/scenarios/extreme_drivers_ed.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -860,19 +860,21 @@ fn logic(engine: &mut Engine, game_state: &mut GameState) {
860860
{
861861
use KeyCode::*;
862862
// Acceleration input
863-
if engine.keyboard_state.pressed_any(&[W, Up, Comma]) {
864-
acceleration += 1.0;
865-
}
866-
if engine.keyboard_state.pressed_any(&[S, Down, O]) {
867-
acceleration -= 1.0;
868-
}
869-
// Rotation/Turning input
870-
if engine.keyboard_state.pressed_any(&[A, Left]) {
871-
rotation += 1.0;
872-
}
873-
if engine.keyboard_state.pressed_any(&[D, Right, E]) {
874-
rotation -= 1.0;
875-
}
863+
engine
864+
.keyboard_state
865+
.pressed_any_do(&[W, Up, Comma], || {
866+
acceleration += 1.0;
867+
})
868+
.pressed_any_do(&[S, Down, O], || {
869+
acceleration -= 1.0;
870+
})
871+
// Rotation/Turning input
872+
.pressed_any_do(&[A, Left], || {
873+
rotation += 1.0;
874+
})
875+
.pressed_any_do(&[D, Right, E], || {
876+
rotation -= 1.0;
877+
});
876878
}
877879
let mut velocity_magnitude = game_state.velocity.length();
878880
velocity_magnitude += (acceleration * ACCELERATION_RATE) * engine.delta_f32;

examples/scenarios/road_race.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,14 @@ fn game_logic(engine: &mut Engine, game_state: &mut GameState) {
7272

7373
// Collect keyboard input
7474
let mut direction = 0.0;
75-
if engine
75+
engine
7676
.keyboard_state
77-
.pressed_any(&[KeyCode::Up, KeyCode::W, KeyCode::Comma])
78-
{
79-
direction += 1.0;
80-
}
81-
if engine
82-
.keyboard_state
83-
.pressed_any(&[KeyCode::Down, KeyCode::S, KeyCode::O])
84-
{
85-
direction -= 1.0;
86-
}
77+
.pressed_any_do(&[KeyCode::Up, KeyCode::W, KeyCode::Comma], || {
78+
direction += 1.0;
79+
})
80+
.pressed_any_do(&[KeyCode::Down, KeyCode::S, KeyCode::O], || {
81+
direction -= 1.0;
82+
});
8783

8884
// Move the player sprite
8985
let player1 = engine.sprites.get_mut("player1").unwrap();

src/keyboard.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,55 @@ impl KeyboardState {
6565
pub fn just_released_any(&self, key_codes: &[KeyCode]) -> bool {
6666
key_codes.iter().any(|k| self.just_released(*k))
6767
}
68+
// -----------------------------------
69+
/// Calls the closure if a key is currently pressed
70+
#[inline]
71+
pub fn pressed_do(&self, key: KeyCode, mut f: impl FnMut()) -> &Self {
72+
if self.pressed(key) {
73+
f();
74+
}
75+
self
76+
}
77+
/// Calls the closure if any of the keys are currently pressed
78+
#[inline]
79+
pub fn pressed_any_do(&self, key_codes: &[KeyCode], mut f: impl FnMut()) -> &Self {
80+
if self.pressed_any(key_codes) {
81+
f();
82+
}
83+
self
84+
}
85+
/// Calls the closure if a key started being pressed this frame
86+
#[inline]
87+
pub fn just_pressed_do(&self, key: KeyCode, mut f: impl FnMut()) -> &Self {
88+
if self.just_pressed(key) {
89+
f();
90+
}
91+
self
92+
}
93+
/// Calls the closure if any of the indicated keys started being pressed this frame
94+
#[inline]
95+
pub fn just_pressed_any_do(&self, key_codes: &[KeyCode], mut f: impl FnMut()) -> &Self {
96+
if self.just_pressed_any(key_codes) {
97+
f();
98+
}
99+
self
100+
}
101+
/// Calls the closure if a key started being released this frame
102+
#[inline]
103+
pub fn just_released_do(&self, key: KeyCode, mut f: impl FnMut()) -> &Self {
104+
if self.just_released(key) {
105+
f();
106+
}
107+
self
108+
}
109+
/// Calls the closure if any of the indicated keys started being released this frame
110+
#[inline]
111+
pub fn just_released_any_do(&self, key_codes: &[KeyCode], mut f: impl FnMut()) -> &Self {
112+
if self.just_released_any(key_codes) {
113+
f();
114+
}
115+
self
116+
}
68117
}
69118

70119
/// store bevy's keyboard state for our own use

0 commit comments

Comments
 (0)