Skip to content

Commit 46e6be7

Browse files
committed
Cargo fmt (scene_create)
1 parent 3a064fb commit 46e6be7

File tree

2 files changed

+67
-74
lines changed

2 files changed

+67
-74
lines changed

examples/hello_world/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ struct HelloWorld;
77

88
#[gdnative::methods]
99
impl HelloWorld {
10-
1110
fn _init(_owner: gdnative::Node) -> Self {
1211
HelloWorld
1312
}

examples/scene_create/src/lib.rs

Lines changed: 67 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#[macro_use]
2-
extern crate gdnative;
2+
extern crate gdnative;
33
extern crate euclid;
44

5-
use gdnative::{GodotString, Variant, ResourceLoader, PackedScene, Spatial};
65
use euclid::vec3;
6+
use gdnative::{GodotString, PackedScene, ResourceLoader, Spatial, Variant};
77

88
#[derive(Debug, Clone, PartialEq)]
99
pub enum ManageErrs {
@@ -13,30 +13,28 @@ pub enum ManageErrs {
1313

1414
#[derive(gdnative::NativeClass)]
1515
#[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,
2020
}
2121

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.
3231

3332
#[gdnative::methods]
3433
impl SceneCreate {
35-
3634
fn _init(_owner: gdnative::Spatial) -> Self {
3735
SceneCreate {
3836
template: None, // Have not loaded this template yet.
39-
children_spawned: 0,
37+
children_spawned: 0,
4038
}
4139
}
4240

@@ -50,9 +48,8 @@ impl SceneCreate {
5048
}
5149

5250
#[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());
5653

5754
let template = if let Some(template) = &self.template {
5855
template
@@ -67,104 +64,101 @@ impl SceneCreate {
6764
Ok(mut spatial) => {
6865
// Here is how you rename the child...
6966
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));
7268

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));
7672

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+
}
8478
Err(err) => godot_print!("Could not instance Child : {:?}", err),
8579
}
8680

87-
let num_children = owner.get_child_count();
81+
let num_children = owner.get_child_count();
8882
update_panel(&mut owner, num_children);
8983
}
9084

9185
#[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();
9589
if num_children <= 0 {
9690
godot_print!("No children to delete");
9791
return;
9892
}
9993

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);
10397
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;
107100
}
108101

109102
update_panel(&mut owner, num_children - 1);
110103
}
111-
112104
}
113105

114-
fn init(handle: gdnative::init::InitHandle) {
106+
fn init(handle: gdnative::init::InitHandle) {
115107
handle.add_class::<SceneCreate>();
116108
}
117109

118-
pub fn load_scene(path: &str) -> Option<PackedScene> {
110+
pub fn load_scene(path: &str) -> Option<PackedScene> {
119111
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+
);
124116

125117
scene.and_then(|s| s.cast::<PackedScene>())
126118
}
127119

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
133127

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>() {
136130
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+
))
141135
}
142136
} else {
143137
Err(ManageErrs::CouldNotMakeInstance)
144138
}
145139
}
146140

147-
unsafe fn update_panel(owner: &mut gdnative::Spatial, num_children : i64) {
141+
unsafe fn update_panel(owner: &mut gdnative::Spatial, num_children: i64) {
148142
// Here is how we call into the panel. First we get its node (we might have saved it
149143
// 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 {
154148
// Put the Node
155149
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"),
160156
}
161-
}
162-
else {
157+
} else {
163158
godot_print!("Could not find panel node");
164159
}
165160
}
166161

167-
168162
godot_gdnative_init!();
169163
godot_nativescript_init!(init);
170164
godot_gdnative_terminate!();

0 commit comments

Comments
 (0)