-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
592 additions
and
234 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,139 @@ | ||
use crate::Variant; | ||
use gdext_sys as sys; | ||
use std::marker::PhantomData; | ||
use std::ops::{Index, IndexMut}; | ||
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) | ||
} | ||
} | ||
|
||
pub fn get_mut(&mut self, index: i64) -> Option<&mut Variant> { | ||
unsafe { | ||
let ptr = (interface_fn!(array_operator_index))(self.sys(), index) as *mut Variant; | ||
if ptr.is_null() { | ||
return None; | ||
} | ||
Some(&mut *ptr) | ||
} | ||
} | ||
} | ||
|
||
impl Index<i64> for Array { | ||
type Output = Variant; | ||
|
||
fn index(&self, index: i64) -> &Self::Output { | ||
self.get(index).unwrap() // Godot will print error if index is OOB | ||
} | ||
} | ||
|
||
impl IndexMut<i64> for Array { | ||
fn index_mut(&mut self, index: i64) -> &mut Self::Output { | ||
self.get_mut(index).unwrap() // Godot will print error if index is OOB | ||
} | ||
} | ||
|
||
#[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> sys::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> TypedArray<T> { | ||
pub fn get(&self, index: i64) -> Option<T> | ||
where | ||
for<'a> T: From<&'a Variant>, | ||
{ | ||
unsafe { | ||
let ptr = (interface_fn!(array_operator_index))(self.sys(), index); | ||
let v = Variant::from_var_sys(ptr); | ||
let ret = T::from(&v); | ||
Some(ret) | ||
} | ||
} | ||
} | ||
|
||
// // TODO: Is this possible to implement? Needs to return a reference. | ||
// impl<T> Index<i64> for TypedArray<T> { | ||
// type Output = Variant; | ||
// | ||
// fn index(&self, index: i64) -> &Self::Output { | ||
// self.get(index).unwrap() // Godot will print error if index is OOB | ||
// } | ||
// } | ||
// | ||
// impl<T> IndexMut<i64> for TypedArray<T> { | ||
// fn index_mut(&mut self, index: i64) -> &mut Self::Output { | ||
// self.get_mut(index).unwrap() // Godot will print error if index is OOB | ||
// } | ||
// } |
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.