@@ -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
132139fn sync_keyboard_state (
133140 keyboard_input : Res < Input < KeyCode > > ,
0 commit comments