Skip to content

Commit

Permalink
Merge #154
Browse files Browse the repository at this point in the history
154: Exports: add PropertyHints as optional Parameters r=Bromeon a=mio991

As the title says I added PropertyHints as optional Parameters.

If you prefere another design tell me.

Co-authored-by: mio991 <[email protected]>
  • Loading branch information
bors[bot] and mio991 authored Mar 9, 2023
2 parents 48b509a + dda817d commit ac5f78c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
8 changes: 6 additions & 2 deletions godot-core/src/builtin/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub trait VariantMetadata {
Self::variant_type(),
Self::class_name(),
StringName::from(property_name),
global::PropertyHint::PROPERTY_HINT_NONE,
GodotString::new(),
)
}

Expand Down Expand Up @@ -64,13 +66,15 @@ impl PropertyInfo {
variant_type: VariantType,
class_name: ClassName,
property_name: StringName,
hint: global::PropertyHint,
hint_string: GodotString,
) -> Self {
Self {
variant_type,
class_name,
property_name,
hint: global::PropertyHint::PROPERTY_HINT_NONE,
hint_string: GodotString::new(),
hint,
hint_string,
usage: global::PropertyUsageFlags::PROPERTY_USAGE_DEFAULT,
}
}
Expand Down
37 changes: 37 additions & 0 deletions godot-macros/src/derive_godot_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,44 @@ struct ExportedField {
field: Field,
getter: String,
setter: String,
hint: Option<ExportHint>,
}

#[derive(Clone)]
struct ExportHint {
hint_type: Ident,
description: String,
}

impl ExportHint {
fn none() -> Self {
Self {
hint_type: ident("PROPERTY_HINT_NONE"),
description: "".to_string(),
}
}
}

impl ExportedField {
pub fn new_from_kv(field: Field, parser: &mut KvParser) -> ParseResult<ExportedField> {
let getter = parser.handle_lit_required("getter")?;
let setter = parser.handle_lit_required("setter")?;

let hint = parser
.handle_ident("hint")?
.map(|hint_type| {
Ok(ExportHint {
hint_type,
description: parser.handle_lit_required("hint_desc")?,
})
})
.transpose()?;

Ok(ExportedField {
field,
getter,
setter,
hint,
})
}
}
Expand Down Expand Up @@ -248,6 +275,14 @@ fn make_exports_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
let setter = proc_macro2::Literal::from_str(&exported_field.setter).unwrap();
let field_type = exported_field.field.ty.clone();

let ExportHint {
hint_type,
description,
} = exported_field.hint.clone().unwrap_or_else(ExportHint::none);

// trims '"' and '\' from both ends of the hint description.
let description = description.trim_matches(|c| c == '\\' || c == '"');

quote! {
use ::godot::builtin::meta::VariantMetadata;

Expand All @@ -256,6 +291,8 @@ fn make_exports_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
<#field_type>::variant_type(),
::godot::builtin::meta::ClassName::of::<#class_name>(),
::godot::builtin::StringName::from(#name),
::godot::engine::global::PropertyHint::#hint_type,
GodotString::from(#description),
);
let property_info_sys = property_info.property_sys();

Expand Down
11 changes: 10 additions & 1 deletion itest/godot/ManualFfiTests.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ func test_export():
var node = Node.new()
obj.object_val = node
assert_eq(obj.object_val, node)


var texture_val_meta = obj.get_property_list().filter(
func(el)->bool:
return el["name"] == "texture_val"
).front()

assert_that(texture_val_meta != null, "'texture_val' is defined")
assert_eq(texture_val_meta["hint"], PropertyHint.PROPERTY_HINT_RESOURCE_TYPE)
assert_eq(texture_val_meta["hint_string"], "Texture")

obj.free()
node.free()

Expand Down
19 changes: 18 additions & 1 deletion itest/rust/src/export_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use godot::prelude::*;
use godot::{engine::Texture, prelude::*};

// No tests currently, tests using HasProperty are in Godot scripts.

Expand All @@ -19,6 +19,8 @@ struct HasProperty {
string_val: GodotString,
#[export(getter = "get_object_val", setter = "set_object_val")]
object_val: Option<Gd<Object>>,
#[export(getter = "get_texture_val", setter = "set_texture_val", hint = PROPERTY_HINT_RESOURCE_TYPE, hint_desc = "Texture")]
texture_val: Option<Gd<Texture>>,
}

#[godot_api]
Expand Down Expand Up @@ -56,6 +58,20 @@ impl HasProperty {
pub fn set_object_val(&mut self, val: Gd<Object>) {
self.object_val = Some(val);
}

#[func]
pub fn get_texture_val(&self) -> Variant {
if let Some(texture_val) = self.texture_val.as_ref() {
texture_val.to_variant()
} else {
Variant::nil()
}
}

#[func]
pub fn set_texture_val(&mut self, val: Gd<Texture>) {
self.texture_val = Some(val);
}
}

#[godot_api]
Expand All @@ -65,6 +81,7 @@ impl GodotExt for HasProperty {
int_val: 0,
object_val: None,
string_val: GodotString::new(),
texture_val: None,
base,
}
}
Expand Down

0 comments on commit ac5f78c

Please sign in to comment.