Skip to content

Commit

Permalink
Rename property-hint related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Aug 4, 2024
1 parent de92e70 commit ff3afd6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 40 deletions.
6 changes: 3 additions & 3 deletions godot-core/src/builtin/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ impl<T: ArrayElement> Var for Array<T> {
*self = FromGodot::from_godot(value)
}

fn property_hint() -> PropertyHintInfo {
fn var_hint() -> PropertyHintInfo {
// For array #[var], the hint string is "PackedInt32Array", "Node" etc. for typed arrays, and "" for untyped arrays.
if Self::has_variant_t() {
PropertyHintInfo::none()
Expand All @@ -914,10 +914,10 @@ impl<T: ArrayElement> Var for Array<T> {
}

impl<T: ArrayElement> Export for Array<T> {
fn default_export_info() -> PropertyHintInfo {
fn export_hint() -> PropertyHintInfo {
// If T == Variant, then we return "Array" builtin type hint.
if Self::has_variant_t() {
PropertyHintInfo::with_type_name::<VariantArray>()
PropertyHintInfo::type_name::<VariantArray>()
} else {
PropertyHintInfo::export_array_element::<T>()
}
Expand Down
4 changes: 2 additions & 2 deletions godot-core/src/builtin/collections/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,12 @@ macro_rules! impl_packed_array {
$crate::meta::impl_godot_as_self!($PackedArray);

impl $crate::registry::property::Export for $PackedArray {
fn default_export_info() -> $crate::registry::property::PropertyHintInfo {
fn export_hint() -> $crate::registry::property::PropertyHintInfo {
// In 4.3 Godot can (and does) use type hint strings for packed arrays, see https://github.com/godotengine/godot/pull/82952.
if sys::GdextBuild::since_api("4.3") {
$crate::registry::property::PropertyHintInfo::export_array_element::<$Element>()
} else {
$crate::registry::property::PropertyHintInfo::with_type_name::<$PackedArray>()
$crate::registry::property::PropertyHintInfo::type_name::<$PackedArray>()
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions godot-core/src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,14 @@ impl PropertyInfo {
///
/// This will generate property info equivalent to what a `#[var]` attribute would.
pub fn new_var<T: Var>(property_name: &str) -> Self {
<T as GodotConvert>::Via::property_info(property_name).with_hint_info(T::property_hint())
<T as GodotConvert>::Via::property_info(property_name).with_hint_info(T::var_hint())
}

/// Create a new `PropertyInfo` representing an exported property named `property_name` with type `T`.
///
/// This will generate property info equivalent to what an `#[export]` attribute would.
pub fn new_export<T: Export>(property_name: &str) -> Self {
<T as GodotConvert>::Via::property_info(property_name)
.with_hint_info(T::default_export_info())
<T as GodotConvert>::Via::property_info(property_name).with_hint_info(T::export_hint())
}

/// Change the `hint` and `hint_string` to be the given `hint_info`.
Expand Down
4 changes: 2 additions & 2 deletions godot-core/src/obj/gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ impl<T: GodotClass> GodotType for Gd<T> {

impl<T: GodotClass> ArrayElement for Gd<T> {
fn element_type_string() -> String {
match Self::default_export_info().hint {
match Self::export_hint().hint {
hint @ (PropertyHint::RESOURCE_TYPE | PropertyHint::NODE_TYPE) => {
format!(
"{}/{}:{}",
Expand Down Expand Up @@ -792,7 +792,7 @@ impl<T: GodotClass> Var for Gd<T> {
}

impl<T: GodotClass> Export for Gd<T> {
fn default_export_info() -> PropertyHintInfo {
fn export_hint() -> PropertyHintInfo {
let hint = if T::inherits::<classes::Resource>() {
PropertyHint::RESOURCE_TYPE
} else if T::inherits::<classes::Node>() {
Expand Down
38 changes: 15 additions & 23 deletions godot-core/src/registry/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,11 @@ use crate::meta::{ArrayElement, FromGodot, GodotConvert, GodotType, ToGodot};
)]
pub trait Var: GodotConvert {
fn get_property(&self) -> Self::Via;

fn set_property(&mut self, value: Self::Via);

/// Specific property hints, only override if they deviate from [`GodotType::property_info`], e.g. for enums/newtypes.
fn property_hint() -> PropertyHintInfo {
// From Godot 4.3 onward, properties are typically exported with "" hint_string. This needs to be manually overridden for types
// that need a hint string, like arrays. See also https://github.com/godotengine/godot/pull/82952 and property_template_test.rs.
// if sys::GdextBuild::since_api("4.3") {
// PropertyHintInfo::with_type_name::<Self::Via>()
// } else {
// Self::Via::property_hint_info()
// }

//PropertyHintInfo::with_hint_none("")
fn var_hint() -> PropertyHintInfo {
Self::Via::property_hint_info()
}
}
Expand All @@ -59,8 +51,8 @@ pub trait Var: GodotConvert {
)]
pub trait Export: Var {
/// The export info to use for an exported field of this type, if no other export info is specified.
fn default_export_info() -> PropertyHintInfo {
<Self as Var>::property_hint()
fn export_hint() -> PropertyHintInfo {
<Self as Var>::var_hint()
}
}

Expand Down Expand Up @@ -95,8 +87,8 @@ where
T: Export,
Option<T>: Var,
{
fn default_export_info() -> PropertyHintInfo {
T::default_export_info()
fn export_hint() -> PropertyHintInfo {
T::export_hint()
}
}

Expand All @@ -119,15 +111,16 @@ impl PropertyHintInfo {
}
}

/// Create a new `PropertyHintInfo` with a property hint of [`PROPERTY_HINT_NONE`](PropertyHint::NONE).
/// Use [`PROPERTY_HINT_NONE`](PropertyHint::NONE) with `T`'s Godot type name.
///
/// Starting with Godot version 4.3, the hint string will always be the empty string. Before that, the hint string is set to
/// be `type_name`.
pub fn with_hint_none(type_name: impl Into<GString>) -> Self {
/// be the Godot type name of `T`.
pub fn type_name<T: GodotType>() -> Self {
let type_name = T::godot_type_name();
let hint_string = if sys::GdextBuild::since_api("4.3") {
GString::new()
} else {
type_name.into()
GString::from(type_name)
};

Self {
Expand All @@ -136,22 +129,21 @@ impl PropertyHintInfo {
}
}

/// Use for `#[var]` properties -- [`PROPERTY_HINT_ARRAY_TYPE`](PropertyHint::ARRAY_TYPE) with the type name as hint string.
pub fn var_array_element<T: ArrayElement>() -> Self {
Self {
hint: PropertyHint::ARRAY_TYPE,
hint_string: GString::from(T::godot_type_name()),
}
}

/// Use for `#[export]` properties -- [`PROPERTY_HINT_TYPE_STRING`](PropertyHint::TYPE_STRING) with the **element** type string as hint string.
pub fn export_array_element<T: ArrayElement>() -> Self {
Self {
hint: PropertyHint::TYPE_STRING,
hint_string: GString::from(T::element_type_string()),
}
}
pub fn with_type_name<T: GodotType>() -> Self {
Self::with_hint_none(T::godot_type_name())
}
}

/// Functions used to translate user-provided arguments into export hints.
Expand Down Expand Up @@ -437,8 +429,8 @@ mod export_impls {

(@export $Ty:ty) => {
impl Export for $Ty {
fn default_export_info() -> PropertyHintInfo {
PropertyHintInfo::with_type_name::<$Ty>()
fn export_hint() -> PropertyHintInfo {
PropertyHintInfo::type_name::<$Ty>()
}
}
};
Expand Down
8 changes: 4 additions & 4 deletions godot-macros/src/class/data_models/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ pub fn make_property_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
} else if export.is_some() {
quote! {
{
let default_export_info = <#field_type as ::godot::register::property::Export>::default_export_info();
(default_export_info.hint, default_export_info.hint_string)
let export_hint = <#field_type as ::godot::register::property::Export>::export_hint();
(export_hint.hint, export_hint.hint_string)
}
}
} else {
quote! {
{
let default_export_info = <#field_type as ::godot::register::property::Var>::property_hint();
(default_export_info.hint, default_export_info.hint_string)
let export_hint = <#field_type as ::godot::register::property::Var>::var_hint();
(export_hint.hint, export_hint.hint_string)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions godot-macros/src/derive/derive_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn derive_var(item: venial::Item) -> ParseResult<TokenStream> {
*self = ::godot::meta::FromGodot::from_godot(value);
}

fn property_hint() -> ::godot::register::property::PropertyHintInfo {
fn var_hint() -> ::godot::register::property::PropertyHintInfo {
#property_hint_impl
}
}
Expand All @@ -49,7 +49,7 @@ fn create_property_hint_impl(convert: &GodotConvert) -> TokenStream {
Data::NewType { field } => {
let ty = &field.ty;
quote! {
<#ty as ::godot::register::property::Var>::property_hint()
<#ty as ::godot::register::property::Var>::var_hint()
}
}
Data::Enum { variants, via } => {
Expand Down
2 changes: 1 addition & 1 deletion itest/rust/src/object_tests/property_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl Var for SomeCStyleEnum {
}

impl Export for SomeCStyleEnum {
fn default_export_info() -> PropertyHintInfo {
fn export_hint() -> PropertyHintInfo {
PropertyHintInfo {
hint: PropertyHint::ENUM,
hint_string: "A,B,C".into(),
Expand Down

0 comments on commit ff3afd6

Please sign in to comment.