Skip to content

Commit

Permalink
Merge pull request #460 from lilizoey/fix/type-string-hint-impls
Browse files Browse the repository at this point in the history
Add missing `TypeStringHint` impls for all relevant types
  • Loading branch information
Bromeon committed Oct 27, 2023
2 parents 43f5d2f + 43439ac commit c9c2793
Show file tree
Hide file tree
Showing 21 changed files with 529 additions and 96 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

# These are supported funding model platforms

# Up to 4 GitHub Sponsors usernames
Expand Down
32 changes: 27 additions & 5 deletions godot-core/src/builtin/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use godot_ffi as sys;

use crate::builtin::*;
use crate::obj::Share;
use crate::property::{Export, ExportInfo, Property, TypeStringHint};
use crate::property::{Export, Property, PropertyHintInfo, TypeStringHint};
use std::fmt;
use std::marker::PhantomData;
use sys::{ffi_methods, interface_fn, GodotFfi};
Expand Down Expand Up @@ -682,6 +682,12 @@ impl<T: GodotType + TypeStringHint> TypeStringHint for Array<T> {
}
}

impl TypeStringHint for VariantArray {
fn type_string() -> String {
format!("{}:Array", VariantType::Array as i32)
}
}

impl<T: GodotType> Property for Array<T> {
type Intermediate = Self;

Expand All @@ -692,20 +698,32 @@ impl<T: GodotType> Property for Array<T> {
fn set_property(&mut self, value: Self::Intermediate) {
*self = value;
}

#[cfg(since_api = "4.2")]
fn property_hint() -> PropertyHintInfo {
if T::Ffi::variant_type() == VariantType::Nil {
return PropertyHintInfo::with_hint_none("");
}

PropertyHintInfo {
hint: crate::engine::global::PropertyHint::PROPERTY_HINT_ARRAY_TYPE,
hint_string: T::godot_type_name().into(),
}
}
}

impl<T: GodotType + TypeStringHint> Export for Array<T> {
fn default_export_info() -> ExportInfo {
ExportInfo {
fn default_export_info() -> PropertyHintInfo {
PropertyHintInfo {
hint: crate::engine::global::PropertyHint::PROPERTY_HINT_TYPE_STRING,
hint_string: T::type_string().into(),
}
}
}

impl Export for Array<Variant> {
fn default_export_info() -> ExportInfo {
ExportInfo::with_hint_none()
fn default_export_info() -> PropertyHintInfo {
PropertyHintInfo::with_hint_none("Array")
}
}

Expand Down Expand Up @@ -747,6 +765,10 @@ impl<T: GodotType> GodotType for Array<T> {
fn try_from_ffi(ffi: Self::Ffi) -> Option<Self> {
Some(ffi)
}

fn godot_type_name() -> String {
"Array".into()
}
}

impl<T: GodotType> GodotFfiVariant for Array<T> {
Expand Down
14 changes: 10 additions & 4 deletions godot-core/src/builtin/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use godot_ffi as sys;
use crate::builtin::meta::{FromGodot, ToGodot};
use crate::builtin::{inner, Variant};
use crate::obj::Share;
use crate::property::{Export, ExportInfo, Property};
use crate::property::{Export, Property, PropertyHintInfo, TypeStringHint};
use std::fmt;
use std::marker::PhantomData;
use std::ptr::addr_of_mut;
use sys::types::OpaqueDictionary;
use sys::{ffi_methods, interface_fn, AsUninit, GodotFfi};
use sys::{ffi_methods, interface_fn, AsUninit, GodotFfi, VariantType};

use super::meta::impl_godot_as_self;
use super::VariantArray;
Expand Down Expand Up @@ -344,9 +344,15 @@ impl Property for Dictionary {
}
}

impl TypeStringHint for Dictionary {
fn type_string() -> String {
format!("{}:Dictionary", VariantType::Dictionary as i32)
}
}

impl Export for Dictionary {
fn default_export_info() -> ExportInfo {
ExportInfo::with_hint_none()
fn default_export_info() -> PropertyHintInfo {
PropertyHintInfo::with_hint_none("Dictionary")
}
}

Expand Down
8 changes: 8 additions & 0 deletions godot-core/src/builtin/meta/godot_convert/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ macro_rules! impl_godot_scalar {
$param_metadata
}
)?

fn godot_type_name() -> String {
<$Via as GodotType>::godot_type_name()
}
}

impl GodotConvert for $T {
Expand Down Expand Up @@ -158,6 +162,10 @@ macro_rules! impl_godot_scalar {
$param_metadata
}
)?

fn godot_type_name() -> String {
<$Via as GodotType>::godot_type_name()
}
}

impl GodotConvert for $T {
Expand Down
7 changes: 7 additions & 0 deletions godot-core/src/builtin/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ pub trait GodotType: GodotConvert<Via = Self> + ToGodot + FromGodot + sealed::Se
Self::param_metadata(),
))
}

