Skip to content

Commit 8b7a12f

Browse files
committed
- Make property template test check exports as well
- Fix some places where property info was misaligned - Rename `ExportInfo` to `PropertyHintInfo` now that it's used in both export and property
1 parent 654e4fc commit 8b7a12f

File tree

8 files changed

+62
-54
lines changed

8 files changed

+62
-54
lines changed

godot-core/src/builtin/array.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use godot_ffi as sys;
88

99
use crate::builtin::*;
1010
use crate::obj::Share;
11-
use crate::property::{Export, ExportInfo, Property, TypeStringHint};
11+
use crate::property::{Export, Property, PropertyHintInfo, TypeStringHint};
1212
use std::fmt;
1313
use std::marker::PhantomData;
1414
use sys::{ffi_methods, interface_fn, GodotFfi};
@@ -700,26 +700,30 @@ impl<T: GodotType> Property for Array<T> {
700700
}
701701

702702
#[cfg(since_api = "4.2")]
703-
fn property_hint() -> ExportInfo {
704-
ExportInfo {
703+
fn property_hint() -> PropertyHintInfo {
704+
if T::Ffi::variant_type() == VariantType::Nil {
705+
return PropertyHintInfo::with_hint_none("");
706+
}
707+
708+
PropertyHintInfo {
705709
hint: crate::engine::global::PropertyHint::PROPERTY_HINT_ARRAY_TYPE,
706710
hint_string: T::godot_type_name().into(),
707711
}
708712
}
709713
}
710714

711715
impl<T: GodotType + TypeStringHint> Export for Array<T> {
712-
fn default_export_info() -> ExportInfo {
713-
ExportInfo {
716+
fn default_export_info() -> PropertyHintInfo {
717+
PropertyHintInfo {
714718
hint: crate::engine::global::PropertyHint::PROPERTY_HINT_TYPE_STRING,
715719
hint_string: T::type_string().into(),
716720
}
717721
}
718722
}
719723

720724
impl Export for Array<Variant> {
721-
fn default_export_info() -> ExportInfo {
722-
ExportInfo::with_hint_none()
725+
fn default_export_info() -> PropertyHintInfo {
726+
PropertyHintInfo::with_hint_none("Array")
723727
}
724728
}
725729

godot-core/src/builtin/dictionary.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use godot_ffi as sys;
99
use crate::builtin::meta::{FromGodot, ToGodot};
1010
use crate::builtin::{inner, Variant};
1111
use crate::obj::Share;
12-
use crate::property::{Export, ExportInfo, Property, TypeStringHint};
12+
use crate::property::{Export, Property, PropertyHintInfo, TypeStringHint};
1313
use std::fmt;
1414
use std::marker::PhantomData;
1515
use std::ptr::addr_of_mut;
@@ -351,8 +351,8 @@ impl TypeStringHint for Dictionary {
351351
}
352352

353353
impl Export for Dictionary {
354-
fn default_export_info() -> ExportInfo {
355-
ExportInfo::with_hint_none()
354+
fn default_export_info() -> PropertyHintInfo {
355+
PropertyHintInfo::with_hint_none("Dictionary")
356356
}
357357
}
358358

godot-core/src/obj/gd.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::builtin::meta::{FromGodot, GodotConvert, GodotType, ToGodot};
1616
use crate::builtin::{Callable, StringName};
1717
use crate::obj::{cap, dom, mem, EngineEnum, GdDerefTarget, GodotClass, Inherits, Share};
1818
use crate::obj::{GdMut, GdRef, InstanceId};
19-
use crate::property::{Export, ExportInfo, Property, TypeStringHint};
19+
use crate::property::{Export, Property, PropertyHintInfo, TypeStringHint};
2020
use crate::{callbacks, engine, out};
2121

2222
use super::RawGd;
@@ -513,7 +513,7 @@ impl<T: GodotClass> Property for Gd<T> {
513513
}
514514

515515
impl<T: GodotClass> Export for Gd<T> {
516-
fn default_export_info() -> ExportInfo {
516+
fn default_export_info() -> PropertyHintInfo {
517517
let hint = if T::inherits::<engine::Resource>() {
518518
engine::global::PropertyHint::PROPERTY_HINT_RESOURCE_TYPE
519519
} else if T::inherits::<engine::Node>() {
@@ -526,7 +526,7 @@ impl<T: GodotClass> Export for Gd<T> {
526526
// but doesn't seem to make a difference otherwise.
527527
let hint_string = T::class_name().to_godot_string();
528528

529-
ExportInfo { hint, hint_string }
529+
PropertyHintInfo { hint, hint_string }
530530
}
531531
}
532532

godot-core/src/property.rs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@ use crate::engine::global::PropertyHint;
1212

1313
/// Trait implemented for types that can be used as `#[var]` fields. This creates a copy of the
1414
/// value, for some type-specific definition of "copy". For example, `Array`, `Dictionary` and `Gd` are
15-
/// returned via `Share::share()` instead of copying the actual data.
15+
/// returned by shared reference instead of copying the actual data.
1616
pub trait Property {
1717
type Intermediate;
1818

19-
fn property_hint() -> ExportInfo {
20-
ExportInfo::with_hint_none()
21-
}
2219
fn get_property(&self) -> Self::Intermediate;
2320
fn set_property(&mut self, value: Self::Intermediate);
21+
22+
fn property_hint() -> PropertyHintInfo {
23+
PropertyHintInfo::with_hint_none("")
24+
}
2425
}
2526

2627
/// Trait implemented for types that can be used as `#[export]` fields.
2728
pub trait Export: Property {
2829
/// The export info to use for an exported field of this type, if no other export info is specified.
29-
fn default_export_info() -> ExportInfo;
30+
fn default_export_info() -> PropertyHintInfo;
3031
}
3132

3233
/// Trait for types that can be represented as a type string for use with
@@ -77,7 +78,7 @@ impl<T> Export for Option<T>
7778
where
7879
T: Export + From<<T as Property>::Intermediate>,
7980
{
80-
fn default_export_info() -> ExportInfo {
81+
fn default_export_info() -> PropertyHintInfo {
8182
T::default_export_info()
8283
}
8384
}
@@ -87,18 +88,20 @@ where
8788

8889
/// Info needed for godot to understand how to export a type to the editor.
8990
#[derive(Clone, Eq, PartialEq, Debug)]
90-
pub struct ExportInfo {
91+
pub struct PropertyHintInfo {
9192
pub hint: PropertyHint,
9293
pub hint_string: GodotString,
9394
}
9495

95-
impl ExportInfo {
96-
/// Create a new `ExportInfo` with a property hint of
96+
impl PropertyHintInfo {
97+
/// Create a new `PropertyHintInfo` with a property hint of
9798
/// [`PROPERTY_HINT_NONE`](PropertyHint::PROPERTY_HINT_NONE).
98-
pub fn with_hint_none() -> Self {
99+
///
100+
/// Usually Godot expects this to be combined with a `hint_string` containing the name of the type.
101+
pub fn with_hint_none<S: Into<GodotString>>(type_name: S) -> Self {
99102
Self {
100103
hint: PropertyHint::PROPERTY_HINT_NONE,
101-
hint_string: GodotString::new(),
104+
hint_string: type_name.into(),
102105
}
103106
}
104107
}
@@ -125,7 +128,7 @@ pub mod export_info_functions {
125128
use crate::builtin::GodotString;
126129
use crate::engine::global::PropertyHint;
127130

128-
use super::ExportInfo;
131+
use super::PropertyHintInfo;
129132

130133
/// Turn a list of variables into a comma separated string containing only the identifiers corresponding
131134
/// to a true boolean variable.
@@ -156,7 +159,7 @@ pub mod export_info_functions {
156159
radians: bool,
157160
degrees: bool,
158161
hide_slider: bool,
159-
) -> ExportInfo {
162+
) -> PropertyHintInfo {
160163
let min_max = format!("{},{}", min, max);
161164

162165
let rest =
@@ -168,7 +171,7 @@ pub mod export_info_functions {
168171
format!("{min_max},{rest}")
169172
};
170173

171-
ExportInfo {
174+
PropertyHintInfo {
172175
hint: PropertyHint::PROPERTY_HINT_RANGE,
173176
hint_string: hint_string.into(),
174177
}
@@ -217,64 +220,64 @@ pub mod export_info_functions {
217220

218221
type EnumVariant = ExportValueWithKey<i64>;
219222

220-
pub fn export_enum<T>(variants: &[T]) -> ExportInfo
223+
pub fn export_enum<T>(variants: &[T]) -> PropertyHintInfo
221224
where
222225
for<'a> &'a T: Into<EnumVariant>,
223226
{
224227
let hint_string: String = EnumVariant::slice_as_hint_string(variants);
225228

226-
ExportInfo {
229+
PropertyHintInfo {
227230
hint: PropertyHint::PROPERTY_HINT_ENUM,
228231
hint_string: hint_string.into(),
229232
}
230233
}
231234

232-
pub fn export_exp_easing(attenuation: bool, positive_only: bool) -> ExportInfo {
235+
pub fn export_exp_easing(attenuation: bool, positive_only: bool) -> PropertyHintInfo {
233236
let hint_string = comma_separate_boolean_idents!(attenuation, positive_only);
234237

235-
ExportInfo {
238+
PropertyHintInfo {
236239
hint: PropertyHint::PROPERTY_HINT_EXP_EASING,
237240
hint_string: hint_string.into(),
238241
}
239242
}
240243

241244
type BitFlag = ExportValueWithKey<u32>;
242245

243-
pub fn export_flags<T>(bits: &[T]) -> ExportInfo
246+
pub fn export_flags<T>(bits: &[T]) -> PropertyHintInfo
244247
where
245248
for<'a> &'a T: Into<BitFlag>,
246249
{
247250
let hint_string = BitFlag::slice_as_hint_string(bits);
248251

249-
ExportInfo {
252+
PropertyHintInfo {
250253
hint: PropertyHint::PROPERTY_HINT_FLAGS,
251254
hint_string: hint_string.into(),
252255
}
253256
}
254257

255-
pub fn export_file<S: AsRef<str>>(filter: S) -> ExportInfo {
258+
pub fn export_file<S: AsRef<str>>(filter: S) -> PropertyHintInfo {
256259
export_file_inner(false, filter)
257260
}
258261

259-
pub fn export_global_file<S: AsRef<str>>(filter: S) -> ExportInfo {
262+
pub fn export_global_file<S: AsRef<str>>(filter: S) -> PropertyHintInfo {
260263
export_file_inner(true, filter)
261264
}
262265

263-
pub fn export_file_inner<S: AsRef<str>>(global: bool, filter: S) -> ExportInfo {
266+
pub fn export_file_inner<S: AsRef<str>>(global: bool, filter: S) -> PropertyHintInfo {
264267
let hint = if global {
265-
PropertyHint::PROPERTY_HINT_FILE
266-
} else {
267268
PropertyHint::PROPERTY_HINT_GLOBAL_FILE
269+
} else {
270+
PropertyHint::PROPERTY_HINT_FILE
268271
};
269272

270-
ExportInfo {
273+
PropertyHintInfo {
271274
hint,
272275
hint_string: filter.as_ref().into(),
273276
}
274277
}
275278

276-
pub fn export_placeholder<S: AsRef<str>>(placeholder: S) -> ExportInfo {
277-
ExportInfo {
279+
pub fn export_placeholder<S: AsRef<str>>(placeholder: S) -> PropertyHintInfo {
280+
PropertyHintInfo {
278281
hint: PropertyHint::PROPERTY_HINT_PLACEHOLDER_TEXT,
279282
hint_string: placeholder.as_ref().into(),
280283
}
@@ -285,8 +288,8 @@ pub mod export_info_functions {
285288
$( $function_name:ident => $property_hint:ident, )*
286289
) => {
287290
$(
288-
pub fn $function_name() -> ExportInfo {
289-
ExportInfo {
291+
pub fn $function_name() -> PropertyHintInfo {
292+
PropertyHintInfo {
290293
hint: PropertyHint::$property_hint,
291294
hint_string: GodotString::new()
292295
}
@@ -356,8 +359,8 @@ mod export_impls {
356359

357360
(@export $Ty:ty) => {
358361
impl Export for $Ty {
359-
fn default_export_info() -> ExportInfo {
360-
ExportInfo::with_hint_none()
362+
fn default_export_info() -> PropertyHintInfo {
363+
PropertyHintInfo::with_hint_none(<$Ty as $crate::builtin::meta::GodotType>::godot_type_name())
361364
}
362365
}
363366
};

godot-macros/src/class/data_models/property.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum FieldHint {
2525
hint_string: TokenStream,
2626
},
2727

28-
/// The hint and hint string are given by a token stream returning an `ExportInfo` struct.
28+
/// The hint and hint string are given by a token stream returning an `PropertyHintInfo` struct.
2929
HintFromExportFunction(TokenStream),
3030
}
3131

@@ -133,7 +133,7 @@ pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
133133
},
134134
FieldHint::HintFromExportFunction(expression) => quote! {
135135
{
136-
let ::godot::bind::property::ExportInfo { hint, hint_string } = #expression;
136+
let ::godot::bind::property::PropertyHintInfo { hint, hint_string } = #expression;
137137
(hint, hint_string)
138138
}
139139
},

godot-macros/src/derive/derive_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ pub fn derive_export(decl: Declaration) -> ParseResult<TokenStream2> {
6565
let out = quote! {
6666
#[allow(unused_parens)]
6767
impl godot::bind::property::Export for #name {
68-
fn default_export_info() -> godot::bind::property::ExportInfo {
69-
godot::bind::property::ExportInfo {
68+
fn default_export_info() -> godot::bind::property::PropertyHintInfo {
69+
godot::bind::property::PropertyHintInfo {
7070
hint: godot::engine::global::PropertyHint::PROPERTY_HINT_ENUM,
7171
hint_string: godot::prelude::GodotString::from(#hint_string),
7272
}

itest/rust/src/object_tests/property_template_test.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,11 @@ struct PropertyTemplateRust {
399399
#[export(multiline)]
400400
export_multiline: GodotString,
401401
#[export(range = (0.0, 20.0))]
402-
export_range_0_20: f64,
402+
export_range_float_0_20: f64,
403403
// We're missing step currently.
404404
// #[export(range = (-10, 20 /* , 0.2 */))]
405405
// export_range_float_neg_10_20_02: f64,
406+
// we can only export ranges of floats currently
406407
// #[export(range = (0, 100, 1, "or_greater", "or_less"))]
407408
// export_range_int_0_100_1_or_greater_or_less: int,
408409
#[export(exp_easing)]
@@ -454,7 +455,7 @@ fn property_template_test(ctx: &TestContext) {
454455
.unwrap()
455456
.to::<GodotString>()
456457
.to_string();
457-
if name.starts_with("property_") {
458+
if name.starts_with("property_") || name.starts_with("export_") {
458459
properties.insert(name, property);
459460
}
460461
}

itest/rust/src/object_tests/property_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
use godot::{
8-
bind::property::ExportInfo,
8+
bind::property::PropertyHintInfo,
99
engine::{
1010
global::{PropertyHint, PropertyUsageFlags},
1111
Texture,
@@ -172,8 +172,8 @@ impl Property for SomeCStyleEnum {
172172
}
173173

174174
impl Export for SomeCStyleEnum {
175-
fn default_export_info() -> ExportInfo {
176-
ExportInfo {
175+
fn default_export_info() -> PropertyHintInfo {
176+
PropertyHintInfo {
177177
hint: PropertyHint::PROPERTY_HINT_ENUM,
178178
hint_string: "A,B,C".into(),
179179
}

0 commit comments

Comments
 (0)