11pub use caw_computer_keyboard:: { Key , Keyboard as KeyboardGeneric } ;
2- use caw_core:: { FrameSig , FrameSigT , SigCtx } ;
2+ use caw_core:: { frame_sig_var , FrameSig , FrameSigT , FrameSigVar } ;
33use sdl2:: { keyboard:: Scancode , mouse:: MouseButton as SdlMouseButton } ;
4- use std:: sync:: { Arc , RwLock } ;
54
65#[ derive( Debug , Clone , Copy ) ]
76pub enum MouseButton {
@@ -19,22 +18,6 @@ pub struct MouseGeneric<P, B> {
1918 pub middle : B ,
2019}
2120
22- impl < P , B > MouseGeneric < P , B > {
23- fn map < P_ , B_ , FP : FnMut ( & P ) -> P_ , FB : FnMut ( & B ) -> B_ > (
24- & self ,
25- mut f_position : FP ,
26- mut f_button : FB ,
27- ) -> MouseGeneric < P_ , B_ > {
28- MouseGeneric {
29- x_01 : f_position ( & self . x_01 ) ,
30- y_01 : f_position ( & self . y_01 ) ,
31- left : f_button ( & self . left ) ,
32- right : f_button ( & self . right ) ,
33- middle : f_button ( & self . middle ) ,
34- }
35- }
36- }
37-
3821impl < P : Clone , B : Clone > MouseGeneric < P , B > {
3922 pub fn button ( & self , mouse_button : MouseButton ) -> B {
4023 use MouseButton :: * ;
@@ -54,27 +37,9 @@ impl<P: Clone, B: Clone> MouseGeneric<P, B> {
5437 }
5538}
5639
57- /// A signal for keyboard and mouse inputs from sdl. Always yields the same value during any given
58- /// frame.
59- pub struct FrameSigInput < T : Copy > ( Arc < RwLock < T > > ) ;
60-
61- impl < T : Copy > FrameSigT for FrameSigInput < T > {
62- type Item = T ;
63-
64- fn frame_sample ( & mut self , _ctx : & SigCtx ) -> Self :: Item {
65- * self . 0 . read ( ) . unwrap ( )
66- }
67- }
68-
69- impl < T : Copy > Clone for FrameSigInput < T > {
70- fn clone ( & self ) -> Self {
71- FrameSigInput ( Arc :: clone ( & self . 0 ) )
72- }
73- }
74-
75- pub type Keyboard = KeyboardGeneric < FrameSig < FrameSigInput < bool > > > ;
40+ pub type Keyboard = KeyboardGeneric < FrameSig < FrameSigVar < bool > > > ;
7641pub type Mouse =
77- MouseGeneric < FrameSig < FrameSigInput < f32 > > , FrameSig < FrameSigInput < bool > > > ;
42+ MouseGeneric < FrameSig < FrameSigVar < f32 > > , FrameSig < FrameSigVar < bool > > > ;
7843
7944#[ derive( Clone ) ]
8045pub struct Input {
@@ -105,66 +70,18 @@ impl Input {
10570
10671#[ derive( Clone ) ]
10772pub struct InputState {
108- keyboard : KeyboardGeneric < Arc < RwLock < bool > > > ,
109- mouse : MouseGeneric < Arc < RwLock < f32 > > , Arc < RwLock < bool > > > ,
73+ keyboard : KeyboardGeneric < FrameSig < FrameSigVar < bool > > > ,
74+ mouse :
75+ MouseGeneric < FrameSig < FrameSigVar < f32 > > , FrameSig < FrameSigVar < bool > > > ,
11076}
11177
11278impl InputState {
11379 pub ( crate ) fn new ( ) -> Self {
114- let mk_key = || Arc :: new ( RwLock :: new ( false ) ) ;
115- let mk_position = || Arc :: new ( RwLock :: new ( 0.0 ) ) ;
116- let mk_button = || Arc :: new ( RwLock :: new ( false ) ) ;
80+ let mk_key = |_| frame_sig_var ( false ) ;
81+ let mk_position = || frame_sig_var ( 0.0 ) ;
82+ let mk_button = || frame_sig_var ( false ) ;
11783 Self {
118- keyboard : KeyboardGeneric {
119- a : mk_key ( ) ,
120- b : mk_key ( ) ,
121- c : mk_key ( ) ,
122- d : mk_key ( ) ,
123- e : mk_key ( ) ,
124- f : mk_key ( ) ,
125- g : mk_key ( ) ,
126- h : mk_key ( ) ,
127- i : mk_key ( ) ,
128- j : mk_key ( ) ,
129- k : mk_key ( ) ,
130- l : mk_key ( ) ,
131- m : mk_key ( ) ,
132- n : mk_key ( ) ,
133- o : mk_key ( ) ,
134- p : mk_key ( ) ,
135- q : mk_key ( ) ,
136- r : mk_key ( ) ,
137- s : mk_key ( ) ,
138- t : mk_key ( ) ,
139- u : mk_key ( ) ,
140- v : mk_key ( ) ,
141- w : mk_key ( ) ,
142- x : mk_key ( ) ,
143- y : mk_key ( ) ,
144- z : mk_key ( ) ,
145- n0 : mk_key ( ) ,
146- n1 : mk_key ( ) ,
147- n2 : mk_key ( ) ,
148- n3 : mk_key ( ) ,
149- n4 : mk_key ( ) ,
150- n5 : mk_key ( ) ,
151- n6 : mk_key ( ) ,
152- n7 : mk_key ( ) ,
153- n8 : mk_key ( ) ,
154- n9 : mk_key ( ) ,
155- left_bracket : mk_key ( ) ,
156- right_bracket : mk_key ( ) ,
157- semicolon : mk_key ( ) ,
158- apostrophe : mk_key ( ) ,
159- comma : mk_key ( ) ,
160- period : mk_key ( ) ,
161- minus : mk_key ( ) ,
162- equals : mk_key ( ) ,
163- slash : mk_key ( ) ,
164- space : mk_key ( ) ,
165- backspace : mk_key ( ) ,
166- backslash : mk_key ( ) ,
167- } ,
84+ keyboard : KeyboardGeneric :: new ( mk_key) ,
16885 mouse : MouseGeneric {
16986 x_01 : mk_position ( ) ,
17087 y_01 : mk_position ( ) ,
@@ -227,12 +144,12 @@ impl InputState {
227144 Scancode :: Backslash => & self . keyboard . backslash ,
228145 _ => return ,
229146 } ;
230- * key_state. write ( ) . unwrap ( ) = pressed;
147+ key_state. 0 . set ( pressed) ;
231148 }
232149
233150 pub ( crate ) fn set_mouse_position ( & self , x_01 : f32 , y_01 : f32 ) {
234- * self . mouse . x_01 . write ( ) . unwrap ( ) = x_01;
235- * self . mouse . y_01 . write ( ) . unwrap ( ) = y_01;
151+ self . mouse . x_01 . 0 . set ( x_01) ;
152+ self . mouse . y_01 . 0 . set ( y_01) ;
236153 }
237154
238155 pub ( crate ) fn set_mouse_button (
@@ -246,19 +163,15 @@ impl InputState {
246163 SdlMouseButton :: Middle => & self . mouse . middle ,
247164 _ => return ,
248165 } ;
249- * button_state. write ( ) . unwrap ( ) = pressed;
166+ button_state. 0 . set ( pressed) ;
250167 }
251168
252169 pub fn keyboard ( & self ) -> Keyboard {
253- self . keyboard
254- . map ( |key| FrameSig ( FrameSigInput ( Arc :: clone ( key) ) ) )
170+ self . keyboard . clone ( )
255171 }
256172
257173 pub fn mouse ( & self ) -> Mouse {
258- self . mouse . map (
259- |position| FrameSig ( FrameSigInput ( Arc :: clone ( position) ) ) ,
260- |button| FrameSig ( FrameSigInput ( Arc :: clone ( button) ) ) ,
261- )
174+ self . mouse . clone ( )
262175 }
263176
264177 pub fn input ( & self ) -> Input {
0 commit comments