-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Waridley/feature/builtin_types
Implement more builtin types
- Loading branch information
Showing
9 changed files
with
404 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use crate::{FromVariant, Variant}; | ||
use gdext_sys as sys; | ||
use std::marker::PhantomData; | ||
use sys::{ffi_methods, interface_fn, types::*, GodotFfi}; | ||
|
||
impl_builtin_stub!(Array, OpaqueArray); | ||
impl_builtin_stub!(ByteArray, OpaquePackedByteArray); | ||
impl_builtin_stub!(ColorArray, OpaquePackedColorArray); | ||
impl_builtin_stub!(Float32Array, OpaquePackedFloat32Array); | ||
impl_builtin_stub!(Float64Array, OpaquePackedFloat64Array); | ||
impl_builtin_stub!(Int32Array, OpaquePackedInt32Array); | ||
impl_builtin_stub!(Int64Array, OpaquePackedInt64Array); | ||
impl_builtin_stub!(StringArray, OpaquePackedStringArray); | ||
impl_builtin_stub!(Vector2Array, OpaquePackedVector2Array); | ||
impl_builtin_stub!(Vector3Array, OpaquePackedVector3Array); | ||
|
||
impl_builtin_froms!(Array; | ||
ByteArray => array_from_packed_byte_array, | ||
ColorArray => array_from_packed_color_array, | ||
Float32Array => array_from_packed_float32_array, | ||
Float64Array => array_from_packed_float64_array, | ||
Int32Array => array_from_packed_int32_array, | ||
Int64Array => array_from_packed_int64_array, | ||
StringArray => array_from_packed_string_array, | ||
Vector2Array => array_from_packed_vector2_array, | ||
Vector3Array => array_from_packed_vector3_array, | ||
); | ||
|
||
impl_builtin_froms!(ByteArray; Array => packed_byte_array_from_array); | ||
impl_builtin_froms!(ColorArray; Array => packed_color_array_from_array); | ||
impl_builtin_froms!(Float32Array; Array => packed_float32_array_from_array); | ||
impl_builtin_froms!(Float64Array; Array => packed_float64_array_from_array); | ||
impl_builtin_froms!(Int32Array; Array => packed_int32_array_from_array); | ||
impl_builtin_froms!(Int64Array; Array => packed_int64_array_from_array); | ||
impl_builtin_froms!(StringArray; Array => packed_string_array_from_array); | ||
impl_builtin_froms!(Vector2Array; Array => packed_vector2_array_from_array); | ||
impl_builtin_froms!(Vector3Array; Array => packed_vector3_array_from_array); | ||
|
||
impl Array { | ||
pub fn get(&self, index: i64) -> Option<Variant> { | ||
unsafe { | ||
let ptr = (interface_fn!(array_operator_index))(self.sys(), index) as *mut Variant; | ||
if ptr.is_null() { | ||
return None; | ||
} | ||
Some((*ptr).clone()) | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct TypedArray<T> { | ||
opaque: OpaqueArray, | ||
_phantom: PhantomData<T>, | ||
} | ||
impl<T> TypedArray<T> { | ||
fn from_opaque(opaque: OpaqueArray) -> Self { | ||
Self { | ||
opaque, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<T> Clone for TypedArray<T> { | ||
fn clone(&self) -> Self { | ||
unsafe { | ||
Self::from_sys_init(|opaque_ptr| { | ||
let ctor = sys::method_table().array_construct_copy; | ||
ctor(opaque_ptr, &self.sys() as *const sys::GDNativeTypePtr); | ||
}) | ||
} | ||
} | ||
} | ||
impl<T> GodotFfi for TypedArray<T> { | ||
ffi_methods! { type sys::GDNativeTypePtr = *mut Opaque; .. } | ||
} | ||
impl<T> Drop for TypedArray<T> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
let destructor = sys::method_table().array_destroy; | ||
destructor(self.sys_mut()); | ||
} | ||
} | ||
} | ||
|
||
impl<T: FromVariant> TypedArray<T> { | ||
pub fn get(&self, index: i64) -> Option<T> { | ||
unsafe { | ||
let ptr = (interface_fn!(array_operator_index))(self.sys(), index); | ||
let v = Variant::from_var_sys(ptr); | ||
T::try_from_variant(&v).ok() | ||
} | ||
} | ||
} |
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
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,63 @@ | ||
/* | ||
* 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 gdext_sys as sys; | ||
use sys::{ffi_methods, real, GodotFfi}; | ||
|
||
//#[cfg(not(feature = "real_is_double"))] | ||
type Inner = glam::f32::Vec4; | ||
// #[cfg(feature = "real_is_double")] | ||
// type Inner = glam::f64::DVec4; | ||
|
||
#[derive(Default, Copy, Clone, Debug, PartialEq)] | ||
#[repr(C)] | ||
pub struct Vector4 { | ||
inner: Inner, | ||
} | ||
|
||
impl Vector4 { | ||
pub fn new(x: real, y: real, z: real, w: real) -> Self { | ||
Self { | ||
inner: Inner::new(x, y, z, w), | ||
} | ||
} | ||
} | ||
|
||
impl GodotFfi for Vector4 { | ||
ffi_methods! { type sys::GDNativeTypePtr = *mut Self; .. } | ||
} | ||
|
||
impl std::fmt::Display for Vector4 { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.inner.fmt(f) | ||
} | ||
} | ||
|
||
type IInner = glam::IVec4; | ||
|
||
#[derive(Default, Copy, Clone, Debug)] | ||
#[repr(C)] | ||
pub struct Vector4i { | ||
inner: IInner, | ||
} | ||
|
||
impl Vector4i { | ||
pub fn new(x: i32, y: i32, z: i32, w: i32) -> Self { | ||
Self { | ||
inner: IInner::new(x, y, z, w), | ||
} | ||
} | ||
} | ||
|
||
impl GodotFfi for Vector4i { | ||
ffi_methods! { type sys::GDNativeTypePtr = *mut Self; .. } | ||
} | ||
|
||
impl std::fmt::Display for Vector4i { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.inner.fmt(f) | ||
} | ||
} |
Oops, something went wrong.