Skip to content

Commit

Permalink
Merge #68
Browse files Browse the repository at this point in the history
68: PR #4/5 Astolfo feature/builtin-quaternion r=Bromeon a=RealAstolfo

Co-Authored-By: Thomas ten Cate <ttencate@gmail.com>

Implemented Quaternion to the best of my current ability to resemble godot's, meant to be merged after #67 

Co-authored-by: RealAstolfo <astolfo.gman@gmail.com>
Co-authored-by: Jan Haller <bromeon@gmail.com>
  • Loading branch information
3 people committed Feb 12, 2023
2 parents cc82849 + 4f3d106 commit cb3632e
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 4 deletions.
74 changes: 74 additions & 0 deletions godot-core/src/builtin/glam_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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/.
*/

// TODO this is experimental -- do not refactor existing types to this yet
// Need to see if ergonomics are worth the generic complexity.
//
// Nice:
// self.glam2(&with, |a, b| a.dot(b))
// self.glam2(&with, glam::f32::Quat::dot)
//
// Alternative with only conversions:
// self.glam().dot(b.glam())
// GlamType::dot(self.glam(), b.glam())

pub(crate) trait GlamConv {
type Glam: GlamType<Mapped = Self>;

fn to_glam(&self) -> Self::Glam {
Self::Glam::from_front(self)
}

fn glam<F, R>(&self, unary_fn: F) -> R::Mapped
where
R: GlamType,
F: FnOnce(Self::Glam) -> R,
{
let arg = Self::Glam::from_front(self);
let result = unary_fn(arg);

result.to_front()
}

fn glam2<F, P, R>(&self, rhs: &P, binary_fn: F) -> R::Mapped
where
P: GlamConv,
R: GlamType,
F: FnOnce(Self::Glam, P::Glam) -> R,
{
let arg0 = Self::Glam::from_front(self);
let arg1 = P::Glam::from_front(rhs);

let result = binary_fn(arg0, arg1);
result.to_front()
}
}

pub(crate) trait GlamType {
type Mapped;

fn to_front(&self) -> Self::Mapped;
fn from_front(mapped: &Self::Mapped) -> Self;
}

macro_rules! impl_glam_map_self {
($T:ty) => {
impl GlamType for $T {
type Mapped = $T;

fn to_front(&self) -> $T {
*self
}

fn from_front(mapped: &$T) -> Self {
*mapped
}
}
};
}

impl_glam_map_self!(f32);
impl_glam_map_self!((f32, f32, f32));
3 changes: 3 additions & 0 deletions godot-core/src/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub use math::*;
pub use node_path::*;
pub use others::*;
pub use packed_array::*;
pub use quaternion::*;
pub use string::*;
pub use string_name::*;
pub use variant::*;
Expand Down Expand Up @@ -79,10 +80,12 @@ mod array_inner;
mod dictionary_inner;

mod color;
mod glam_helpers;
mod math;
mod node_path;
mod others;
mod packed_array;
mod quaternion;
mod string;
mod string_name;
mod variant;
Expand Down
1 change: 0 additions & 1 deletion godot-core/src/builtin/others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use sys::{ffi_methods, GodotFfi};
impl_builtin_stub!(Rect2, OpaqueRect2);
impl_builtin_stub!(Rect2i, OpaqueRect2i);
impl_builtin_stub!(Plane, OpaquePlane);
impl_builtin_stub!(Quaternion, OpaqueQuaternion);
impl_builtin_stub!(Aabb, OpaqueAabb);
impl_builtin_stub!(Basis, OpaqueBasis);
impl_builtin_stub!(Transform2D, OpaqueTransform2D);
Expand Down
Loading

0 comments on commit cb3632e

Please sign in to comment.