Skip to content

Commit 38809f9

Browse files
committed
Vector conversion functions between int and float
1 parent 7d1888b commit 38809f9

File tree

7 files changed

+26
-12
lines changed

7 files changed

+26
-12
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl Vector2 {
164164
}
165165
}
166166

167-
impl_float_vector_fns!(Vector2, (x, y));
167+
impl_float_vector_fns!(Vector2, Vector2i, (x, y));
168168
impl_vector2x_fns!(Vector2, Vector3, real);
169169
impl_vector2_vector3_fns!(Vector2, (x, y));
170170

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl Vector2i {
6565

6666
/// # Specialized `Vector2i` functions
6767
impl Vector2i {
68-
inline_impl_integer_vector_fns!(x, y);
68+
inline_impl_integer_vector_fns!(Vector2, x, y);
6969

7070
/// Constructs a new `Vector2i` from a [`Vector2`]. The floating point coordinates will be truncated.
7171
#[inline]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl Vector3 {
231231
}
232232
}
233233

234-
impl_float_vector_fns!(Vector3, (x, y, z));
234+
impl_float_vector_fns!(Vector3, Vector3i, (x, y, z));
235235
impl_vector3x_fns!(Vector3, Vector2, real);
236236
impl_vector2_vector3_fns!(Vector3, (x, y, z));
237237
impl_vector3_vector4_fns!(Vector3, (x, y, z));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl Vector3i {
8080
}
8181
}
8282

83-
inline_impl_integer_vector_fns!(x, y, z);
83+
inline_impl_integer_vector_fns!(Vector3, x, y, z);
8484

8585
/// Converts `self` to the corresponding [`real`] `glam` type.
8686
#[doc(hidden)]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Vector4 {
8989
}
9090
}
9191

92-
impl_float_vector_fns!(Vector4, (x, y, z, w));
92+
impl_float_vector_fns!(Vector4, Vector4i, (x, y, z, w));
9393
impl_vector4x_fns!(Vector4, real);
9494
impl_vector3_vector4_fns!(Vector4, (x, y, z, w));
9595

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl_vector_fns!(Vector4i, glam::IVec4, i32, (x, y, z, w));
7171

7272
/// # Specialized `Vector4i` functions
7373
impl Vector4i {
74-
inline_impl_integer_vector_fns!(x, y, z, w);
74+
inline_impl_integer_vector_fns!(Vector4, x, y, z, w);
7575

7676
/// Constructs a new `Vector4i` from a [`Vector4`]. The floating point coordinates will be
7777
/// truncated.

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ pub(super) fn snap_one(mut value: i32, step: i32) -> i32 {
532532
/// Implements functions that are present only on integer vectors.
533533
macro_rules! inline_impl_integer_vector_fns {
534534
(
535+
// Name of the float-equivalent vector type.
536+
$VectorFloat:ty,
535537
// Names of the components, for example `x, y`.
536538
$($comp:ident),*
537539
) => {
@@ -551,20 +553,38 @@ macro_rules! inline_impl_integer_vector_fns {
551553
),*
552554
)
553555
}
556+
557+
/// Converts to a vector with floating-point [`real`](type.real.html) components, using `as` casts.
558+
pub fn to_float_vector(self) -> $VectorFloat {
559+
<$VectorFloat>::new( $(self.$comp as real),* )
560+
}
554561
};
555562
}
556563

557564
macro_rules! impl_float_vector_fns {
558565
(
559566
// Name of the vector type.
560567
$Vector:ty,
568+
// Name of the integer-equivalent vector type.
569+
$VectorInt:ty,
561570
// Names of the components, with parentheses, for example `(x, y)`.
562571
($($comp:ident),*)
563572
) => {
564573
/// # Float-specific functions
565574
///
566575
/// The following methods are only available on floating-point vectors.
567576
impl $Vector {
577+
/// Converts to a vector with integer components, using `as` casts.
578+
pub fn to_int_vector(self) -> $VectorInt {
579+
<$VectorInt>::new( $(self.$comp as i32),* )
580+
}
581+
582+
/// Returns a new vector with all components rounded down (towards negative infinity).
583+
#[inline]
584+
pub fn floor(self) -> Self {
585+
Self::from_glam(self.to_glam().floor())
586+
}
587+
568588
/// Returns a new vector with all components rounded up (towards positive infinity).
569589
#[inline]
570590
pub fn ceil(self) -> Self {
@@ -649,12 +669,6 @@ macro_rules! impl_float_vector_fns {
649669
self.to_glam().dot(with.to_glam())
650670
}
651671

652-
/// Returns a new vector with all components rounded down (towards negative infinity).
653-
#[inline]
654-
pub fn floor(self) -> Self {
655-
Self::from_glam(self.to_glam().floor())
656-
}
657-
658672
/// Returns true if each component of this vector is finite.
659673
#[inline]
660674
pub fn is_finite(self) -> bool {

0 commit comments

Comments
 (0)