Skip to content

Commit 4f3d106

Browse files
committed
Make Quaternion work with latest master; new experimental glam pattern
1 parent 7bffee3 commit 4f3d106

File tree

5 files changed

+235
-102
lines changed

5 files changed

+235
-102
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

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ mod array_inner;
8080
mod dictionary_inner;
8181

8282
mod color;
83+
mod glam_helpers;
8384
mod math;
8485
mod node_path;
8586
mod others;

0 commit comments

Comments
 (0)