1
1
#[ macro_use]
2
- extern crate gdnative;
2
+ extern crate gdnative;
3
3
extern crate euclid;
4
4
5
- use gdnative:: { GodotString , Variant , ResourceLoader , PackedScene , Spatial } ;
6
5
use euclid:: vec3;
6
+ use gdnative:: { GodotString , PackedScene , ResourceLoader , Spatial , Variant } ;
7
7
8
8
#[ derive( Debug , Clone , PartialEq ) ]
9
9
pub enum ManageErrs {
@@ -13,30 +13,28 @@ pub enum ManageErrs {
13
13
14
14
#[ derive( gdnative:: NativeClass ) ]
15
15
#[ inherit( gdnative:: Spatial ) ]
16
- struct SceneCreate {
17
- // Store the loaded scene for a very slight performance boost but mostly to show you how.
18
- template : Option < PackedScene > ,
19
- children_spawned : u32 ,
16
+ struct SceneCreate {
17
+ // Store the loaded scene for a very slight performance boost but mostly to show you how.
18
+ template : Option < PackedScene > ,
19
+ children_spawned : u32 ,
20
20
}
21
21
22
-
23
- // Demonstrates Scene creation, calling to/from gdscript
24
- //
25
- // 1. Child scene is created when spawn_one is called
26
- // 2. Child scenes are deleted when remove_one is called
27
- // 3. Find and call functions in a node (Panel)
28
- // 4. Call functions in GDNative (from panel into spawn/remove)
29
- //
30
- // Note, the same mechanism which is used to call from panel into spawn_one and remove_one can be
31
- // used to call other GDNative classes here in rust.
22
+ // Demonstrates Scene creation, calling to/from gdscript
23
+ //
24
+ // 1. Child scene is created when spawn_one is called
25
+ // 2. Child scenes are deleted when remove_one is called
26
+ // 3. Find and call functions in a node (Panel)
27
+ // 4. Call functions in GDNative (from panel into spawn/remove)
28
+ //
29
+ // Note, the same mechanism which is used to call from panel into spawn_one and remove_one can be
30
+ // used to call other GDNative classes here in rust.
32
31
33
32
#[ gdnative:: methods]
34
33
impl SceneCreate {
35
-
36
34
fn _init ( _owner : gdnative:: Spatial ) -> Self {
37
35
SceneCreate {
38
36
template : None , // Have not loaded this template yet.
39
- children_spawned : 0 ,
37
+ children_spawned : 0 ,
40
38
}
41
39
}
42
40
@@ -50,9 +48,8 @@ impl SceneCreate {
50
48
}
51
49
52
50
#[ export]
53
- unsafe fn spawn_one ( & mut self , mut owner : gdnative:: Spatial , message : GodotString ) {
54
-
55
- godot_print ! ( "Called spawn_one({})" , message. to_string( ) ) ;
51
+ unsafe fn spawn_one ( & mut self , mut owner : gdnative:: Spatial , message : GodotString ) {
52
+ godot_print ! ( "Called spawn_one({})" , message. to_string( ) ) ;
56
53
57
54
let template = if let Some ( template) = & self . template {
58
55
template
@@ -67,104 +64,101 @@ impl SceneCreate {
67
64
Ok ( mut spatial) => {
68
65
// Here is how you rename the child...
69
66
let key_str = format ! ( "child_{}" , self . children_spawned) ;
70
- spatial. set_name ( GodotString :: from_str ( & key_str) ) ;
71
-
67
+ spatial. set_name ( GodotString :: from_str ( & key_str) ) ;
72
68
73
- let x = ( self . children_spawned % 10 ) as f32 ;
74
- let z = ( self . children_spawned / 10 ) as f32 ;
75
- spatial. translate ( vec3 ( -10.0 + x * 2.0 , 0.0 , -10.0 + z * 2.0 ) ) ;
69
+ let x = ( self . children_spawned % 10 ) as f32 ;
70
+ let z = ( self . children_spawned / 10 ) as f32 ;
71
+ spatial. translate ( vec3 ( -10.0 + x * 2.0 , 0.0 , -10.0 + z * 2.0 ) ) ;
76
72
77
-
78
- // You need to parent the new scene under some node if you want it in the scene.
79
- // We parent it under ourselves.
80
- owner. add_child ( Some ( spatial. to_object ( ) ) , false ) ;
81
- self . children_spawned += 1 ;
82
-
83
- } ,
73
+ // You need to parent the new scene under some node if you want it in the scene.
74
+ // We parent it under ourselves.
75
+ owner. add_child ( Some ( spatial. to_object ( ) ) , false ) ;
76
+ self . children_spawned += 1 ;
77
+ }
84
78
Err ( err) => godot_print ! ( "Could not instance Child : {:?}" , err) ,
85
79
}
86
80
87
- let num_children = owner. get_child_count ( ) ;
81
+ let num_children = owner. get_child_count ( ) ;
88
82
update_panel ( & mut owner, num_children) ;
89
83
}
90
84
91
85
#[ export]
92
- unsafe fn remove_one ( & mut self , mut owner : gdnative:: Spatial , str : GodotString ) {
93
- godot_print ! ( "Called remove_one({})" , str . to_string( ) ) ;
94
- let num_children = owner. get_child_count ( ) ;
86
+ unsafe fn remove_one ( & mut self , mut owner : gdnative:: Spatial , str : GodotString ) {
87
+ godot_print ! ( "Called remove_one({})" , str . to_string( ) ) ;
88
+ let num_children = owner. get_child_count ( ) ;
95
89
if num_children <= 0 {
96
90
godot_print ! ( "No children to delete" ) ;
97
91
return ;
98
92
}
99
93
100
- assert_eq ! ( self . children_spawned as i64 , num_children) ;
101
-
102
- let last_child = owner. get_child ( num_children - 1 ) ;
94
+ assert_eq ! ( self . children_spawned as i64 , num_children) ;
95
+
96
+ let last_child = owner. get_child ( num_children - 1 ) ;
103
97
if let Some ( mut node) = last_child {
104
- node. queue_free ( ) ;
105
- self . children_spawned -= 1 ;
106
-
98
+ node. queue_free ( ) ;
99
+ self . children_spawned -= 1 ;
107
100
}
108
101
109
102
update_panel ( & mut owner, num_children - 1 ) ;
110
103
}
111
-
112
104
}
113
105
114
- fn init ( handle : gdnative:: init:: InitHandle ) {
106
+ fn init ( handle : gdnative:: init:: InitHandle ) {
115
107
handle. add_class :: < SceneCreate > ( ) ;
116
108
}
117
109
118
- pub fn load_scene ( path : & str ) -> Option < PackedScene > {
110
+ pub fn load_scene ( path : & str ) -> Option < PackedScene > {
119
111
let scene = ResourceLoader :: godot_singleton ( ) . load (
120
- GodotString :: from_str ( path) , // could also use path.into() here
121
- GodotString :: from_str ( "PackedScene" ) ,
122
- false ,
123
- ) ;
112
+ GodotString :: from_str ( path) , // could also use path.into() here
113
+ GodotString :: from_str ( "PackedScene" ) ,
114
+ false ,
115
+ ) ;
124
116
125
117
scene. and_then ( |s| s. cast :: < PackedScene > ( ) )
126
118
}
127
119
128
- /// Root here is needs to be the same type (or a parent type) of the node that you put in the child
129
- /// scene as the root. For instance Spatial is used for this example.
130
- unsafe fn instance_scene < Root > ( scene : & PackedScene ) -> Result < Root , ManageErrs >
131
- where Root : gdnative:: GodotObject {
132
- let inst_option = scene. instance ( 0 ) ; // 0 - GEN_EDIT_STATE_DISABLED
120
+ /// Root here is needs to be the same type (or a parent type) of the node that you put in the child
121
+ /// scene as the root. For instance Spatial is used for this example.
122
+ unsafe fn instance_scene < Root > ( scene : & PackedScene ) -> Result < Root , ManageErrs >
123
+ where
124
+ Root : gdnative:: GodotObject ,
125
+ {
126
+ let inst_option = scene. instance ( 0 ) ; // 0 - GEN_EDIT_STATE_DISABLED
133
127
134
- if let Some ( instance) = inst_option {
135
- if let Some ( instance_root) = instance. cast :: < Root > ( ) {
128
+ if let Some ( instance) = inst_option {
129
+ if let Some ( instance_root) = instance. cast :: < Root > ( ) {
136
130
Ok ( instance_root)
137
- }
138
- else {
139
- Err ( ManageErrs :: RootClassNotSpatial ( instance. get_name ( ) . to_string ( ) ) )
140
-
131
+ } else {
132
+ Err ( ManageErrs :: RootClassNotSpatial (
133
+ instance. get_name ( ) . to_string ( ) ,
134
+ ) )
141
135
}
142
136
} else {
143
137
Err ( ManageErrs :: CouldNotMakeInstance )
144
138
}
145
139
}
146
140
147
- unsafe fn update_panel ( owner : & mut gdnative:: Spatial , num_children : i64 ) {
141
+ unsafe fn update_panel ( owner : & mut gdnative:: Spatial , num_children : i64 ) {
148
142
// Here is how we call into the panel. First we get its node (we might have saved it
149
143
// from earlier)
150
- let panel_node_opt = owner. get_parent ( ) . and_then ( |parent|
151
- parent . find_node ( GodotString :: from_str ( "Panel" ) , true , false )
152
- ) ;
153
- if let Some ( panel_node) = panel_node_opt {
144
+ let panel_node_opt = owner
145
+ . get_parent ( )
146
+ . and_then ( |parent| parent . find_node ( GodotString :: from_str ( "Panel" ) , true , false ) ) ;
147
+ if let Some ( panel_node) = panel_node_opt {
154
148
// Put the Node
155
149
let mut as_variant = Variant :: from_object ( & panel_node) ;
156
- match as_variant. call ( & GodotString :: from_str ( "set_num_children" ) ,
157
- & [ Variant :: from_u64 ( num_children as u64 ) ] ) {
158
- Ok ( _) =>{ godot_print ! ( "Called Panel OK." ) }
159
- Err ( _) =>{ godot_print ! ( "Error calling Panel" ) }
150
+ match as_variant. call (
151
+ & GodotString :: from_str ( "set_num_children" ) ,
152
+ & [ Variant :: from_u64 ( num_children as u64 ) ] ,
153
+ ) {
154
+ Ok ( _) => godot_print ! ( "Called Panel OK." ) ,
155
+ Err ( _) => godot_print ! ( "Error calling Panel" ) ,
160
156
}
161
- }
162
- else {
157
+ } else {
163
158
godot_print ! ( "Could not find panel node" ) ;
164
159
}
165
160
}
166
161
167
-
168
162
godot_gdnative_init ! ( ) ;
169
163
godot_nativescript_init ! ( init) ;
170
164
godot_gdnative_terminate ! ( ) ;
0 commit comments