Skip to content

Commit e70e4ba

Browse files
authored
Merge pull request #181 from toasteater/feature/nativeclass-instance
Construct object from custom native class
2 parents f11ca2b + dd9b597 commit e70e4ba

File tree

16 files changed

+908
-117
lines changed

16 files changed

+908
-117
lines changed

bindings_generator/api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150414,7 +150414,7 @@
150414150414
},
150415150415
{
150416150416
"name": "new",
150417-
"return_type": "Object",
150417+
"return_type": "Variant",
150418150418
"is_editor": false,
150419150419
"is_noscript": false,
150420150420
"is_const": false,

bindings_generator/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ fn generate_class_bindings(
106106
generate_singleton_getter(output_types_impls, class)?;
107107
}
108108

109+
if class.name == "GDNativeLibrary" {
110+
generate_gdnative_library_singleton_getter(output_types_impls, class)?;
111+
}
112+
109113
if class.instanciable {
110114
if class.is_refcounted() {
111115
generate_reference_ctor(output_types_impls, class)?;
@@ -158,6 +162,10 @@ fn generate_class_bindings(
158162
if class.is_refcounted() && class.instanciable {
159163
generate_drop(output_trait_impls, class)?;
160164
}
165+
166+
if class.instanciable {
167+
generate_instanciable_impl(output_trait_impls, class)?;
168+
}
161169
}
162170

163171
// methods and method table

bindings_generator/src/special_methods.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,24 @@ impl FromVariant for {name} {{
112112
"object::add_ref(obj);"
113113
} else {
114114
"// Not reference-counted."
115-
}
115+
},
116+
)?;
117+
118+
Ok(())
119+
}
120+
121+
pub fn generate_instanciable_impl(output: &mut impl Write, class: &GodotClass) -> GeneratorResult {
122+
assert!(class.instanciable);
123+
124+
writeln!(
125+
output,
126+
r#"
127+
impl Instanciable for {name} {{
128+
fn construct() -> Self {{
129+
{name}::new()
130+
}}
131+
}}"#,
132+
name = class.name,
116133
)?;
117134

118135
Ok(())
@@ -306,6 +323,27 @@ impl Drop for {name} {{
306323
Ok(())
307324
}
308325

326+
pub fn generate_gdnative_library_singleton_getter(output: &mut impl Write, class: &GodotClass) -> GeneratorResult {
327+
assert_eq!("GDNativeLibrary", class.name);
328+
writeln!(
329+
output,
330+
r#"
331+
/// Returns the GDNativeLibrary object of this library. Can be used to construct NativeScript objects.
332+
///
333+
/// See also `Instance::new` for a typed API.
334+
#[inline]
335+
pub fn current_library() -> Self {{
336+
let this = get_gdnative_library_sys();
337+
338+
Self {{
339+
this
340+
}}
341+
}}"#
342+
)?;
343+
344+
Ok(())
345+
}
346+
309347
pub fn class_name_to_snake_case(name: &str) -> String {
310348
// TODO: this is a quick-n-dirty band-aid, it'd be better to
311349
// programmatically do the right conversion, but to_snake_case

examples/hello_world/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extern crate gdnative;
33

44
#[derive(gdnative::NativeClass)]
55
#[inherit(gdnative::Node)]
6+
#[user_data(gdnative::user_data::ArcData<HelloWorld>)]
67
struct HelloWorld;
78

89
#[gdnative::methods]

examples/scene_create/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ struct SceneCreate {
1919
children_spawned: u32,
2020
}
2121

22+
// Assume godot objects are safe to Send
23+
unsafe impl Send for SceneCreate { }
24+
2225
// Demonstrates Scene creation, calling to/from gdscript
2326
//
2427
// 1. Child scene is created when spawn_one is called

examples/spinning_cube/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct RustTest {
1212

1313
impl godot::NativeClass for RustTest {
1414
type Base = godot::MeshInstance;
15+
type UserData = godot::user_data::MutexData<RustTest>;
1516

1617
fn class_name() -> &'static str {
1718
"RustTest"
@@ -30,7 +31,7 @@ impl godot::NativeClass for RustTest {
3031
step: 0.01,
3132
slider: true,
3233
},
33-
getter: |this: &mut RustTest| this.rotate_speed,
34+
getter: |this: &RustTest| this.rotate_speed,
3435
setter: |this: &mut RustTest, v| this.rotate_speed = v,
3536
usage: PropertyUsage::DEFAULT,
3637
});
@@ -41,7 +42,7 @@ impl godot::NativeClass for RustTest {
4142
hint: PropertyHint::Enum {
4243
values: &["Hello", "World", "Testing"],
4344
},
44-
getter: |_: &mut RustTest| GodotString::from_str("Hello"),
45+
getter: |_: &RustTest| GodotString::from_str("Hello"),
4546
setter: (),
4647
usage: PropertyUsage::DEFAULT,
4748
});
@@ -52,7 +53,7 @@ impl godot::NativeClass for RustTest {
5253
hint: PropertyHint::Flags {
5354
values: &["A", "B", "C", "D"],
5455
},
55-
getter: |_: &mut RustTest| 0,
56+
getter: |_: &RustTest| 0,
5657
setter: (),
5758
usage: PropertyUsage::DEFAULT,
5859
});

gdnative-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gdnative-sys = { path = "../gdnative-sys", version = "0.5.0" }
1717
libc = "0.2"
1818
bitflags = "1.0"
1919
euclid = "0.19.6"
20+
parking_lot = "0.9.0"
2021

2122
[build-dependencies]
2223
gdnative_bindings_generator = { path = "../bindings_generator", version = "0.2.0" }

0 commit comments

Comments
 (0)