Skip to content

Commit cb3632e

Browse files
bors[bot]RealAstolfoBromeon
authored
Merge #68
68: PR #4/5 Astolfo feature/builtin-quaternion r=Bromeon a=RealAstolfo Co-Authored-By: Thomas ten Cate <[email protected]> Implemented Quaternion to the best of my current ability to resemble godot's, meant to be merged after #67 Co-authored-by: RealAstolfo <[email protected]> Co-authored-by: Jan Haller <[email protected]>
2 parents cc82849 + 4f3d106 commit cb3632e

File tree

6 files changed

+467
-4
lines changed

6 files changed

+467
-4
lines changed
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
// TODO this is experimental -- do not refactor existing types to this yet
8+
// Need to see if ergonomics are worth the generic complexity.
9+
//
10+
// Nice:
11+
// self.glam2(&with, |a, b| a.dot(b))
12+
// self.glam2(&with, glam::f32::Quat::dot)
13+
//
14+
// Alternative with only conversions:
15+
// self.glam().dot(b.glam())
16+
// GlamType::dot(self.glam(), b.glam())
17+
18+
pub(crate) trait GlamConv {
19+
type Glam: GlamType<Mapped = Self>;
20+
21+
fn to_glam(&self) -> Self::Glam {
22+
Self::Glam::from_front(self)
23+
}
24+
25+
fn glam<F, R>(&self, unary_fn: F) -> R::Mapped
26+
where
27+
R: GlamType,
28+
F: FnOnce(Self::Glam) -> R,
29+
{
30+
let arg = Self::Glam::from_front(self);
31+
let result = unary_fn(arg);
32+
33+
result.to_front()
34+
}
35+
36+
fn glam2<F, P, R>(&self, rhs: &P, binary_fn: F) -> R::Mapped
37+
where
38+
P: GlamConv,
39+
R: GlamType,
40+
F: FnOnce(Self::Glam, P::Glam) -> R,
41+
{
42+
let arg0 = Self::Glam::from_front(self);
43+
let arg1 = P::Glam::from_front(rhs);
44+
45+
let result = binary_fn(arg0, arg1);
46+
result.to_front()
47+
}
48+
}
49+
50+
pub(crate) trait GlamType {
51+
type Mapped;
52+
53+
fn to_front(&self) -> Self::Mapped;
54+
fn from_front(mapped: &Self::Mapped) -> Self;
55+
}
56+
57+
macro_rules! impl_glam_map_self {
58+
($T:ty) => {
59+
impl GlamType for $T {
60+
type Mapped = $T;
61+
62+
fn to_front(&self) -> $T {
63+
*self
64+
}
65+
66+
fn from_front(mapped: &$T) -> Self {
67+
*mapped
68+
}
69+
}
70+
};
71+
}
72+
73+
impl_glam_map_self!(f32);
74+
impl_glam_map_self!((f32, f32, f32));

godot-core/src/builtin/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub use math::*;
4242
pub use node_path::*;
4343
pub use others::*;
4444
pub use packed_array::*;
45+
pub use quaternion::*;
4546
pub use string::*;
4647
pub use string_name::*;
4748
pub use variant::*;
@@ -79,10 +80,12 @@ mod array_inner;
7980
mod dictionary_inner;
8081

8182
mod color;
83+
mod glam_helpers;
8284
mod math;
8385
mod node_path;
8486
mod others;
8587
mod packed_array;
88+
mod quaternion;
8689
mod string;
8790
mod string_name;
8891
mod variant;

godot-core/src/builtin/others.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use sys::{ffi_methods, GodotFfi};
1616
impl_builtin_stub!(Rect2, OpaqueRect2);
1717
impl_builtin_stub!(Rect2i, OpaqueRect2i);
1818
impl_builtin_stub!(Plane, OpaquePlane);
19-
impl_builtin_stub!(Quaternion, OpaqueQuaternion);
2019
impl_builtin_stub!(Aabb, OpaqueAabb);
2120
impl_builtin_stub!(Basis, OpaqueBasis);
2221
impl_builtin_stub!(Transform2D, OpaqueTransform2D);

0 commit comments

Comments
 (0)