Skip to content

Commit 5f40796

Browse files
committed
[changed] _state.[just_]pressed[_any]_do(..) -> _state.if_().[just_]pressed[_any]_and_then(..) now you can use &mut Engine inside the closure
1 parent ec46f0c commit 5f40796

File tree

3 files changed

+149
-130
lines changed

3 files changed

+149
-130
lines changed

examples/keyboard_state.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ fn logic(game_state: &mut Engine, _: &mut ()) {
5858
race_car.scale *= 1.0 - scale_amount;
5959
}
6060

61-
// If you prefer a more functional style, there are also methods ending in `_do` that accept a
62-
// closure to perform your logic with and are chainable, like this:
61+
// If you prefer a more functional style, there are also methods ending in `_and_then` of `_if()` method
62+
// that accept a closure to perform your logic with and are chainable, like this:
6363
//
64-
// ks.pressed_any_do(&[KeyCode::A, KeyCode::Left], |_| {
65-
// race_car.translation.x -= move_amount;
66-
// })
67-
// .pressed_any_do(&[KeyCode::D, KeyCode::Right, KeyCode::E], |_| {
68-
// race_car.translation.x += move_amount;
69-
// })
70-
// ...etc
64+
// ks.if_()
65+
// .pressed_any_and_then(&[KeyCode::A, KeyCode::Left], |_| {
66+
// race_car.translation.x -= move_amount;
67+
// })
68+
// .pressed_any_and_then(&[KeyCode::D, KeyCode::Right, KeyCode::E], |_| {
69+
// race_car.translation.x += move_amount;
70+
// })
71+
// ...etc
7172

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

src/keyboard.rs

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,104 +30,111 @@ fn sync_keyboard_events(
3030
}
3131
}
3232

33-
/// Represents the end-state of all keys during the last frame. Access it through
34-
/// [`Engine.keyboard_state`](crate::prelude::Engine) in your game logic function.
35-
#[derive(Clone, Debug, Default)]
36-
pub struct KeyboardState {
37-
this_frame: HashMap<KeyCode, bool>,
38-
last_frame: HashMap<KeyCode, bool>,
39-
}
33+
pub struct KeyboardStateChainer(KeyboardState);
4034

