Skip to content

Commit 8ae92ef

Browse files
committed
Refactor vector_macros to consistently generate all vector types
1 parent 27b0054 commit 8ae92ef

File tree

10 files changed

+1557
-508
lines changed

10 files changed

+1557
-508
lines changed

godot-core/src/builtin/math/float.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub trait FloatExt: private::Sealed + Copy {
3232
/// Check if `self` is within [`Self::CMP_EPSILON`] of `0.0`.
3333
fn is_zero_approx(self) -> bool;
3434

35+
/// Returns the floating-point modulus of `self` divided by `pmod`, wrapping equally in positive and negative.
3536
fn fposmod(self, pmod: Self) -> Self;
3637

3738
/// Returns the multiple of `step` that is closest to `self`.

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

Lines changed: 21 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,25 @@ pub struct Vector2 {
3737
pub y: real,
3838
}
3939

40-
impl Vector2 {
41-
/// Vector with all components set to `0.0`.
42-
pub const ZERO: Self = Self::splat(0.0);
43-
44-
/// Vector with all components set to `1.0`.
45-
pub const ONE: Self = Self::splat(1.0);
46-
47-
/// Vector with all components set to `real::INFINITY`.
48-
pub const INF: Self = Self::splat(real::INFINITY);
49-
50-
/// Unit vector in -X direction (right in 2D coordinate system).
51-
pub const LEFT: Self = Self::new(-1.0, 0.0);
52-
53-
/// Unit vector in +X direction (right in 2D coordinate system).
54-
pub const RIGHT: Self = Self::new(1.0, 0.0);
40+
impl_vector_operators!(Vector2, real, (x, y));
5541

56-
/// Unit vector in -Y direction (up in 2D coordinate system).
57-
pub const UP: Self = Self::new(0.0, -1.0);
42+
impl_vector_consts!(Vector2, real);
43+
impl_float_vector_consts!(Vector2);
44+
impl_vector2x_consts!(Vector2, real);
5845

59-
/// Unit vector in +Y direction (down in 2D coordinate system).
60-
pub const DOWN: Self = Self::new(0.0, 1.0);
46+
impl_vector_fns!(Vector2, real, (x, y));
47+
impl_float_vector_fns!(Vector2, (x, y));
48+
impl_vector2x_fns!(Vector2, real);
49+
impl_vector2_vector3_fns!(Vector2, (x, y));
6150

62-
/// Constructs a new `Vector2` from the given `x` and `y`.
63-
pub const fn new(x: real, y: real) -> Self {
64-
Self { x, y }
65-
}
51+
// TODO: remove old impls
52+
impl_float_vector_glam_fns!(Vector2, real);
6653

67-
/// Constructs a new `Vector2` with both components set to `v`.
68-
pub const fn splat(v: real) -> Self {
69-
Self::new(v, v)
70-
}
54+
// TODO: add functions only found in Vector2: angle, angle_to_point, from_angle, orthogonal, rotate
55+
impl Vector2 {}
7156

57+
// TODO: remove old impls
58+
impl Vector2 {
7259
/// Constructs a new `Vector2` from a [`Vector2i`].
7360
pub const fn from_vector2i(v: Vector2i) -> Self {
7461
Self {
@@ -99,14 +86,6 @@ impl Vector2 {
9986
(to - self).angle()
10087
}
10188

102-
pub fn aspect(self) -> real {
103-
self.x / self.y
104-
}
105-
106-
pub fn bounce(self, normal: Self) -> Self {
107-
-self.reflect(normal)
108-
}
109-
11089
pub fn ceil(self) -> Self {
11190
Self::from_glam(self.to_glam().ceil())
11291
}
@@ -131,10 +110,6 @@ impl Vector2 {
131110
(to - self).length()
132111
}
133112

134-
pub fn dot(self, other: Self) -> real {
135-
self.to_glam().dot(other.to_glam())
136-
}
137-
138113
pub fn floor(self) -> Self {
139114
Self::from_glam(self.to_glam().floor())
140115
}
@@ -175,16 +150,6 @@ impl Vector2 {
175150
}
176151
}
177152

178-
pub fn move_toward(self, to: Self, delta: real) -> Self {
179-
let vd = to - self;
180-
let len = vd.length();
181-
if len <= delta || len < real::CMP_EPSILON {
182-
to
183-
} else {
184-
self + vd / len * delta
185-
}
186-
}
187-
188153
pub fn orthogonal(self) -> Self {
189154
Self::new(self.y, -self.x)
190155
}
@@ -193,10 +158,6 @@ impl Vector2 {
193158
Self::from_glam(self.to_glam().project_onto(b.to_glam()))
194159
}
195160

196-
pub fn reflect(self, normal: Self) -> Self {
197-
2.0 * normal * self.dot(normal) - self
198-
}
199-
200161
pub fn round(self) -> Self {
201162
Self::from_glam(self.to_glam().round())
202163
}
@@ -215,10 +176,6 @@ impl Vector2 {
215176
self.rotated(angle * weight) * (result_length / start_length)
216177
}
217178

218-
pub fn slide(self, normal: Self) -> Self {
219-
self - normal * self.dot(normal)
220-
}
221-
222179
/// Returns the result of rotating this vector by `angle` (in radians).
223180
pub fn rotated(self, angle: real) -> Self {
224181
Self::from_glam(RAffine2::from_angle(angle).transform_vector2(self.to_glam()))
@@ -241,12 +198,6 @@ impl fmt::Display for Vector2 {
241198
}
242199
}
243200

244-
impl_common_vector_fns!(Vector2, real);
245-
impl_float_vector_glam_fns!(Vector2, real);
246-
impl_float_vector_component_fns!(Vector2, real, (x, y));
247-
impl_vector_operators!(Vector2, real, (x, y));
248-
impl_from_tuple_for_vector2x!(Vector2, real);
249-
250201
// SAFETY:
251202
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
252203
unsafe impl GodotFfi for Vector2 {
@@ -290,6 +241,13 @@ mod test {
290241
assert_eq_approx!(a.coord_max(b), Vector2::new(1.2, 5.6));
291242
}
292243

244+
#[test]
245+
fn new_aspect() {
246+
let v = Vector2::new(1.0, 2.0);
247+
248+
assert_eq_approx!(v.aspect(), 0.5);
249+
}
250+
293251
#[cfg(feature = "serde")]
294252
#[test]
295253
fn serde_roundtrip() {

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

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
*/
77

88
use godot_ffi as sys;
9-
use std::cmp::Ordering;
109
use sys::{ffi_methods, GodotFfi};
1110

1211
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
1312
use crate::builtin::meta::impl_godot_as_self;
14-
use crate::builtin::{real, RVec2, Vector2, Vector2Axis};
13+
use crate::builtin::{real, RVec2, Vector2};
1514

1615
use std::fmt;
1716

@@ -35,58 +34,21 @@ pub struct Vector2i {
3534
pub y: i32,
3635
}
3736

38-
impl Vector2i {
39-
/// Vector with all components set to `0`.
40-
pub const ZERO: Self = Self::splat(0);
41-
42-
/// Vector with all components set to `1`.
43-
pub const ONE: Self = Self::splat(1);
44-
45-
/// Unit vector in -X direction (right in 2D coordinate system).
46-
pub const LEFT: Self = Self::new(-1, 0);
47-
48-
/// Unit vector in +X direction (right in 2D coordinate system).
49-
pub const RIGHT: Self = Self::new(1, 0);
50-
51-
/// Unit vector in -Y direction (up in 2D coordinate system).
52-
pub const UP: Self = Self::new(0, -1);
53-
54-
/// Unit vector in +Y direction (down in 2D coordinate system).
55-
pub const DOWN: Self = Self::new(0, 1);
56-
57-
/// Constructs a new `Vector2i` from the given `x` and `y`.
58-
pub const fn new(x: i32, y: i32) -> Self {
59-
Self { x, y }
60-
}
61-
62-
/// Aspect ratio: x / y, as a `real` value.
63-
pub fn aspect(self) -> real {
64-
self.x as real / self.y as real
65-
}
37+
impl_vector_operators!(Vector2i, i32, (x, y));
6638

67-
/// Axis of the vector's highest value. [`None`] if components are equal.
68-
pub fn max_axis(self) -> Option<Vector2Axis> {
69-
match self.x.cmp(&self.y) {
70-
Ordering::Less => Some(Vector2Axis::Y),
71-
Ordering::Equal => None,
72-
Ordering::Greater => Some(Vector2Axis::X),
73-
}
74-
}
39+
impl_vector_consts!(Vector2i, i32);
40+
impl_integer_vector_consts!(Vector2i);
41+
impl_vector2x_consts!(Vector2i, i32);
7542

76-
/// Axis of the vector's highest value. [`None`] if components are equal.
77-
pub fn min_axis(self) -> Option<Vector2Axis> {
78-
match self.x.cmp(&self.y) {
79-
Ordering::Less => Some(Vector2Axis::X),
80-
Ordering::Equal => None,
81-
Ordering::Greater => Some(Vector2Axis::Y),
82-
}
83-
}
43+
impl_vector_fns!(Vector2i, i32, (x, y));
44+
impl_vector2x_fns!(Vector2i, i32);
8445

85-
/// Constructs a new `Vector2i` with both components set to `v`.
86-
pub const fn splat(v: i32) -> Self {
87-
Self::new(v, v)
88-
}
46+
// TODO: remove old impls
47+
impl_integer_vector_glam_fns!(Vector2i, real);
48+
impl_integer_vector_component_fns!(Vector2i, real, (x, y));
8949

50+
// TODO: remove old impls
51+
impl Vector2i {
9052
/// Constructs a new `Vector2i` from a [`Vector2`]. The floating point coordinates will be truncated.
9153
pub const fn from_vector2(v: Vector2) -> Self {
9254
Self {
@@ -122,12 +84,6 @@ impl fmt::Display for Vector2i {
12284
}
12385
}
12486

125-
impl_common_vector_fns!(Vector2i, i32);
126-
impl_integer_vector_glam_fns!(Vector2i, real);
127-
impl_integer_vector_component_fns!(Vector2i, real, (x, y));
128-
impl_vector_operators!(Vector2i, i32, (x, y));
129-
impl_from_tuple_for_vector2x!(Vector2i, i32);
130-
13187
// SAFETY:
13288
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
13389
unsafe impl GodotFfi for Vector2i {
@@ -158,6 +114,8 @@ impl GlamConv for Vector2i {
158114

159115
#[cfg(test)]
160116
mod test {
117+
use crate::builtin::Vector2Axis;
118+
161119
use super::*;
162120

163121
#[test]
@@ -179,13 +137,13 @@ mod test {
179137

180138
#[test]
181139
fn axis_min_max() {
182-
assert_eq!(Vector2i::new(10, 5).max_axis(), Some(Vector2Axis::X));
183-
assert_eq!(Vector2i::new(5, 10).max_axis(), Some(Vector2Axis::Y));
140+
assert_eq!(Vector2i::new(10, 5).max_axis(), Vector2Axis::X);
141+
assert_eq!(Vector2i::new(5, 10).max_axis(), Vector2Axis::Y);
184142

185-
assert_eq!(Vector2i::new(-5, 5).min_axis(), Some(Vector2Axis::X));
186-
assert_eq!(Vector2i::new(5, -5).min_axis(), Some(Vector2Axis::Y));
143+
assert_eq!(Vector2i::new(-5, 5).min_axis(), Vector2Axis::X);
144+
assert_eq!(Vector2i::new(5, -5).min_axis(), Vector2Axis::Y);
187145

188-
assert_eq!(Vector2i::new(15, 15).max_axis(), None);
189-
assert_eq!(Vector2i::new(15, 15).min_axis(), None);
146+
assert_eq!(Vector2i::new(15, 15).max_axis(), Vector2Axis::X);
147+
assert_eq!(Vector2i::new(15, 15).min_axis(), Vector2Axis::Y);
190148
}
191149
}

0 commit comments

Comments
 (0)