Skip to content

Commit ac5f78c

Browse files
bors[bot]mio991
andauthored
Merge #154
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]>
2 parents 48b509a + dda817d commit ac5f78c

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

godot-core/src/builtin/meta/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub trait VariantMetadata {
3131
Self::variant_type(),
3232
Self::class_name(),
3333
StringName::from(property_name),
34+
global::PropertyHint::PROPERTY_HINT_NONE,
35+
GodotString::new(),
3436
)
3537
}
3638

@@ -64,13 +66,15 @@ impl PropertyInfo {
6466
variant_type: VariantType,
6567
class_name: ClassName,
6668
property_name: StringName,
69+
hint: global::PropertyHint,
70+
hint_string: GodotString,
6771
) -> Self {
6872
Self {
6973
variant_type,
7074
class_name,
7175
property_name,
72-
hint: global::PropertyHint::PROPERTY_HINT_NONE,
73-
hint_string: GodotString::new(),
76+
hint,
77+
hint_string,
7478
usage: global::PropertyUsageFlags::PROPERTY_USAGE_DEFAULT,
7579
}
7680
}

godot-macros/src/derive_godot_class.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,44 @@ struct ExportedField {
176176
field: Field,
177177
getter: String,
178178
setter: String,
179+
hint: Option<ExportHint>,
180+
}
181+
182+
#[derive(Clone)]
183+
struct ExportHint {
184+
hint_type: Ident,
185+
description: String,
186+
}
187+
188+
impl ExportHint {
189+
fn none() -> Self {
190+
Self {
191+
hint_type: ident("PROPERTY_HINT_NONE"),
192+
description: "".to_string(),
193+
}
194+
}
179195
}
180196

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

202+
let hint = parser
203+
.handle_ident("hint")?
204+
.map(|hint_type| {
205+
Ok(ExportHint {
206+
hint_type,
207+
description: parser.handle_lit_required("hint_desc")?,
208+
})
209+
})
210+
.transpose()?;
211+
186212
Ok(ExportedField {
187213
field,
188214
getter,
189215
setter,
216+
hint,
190217
})
191218
}
192219
}
@@ -248,6 +275,14 @@ fn make_exports_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
248275
let setter = proc_macro2::Literal::from_str(&exported_field.setter).unwrap();
249276
let field_type = exported_field.field.ty.clone();
250277

278+
let ExportHint {
279+
hint_type,
280+
description,
281+
} = exported_field.hint.clone().unwrap_or_else(ExportHint::none);
282+
283+
// trims '"' and '\' from both ends of the hint description.
284+
let description = description.trim_matches(|c| c == '\\' || c == '"');
285+
251286
quote! {
252287
use ::godot::builtin::meta::VariantMetadata;
253288

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

itest/godot/ManualFfiTests.gd

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,16 @@ func test_export():
3535
var node = Node.new()
3636
obj.object_val = node
3737
assert_eq(obj.object_val, node)
38-
38+
39+
var texture_val_meta = obj.get_property_list().filter(
40+
func(el)->bool:
41+
return el["name"] == "texture_val"
42+
).front()
43+
44+
assert_that(texture_val_meta != null, "'texture_val' is defined")
45+
assert_eq(texture_val_meta["hint"], PropertyHint.PROPERTY_HINT_RESOURCE_TYPE)
46+
assert_eq(texture_val_meta["hint_string"], "Texture")
47+
3948
obj.free()
4049
node.free()
4150

itest/rust/src/export_test.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7-
use godot::prelude::*;
7+
use godot::{engine::Texture, prelude::*};
88

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

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

2426
#[godot_api]
@@ -56,6 +58,20 @@ impl HasProperty {
5658
pub fn set_object_val(&mut self, val: Gd<Object>) {
5759
self.object_val = Some(val);
5860
}
61+
62+
#[func]
63+
pub fn get_texture_val(&self) -> Variant {
64+
if let Some(texture_val) = self.texture_val.as_ref() {
65+
texture_val.to_variant()
66+
} else {
67+
Variant::nil()
68+
}
69+
}
70+
71+
#[func]
72+
pub fn set_texture_val(&mut self, val: Gd<Texture>) {
73+
self.texture_val = Some(val);
74+
}
5975
}
6076

6177
#[godot_api]
@@ -65,6 +81,7 @@ impl GodotExt for HasProperty {
6581
int_val: 0,
6682
object_val: None,
6783
string_val: GodotString::new(),
84+
texture_val: None,
6885
base,
6986
}
7087
}

0 commit comments

Comments
 (0)