Skip to content

Commit 4d27611

Browse files
hapeniaBromeon
authored andcommitted
Implements swizzle and converts from/to tuples
1 parent 627d408 commit 4d27611

File tree

11 files changed

+283
-161
lines changed

11 files changed

+283
-161
lines changed

godot-core/src/builtin/mod.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ pub use string::*;
5454
pub use transform2d::*;
5555
pub use transform3d::*;
5656
pub use variant::*;
57-
pub use vector2::*;
58-
pub use vector2i::*;
59-
pub use vector3::*;
60-
pub use vector3i::*;
61-
pub use vector4::*;
62-
pub use vector4i::*;
57+
pub use vectors::vector2::*;
58+
pub use vectors::vector2i::*;
59+
pub use vectors::vector3::*;
60+
pub use vectors::vector3i::*;
61+
pub use vectors::vector4::*;
62+
pub use vectors::vector4i::*;
63+
pub use vectors::vector_utils::*;
6364

6465
/// Meta-information about variant types, properties and class names.
6566
pub mod meta;
@@ -79,7 +80,6 @@ pub mod dictionary {
7980

8081
// Modules exporting declarative macros must appear first.
8182
mod macros;
82-
mod vector_macros;
8383

8484
// Rename imports because we re-export a subset of types under same module names.
8585
#[path = "array.rs"]
@@ -105,12 +105,7 @@ mod string;
105105
mod transform2d;
106106
mod transform3d;
107107
mod variant;
108-
mod vector2;
109-
mod vector2i;
110-
mod vector3;
111-
mod vector3i;
112-
mod vector4;
113-
mod vector4i;
108+
mod vectors;
114109

115110
#[doc(hidden)]
116111
pub mod inner {

godot-core/src/builtin/quaternion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
1010

1111
use crate::builtin::glam_helpers::{GlamConv, GlamType};
12-
use crate::builtin::{inner, math::*, vector3::*};
12+
use crate::builtin::{inner, math::*, Vector3};
1313

1414
use super::{real, RQuat};
1515
use super::{Basis, EulerOrder};

godot-core/src/builtin/vectors/mod.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
mod vector_macros;
8+
9+
pub mod vector2;
10+
pub mod vector2i;
11+
pub mod vector3;
12+
pub mod vector3i;
13+
pub mod vector4;
14+
pub mod vector4i;
15+
16+
pub mod vector_utils;
17+
18+
#[cfg(test)]
19+
mod test {
20+
use crate::assert_eq_approx;
21+
use crate::builtin::*;
22+
#[test]
23+
fn test_vector_swizzle() {
24+
// * VectorN swizzle
25+
let vector2 = Vector2::new(1.0, 2.0);
26+
let vector3 = Vector3::new(1.0, 2.0, 3.0);
27+
let vector4 = Vector4::new(1.0, 2.0, 3.0, 4.0);
28+
29+
// VectorN to Vector2
30+
31+
let vc2swiz2: Vector2 = swizzle!(vector2 => y, x);
32+
let vc3swiz2: Vector2 = swizzle!(vector3 => y, x);
33+
let vc4swiz2: Vector2 = swizzle!(vector4 => y, x);
34+
assert_eq_approx!(Vector2::new(2.0, 1.0), vc2swiz2, Vector2::is_equal_approx);
35+
assert_eq_approx!(Vector2::new(2.0, 1.0), vc3swiz2, Vector2::is_equal_approx);
36+
assert_eq_approx!(Vector2::new(2.0, 1.0), vc4swiz2, Vector2::is_equal_approx);
37+
38+
// VectorN to Vector3
39+
40+
let vc2swiz3: Vector3 = swizzle!(vector2 => y, x, x);
41+
let vc3swiz3: Vector3 = swizzle!(vector3 => y, x, z);
42+
let vc4swiz3: Vector3 = swizzle!(vector4 => y, x, z);
43+
assert_eq_approx!(
44+
Vector3::new(2.0, 1.0, 1.0),
45+
vc2swiz3,
46+
Vector3::is_equal_approx
47+
);
48+
assert_eq_approx!(
49+
Vector3::new(2.0, 1.0, 3.0),
50+
vc3swiz3,
51+
Vector3::is_equal_approx
52+
);
53+
assert_eq_approx!(
54+
Vector3::new(2.0, 1.0, 3.0),
55+
vc4swiz3,
56+
Vector3::is_equal_approx
57+
);
58+
59+
// VectorN to Vector4
60+
61+
let vc2swiz4: Vector4 = swizzle!(vector2 => y, x, x, y);
62+
let vc3swiz4: Vector4 = swizzle!(vector3 => y, x, z, y);
63+
let vc4swiz4: Vector4 = swizzle!(vector4 => y, x, z, w);
64+
assert_eq_approx!(
65+
Vector4::new(2.0, 1.0, 1.0, 2.0),
66+
vc2swiz4,
67+
Vector4::is_equal_approx
68+
);
69+
assert_eq_approx!(
70+
Vector4::new(2.0, 1.0, 3.0, 2.0),
71+
vc3swiz4,
72+
Vector4::is_equal_approx
73+
);
74+
assert_eq_approx!(
75+
Vector4::new(2.0, 1.0, 3.0, 4.0),
76+
vc4swiz4,
77+
Vector4::is_equal_approx
78+
);
79+
80+
// * VectorNi swizzle
81+
let vector2i = Vector2i::new(1, 2);
82+
let vector3i = Vector3i::new(1, 2, 3);
83+
let vector4i = Vector4i::new(1, 2, 3, 4);
84+
// VectorNi to Vector2i
85+
assert_eq!(Vector2i::new(2, 1), swizzle!(vector2i => y, x));
86+
assert_eq!(Vector2i::new(2, 1), swizzle!(vector3i => y, x));
87+
assert_eq!(Vector2i::new(2, 1), swizzle!(vector4i => y, x));
88+
// VectorNi to Vector3i
89+
assert_eq!(Vector3i::new(2, 1, 1), swizzle!(vector2i => y, x, x));
90+
assert_eq!(Vector3i::new(2, 1, 3), swizzle!(vector3i => y, x, z));
91+
assert_eq!(Vector3i::new(2, 1, 3), swizzle!(vector4i => y, x, z));
92+
// VectorNi to Vector4i
93+
assert_eq!(Vector4i::new(2, 1, 1, 2), swizzle!(vector2i => y, x, x, y));
94+
assert_eq!(Vector4i::new(2, 1, 3, 2), swizzle!(vector3i => y, x, z, y));
95+
assert_eq!(Vector4i::new(2, 1, 3, 4), swizzle!(vector4i => y, x, z, w));
96+
}
97+
}

godot-core/src/builtin/vector2.rs renamed to godot-core/src/builtin/vectors/vector2.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ use sys::{ffi_methods, GodotFfi};
1212
use crate::builtin::math::*;
1313
use crate::builtin::{inner, Vector2i};
1414

15-
use super::glam_helpers::GlamConv;
16-
use super::glam_helpers::GlamType;
17-
use super::{real, RAffine2, RVec2};
15+
use super::super::glam_helpers::GlamConv;
16+
use super::super::glam_helpers::GlamType;
17+
use super::super::{real, RAffine2, RVec2};
18+
use super::vector_utils::*;
1819

1920
/// Vector used for 2D math using floating point coordinates.
2021
///
@@ -298,6 +299,10 @@ impl Vector2 {
298299
pub fn as_inner(&self) -> inner::InnerVector2 {
299300
inner::InnerVector2::from_outer(self)
300301
}
302+
303+
pub fn coords(&self) -> (real, real) {
304+
(self.x, self.y)
305+
}
301306
}
302307

303308
/// Formats the vector like Godot: `(x, y)`.
@@ -310,29 +315,16 @@ impl fmt::Display for Vector2 {
310315
impl_common_vector_fns!(Vector2, real);
311316
impl_float_vector_fns!(Vector2, real);
312317
impl_vector_operators!(Vector2, real, (x, y));
313-
impl_vector_index!(Vector2, real, (x, y), Vector2Axis, (X, Y));
318+
impl_from_tuple_for_vector2x!(Vector2, real);
314319

315320
// SAFETY:
316321
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
317322
unsafe impl GodotFfi for Vector2 {
318323
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
319324
}
320325

321-
/// Enumerates the axes in a [`Vector2`].
322-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
323-
#[repr(i32)]
324-
pub enum Vector2Axis {
325-
/// The X axis.
326-
X,
327-
328-
/// The Y axis.
329-
Y,
330-
}
331-
332-
// SAFETY:
333-
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
334-
unsafe impl GodotFfi for Vector2Axis {
335-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
326+
impl GlamConv for Vector2 {
327+
type Glam = RVec2;
336328
}
337329

338330
impl GlamType for RVec2 {
@@ -347,10 +339,6 @@ impl GlamType for RVec2 {
347339
}
348340
}
349341

350-
impl GlamConv for Vector2 {
351-
type Glam = RVec2;
352-
}
353-
354342
#[cfg(test)]
355343
mod test {
356344
use crate::assert_eq_approx;

godot-core/src/builtin/vector2i.rs renamed to godot-core/src/builtin/vectors/vector2i.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use std::fmt;
99
use godot_ffi as sys;
1010
use sys::{ffi_methods, GodotFfi};
1111

12+
use crate::builtin::glam_helpers::{GlamConv, GlamType};
13+
use crate::builtin::IVec2;
1214
use crate::builtin::Vector2;
1315

14-
use super::glam_helpers::{GlamConv, GlamType};
15-
use super::IVec2;
16-
1716
/// Vector used for 2D math using integer coordinates.
1817
///
1918
/// 2-element structure that can be used to represent positions in 2D space or any other pair of
@@ -80,6 +79,10 @@ impl Vector2i {
8079
fn to_glam(self) -> glam::IVec2 {
8180
IVec2::new(self.x, self.y)
8281
}
82+
83+
pub fn coords(&self) -> (i32, i32) {
84+
(self.x, self.y)
85+
}
8386
}
8487

8588
/// Formats the vector like Godot: `(x, y)`.
@@ -91,31 +94,14 @@ impl fmt::Display for Vector2i {
9194

9295
impl_common_vector_fns!(Vector2i, i32);
9396
impl_vector_operators!(Vector2i, i32, (x, y));
94-
impl_vector_index!(Vector2i, i32, (x, y), Vector2iAxis, (X, Y));
97+
impl_from_tuple_for_vector2x!(Vector2i, i32);
9598

9699
// SAFETY:
97100
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
98101
unsafe impl GodotFfi for Vector2i {
99102
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
100103
}
101104

102-
/// Enumerates the axes in a [`Vector2i`].
103-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
104-
#[repr(i32)]
105-
pub enum Vector2iAxis {
106-
/// The X axis.
107-
X,
108-
109-
/// The Y axis.
110-
Y,
111-
}
112-
113-
// SAFETY:
114-
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
115-
unsafe impl GodotFfi for Vector2iAxis {
116-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
117-
}
118-
119105
impl GlamType for IVec2 {
120106
type Mapped = Vector2i;
121107

godot-core/src/builtin/vector3.rs renamed to godot-core/src/builtin/vectors/vector3.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use sys::{ffi_methods, GodotFfi};
1313
use crate::builtin::math::*;
1414
use crate::builtin::Vector3i;
1515

16-
use super::glam_helpers::GlamConv;
17-
use super::glam_helpers::GlamType;
18-
use super::{real, Basis, RVec3};
16+
use super::super::glam_helpers::GlamConv;
17+
use super::super::glam_helpers::GlamType;
18+
use super::super::{real, Basis, RVec3};
19+
use super::vector_utils::*;
1920

2021
/// Vector used for 3D math using floating point coordinates.
2122
///
@@ -318,6 +319,10 @@ impl Vector3 {
318319
assert!(axis.is_normalized());
319320
Basis::from_axis_angle(axis, angle) * self
320321
}
322+
323+
pub fn coords(&self) -> (real, real, real) {
324+
(self.x, self.y, self.z)
325+
}
321326
}
322327

323328
/// Formats the vector like Godot: `(x, y, z)`.
@@ -330,35 +335,14 @@ impl fmt::Display for Vector3 {
330335
impl_common_vector_fns!(Vector3, real);
331336
impl_float_vector_fns!(Vector3, real);
332337
impl_vector_operators!(Vector3, real, (x, y, z));
333-
impl_vector_index!(Vector3, real, (x, y, z), Vector3Axis, (X, Y, Z));
338+
impl_from_tuple_for_vector3x!(Vector3, real);
334339

335340
// SAFETY:
336341
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
337342
unsafe impl GodotFfi for Vector3 {
338343
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
339344
}
340345

341-
/// Enumerates the axes in a [`Vector3`].
342-
// TODO auto-generate this, alongside all the other builtin type's enums
343-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
344-
#[repr(i32)]
345-
pub enum Vector3Axis {
346-
/// The X axis.
347-
X,
348-
349-
/// The Y axis.
350-
Y,
351-
352-
/// The Z axis.
353-
Z,
354-
}
355-
356-
// SAFETY:
357-
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
358-
unsafe impl GodotFfi for Vector3Axis {
359-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
360-
}
361-
362346
impl GlamType for RVec3 {
363347
type Mapped = Vector3;
364348

godot-core/src/builtin/vector3i.rs renamed to godot-core/src/builtin/vectors/vector3i.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use sys::{ffi_methods, GodotFfi};
1111

1212
use crate::builtin::Vector3;
1313

14-
use super::glam_helpers::{GlamConv, GlamType};
15-
use super::IVec3;
14+
use super::super::glam_helpers::{GlamConv, GlamType};
15+
use super::super::IVec3;
1616

1717
/// Vector used for 3D math using integer coordinates.
1818
///
@@ -90,6 +90,10 @@ impl Vector3i {
9090
fn to_glam(self) -> IVec3 {
9191
IVec3::new(self.x, self.y, self.z)
9292
}
93+
94+
pub fn coords(&self) -> (i32, i32, i32) {
95+
(self.x, self.y, self.z)
96+
}
9397
}
9498

9599
/// Formats the vector like Godot: `(x, y, z)`.
@@ -101,34 +105,14 @@ impl fmt::Display for Vector3i {
101105

102106
impl_common_vector_fns!(Vector3i, i32);
103107
impl_vector_operators!(Vector3i, i32, (x, y, z));
104-
impl_vector_index!(Vector3i, i32, (x, y, z), Vector3iAxis, (X, Y, Z));
108+
impl_from_tuple_for_vector3x!(Vector3i, i32);
105109

106110
// SAFETY:
107111
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
108112
unsafe impl GodotFfi for Vector3i {
109113
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
110114
}
111115

112-
/// Enumerates the axes in a [`Vector3i`].
113-
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
114-
#[repr(i32)]
115-
pub enum Vector3iAxis {
116-
/// The X axis.
117-
X,
118-
119-
/// The Y axis.
120-
Y,
121-
122-
/// The Z axis.
123-
Z,
124-
}
125-
126-
// SAFETY:
127-
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
128-
unsafe impl GodotFfi for Vector3iAxis {
129-
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
130-
}
131-
132116
impl GlamType for IVec3 {
133117
type Mapped = Vector3i;
134118

0 commit comments

Comments
 (0)