Skip to content

Construct object from custom native class #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings_generator/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -150414,7 +150414,7 @@
},
{
"name": "new",
"return_type": "Object",
"return_type": "Variant",
"is_editor": false,
"is_noscript": false,
"is_const": false,
Expand Down
8 changes: 8 additions & 0 deletions bindings_generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ fn generate_class_bindings(
generate_singleton_getter(output_types_impls, class)?;
}

if class.name == "GDNativeLibrary" {
generate_gdnative_library_singleton_getter(output_types_impls, class)?;
}

if class.instanciable {
if class.is_refcounted() {
generate_reference_ctor(output_types_impls, class)?;
Expand Down Expand Up @@ -158,6 +162,10 @@ fn generate_class_bindings(
if class.is_refcounted() && class.instanciable {
generate_drop(output_trait_impls, class)?;
}

if class.instanciable {
generate_instanciable_impl(output_trait_impls, class)?;
}
}

// methods and method table
Expand Down
40 changes: 39 additions & 1 deletion bindings_generator/src/special_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,24 @@ impl FromVariant for {name} {{
"object::add_ref(obj);"
} else {
"// Not reference-counted."
}
},
)?;

Ok(())
}

pub fn generate_instanciable_impl(output: &mut impl Write, class: &GodotClass) -> GeneratorResult {
assert!(class.instanciable);

writeln!(
output,
r#"
impl Instanciable for {name} {{
fn construct() -> Self {{
{name}::new()
}}
}}"#,
name = class.name,
)?;

Ok(())
Expand Down Expand Up @@ -306,6 +323,27 @@ impl Drop for {name} {{
Ok(())
}

pub fn generate_gdnative_library_singleton_getter(output: &mut impl Write, class: &GodotClass) -> GeneratorResult {
assert_eq!("GDNativeLibrary", class.name);
writeln!(
output,
r#"
/// Returns the GDNativeLibrary object of this library. Can be used to construct NativeScript objects.
///
/// See also `Instance::new` for a typed API.
#[inline]
pub fn current_library() -> Self {{
let this = get_gdnative_library_sys();

Self {{
this
}}
}}"#
)?;

Ok(())
}

pub fn class_name_to_snake_case(name: &str) -> String {
// TODO: this is a quick-n-dirty band-aid, it'd be better to
// programmatically do the right conversion, but to_snake_case
Expand Down
1 change: 1 addition & 0 deletions examples/hello_world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate gdnative;

#[derive(gdnative::NativeClass)]
#[inherit(gdnative::Node)]
#[user_data(gdnative::user_data::ArcData<HelloWorld>)]
struct HelloWorld;

#[gdnative::methods]
Expand Down
3 changes: 3 additions & 0 deletions examples/scene_create/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct SceneCreate {
children_spawned: u32,
}

// Assume godot objects are safe to Send
unsafe impl Send for SceneCreate { }

// Demonstrates Scene creation, calling to/from gdscript
//
// 1. Child scene is created when spawn_one is called
Expand Down
7 changes: 4 additions & 3 deletions examples/spinning_cube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct RustTest {

impl godot::NativeClass for RustTest {
type Base = godot::MeshInstance;
type UserData = godot::user_data::MutexData<RustTest>;

fn class_name() -> &'static str {
"RustTest"
Expand All @@ -30,7 +31,7 @@ impl godot::NativeClass for RustTest {
step: 0.01,
slider: true,
},
getter: |this: &mut RustTest| this.rotate_speed,
getter: |this: &RustTest| this.rotate_speed,
setter: |this: &mut RustTest, v| this.rotate_speed = v,
usage: PropertyUsage::DEFAULT,
});
Expand All @@ -41,7 +42,7 @@ impl godot::NativeClass for RustTest {
hint: PropertyHint::Enum {
values: &["Hello", "World", "Testing"],
},
getter: |_: &mut RustTest| GodotString::from_str("Hello"),
getter: |_: &RustTest| GodotString::from_str("Hello"),
setter: (),
usage: PropertyUsage::DEFAULT,
});
Expand All @@ -52,7 +53,7 @@ impl godot::NativeClass for RustTest {
hint: PropertyHint::Flags {
values: &["A", "B", "C", "D"],
},
getter: |_: &mut RustTest| 0,
getter: |_: &RustTest| 0,
setter: (),
usage: PropertyUsage::DEFAULT,
});
Expand Down
1 change: 1 addition & 0 deletions gdnative-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gdnative-sys = { path = "../gdnative-sys", version = "0.5.0" }
libc = "0.2"
bitflags = "1.0"
euclid = "0.19.6"
parking_lot = "0.9.0"

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