Skip to content

Commit

Permalink
Implement more builtin types
Browse files Browse the repository at this point in the history
  • Loading branch information
Waridley committed Oct 13, 2022
1 parent f18e658 commit b63a03c
Show file tree
Hide file tree
Showing 9 changed files with 592 additions and 234 deletions.
139 changes: 139 additions & 0 deletions gdext-builtin/src/arrays.rs
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
// }
// }
4 changes: 4 additions & 0 deletions gdext-builtin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@

pub mod macros;

mod arrays;
mod color;
mod others;
mod string;
mod variant;
mod vector2;
mod vector3;
mod vector4;

pub use arrays::*;
pub use color::*;
pub use others::*;
pub use string::*;
pub use variant::*;
pub use vector2::*;
pub use vector3::*;
pub use vector4::*;

pub use glam;

Expand Down
Loading

0 comments on commit b63a03c

Please sign in to comment.