|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +use crate::builtin::meta::{ClassName, PropertyInfo}; |
| 8 | +use crate::builtin::{GodotString, StringName}; |
| 9 | +use crate::engine::global::{PropertyHint, PropertyUsageFlags}; |
| 10 | +use crate::obj::GodotClass; |
| 11 | + |
| 12 | +use godot_ffi as sys; |
| 13 | +use sys::VariantType; |
| 14 | + |
| 15 | +/// Trait implemented for types that can be used as `#[export]` fields. This creates a copy of the |
| 16 | +/// value, for some type-specific definition of "copy". For example, `Array` and `Gd` are returned |
| 17 | +/// via `Share::share()` instead of copying the actual data. |
| 18 | +pub trait Export { |
| 19 | + /// Creates a copy to be returned from a getter. |
| 20 | + fn export(&self) -> Self; |
| 21 | + |
| 22 | + /// The export info to use for an exported field of this type, if no other export info is specified. |
| 23 | + fn default_export_info() -> ExportInfo; |
| 24 | +} |
| 25 | + |
| 26 | +/// Info needed for godot to understand how to export a type to the editor. |
| 27 | +#[derive(Clone, Eq, PartialEq, Debug)] |
| 28 | +pub struct ExportInfo { |
| 29 | + pub variant_type: VariantType, |
| 30 | + pub hint: PropertyHint, |
| 31 | + pub hint_string: GodotString, |
| 32 | +} |
| 33 | + |
| 34 | +impl ExportInfo { |
| 35 | + /// Create a new `ExportInfo` with a property hint of |
| 36 | + /// [`PROPERTY_HINT_NONE`](PropertyHint::PROPERTY_HINT_NONE). |
| 37 | + pub fn with_hint_none(variant_type: VariantType) -> Self { |
| 38 | + Self { |
| 39 | + variant_type, |
| 40 | + hint: PropertyHint::PROPERTY_HINT_NONE, |
| 41 | + hint_string: GodotString::new(), |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// Create a `PropertyInfo` from this export info, using the given property_name and usage, as well as the class name of `C`. |
| 46 | + pub fn to_property_info<C: GodotClass>( |
| 47 | + self, |
| 48 | + property_name: StringName, |
| 49 | + usage: PropertyUsageFlags, |
| 50 | + ) -> PropertyInfo { |
| 51 | + let Self { |
| 52 | + variant_type, |
| 53 | + hint, |
| 54 | + hint_string, |
| 55 | + } = self; |
| 56 | + |
| 57 | + PropertyInfo { |
| 58 | + variant_type, |
| 59 | + class_name: ClassName::of::<C>(), |
| 60 | + property_name, |
| 61 | + hint, |
| 62 | + hint_string, |
| 63 | + usage, |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<T: Export> Export for Option<T> { |
| 69 | + fn export(&self) -> Self { |
| 70 | + self.as_ref().map(Export::export) |
| 71 | + } |
| 72 | + |
| 73 | + fn default_export_info() -> ExportInfo { |
| 74 | + T::default_export_info() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Trait for types that can be represented as a type string for use with |
| 79 | +/// [`PropertyHint::PROPERTY_HINT_TYPE_STRING`]. |
| 80 | +pub trait TypeStringHint { |
| 81 | + /// Returns the representation of this type as a type string. |
| 82 | + /// |
| 83 | + /// See [`PropertyHint.PROPERTY_HINT_TYPE_STRING`]( |
| 84 | + /// https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#enum-globalscope-propertyhint |
| 85 | + /// ). |
| 86 | + fn type_string() -> String; |
| 87 | +} |
| 88 | + |
| 89 | +mod export_impls { |
| 90 | + use super::*; |
| 91 | + use crate::builtin::meta::VariantMetadata; |
| 92 | + use crate::builtin::*; |
| 93 | + |
| 94 | + macro_rules! impl_export_by_clone { |
| 95 | + ($Ty:ty => $variant_type:ident) => { |
| 96 | + impl Export for $Ty { |
| 97 | + fn export(&self) -> Self { |
| 98 | + // If `Self` does not implement `Clone`, this gives a clearer error message |
| 99 | + // than simply `self.clone()`. |
| 100 | + Clone::clone(self) |
| 101 | + } |
| 102 | + |
| 103 | + fn default_export_info() -> ExportInfo { |
| 104 | + ExportInfo::with_hint_none(Self::variant_type()) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + impl TypeStringHint for $Ty { |
| 109 | + fn type_string() -> String { |
| 110 | + format!("{}:", sys::VariantType::$variant_type as i32) |
| 111 | + } |
| 112 | + } |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + impl_export_by_clone!(Aabb => Aabb); |
| 117 | + impl_export_by_clone!(bool => Bool); |
| 118 | + impl_export_by_clone!(Basis => Basis); |
| 119 | + impl_export_by_clone!(Vector2 => Vector2); |
| 120 | + impl_export_by_clone!(Vector3 => Vector3); |
| 121 | + impl_export_by_clone!(Vector4 => Vector4); |
| 122 | + impl_export_by_clone!(Vector2i => Vector2i); |
| 123 | + impl_export_by_clone!(Vector3i => Vector3i); |
| 124 | + impl_export_by_clone!(Quaternion => Quaternion); |
| 125 | + impl_export_by_clone!(Color => Color); |
| 126 | + impl_export_by_clone!(GodotString => String); |
| 127 | + impl_export_by_clone!(StringName => StringName); |
| 128 | + impl_export_by_clone!(NodePath => NodePath); |
| 129 | + impl_export_by_clone!(PackedByteArray => PackedByteArray); |
| 130 | + impl_export_by_clone!(PackedInt32Array => PackedInt32Array); |
| 131 | + impl_export_by_clone!(PackedInt64Array => PackedInt64Array); |
| 132 | + impl_export_by_clone!(PackedFloat32Array => PackedFloat32Array); |
| 133 | + impl_export_by_clone!(PackedFloat64Array => PackedFloat64Array); |
| 134 | + impl_export_by_clone!(PackedStringArray => PackedStringArray); |
| 135 | + impl_export_by_clone!(PackedVector2Array => PackedVector2Array); |
| 136 | + impl_export_by_clone!(PackedVector3Array => PackedVector3Array); |
| 137 | + impl_export_by_clone!(PackedColorArray => PackedColorArray); |
| 138 | + impl_export_by_clone!(Plane => Plane); |
| 139 | + impl_export_by_clone!(Projection => Projection); |
| 140 | + impl_export_by_clone!(Rid => Rid); |
| 141 | + impl_export_by_clone!(Rect2 => Rect2); |
| 142 | + impl_export_by_clone!(Rect2i => Rect2i); |
| 143 | + impl_export_by_clone!(Transform2D => Transform2D); |
| 144 | + impl_export_by_clone!(Transform3D => Transform3D); |
| 145 | + impl_export_by_clone!(f64 => Float); |
| 146 | + impl_export_by_clone!(i64 => Int); |
| 147 | + |
| 148 | + // Godot uses f64 internally for floats, and if Godot tries to pass an invalid f32 into a rust property |
| 149 | + // then the property will just round the value or become inf. |
| 150 | + impl_export_by_clone!(f32 => Float); |
| 151 | + |
| 152 | + // Godot uses i64 internally for integers, and if Godot tries to pass an invalid integer into a property |
| 153 | + // accepting one of the below values then rust will panic. In the editor this will appear as the property |
| 154 | + // failing to be set to a value and an error printed in the console. During runtime this will crash the |
| 155 | + // program and print the panic from rust stating that the property cannot store the value. |
| 156 | + impl_export_by_clone!(i32 => Int); |
| 157 | + impl_export_by_clone!(i16 => Int); |
| 158 | + impl_export_by_clone!(i8 => Int); |
| 159 | + impl_export_by_clone!(u32 => Int); |
| 160 | + impl_export_by_clone!(u16 => Int); |
| 161 | + impl_export_by_clone!(u8 => Int); |
| 162 | + |
| 163 | + // Callables can be exported, however you can't do anything with them in the editor. |
| 164 | + // But we do need to be able to export them since we can't make something a property without exporting. |
| 165 | + // And it should be possible to access Callables by property from for instance GDScript. |
| 166 | + // TODO: |
| 167 | + // Remove export impl when we can create properties without exporting them. |
| 168 | + impl_export_by_clone!(Callable => Callable); |
| 169 | + |
| 170 | + // impl_export_by_clone!(Signal => Signal); |
| 171 | +} |
0 commit comments