41-
impl KeyboardState {
42-
/// Returns true if a key is currently pressed
43-
pub fn pressed(&self, key: KeyCode) -> bool {
44-
*self.this_frame.get(&key).unwrap_or(&false)
45-
}
46-
/// Returns true if any of the keys are currently pressed
47-
pub fn pressed_any(&self, key_codes: &[KeyCode]) -> bool {
48-
key_codes.iter().any(|k| self.pressed(*k))
49-
}
50-
/// Returns true if a key started being pressed this frame
51-
pub fn just_pressed(&self, key: KeyCode) -> bool {
52-
*self.this_frame.get(&key).unwrap_or(&false)
53-
&& !*self.last_frame.get(&key).unwrap_or(&false)
54-
}
55-
/// Returns true if any of the indicated keys started being pressed this frame
56-
pub fn just_pressed_any(&self, key_codes: &[KeyCode]) -> bool {
57-
key_codes.iter().any(|k| self.just_pressed(*k))
58-
}
59-
/// Returns true if a key started being released this frame
60-
pub fn just_released(&self, key: KeyCode) -> bool {
61-
!*self.this_frame.get(&key).unwrap_or(&false)
62-
&& *self.last_frame.get(&key).unwrap_or(&false)
63-
}
64-
/// Returns true if any of the indicated keys started being released this frame
65-
pub fn just_released_any(&self, key_codes: &[KeyCode]) -> bool {
66-
key_codes.iter().any(|k| self.just_released(*k))
67-
}
68-
// -----------------------------------
35+
impl KeyboardStateChainer {
6936
/// Calls the closure if a key is currently pressed
7037
#[inline]
71-
pub fn pressed_do(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
72-
if self.pressed(key) {
73-
f(self);
38+
pub fn pressed_and_then(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
39+
if self.0.pressed(key) {
40+
f(&self.0);
7441
}
7542
self
7643
}
7744
/// Calls the closure if any of the keys are currently pressed
7845
#[inline]
79-
pub fn pressed_any_do(
46+
pub fn pressed_any_and_then(
8047
&self,
8148
key_codes: &[KeyCode],
8249
mut f: impl FnMut(&KeyboardState),
8350
) -> &Self {
84-
if self.pressed_any(key_codes) {
85-
f(self);
51+
if self.0.pressed_any(key_codes) {
52+
f(&self.0);
8653
}
8754
self
8855
}
8956
/// Calls the closure if a key started being pressed this frame
9057
#[inline]
91-
pub fn just_pressed_do(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
92-
if self.just_pressed(key) {
93-
f(self);
58+
pub fn just_pressed_and_then(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
59+
if self.0.just_pressed(key) {
60+
f(&self.0);
9461
}
9562
self
9663
}
9764
/// Calls the closure if any of the indicated keys started being pressed this frame
9865
#[inline]
99-
pub fn just_pressed_any_do(
66+
pub fn just_pressed_any_and_then(
10067
&self,
10168
key_codes: &[KeyCode],
10269
mut f: impl FnMut(&KeyboardState),
10370
) -> &Self {
104-
if self.just_pressed_any(key_codes) {
105-
f(self);
71+
if self.0.just_pressed_any(key_codes) {
72+
f(&self.0);
10673
}
10774
self
10875
}
10976
/// Calls the closure if a key started being released this frame
11077
#[inline]
111-
pub fn just_released_do(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
112-
if self.just_released(key) {
113-
f(self);
78+
pub fn just_released_and_then(&self, key: KeyCode, mut f: impl FnMut(&KeyboardState)) -> &Self {
79+
if self.0.just_released(key) {
80+
f(&self.0);
11481
}
11582
self
11683
}
11784
/// Calls the closure if any of the indicated keys started being released this frame
11885
#[inline]
119-
pub fn just_released_any_do(
86+
pub fn just_released_any_and_then(
12087
&self,
12188
key_codes: &[KeyCode],
12289
mut f: impl FnMut(&KeyboardState),
12390
) -> &Self {
124-
if self.just_released_any(key_codes) {
125-
f(self);
91+
if self.0.just_released_any(key_codes) {
92+
f(&self.0);
12693
}
12794
self
12895
}
12996
}
13097

98+
/// Represents the end-state of all keys during the last frame. Access it through
99+
/// [`Engine.keyboard_state`](crate::prelude::Engine) in your game logic function.
100+
#[derive(Clone, Debug, Default)]
101+
pub struct KeyboardState {
102+
this_frame: HashMap<KeyCode, bool>,
103+
last_frame: HashMap<KeyCode, bool>,
104+
}
105+
106+
impl KeyboardState {
107+
/// Returns true if a key is currently pressed
108+
pub fn pressed(&self, key: KeyCode) -> bool {
109+
*self.this_frame.get(&key).unwrap_or(&false)
110+
}
111+
/// Returns true if any of the keys are currently pressed
112+
pub fn pressed_any(&self, key_codes: &[KeyCode]) -> bool {
113+
key_codes.iter().any(|k| self.pressed(*k))
114+
}
115+
/// Returns true if a key started being pressed this frame
116+
pub fn just_pressed(&self, key: KeyCode) -> bool {
117+
*self.this_frame.get(&key).unwrap_or(&false)
118+
&& !*self.last_frame.get(&key).unwrap_or(&false)
119+
}
120+
/// Returns true if any of the indicated keys started being pressed this frame
121+
pub fn just_pressed_any(&self, key_codes: &[KeyCode]) -> bool {
122+
key_codes.iter().any(|k| self.just_pressed(*k))
123+
}
124+
/// Returns true if a key started being released this frame
125+
pub fn just_released(&self, key: KeyCode) -> bool {
126+
!*self.this_frame.get(&key).unwrap_or(&false)
127+
&& *self.last_frame.get(&key).unwrap_or(&false)
128+
}
129+
/// Returns true if any of the indicated keys started being released this frame
130+
pub fn just_released_any(&self, key_codes: &[KeyCode]) -> bool {
131+
key_codes.iter().any(|k| self.just_released(*k))
132+
}
133+
pub fn if_(&self) -> KeyboardStateChainer {
134+
KeyboardStateChainer(self.clone())
135+
}
136+
}
137+
131138
/// store bevy's keyboard state for our own use
132139
fn sync_keyboard_state(
133140
keyboard_input: Res<Input<KeyCode>>,

src/mouse.rs

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

25+
pub struct MouseStateChainer(MouseState);
26+
27+
impl MouseStateChainer {
28+
/// Calls the closure if the mouse button was pressed
29+
#[inline]
30+
pub fn pressed_and_then(
31+
&self,
32+
mouse_button: MouseButton,
33+
mut f: impl FnMut(&MouseState),
34+
) -> &Self {
35+
if self.0.pressed(mouse_button) {
36+
f(&self.0);
37+
}
38+
self
39+
}
40+
/// Calls the closure if any of the indicated mouse buttons were pressed
41+
#[inline]
42+
pub fn pressed_any_and_then(
43+
&self,
44+
mouse_buttons: &[MouseButton],
45+
mut f: impl FnMut(&MouseState),
46+
) -> &Self {
47+
if self.0.pressed_any(mouse_buttons) {
48+
f(&self.0);
49+
}
50+
self
51+
}
52+
/// Calls the closure if the mouse button started being pressed during the last frame
53+
#[inline]
54+
pub fn just_pressed_and_then(
55+
&self,
56+
mouse_button: MouseButton,
57+
mut f: impl FnMut(&MouseState),
58+
) -> &Self {
59+
if self.0.just_pressed(mouse_button) {
60+
f(&self.0);
61+
}
62+
self
63+
}
64+
/// Calls the closure if any of the indicated mouse buttons were just pressed this frame
65+
#[inline]
66+
pub fn just_pressed_any_and_then(
67+
&self,
68+
mouse_buttons: &[MouseButton],
69+
mut f: impl FnMut(&MouseState),
70+
) -> &Self {
71+
if self.0.just_pressed_any(mouse_buttons) {
72+
f(&self.0);
73+
}
74+
self
75+
}
76+
/// Calls the closure if the mouse button started being released during the last frame
77+
#[inline]
78+
pub fn just_released_and_then(
79+
&self,
80+
mouse_button: MouseButton,
81+
mut f: impl FnMut(&MouseState),
82+
) -> &Self {
83+
if self.0.just_released(mouse_button) {
84+
f(&self.0);
85+
}
86+
self
87+
}
88+
/// Calls the closure if any of the indicated mouse buttons were just released this frame
89+
#[inline]
90+
pub fn just_released_any_and_then(
91+
&self,
92+
mouse_buttons: &[MouseButton],
93+
mut f: impl FnMut(&MouseState),
94+
) -> &Self {
95+
if self.0.just_released_any(mouse_buttons) {
96+
f(&self.0);
97+
}
98+
self
99+
}
100+
}
101+
25102
/// `MouseState` represents the end-state of the mouse during the last frame. This should be used
26103
/// for "real time" processing of most input (except mousewheel scrolling), where you only care
27104
/// about the final state of buttons or mouse position for your logic.
@@ -99,74 +176,8 @@ impl MouseState {
99176
pub fn just_released_any(&self, mouse_buttons: &[MouseButton]) -> bool {
100177
mouse_buttons.iter().any(|k| self.just_released(*k))
101178
}
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(&MouseState)) -> &Self {
106-
if self.pressed(mouse_button) {
107-
f(self);
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(
114-
&self,
115-
mouse_buttons: &[MouseButton],
116-
mut f: impl FnMut(&MouseState),
117-
) -> &Self {
118-
if self.pressed_any(mouse_buttons) {
119-
f(self);
120-
}
121-
self
122-
}
123-
/// Calls the closure if the mouse button started being pressed during the last frame
124-
#[inline]
125-
pub fn just_pressed_do(
126-
&self,
127-
mouse_button: MouseButton,
128-
mut f: impl FnMut(&MouseState),
129-
) -> &Self {
130-
if self.just_pressed(mouse_button) {
131-
f(self);
132-
}
133-
self
134-
}
135-
/// Calls the closure if any of the indicated mouse buttons were just pressed this frame
136-
#[inline]
137-
pub fn just_pressed_any_do(
138-
&self,
139-
mouse_buttons: &[MouseButton],
140-
mut f: impl FnMut(&MouseState),
141-
) -> &Self {
142-
if self.just_pressed_any(mouse_buttons) {
143-
f(self);
144-
}
145-
self
146-
}
147-
/// Calls the closure if the mouse button started being released during the last frame
148-
#[inline]
149-
pub fn just_released_do(
150-
&self,
151-
mouse_button: MouseButton,
152-
mut f: impl FnMut(&MouseState),
153-
) -> &Self {
154-
if self.just_released(mouse_button) {
155-
f(self);
156-
}
157-
self
158-
}
159-
/// Calls the closure if any of the indicated mouse buttons were just released this frame
160-
#[inline]
161-
pub fn just_released_any_do(
162-
&self,
163-
mouse_buttons: &[MouseButton],
164-
mut f: impl FnMut(&MouseState),
165-
) -> &Self {
166-
if self.just_released_any(mouse_buttons) {
167-
f(self);
168-
}
169-
self
179+
pub fn if_(&self) -> MouseStateChainer {
180+
MouseStateChainer(self.clone())
170181
}
171182
}
172183

0 commit comments

Comments
 (0)