7
7
use std:: marker:: PhantomData ;
8
8
9
9
use godot:: builtin:: {
10
- Callable , Dictionary , GString , NodePath , StringName , Variant , Vector2 , Vector3 ,
10
+ Callable , Dictionary , GString , NodePath , StringName , Variant , Vector2 , Vector3 , Vector4 ,
11
11
} ;
12
12
use godot:: classes:: Object ;
13
13
use godot:: global:: { Error , PropertyHint } ;
14
14
use godot:: meta:: { GodotConvert , GodotType , ToGodot } ;
15
- use godot:: obj:: Gd ;
15
+ use godot:: obj:: { Gd , GodotClass } ;
16
16
17
17
use crate :: static_script_registry:: RustScriptPropDesc ;
18
-
19
- pub trait ScriptSignal {
20
- type Args : SignalArguments ;
21
-
22
- fn new ( host : Gd < Object > , name : & ' static str ) -> Self ;
23
-
24
- fn emit ( & self , args : Self :: Args ) ;
25
-
26
- fn connect ( & mut self , callable : Callable ) -> Result < ( ) , Error > ;
27
-
28
- fn argument_desc ( ) -> Box < [ RustScriptPropDesc ] > ;
29
-
30
- fn name ( & self ) -> & str ;
31
- }
18
+ use crate :: { GodotScript , RsRef } ;
32
19
33
20
pub trait SignalArguments {
34
- fn count ( ) -> u8 ;
21
+ const COUNT : u8 ;
35
22
36
23
fn to_variants ( & self ) -> Vec < Variant > ;
37
24
38
- fn argument_desc ( ) -> Box < [ RustScriptPropDesc ] > ;
25
+ fn argument_desc ( arg_names : Option < & [ & ' static str ] > ) -> Box < [ RustScriptPropDesc ] > ;
39
26
}
40
27
41
28
impl SignalArguments for ( ) {
42
- fn count ( ) -> u8 {
43
- 0
44
- }
29
+ const COUNT : u8 = 0 ;
45
30
46
31
fn to_variants ( & self ) -> Vec < Variant > {
47
32
vec ! [ ]
48
33
}
49
34
50
- fn argument_desc ( ) -> Box < [ RustScriptPropDesc ] > {
35
+ fn argument_desc ( _arg_names : Option < & [ & ' static str ] > ) -> Box < [ RustScriptPropDesc ] > {
51
36
Box :: new ( [ ] )
52
37
}
53
38
}
@@ -60,9 +45,7 @@ macro_rules! count_tts {
60
45
macro_rules! tuple_args {
61
46
( impl $( $arg: ident) ,+) => {
62
47
impl <$( $arg: ToGodot ) ,+> SignalArguments for ( $( $arg, ) +) {
63
- fn count( ) -> u8 {
64
- count_tts!( $( $arg) +)
65
- }
48
+ const COUNT : u8 = count_tts!( $( $arg) +) ;
66
49
67
50
fn to_variants( & self ) -> Vec <Variant > {
68
51
#[ allow( non_snake_case) ]
@@ -73,9 +56,12 @@ macro_rules! tuple_args {
73
56
]
74
57
}
75
58
76
- fn argument_desc( ) -> Box <[ RustScriptPropDesc ] > {
59
+ fn argument_desc( arg_names: Option <& [ & ' static str ] >) -> Box <[ RustScriptPropDesc ] > {
60
+ #[ expect( non_snake_case) ]
61
+ let [ $( $arg) ,+] = arg_names. unwrap_or( & [ $( stringify!( $arg) ) ,+] ) . try_into( ) . unwrap( ) ; //.unwrap_or_else(|| [$(stringify!($arg)),+]);
62
+
77
63
Box :: new( [
78
- $( signal_argument_desc!( "0" , $arg) ) ,+
64
+ $( signal_argument_desc!( $arg , $arg) ) ,+
79
65
] )
80
66
}
81
67
}
@@ -98,17 +84,17 @@ macro_rules! tuple_args {
98
84
macro_rules! single_args {
99
85
( impl $arg: ty) => {
100
86
impl SignalArguments for $arg {
101
- fn count( ) -> u8 {
102
- 1
103
- }
87
+ const COUNT : u8 = 1 ;
104
88
105
89
fn to_variants( & self ) -> Vec <Variant > {
106
90
vec![ self . to_variant( ) ]
107
91
}
108
92
109
- fn argument_desc( ) -> Box <[ RustScriptPropDesc ] > {
93
+ fn argument_desc( arg_names: Option <& [ & ' static str ] >) -> Box <[ RustScriptPropDesc ] > {
94
+ let [ arg_name] = arg_names. unwrap_or_else( || & [ "0" ] ) . try_into( ) . unwrap( ) ;
95
+
110
96
Box :: new( [
111
- signal_argument_desc!( "0" , $arg) ,
97
+ signal_argument_desc!( arg_name , $arg) ,
112
98
] )
113
99
}
114
100
}
@@ -120,7 +106,7 @@ macro_rules! single_args {
120
106
}
121
107
122
108
macro_rules! signal_argument_desc {
123
- ( $name: literal , $type: ty) => {
109
+ ( $name: expr , $type: ty) => {
124
110
RustScriptPropDesc {
125
111
name: $name,
126
112
ty: <<<$type as GodotConvert >:: Via as GodotType >:: Ffi as godot:: sys:: GodotFfi >:: VARIANT_TYPE . variant_as_nil( ) ,
@@ -135,55 +121,96 @@ macro_rules! signal_argument_desc {
135
121
136
122
tuple_args ! ( A1 , A2 , A3 , A4 , A5 , A6 , A7 , A8 , A9 , A10 ) ;
137
123
single_args ! (
138
- bool , u8 , u16 , u32 , u64 , i8 , i16 , i32 , i64 , f64 , GString , StringName , NodePath , Vector2 ,
139
- Vector3 , Dictionary
124
+ bool , u8 , u16 , u32 , u64 , i8 , i16 , i32 , i64 , f32 , f64 , GString , StringName , NodePath , Vector2 ,
125
+ Vector3 , Vector4 , Dictionary
140
126
) ;
141
127
128
+ impl < T : GodotClass > SignalArguments for Gd < T > {
129
+ const COUNT : u8 = 1 ;
130
+
131
+ fn to_variants ( & self ) -> Vec < Variant > {
132
+ vec ! [ self . to_variant( ) ]
133
+ }
134
+
135
+ fn argument_desc ( arg_names : Option < & [ & ' static str ] > ) -> Box < [ RustScriptPropDesc ] > {
136
+ let name = arg_names
137
+ . and_then ( |list| list. first ( ) )
138
+ . copied ( )
139
+ . unwrap_or ( "0" ) ;
140
+
141
+ Box :: new ( [ signal_argument_desc ! ( name, Self ) ] )
142
+ }
143
+ }
144
+
145
+ impl < T : GodotScript > SignalArguments for RsRef < T > {
146
+ const COUNT : u8 = 1 ;
147
+
148
+ fn to_variants ( & self ) -> Vec < Variant > {
149
+ vec ! [ self . to_variant( ) ]
150
+ }
151
+
152
+ fn argument_desc ( arg_names : Option < & [ & ' static str ] > ) -> Box < [ RustScriptPropDesc ] > {
153
+ Box :: new ( [ signal_argument_desc ! (
154
+ arg_names
155
+ . and_then( |list| list. first( ) )
156
+ . copied( )
157
+ . unwrap_or( "0" ) ,
158
+ Self
159
+ ) ] )
160
+ }
161
+ }
162
+
142
163
#[ derive( Debug ) ]
143
- pub struct Signal < T : SignalArguments > {
164
+ pub struct ScriptSignal < T : SignalArguments > {
144
165
host : Gd < Object > ,
145
166
name : & ' static str ,
146
167
args : PhantomData < T > ,
147
168
}
148
169
149
- impl < T : SignalArguments > ScriptSignal for Signal < T > {
150
- type Args = T ;
170
+ #[ deprecated(
171
+ note = "The Signal type has been deprecated and will be removed soon. Please use the ScriptSignal instead."
172
+ ) ]
173
+ pub type Signal < T > = ScriptSignal < T > ;
174
+
175
+ impl < T : SignalArguments > ScriptSignal < T > {
176
+ pub const ARG_COUNT : u8 = T :: COUNT ;
151
177
152
- fn new ( host : Gd < Object > , name : & ' static str ) -> Self {
178
+ pub fn new ( host : Gd < Object > , name : & ' static str ) -> Self {
153
179
Self {
154
180
host,
155
181
name,
156
182
args : PhantomData ,
157
183
}
158
184
}
159
185
160
- fn emit ( & self , args : Self :: Args ) {
186
+ pub fn emit ( & self , args : T ) {
161
187
self . host
162
188
. clone ( )
163
189
. emit_signal ( self . name , & args. to_variants ( ) ) ;
164
190
}
165
191
166
- fn connect ( & mut self , callable : Callable ) -> Result < ( ) , Error > {
192
+ pub fn connect ( & mut self , callable : Callable ) -> Result < ( ) , Error > {
167
193
match self . host . connect ( self . name , & callable) {
168
194
Error :: OK => Ok ( ( ) ) ,
169
195
error => Err ( error) ,
170
196
}
171
197
}
172
198
173
- fn argument_desc ( ) -> Box < [ RustScriptPropDesc ] > {
174
- <T as SignalArguments >:: argument_desc ( )
199
+ #[ doc( hidden) ]
200
+ pub fn argument_desc ( arg_names : Option < & [ & ' static str ] > ) -> Box < [ RustScriptPropDesc ] > {
201
+ <T as SignalArguments >:: argument_desc ( arg_names)
175
202
}
176
203
177
- fn name ( & self ) -> & str {
204
+ pub fn name ( & self ) -> & str {
178
205
self . name
179
206
}
180
207
}
181
208
182
- impl < T : SignalArguments > GodotConvert for Signal < T > {
209
+ impl < T : SignalArguments > GodotConvert for ScriptSignal < T > {
183
210
type Via = godot:: builtin:: Signal ;
184
211
}
185
212
186
- impl < T : SignalArguments > ToGodot for Signal < T > {
213
+ impl < T : SignalArguments > ToGodot for ScriptSignal < T > {
187
214
type ToVia < ' v >
188
215
= Self :: Via
189
216
where
0 commit comments