Skip to content

Commit ad2d5f9

Browse files
committed
Vector/Rect2 conversion functions between int and float
Deprecate from_rect2[i], from_vectorX[i] constructors.
1 parent 7d1888b commit ad2d5f9

File tree

9 files changed

+74
-71
lines changed

9 files changed

+74
-71
lines changed

godot-core/src/builtin/rect2.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ impl Rect2 {
6363
}
6464
}
6565

66-
/// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions.
67-
///
68-
/// _Godot equivalent: `Rect2(Rect2i from)`_
66+
#[deprecated = "Moved to `Rect2i::cast_float()`"]
6967
#[inline]
7068
pub const fn from_rect2i(rect: Rect2i) -> Self {
71-
Self {
72-
position: Vector2::from_vector2i(rect.position),
73-
size: Vector2::from_vector2i(rect.size),
69+
rect.cast_float()
70+
}
71+
72+
/// Create a new `Rect2i` from a `Rect2`, using `as` for `real` to `i32` conversions.
73+
///
74+
/// _Godot equivalent: `Rect2i(Rect2 from)`_
75+
#[inline]
76+
pub const fn cast_int(self) -> Rect2i {
77+
Rect2i {
78+
position: self.position.cast_int(),
79+
size: self.size.cast_int(),
7480
}
7581
}
7682

godot-core/src/builtin/rect2i.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ impl Rect2i {
6060
}
6161
}
6262

63-
/// Create a new `Rect2i` from a `Rect2`, using `as` for `real` to `i32` conversions.
64-
///
65-
/// _Godot equivalent: `Rect2i(Rect2 from)`_
66-
#[inline]
67-
pub const fn from_rect2(rect: Rect2) -> Self {
68-
Self {
69-
position: Vector2i::from_vector2(rect.position),
70-
size: Vector2i::from_vector2(rect.size),
71-
}
72-
}
73-
7463
/// Create a new `Rect2i` with the first corner at `position` and the opposite corner at `end`.
7564
#[inline]
7665
pub fn from_corners(position: Vector2i, end: Vector2i) -> Self {
@@ -80,6 +69,23 @@ impl Rect2i {
8069
}
8170
}
8271

72+
#[deprecated = "Moved to `Rect2::cast_int()`"]
73+
#[inline]
74+
pub const fn from_rect2(rect: Rect2) -> Self {
75+
rect.cast_int()
76+
}
77+
78+
/// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions.
79+
///
80+
/// _Godot equivalent: `Rect2(Rect2i from)`_
81+
#[inline]
82+
pub const fn cast_float(self) -> Rect2 {
83+
Rect2 {
84+
position: self.position.cast_float(),
85+
size: self.size.cast_float(),
86+
}
87+
}
88+
8389
/// The end of the `Rect2i` calculated as `position + size`.
8490
///
8591
/// _Godot equivalent: `Rect2i.size` property_

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,10 @@ impl_vector_fns!(Vector2, RVec2, real, (x, y));
6969

7070
/// # Specialized `Vector2` functions
7171
impl Vector2 {
72-
/// Constructs a new `Vector2` from a [`Vector2i`].
72+
#[deprecated = "Moved to `Vector2i::cast_float()`"]
7373
#[inline]
7474
pub const fn from_vector2i(v: Vector2i) -> Self {
75-
Self {
76-
x: v.x as real,
77-
y: v.y as real,
78-
}
75+
v.cast_float()
7976
}
8077

8178
/// Returns this vector's angle with respect to the positive X axis, or `(1.0, 0.0)` vector, in radians.
@@ -164,7 +161,7 @@ impl Vector2 {
164161
}
165162
}
166163

167-
impl_float_vector_fns!(Vector2, (x, y));
164+
impl_float_vector_fns!(Vector2, Vector2i, (x, y));
168165
impl_vector2x_fns!(Vector2, Vector3, real);
169166
impl_vector2_vector3_fns!(Vector2, (x, y));
170167

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@ 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

70-
/// Constructs a new `Vector2i` from a [`Vector2`]. The floating point coordinates will be truncated.
70+
#[deprecated = "Moved to `Vector2::cast_int()`"]
7171
#[inline]
7272
pub const fn from_vector2(v: Vector2) -> Self {
73-
Self {
74-
x: v.x as i32,
75-
y: v.y as i32,
76-
}
73+
v.cast_int()
7774
}
7875

7976
/// Converts `self` to the corresponding [`real`] `glam` type.

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ impl Vector3 {
6767
impl_vector_consts!(real);
6868
impl_float_vector_consts!();
6969
impl_vector3x_consts!(real);
70-
}
71-
72-
impl_vector_fns!(Vector3, RVec3, real, (x, y, z));
7370