#[doc(hidden)]
fn godot_type_name() -> String;
}

impl<T> GodotType for Option<T>
Expand Down Expand Up @@ -217,6 +220,10 @@ where
fn return_info() -> Option<MethodParamOrReturnInfo> {
T::return_info()
}

fn godot_type_name() -> String {
T::godot_type_name()
}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
Expand Down
36 changes: 29 additions & 7 deletions godot-core/src/builtin/variant/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sys::GodotFfi;
//
// Thus we can use `init` to indicate when it must be initialized in 4.0.
macro_rules! impl_ffi_variant {
($T:ty, $from_fn:ident, $to_fn:ident) => {
($T:ty, $from_fn:ident, $to_fn:ident $(; $godot_type_name:ident)?) => {
impl GodotFfiVariant for $T {
fn ffi_to_variant(&self) -> Variant {
let variant = unsafe {
Expand Down Expand Up @@ -73,6 +73,20 @@ macro_rules! impl_ffi_variant {
fn try_from_ffi(ffi: Self::Ffi) -> Option<Self> {
Some(ffi)
}

impl_ffi_variant!(@godot_type_name $T $(, $godot_type_name)?);
}
};

(@godot_type_name $T:ty) => {
fn godot_type_name() -> String {
stringify!($T).into()
}
};

(@godot_type_name $T:ty, $godot_type_name:ident) => {
fn godot_type_name() -> String {
stringify!($godot_type_name).into()
}
};
}
Expand All @@ -85,7 +99,7 @@ macro_rules! impl_ffi_variant {
mod impls {
use super::*;

impl_ffi_variant!(Aabb, aabb_to_variant, aabb_from_variant);
impl_ffi_variant!(Aabb, aabb_to_variant, aabb_from_variant; AABB);
impl_ffi_variant!(bool, bool_to_variant, bool_from_variant);
impl_ffi_variant!(Basis, basis_to_variant, basis_from_variant);
impl_ffi_variant!(Callable, callable_to_variant, callable_from_variant);
Expand All @@ -94,10 +108,10 @@ mod impls {
impl_ffi_variant!(Vector4, vector4_to_variant, vector4_from_variant);
impl_ffi_variant!(Vector2i, vector2i_to_variant, vector2i_from_variant);
impl_ffi_variant!(Vector3i, vector3i_to_variant, vector3i_from_variant);
impl_ffi_variant!(Vector4i, vector3i_to_variant, vector3i_from_variant);
impl_ffi_variant!(Vector4i, vector4i_to_variant, vector4i_from_variant);
impl_ffi_variant!(Quaternion, quaternion_to_variant, quaternion_from_variant);
impl_ffi_variant!(Color, color_to_variant, color_from_variant);
impl_ffi_variant!(GodotString, string_to_variant, string_from_variant);
impl_ffi_variant!(GodotString, string_to_variant, string_from_variant; String);
impl_ffi_variant!(StringName, string_name_to_variant, string_name_from_variant);
impl_ffi_variant!(NodePath, node_path_to_variant, node_path_from_variant);
impl_ffi_variant!(PackedByteArray, packed_byte_array_to_variant, packed_byte_array_from_variant);
Expand All @@ -111,15 +125,15 @@ mod impls {
impl_ffi_variant!(PackedColorArray, packed_color_array_to_variant, packed_color_array_from_variant);
impl_ffi_variant!(Plane, plane_to_variant, plane_from_variant);
impl_ffi_variant!(Projection, projection_to_variant, projection_from_variant);
impl_ffi_variant!(Rid, rid_to_variant, rid_from_variant);
impl_ffi_variant!(Rid, rid_to_variant, rid_from_variant; RID);
impl_ffi_variant!(Rect2, rect2_to_variant, rect2_from_variant);
impl_ffi_variant!(Rect2i, rect2i_to_variant, rect2i_from_variant);
impl_ffi_variant!(Signal, signal_to_variant, signal_from_variant);
impl_ffi_variant!(Transform2D, transform_2d_to_variant, transform_2d_from_variant);
impl_ffi_variant!(Transform3D, transform_3d_to_variant, transform_3d_from_variant);
impl_ffi_variant!(Dictionary, dictionary_to_variant, dictionary_from_variant);
impl_ffi_variant!(i64, int_to_variant, int_from_variant);
impl_ffi_variant!(f64, float_to_variant, float_from_variant);
impl_ffi_variant!(i64, int_to_variant, int_from_variant; int);
impl_ffi_variant!(f64, float_to_variant, float_from_variant; float);

}

Expand Down Expand Up @@ -151,6 +165,10 @@ impl GodotType for () {
fn try_from_ffi(_: Self::Ffi) -> Option<Self> {
Some(())
}

fn godot_type_name() -> String {
"Variant".into()
}
}

impl GodotFfiVariant for Variant {
Expand Down Expand Up @@ -192,4 +210,8 @@ impl GodotType for Variant {
fn param_metadata() -> sys::GDExtensionClassMethodArgumentMetadata {
sys::GDEXTENSION_METHOD_ARGUMENT_METADATA_INT_IS_INT8
}

fn godot_type_name() -> String {
"Variant".into()
}
}
10 changes: 7 additions & 3 deletions godot-core/src/obj/gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::builtin::meta::{FromGodot, GodotConvert, GodotType, ToGodot};
use crate::builtin::{Callable, StringName};
use crate::obj::{cap, dom, mem, EngineEnum, GdDerefTarget, GodotClass, Inherits, Share};
use crate::obj::{GdMut, GdRef, InstanceId};
use crate::property::{Export, ExportInfo, Property, TypeStringHint};
use crate::property::{Export, Property, PropertyHintInfo, TypeStringHint};
use crate::{callbacks, engine, out};

use super::RawGd;
Expand Down Expand Up @@ -462,6 +462,10 @@ impl<T: GodotClass> GodotType for Gd<T> {
fn class_name() -> crate::builtin::meta::ClassName {
T::class_name()
}

fn godot_type_name() -> String {
T::class_name().to_string()
}
}

impl<T: GodotClass> Clone for Gd<T> {
Expand Down Expand Up @@ -509,7 +513,7 @@ impl<T: GodotClass> Property for Gd<T> {
}

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

ExportInfo { hint, hint_string }
PropertyHintInfo { hint, hint_string }
}
}

Expand Down
4 changes: 4 additions & 0 deletions godot-core/src/obj/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ impl<T: GodotClass> GodotType for RawGd<T> {
fn class_name() -> ClassName {
T::class_name()
}

fn godot_type_name() -> String {
T::class_name().to_string()
}
}

impl<T: GodotClass> GodotFfiVariant for RawGd<T> {
Expand Down
Loading

0 comments on commit c9c2793

Please sign in to comment.