1- use crate :: utils:: * ;
21use glam:: * ;
32
4- pub fn key_down ( state : & State , args : & [ Value ] ) -> Result {
3+ use crate :: utils:: { State , Value , function, keycode_to_string, string_to_keycode, string_to_mouse} ;
4+
5+ pub fn key_down ( state : & State , args : & [ Value ] ) -> function:: Result {
56 if let [ Value :: String ( key) ] = args {
67 let key_code = string_to_keycode ( key) . ok_or ( format ! ( "Invalid key code: '{}'" , key) ) ?;
78 Ok ( Value :: Boolean ( state. input_manager . is_key_down ( key_code) ) )
@@ -10,7 +11,7 @@ pub fn key_down(state: &State, args: &[Value]) -> Result {
1011 }
1112}
1213
13- pub fn key_pressed ( state : & State , args : & [ Value ] ) -> Result {
14+ pub fn key_pressed ( state : & State , args : & [ Value ] ) -> function :: Result {
1415 if let [ Value :: String ( key) ] = args {
1516 let key_code = string_to_keycode ( key) . ok_or ( format ! ( "Invalid key code: '{}'" , key) ) ?;
1617 Ok ( Value :: Boolean ( state. input_manager . is_key_pressed ( key_code) ) )
@@ -19,7 +20,7 @@ pub fn key_pressed(state: &State, args: &[Value]) -> Result {
1920 }
2021}
2122
22- pub fn key_released ( state : & State , args : & [ Value ] ) -> Result {
23+ pub fn key_released ( state : & State , args : & [ Value ] ) -> function :: Result {
2324 if let [ Value :: String ( key) ] = args {
2425 let key_code = string_to_keycode ( key) . ok_or ( format ! ( "Invalid key code: '{}'" , key) ) ?;
2526 Ok ( Value :: Boolean (
@@ -30,7 +31,34 @@ pub fn key_released(state: &State, args: &[Value]) -> Result {
3031 }
3132}
3233
33- pub fn mouse_button_down ( state : & State , args : & [ Value ] ) -> Result {
34+ pub fn last_key ( state : & State ) -> function:: Result {
35+ let key_history = state. input_manager . key_history ( ) ;
36+ if let Some ( key) = key_history. last ( ) {
37+ Ok ( Value :: String ( keycode_to_string ( * key) ) )
38+ } else {
39+ Ok ( Value :: Null )
40+ }
41+ }
42+
43+ pub fn combination_pressed ( state : & State , args : & [ Value ] ) -> function:: Result {
44+ let key_codes: Result < Vec < glfw:: Key > , _ > = args
45+ . iter ( )
46+ . map ( |arg| match arg {
47+ Value :: String ( s) => string_to_keycode ( s) . ok_or_else ( || format ! ( "Invalid key: {}" , s) ) ,
48+ _ => Err ( "All arguments must be strings" . to_string ( ) ) ,
49+ } )
50+ . collect ( ) ;
51+
52+ let key_codes = match key_codes {
53+ Ok ( codes) if !codes. is_empty ( ) => codes,
54+ _ => return Ok ( Value :: Boolean ( false ) ) ,
55+ } ;
56+
57+ let matches = state. input_manager . key_history ( ) . ends_with ( & key_codes) ;
58+ Ok ( Value :: Boolean ( matches) )
59+ }
60+
61+ pub fn mouse_button_down ( state : & State , args : & [ Value ] ) -> function:: Result {
3462 if let [ Value :: String ( button) ] = args {
3563 let button_code =
3664 string_to_mouse ( button) . ok_or ( format ! ( "Invalid mouse button: '{}'" , button) ) ?;
@@ -42,7 +70,7 @@ pub fn mouse_button_down(state: &State, args: &[Value]) -> Result {
4270 }
4371}
4472
45- pub fn mouse_button_pressed ( state : & State , args : & [ Value ] ) -> Result {
73+ pub fn mouse_button_pressed ( state : & State , args : & [ Value ] ) -> function :: Result {
4674 if let [ Value :: String ( button) ] = args {
4775 let button_code =
4876 string_to_mouse ( button) . ok_or ( format ! ( "Invalid mouse button: '{}'" , button) ) ?;
@@ -54,7 +82,7 @@ pub fn mouse_button_pressed(state: &State, args: &[Value]) -> Result {
5482 }
5583}
5684
57- pub fn mouse_button_released ( state : & State , args : & [ Value ] ) -> Result {
85+ pub fn mouse_button_released ( state : & State , args : & [ Value ] ) -> function :: Result {
5886 if let [ Value :: String ( button) ] = args {
5987 let button_code =
6088 string_to_mouse ( button) . ok_or ( format ! ( "Invalid mouse button: '{}'" , button) ) ?;
@@ -66,19 +94,19 @@ pub fn mouse_button_released(state: &State, args: &[Value]) -> Result {
6694 }
6795}
6896
69- pub fn mouse_x ( state : & State ) -> Result {
97+ pub fn mouse_x ( state : & State ) -> function :: Result {
7098 Ok ( Value :: Number (
7199 state. window . get_cursor_pos ( ) . 0 as f32 * 2.0 - state. window . get_size ( ) . 0 as f32 ,
72100 ) )
73101}
74102
75- pub fn mouse_y ( state : & State ) -> Result {
103+ pub fn mouse_y ( state : & State ) -> function :: Result {
76104 Ok ( Value :: Number (
77105 -( state. window . get_cursor_pos ( ) . 1 as f32 * 2.0 - state. window . get_size ( ) . 1 as f32 ) ,
78106 ) )
79107}
80108
81- pub fn sprite_clicked ( state : & State ) -> Result {
109+ pub fn sprite_clicked ( state : & State ) -> function :: Result {
82110 if !state
83111 . input_manager
84112 . is_mouse_button_pressed ( glfw:: MouseButton :: Left )
@@ -111,7 +139,7 @@ pub fn sprite_clicked(state: &State) -> Result {
111139 }
112140}
113141
114- pub fn is_backdrop ( state : & State , args : & [ Value ] ) -> Result {
142+ pub fn is_backdrop ( state : & State , args : & [ Value ] ) -> function :: Result {
115143 if let [ Value :: Number ( index) ] = args {
116144 let backdrop = state. project . stage . backdrop ( ) ;
117145 if backdrop == * index as usize {
@@ -124,7 +152,7 @@ pub fn is_backdrop(state: &State, args: &[Value]) -> Result {
124152 }
125153}
126154
127- pub fn broadcast_id_of ( state : & State , args : & [ Value ] ) -> Result {
155+ pub fn broadcast_id_of ( state : & State , args : & [ Value ] ) -> function :: Result {
128156 if let [ Value :: String ( message) ] = args {
129157 if let Some ( broadcast) = state. project . get_broadcast ( message) {
130158 Ok ( Value :: Number ( broadcast. id as f32 ) )
@@ -136,7 +164,7 @@ pub fn broadcast_id_of(state: &State, args: &[Value]) -> Result {
136164 }
137165}
138166
139- pub fn broadcast ( state : & mut State , args : & [ Value ] ) -> Result {
167+ pub fn broadcast ( state : & mut State , args : & [ Value ] ) -> function :: Result {
140168 if let [ Value :: String ( message) ] = args {
141169 state. project . broadcast ( message. clone ( ) ) ;
142170 Ok ( Value :: Null )
0 commit comments