74-
/// # Specialized `Vector3` functions
75-
impl Vector3 {
7671
/// Unit vector pointing towards the left side of imported 3D assets.
7772
pub const MODEL_LEFT: Self = Self::new(1.0, 0.0, 0.0);
7873

@@ -90,15 +85,16 @@ impl Vector3 {
9085

9186
/// Unit vector pointing towards the rear side (back) of imported 3D assets.
9287
pub const MODEL_REAR: Self = Self::new(0.0, 0.0, -1.0);
88+
}
9389

94-
/// Constructs a new `Vector3` from a [`Vector3i`].
90+
impl_vector_fns!(Vector3, RVec3, real, (x, y, z));
91+
92+
/// # Specialized `Vector3` functions
93+
impl Vector3 {
94+
#[deprecated = "Moved to `Vector3i::cast_float()`"]
9595
#[inline]
9696
pub const fn from_vector3i(v: Vector3i) -> Self {
97-
Self {
98-
x: v.x as real,
99-
y: v.y as real,
100-
z: v.z as real,
101-
}
97+
v.cast_float()
10298
}
10399

104100
#[doc(hidden)]
@@ -231,7 +227,7 @@ impl Vector3 {
231227
}
232228
}
233229

234-
impl_float_vector_fns!(Vector3, (x, y, z));
230+
impl_float_vector_fns!(Vector3, Vector3i, (x, y, z));
235231
impl_vector3x_fns!(Vector3, Vector2, real);
236232
impl_vector2_vector3_fns!(Vector3, (x, y, z));
237233
impl_vector3_vector4_fns!(Vector3, (x, y, z));

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,13 @@ impl_vector_fns!(Vector3i, glam::IVec3, i32, (x, y, z));
7070

7171
/// # Specialized `Vector3i` functions
7272
impl Vector3i {
73-
/// Constructs a new `Vector3i` from a [`Vector3`]. The floating point coordinates will be truncated.
73+
#[deprecated = "Moved to `Vector3::cast_int()`"]
7474
#[inline]
7575
pub const fn from_vector3(v: Vector3) -> Self {
76-
Self {
77-
x: v.x as i32,
78-
y: v.y as i32,
79-
z: v.z as i32,
80-
}
76+
v.cast_int()
8177
}
8278

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

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

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

+3-8
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,9 @@ impl_vector_fns!(Vector4, RVec4, real, (x, y, z, w));
7272

7373
/// # Specialized `Vector4` functions
7474
impl Vector4 {
75-
/// Constructs a new `Vector4` from a [`Vector4i`][crate::builtin::Vector4i].
75+
#[deprecated = "Moved to `Vector4i::cast_float()`"]
7676
pub const fn from_vector4i(v: Vector4i) -> Self {
77-
Self {
78-
x: v.x as real,
79-
y: v.y as real,
80-
z: v.z as real,
81-
w: v.w as real,
82-
}
77+
v.cast_float()
8378
}
8479

8580
#[doc(hidden)]
@@ -89,7 +84,7 @@ impl Vector4 {
8984
}
9085
}
9186

92-
impl_float_vector_fns!(Vector4, (x, y, z, w));
87+
impl_float_vector_fns!(Vector4, Vector4i, (x, y, z, w));
9388
impl_vector4x_fns!(Vector4, real);
9489
impl_vector3_vector4_fns!(Vector4, (x, y, z, w));
9590

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

+3-9
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,12 @@ 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

76-
/// Constructs a new `Vector4i` from a [`Vector4`]. The floating point coordinates will be
77-
/// truncated.
76+
#[deprecated = "Moved to `Vector4::cast_int()`"]
7877
#[inline]
7978
pub const fn from_vector4(v: Vector4) -> Self {
80-
Self {
81-
x: v.x as i32,
82-
y: v.y as i32,
83-
z: v.z as i32,
84-
w: v.w as i32,
85-
}
79+
v.cast_int()
8680
}
8781

8882
/// Converts `self` to the corresponding [`real`] `glam` type.

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

+22-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
) => {
@@ -542,6 +544,7 @@ macro_rules! inline_impl_integer_vector_fns {
542544
/// On under- or overflow:
543545
/// - If any component of `self` is [`i32::MIN`] while the same component on `step` is `-1`.
544546
/// - If any component of `self` plus half of the same component of `step` is not in range on [`i32`].
547+
#[inline]
545548
pub fn snapped(self, step: Self) -> Self {
546549
use crate::builtin::vectors::vector_macros::snap_one;
547550

@@ -551,20 +554,39 @@ macro_rules! inline_impl_integer_vector_fns {
551554
),*
552555
)
553556
}
557+
558+
/// Converts to a vector with floating-point [`real`](type.real.html) components, using `as` casts.
559+
#[inline]
560+
pub const fn cast_float(self) -> $VectorFloat {
561+
<$VectorFloat>::new( $(self.$comp as real),* )
562+
}
554563
};
555564
}
556565

557566
macro_rules! impl_float_vector_fns {
558567
(
559568
// Name of the vector type.
560569
$Vector:ty,
570+
// Name of the integer-equivalent vector type.
571+
$VectorInt:ty,
561572
// Names of the components, with parentheses, for example `(x, y)`.
562573
($($comp:ident),*)
563574
) => {
564575
/// # Float-specific functions
565576
///
566577
/// The following methods are only available on floating-point vectors.
567578
impl $Vector {
579+
/// Converts to a vector with integer components, using `as` casts.
580+
pub const fn cast_int(self) -> $VectorInt {
581+
<$VectorInt>::new( $(self.$comp as i32),* )
582+
}
583+
584+
/// Returns a new vector with all components rounded down (towards negative infinity).
585+
#[inline]
586+
pub fn floor(self) -> Self {
587+
Self::from_glam(self.to_glam().floor())
588+
}
589+
568590
/// Returns a new vector with all components rounded up (towards positive infinity).
569591
#[inline]
570592
pub fn ceil(self) -> Self {
@@ -649,12 +671,6 @@ macro_rules! impl_float_vector_fns {
649671
self.to_glam().dot(with.to_glam())
650672
}
651673

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-
658674
/// Returns true if each component of this vector is finite.
659675
#[inline]
660676
pub fn is_finite(self) -> bool {

0 commit comments

Comments
 (0)