-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ToTypeString Add support for exporting nodes, resources, and arrays with type info
- Loading branch information
Showing
10 changed files
with
249 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* 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/. | ||
*/ | ||
|
||
use crate::{builtin::GodotString, engine::global::PropertyHint}; | ||
|
||
use godot_ffi as sys; | ||
|
||
/// Trait implemented for types that can be used as `#[export]` fields. This creates a copy of the | ||
/// value, for some type-specific definition of "copy". For example, `Array` and `Gd` are returned | ||
/// via `Share::share()` instead of copying the actual data. | ||
pub trait Export: ExportDefaultInfo { | ||
/// Creates a copy to be returned from a getter. | ||
fn export(&self) -> Self; | ||
} | ||
|
||
/// Trait implemented for types that can be used in an `#[export]` field without specifying a custom `hint` | ||
/// and `hint_string`. | ||
pub trait ExportDefaultInfo { | ||
/// The property hint to use when exporting properties if a custom one isn't specified. | ||
fn default_property_hint() -> PropertyHint { | ||
PropertyHint::PROPERTY_HINT_NONE | ||
} | ||
|
||
/// The property hint string to use when exporting properties if a custom one isn't specified. | ||
fn default_property_hint_string() -> GodotString { | ||
GodotString::new() | ||
} | ||
} | ||
|
||
impl<T: ExportDefaultInfo> ExportDefaultInfo for Option<T> { | ||
fn default_property_hint() -> PropertyHint { | ||
T::default_property_hint() | ||
} | ||
|
||
fn default_property_hint_string() -> GodotString { | ||
T::default_property_hint_string() | ||
} | ||
} | ||
|
||
impl<T: Export> Export for Option<T> { | ||
fn export(&self) -> Self { | ||
self.as_ref().map(|val| val.export()) | ||
} | ||
} | ||
|
||
// Implement ExportDefaultInfo for some various common types so people can use them without specifying a | ||
// hint_type and hint_string. | ||
impl ExportDefaultInfo for () {} | ||
impl ExportDefaultInfo for String {} | ||
impl ExportDefaultInfo for i128 {} | ||
impl ExportDefaultInfo for isize {} | ||
impl ExportDefaultInfo for u64 {} | ||
impl ExportDefaultInfo for u128 {} | ||
impl ExportDefaultInfo for usize {} | ||
|
||
/// Trait for types that can be represented as a type string for use with | ||
/// [`PropertyHint::PROPERTY_HINT_TYPE_STRING`]. | ||
pub trait TypeString { | ||
/// Returns the representation of this type as a type string. | ||
/// | ||
/// See [`PropertyHint.PROPERTY_HINT_TYPE_STRING`]( | ||
/// https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-propertyhint | ||
/// ). | ||
fn type_string() -> String; | ||
} | ||
|
||
mod export_impls { | ||
use super::*; | ||
use crate::builtin::*; | ||
|
||
macro_rules! impl_export_by_clone { | ||
($Ty:ty => $variant_type:ident) => { | ||
impl ExportDefaultInfo for $Ty {} | ||
|
||
impl Export for $Ty { | ||
fn export(&self) -> Self { | ||
// If `Self` does not implement `Clone`, this gives a clearer error message | ||
// than simply `self.clone()`. | ||
Clone::clone(self) | ||
} | ||
} | ||
|
||
impl TypeString for $Ty { | ||
fn type_string() -> String { | ||
format!("{}:", sys::VariantType::$variant_type as i32) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_export_by_clone!(Aabb => Aabb); | ||
impl_export_by_clone!(bool => Bool); | ||
impl_export_by_clone!(Basis => Basis); | ||
impl_export_by_clone!(Vector2 => Vector2); | ||
impl_export_by_clone!(Vector3 => Vector3); | ||
impl_export_by_clone!(Vector4 => Vector4); | ||
impl_export_by_clone!(Vector2i => Vector2i); | ||
impl_export_by_clone!(Vector3i => Vector3i); | ||
impl_export_by_clone!(Quaternion => Quaternion); | ||
impl_export_by_clone!(Color => Color); | ||
impl_export_by_clone!(GodotString => String); | ||
impl_export_by_clone!(StringName => StringName); | ||
impl_export_by_clone!(NodePath => NodePath); | ||
impl_export_by_clone!(PackedByteArray => PackedByteArray); | ||
impl_export_by_clone!(PackedInt32Array => PackedInt32Array); | ||
impl_export_by_clone!(PackedInt64Array => PackedInt64Array); | ||
impl_export_by_clone!(PackedFloat32Array => PackedFloat32Array); | ||
impl_export_by_clone!(PackedFloat64Array => PackedFloat64Array); | ||
impl_export_by_clone!(PackedStringArray => PackedStringArray); | ||
impl_export_by_clone!(PackedVector2Array => PackedVector2Array); | ||
impl_export_by_clone!(PackedVector3Array => PackedVector3Array); | ||
impl_export_by_clone!(PackedColorArray => PackedColorArray); | ||
impl_export_by_clone!(Plane => Plane); | ||
impl_export_by_clone!(Projection => Projection); | ||
impl_export_by_clone!(Rid => Rid); | ||
impl_export_by_clone!(Rect2 => Rect2); | ||
impl_export_by_clone!(Rect2i => Rect2i); | ||
impl_export_by_clone!(Transform2D => Transform2D); | ||
impl_export_by_clone!(Transform3D => Transform3D); | ||
impl_export_by_clone!(f64 => Float); | ||
impl_export_by_clone!(f32 => Float); | ||
impl_export_by_clone!(i64 => Int); | ||
impl_export_by_clone!(i32 => Int); | ||
impl_export_by_clone!(i16 => Int); | ||
impl_export_by_clone!(i8 => Int); | ||
impl_export_by_clone!(u32 => Int); | ||
impl_export_by_clone!(u16 => Int); | ||
impl_export_by_clone!(u8 => Int); | ||
|
||
// Callables can be exported, however you can't do anything with them in the editor. | ||
// But we do need to be able to export them since we can't make something a property without exporting. | ||
// And it should be possible to access Callables by property from for instance GDScript. | ||
impl_export_by_clone!(Callable => Callable); | ||
|
||
// impl_export_by_clone!(Signal => Signal); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.