@@ -13,14 +13,21 @@ use quote::{format_ident, quote};
13
13
14
14
type IoResult = std:: io:: Result < ( ) > ;
15
15
16
+ #[ derive( Eq , PartialEq ) ]
17
+ enum ExportKind {
18
+ NoExport ,
19
+ Export ,
20
+ ExportRange { min : i64 , max : i64 } ,
21
+ }
22
+
16
23
struct Input {
17
24
ident : String ,
18
25
gdscript_ty : & ' static str ,
19
26
gdscript_val : & ' static str ,
20
27
rust_ty : TokenStream ,
21
28
rust_val : TokenStream ,
22
29
is_property : bool ,
23
- is_exportable : bool ,
30
+ export_kind : ExportKind ,
24
31
initializer : Option < TokenStream > ,
25
32
extra : TokenStream ,
26
33
}
@@ -34,7 +41,7 @@ macro_rules! pushs {
34
41
$gdscript_val: expr,
35
42
$rust_val: expr,
36
43
$property: expr,
37
- $export : expr,
44
+ $export_kind : expr,
38
45
$initializer: expr
39
46
$( ; $( $extra: tt) * ) ?
40
47
) => {
@@ -48,21 +55,27 @@ macro_rules! pushs {
48
55
rust_ty: quote! { $RustTy } ,
49
56
rust_val: quote! { $rust_val } ,
50
57
is_property: $property,
51
- is_exportable : $export ,
58
+ export_kind : $export_kind ,
52
59
initializer: $initializer,
53
60
extra: quote! { $( $( $extra) * ) ? } ,
54
61
} ) ;
55
62
} ;
56
63
}
57
64
58
- /// Push simple GDScript expression, outside string
65
+ /// Push simple GDScript expression, outside string.
59
66
macro_rules! push {
60
67
( $inputs: ident; $GDScriptTy: expr, $RustTy: ty, $val: expr) => {
61
68
push!( $inputs; $GDScriptTy, $RustTy, $val, $val) ;
62
69
} ;
63
70
71
+ ( $inputs: ident; $GDScriptTy: expr, $RustTy: ty, $val: expr, export_range) => {
72
+ pushs!( $inputs; $GDScriptTy, $RustTy, stringify!( $val) , $val, true ,
73
+ ExportKind :: ExportRange { min: <$RustTy>:: MIN as i64 , max: <$RustTy>:: MAX as i64 } ,
74
+ None ) ;
75
+ } ;
76
+
64
77
( $inputs: ident; $GDScriptTy: expr, $RustTy: ty, $gdscript_val: expr, $rust_val: expr) => {
65
- pushs!( $inputs; $GDScriptTy, $RustTy, stringify!( $gdscript_val) , $rust_val, true , true , None ) ;
78
+ pushs!( $inputs; $GDScriptTy, $RustTy, stringify!( $gdscript_val) , $rust_val, true , ExportKind :: Export , None ) ;
66
79
} ;
67
80
}
68
81
@@ -77,7 +90,7 @@ macro_rules! push_newtype {
77
90
78
91
( @s $inputs: ident; $GDScriptTy: expr, $name: ident( $T: ty) , $gdscript_val: expr, $rust_val: expr) => {
79
92
pushs!(
80
- $inputs; $GDScriptTy, $name, $gdscript_val, $rust_val, false , false , None ;
93
+ $inputs; $GDScriptTy, $name, $gdscript_val, $rust_val, false , NoExport , None ;
81
94
82
95
#[ derive( Clone , PartialEq , Debug ) ]
83
96
pub struct $name( $T) ;
@@ -106,41 +119,43 @@ macro_rules! push_newtype {
106
119
107
120
// Edit this to change involved types
108
121
fn collect_inputs ( ) -> Vec < Input > {
122
+ use ExportKind :: * ;
123
+
109
124
let mut inputs = vec ! [ ] ;
110
125
111
126
// Scalar
112
127
push ! ( inputs; int, i64 , -922337203685477580 ) ;
113
- push ! ( inputs; int, i32 , -2147483648 ) ;
114
- push ! ( inputs; int, u32 , 4294967295 ) ;
115
- push ! ( inputs; int, i16 , -32767 ) ;
116
- push ! ( inputs; int, u16 , 65535 ) ;
117
- push ! ( inputs; int, i8 , -128 ) ;
118
- push ! ( inputs; int, u8 , 255 ) ;
128
+ push ! ( inputs; int, i32 , -2147483648 , export_range ) ;
129
+ push ! ( inputs; int, u32 , 4294967295 , export_range ) ;
130
+ push ! ( inputs; int, i16 , -32767 , export_range ) ;
131
+ push ! ( inputs; int, u16 , 65535 , export_range ) ;
132
+ push ! ( inputs; int, i8 , -128 , export_range ) ;
133
+ push ! ( inputs; int, u8 , 255 , export_range ) ;
119
134
push ! ( inputs; float, f32 , 12.5 ) ;
120
135
push ! ( inputs; float, f64 , 127.83156478 ) ;
121
136
push ! ( inputs; bool , bool , true ) ;
122
137
push ! ( inputs; Color , Color , Color ( 0.7 , 0.5 , 0.3 , 0.2 ) , Color :: from_rgba( 0.7 , 0.5 , 0.3 , 0.2 ) ) ;
123
138
push ! ( inputs; String , GString , "hello" , "hello" . into( ) ) ;
124
139
push ! ( inputs; StringName , StringName , & "hello" , "hello" . into( ) ) ;
125
- pushs ! ( inputs; NodePath , NodePath , r#"^"hello""# , "hello" . into( ) , true , true , None ) ;
140
+ pushs ! ( inputs; NodePath , NodePath , r#"^"hello""# , "hello" . into( ) , true , Export , None ) ;
126
141
push ! ( inputs; Vector2 , Vector2 , Vector2 ( 12.5 , -3.5 ) , Vector2 :: new( 12.5 , -3.5 ) ) ;
127
142
push ! ( inputs; Vector3 , Vector3 , Vector3 ( 117.5 , 100.0 , -323.25 ) , Vector3 :: new( 117.5 , 100.0 , -323.25 ) ) ;
128
143
push ! ( inputs; Vector4 , Vector4 , Vector4 ( -18.5 , 24.75 , -1.25 , 777.875 ) , Vector4 :: new( -18.5 , 24.75 , -1.25 , 777.875 ) ) ;
129
144
push ! ( inputs; Vector2i , Vector2i , Vector2i ( -2147483648 , 2147483647 ) , Vector2i :: new( -2147483648 , 2147483647 ) ) ;
130
145
push ! ( inputs; Vector3i , Vector3i , Vector3i ( -1 , -2147483648 , 2147483647 ) , Vector3i :: new( -1 , -2147483648 , 2147483647 ) ) ;
131
146
push ! ( inputs; Vector4i , Vector4i , Vector4i ( -1 , -2147483648 , 2147483647 , 1000 ) , Vector4i :: new( -1 , -2147483648 , 2147483647 , 100 ) ) ;
132
- pushs ! ( inputs; Callable , Callable , "Callable()" , Callable :: invalid( ) , true , false , Some ( quote! { Callable :: invalid( ) } ) ) ;
133
- pushs ! ( inputs; Signal , Signal , "Signal()" , Signal :: invalid( ) , true , false , Some ( quote! { Signal :: invalid( ) } ) ) ;
147
+ pushs ! ( inputs; Callable , Callable , "Callable()" , Callable :: invalid( ) , true , NoExport , Some ( quote! { Callable :: invalid( ) } ) ) ;
148
+ pushs ! ( inputs; Signal , Signal , "Signal()" , Signal :: invalid( ) , true , NoExport , Some ( quote! { Signal :: invalid( ) } ) ) ;
134
149
push ! ( inputs; Rect2 , Rect2 , Rect2 ( ) , Rect2 :: default ( ) ) ;
135
150
push ! ( inputs; Rect2i , Rect2i , Rect2i ( ) , Rect2i :: default ( ) ) ;
136
151
push ! ( inputs; Transform2D , Transform2D , Transform2D ( ) , Transform2D :: default ( ) ) ;
137
- pushs ! ( inputs; Plane , Plane , "Plane()" , Plane :: new( Vector3 :: new( 1.0 , 0.0 , 0.0 ) , 0.0 ) , true , true , Some ( quote! { Plane :: new( Vector3 :: new( 1.0 , 0.0 , 0.0 ) , 0.0 ) } ) ) ;
152
+ pushs ! ( inputs; Plane , Plane , "Plane()" , Plane :: new( Vector3 :: new( 1.0 , 0.0 , 0.0 ) , 0.0 ) , true , Export , Some ( quote! { Plane :: new( Vector3 :: new( 1.0 , 0.0 , 0.0 ) , 0.0 ) } ) ) ;
138
153
push ! ( inputs; Quaternion , Quaternion , Quaternion ( ) , Quaternion :: default ( ) ) ;
139
154
push ! ( inputs; AABB , Aabb , AABB ( ) , Aabb :: default ( ) ) ;
140
155
push ! ( inputs; Basis , Basis , Basis ( ) , Basis :: default ( ) ) ;
141
156
push ! ( inputs; Transform3D , Transform3D , Transform3D ( ) , Transform3D :: default ( ) ) ;
142
157
push ! ( inputs; Projection , Projection , Projection ( ) , Projection :: default ( ) ) ;
143
- pushs ! ( inputs; RID , Rid , "RID()" , Rid :: Invalid , true , false , Some ( quote! { Rid :: Invalid } ) ) ;
158
+ pushs ! ( inputs; RID , Rid , "RID()" , Rid :: Invalid , true , NoExport , Some ( quote! { Rid :: Invalid } ) ) ;
144
159
push ! ( inputs; Node , Option <Gd <Node >>, null, None ) ;
145
160
push ! ( inputs; Resource , Option <Gd <Resource >>, null, None ) ;
146
161
push ! ( inputs; PackedByteArray , PackedByteArray , PackedByteArray ( ) , PackedByteArray :: new( ) ) ;
@@ -195,16 +210,16 @@ fn collect_inputs() -> Vec<Input> {
195
210
pushs ! ( inputs; Dictionary , Dictionary ,
196
211
r#"{"key": 83, -3: Vector2(1, 2), 0.03: true}"# ,
197
212
vdict! { "key" : 83 , ( -3 ) : Vector2 :: new( 1.0 , 2.0 ) , 0.03 : true } ,
198
- true , true , None
213
+ true , Export , None
199
214
) ;
200
215
201
216
// Composite
202
- pushs ! ( inputs; int, InstanceId , "-1" , InstanceId :: from_i64( 0xFFFFFFFFFFFFFFF ) , false , false , None ) ;
217
+ pushs ! ( inputs; int, InstanceId , "-1" , InstanceId :: from_i64( 0xFFFFFFFFFFFFFFF ) , false , NoExport , None ) ;
203
218
// TODO: should `Variant` implement property?
204
- pushs ! ( inputs; Variant , Variant , "123" , 123i64 . to_variant( ) , false , false , None ) ;
219
+ pushs ! ( inputs; Variant , Variant , "123" , 123i64 . to_variant( ) , false , NoExport , None ) ;
205
220
206
221
// EngineEnum
207
- pushs ! ( inputs; int, Error , "0" , Error :: OK , false , false , None ) ;
222
+ pushs ! ( inputs; int, Error , "0" , Error :: OK , false , NoExport , None ) ;
208
223
209
224
inputs
210
225
}
@@ -419,7 +434,7 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {
419
434
gdscript_ty,
420
435
rust_ty,
421
436
is_property,
422
- is_exportable ,
437
+ export_kind ,
423
438
initializer,
424
439
..
425
440
} = input;
@@ -451,7 +466,7 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {
451
466
format ! ( "var {var_array}: Array[{gdscript_ty}]" ) ,
452
467
] ) ;
453
468
454
- if * is_exportable {
469
+ if * export_kind != ExportKind :: NoExport {
455
470
rust. extend ( [
456
471
quote ! {
457
472
#[ export]
@@ -461,8 +476,18 @@ fn generate_property_template(inputs: &[Input]) -> PropertyTests {
461
476
quote ! { #[ export] #export_array: Array <#rust_ty> } ,
462
477
] ) ;
463
478
479
+ let export_single = match * export_kind {
480
+ ExportKind :: Export => {
481
+ format ! ( "@export var {export}: {gdscript_ty}" )
482
+ }
483
+ ExportKind :: ExportRange { min, max } => {
484
+ format ! ( "@export_range({min}, {max}) var {export}: {gdscript_ty}" )
485
+ }
486
+ _ => unreachable ! ( ) ,
487
+ } ;
488
+
464
489
gdscript. extend ( [
465
- format ! ( "@export var {export}: {gdscript_ty}" ) ,
490
+ export_single ,
466
491
format ! ( "@export var {export_array}: Array[{gdscript_ty}]" ) ,
467
492
] ) ;
468
493
}
0 commit comments