Skip to content

Commit 15bc053

Browse files
authored
Merge pull request #824 from godot-rust/feature/vector-conversions
Vector conversion functions
2 parents 71b869f + 4a0a99a commit 15bc053

File tree

9 files changed

+189
-84
lines changed

9 files changed

+189
-84
lines changed

godot-core/src/builtin/rect2.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use crate::builtin::{real, Rect2i, Side, Vector2};
2323
/// | 2D | **`Rect2`** | [`Rect2i`] |
2424
/// | 3D | [`Aabb`] | |
2525
///
26+
/// <br>You can convert to `Rect2i` using [`cast_int()`][Self::cast_int].
27+
///
2628
/// [`Aabb`]: crate::builtin::Aabb
2729
///
2830
/// # Godot docs
@@ -63,14 +65,20 @@ impl Rect2 {
6365
}
6466
}
6567

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

godot-core/src/builtin/rect2i.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use sys::{ffi_methods, GodotFfi};
2424
/// | 2D | [`Rect2`] | **`Rect2i`** |
2525
/// | 3D | [`Aabb`] | |
2626
///
27+
/// <br>You can convert to `Rect2` using [`cast_float()`][Self::cast_float].
28+
///
2729
/// [`Aabb`]: crate::builtin::Aabb
2830
///
2931
/// # Godot docs
@@ -60,17 +62,6 @@ impl Rect2i {
6062
}
6163
}
6264

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-
7465
/// Create a new `Rect2i` with the first corner at `position` and the opposite corner at `end`.
7566
#[inline]
7667
pub fn from_corners(position: Vector2i, end: Vector2i) -> Self {
@@ -80,6 +71,23 @@ impl Rect2i {
8071
}
8172
}
8273

74+
#[deprecated = "Moved to `Rect2::cast_int()`"]
75+
#[inline]
76+
pub const fn from_rect2(rect: Rect2) -> Self {
77+
rect.cast_int()
78+
}
79+
80+
/// Create a new `Rect2` from a `Rect2i`, using `as` for `i32` to `real` conversions.
81+
///
82+
/// _Godot equivalent: `Rect2(Rect2i from)`_
83+
#[inline]
84+
pub const fn cast_float(self) -> Rect2 {
85+
Rect2 {
86+
position: self.position.cast_float(),
87+
size: self.size.cast_float(),
88+
}
89+
}
90+
8391
/// The end of the `Rect2i` calculated as `position + size`.
8492
///
8593
/// _Godot equivalent: `Rect2i.size` property_
@@ -312,7 +320,7 @@ mod test {
312320
let zero = Rect2i::default();
313321
let new = Rect2i::new(Vector2i::new(0, 100), Vector2i::new(1280, 720));
314322
let from_components = Rect2i::from_components(0, 100, 1280, 720);
315-
let from_rect2 = Rect2i::from_rect2(Rect2::from_components(0.1, 100.3, 1280.1, 720.42));
323+
let from_rect2 = Rect2::from_components(0.1, 100.3, 1280.1, 720.42).cast_int();
316324
let from_corners = Rect2i::from_corners(Vector2i::new(0, 100), Vector2i::new(1280, 820));
317325

318326
assert_eq!(zero.position.x, 0);

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use sys::{ffi_methods, GodotFfi};
1111

1212
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
1313
use crate::builtin::vectors::Vector2Axis;
14-
use crate::builtin::{inner, real, RAffine2, RVec2, Vector2i};
14+
use crate::builtin::{inner, real, RAffine2, RVec2, Vector2i, Vector3};
1515

1616
use std::fmt;
1717

@@ -24,6 +24,8 @@ use std::fmt;
2424
/// is always 64-bit. The engine can be compiled with the option `precision=double` to use 64-bit
2525
/// vectors; use the gdext library with the `double-precision` feature in that case.
2626
///
27+
#[doc = shared_vector_docs!()]
28+
///
2729
/// ### Navigation to `impl` blocks within this page
2830
///
2931
/// - [Constants](#constants)
@@ -42,6 +44,8 @@ use std::fmt;
4244
/// | 3D | [`Vector3`][crate::builtin::Vector3] | [`Vector3i`][crate::builtin::Vector3i] |
4345
/// | 4D | [`Vector4`][crate::builtin::Vector4] | [`Vector4i`][crate::builtin::Vector4i] |
4446
///
47+
/// <br>You can convert to 3D vectors using [`to_3d(z)`][Self::to_3d], and to `Vector2i` using [`cast_int()`][Self::cast_int].
48+
///
4549
/// # Godot docs
4650
///
4751
/// [`Vector2` (stable)](https://docs.godotengine.org/en/stable/classes/class_vector2.html)
@@ -67,13 +71,10 @@ impl_vector_fns!(Vector2, RVec2, real, (x, y));
6771

6872
/// # Specialized `Vector2` functions
6973
impl Vector2 {
70-
/// Constructs a new `Vector2` from a [`Vector2i`].
74+
#[deprecated = "Moved to `Vector2i::cast_float()`"]
7175
#[inline]
7276
pub const fn from_vector2i(v: Vector2i) -> Self {
73-
Self {
74-
x: v.x as real,
75-
y: v.y as real,
76-
}
77+
v.cast_float()
7778
}
7879

7980
/// Returns this vector's angle with respect to the positive X axis, or `(1.0, 0.0)` vector, in radians.
@@ -162,8 +163,8 @@ impl Vector2 {
162163
}
163164
}
164165

165-
impl_float_vector_fns!(Vector2, (x, y));
166-
impl_vector2x_fns!(Vector2, real);
166+
impl_float_vector_fns!(Vector2, Vector2i, (x, y));
167+
impl_vector2x_fns!(Vector2, Vector3, real);
167168
impl_vector2_vector3_fns!(Vector2, (x, y));
168169

169170
impl_vector_operators!(Vector2, real, (x, y));

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::cmp::Ordering;
1010
use sys::{ffi_methods, GodotFfi};
1111

1212
use crate::builtin::math::{GlamConv, GlamType};
13-
use crate::builtin::{inner, real, RVec2, Vector2, Vector2Axis};
13+
use crate::builtin::{inner, real, RVec2, Vector2, Vector2Axis, Vector3i};
1414

1515
use std::fmt;
1616

@@ -19,11 +19,13 @@ use std::fmt;
1919
/// 2-element structure that can be used to represent discrete positions or directions in 2D space,
2020
/// as well as any other pair of numeric values.
2121
///
22-
/// It uses integer coordinates and is therefore preferable to [`Vector2`] when exact precision is
22+
/// `Vector2i` uses integer coordinates and is therefore preferable to [`Vector2`] when exact precision is
2323
/// required. Note that the values are limited to 32 bits, and unlike `Vector2` this cannot be
2424
/// configured with an engine build option. Use `i64` or [`PackedInt64Array`][crate::builtin::PackedInt64Array]
2525
/// if 64-bit values are needed.
2626
///
27+
#[doc = shared_vector_docs!()]
28+
///
2729
/// ### Navigation to `impl` blocks within this page
2830
///
2931
/// - [Constants](#constants)
@@ -40,6 +42,8 @@ use std::fmt;
4042
/// | 3D | [`Vector3`][crate::builtin::Vector3] | [`Vector3i`][crate::builtin::Vector3i] |
4143
/// | 4D | [`Vector4`][crate::builtin::Vector4] | [`Vector4i`][crate::builtin::Vector4i] |
4244
///
45+
/// <br>You can convert to 3D vectors using [`to_3d(z)`][Self::to_3d], and to `Vector2` using [`cast_float()`][Self::cast_float].
46+
///
4347
/// # Godot docs
4448
///
4549
/// [`Vector2i` (stable)](https://docs.godotengine.org/en/stable/classes/class_vector2i.html)
@@ -63,15 +67,12 @@ impl Vector2i {
6367

6468
/// # Specialized `Vector2i` functions
6569
impl Vector2i {
66-
inline_impl_integer_vector_fns!(x, y);
70+
inline_impl_integer_vector_fns!(Vector2, x, y);
6771

68-
/// Constructs a new `Vector2i` from a [`Vector2`]. The floating point coordinates will be truncated.
72+
#[deprecated = "Moved to `Vector2::cast_int()`"]
6973
#[inline]
7074
pub const fn from_vector2(v: Vector2) -> Self {
71-
Self {
72-
x: v.x as i32,
73-
y: v.y as i32,
74-
}
75+
v.cast_int()
7576
}
7677

7778
/// Converts `self` to the corresponding [`real`] `glam` type.
@@ -89,7 +90,7 @@ impl Vector2i {
8990
}
9091

9192
impl_vector_fns!(Vector2i, glam::IVec2, i32, (x, y));
92-
impl_vector2x_fns!(Vector2i, i32);
93+
impl_vector2x_fns!(Vector2i, Vector3i, i32);
9394

9495
impl_vector_operators!(Vector2i, i32, (x, y));
9596

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use std::fmt;
2424
/// is always 64-bit. The engine can be compiled with the option `precision=double` to use 64-bit
2525
/// vectors instead; use the gdext library with the `double-precision` feature in that case.
2626
///
27+
#[doc = shared_vector_docs!()]
28+
///
2729
/// ### Navigation to `impl` blocks within this page
2830
///
2931
/// - [Constants](#constants)
@@ -43,6 +45,8 @@ use std::fmt;
4345
/// | 3D | **`Vector3`** | [`Vector3i`][crate::builtin::Vector3i] |
4446
/// | 4D | [`Vector4`][crate::builtin::Vector4] | [`Vector4i`][crate::builtin::Vector4i] |
4547
///
48+
/// <br>You can convert to 2D vectors using [`to_2d()`][Self::to_2d], and to `Vector3i` using [`cast_int()`][Self::cast_int].
49+
///
4650
/// # Godot docs
4751
///
4852
/// [`Vector3` (stable)](https://docs.godotengine.org/en/stable/classes/class_vector3.html)
@@ -65,12 +69,7 @@ impl Vector3 {
6569
impl_vector_consts!(real);
6670
impl_float_vector_consts!();
6771
impl_vector3x_consts!(real);
68-
}
6972

70-
impl_vector_fns!(Vector3, RVec3, real, (x, y, z));
71-
72-
/// # Specialized `Vector3` functions
73-
impl Vector3 {
7473
/// Unit vector pointing towards the left side of imported 3D assets.
7574
pub const MODEL_LEFT: Self = Self::new(1.0, 0.0, 0.0);
7675

@@ -88,15 +87,16 @@ impl Vector3 {
8887

8988
/// Unit vector pointing towards the rear side (back) of imported 3D assets.
9089
pub const MODEL_REAR: Self = Self::new(0.0, 0.0, -1.0);
90+
}
91+
92+
impl_vector_fns!(Vector3, RVec3, real, (x, y, z));
9193

92-
/// Constructs a new `Vector3` from a [`Vector3i`].
94+
/// # Specialized `Vector3` functions
95+
impl Vector3 {
96+
#[deprecated = "Moved to `Vector3i::cast_float()`"]
9397
#[inline]
9498
pub const fn from_vector3i(v: Vector3i) -> Self {
95-
Self {
96-
x: v.x as real,
97-
y: v.y as real,
98-
z: v.z as real,
99-
}
99+
v.cast_float()
100100
}
101101

102102
#[doc(hidden)]
@@ -229,8 +229,8 @@ impl Vector3 {
229229
}
230230
}
231231

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

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ use godot_ffi as sys;
1212
use sys::{ffi_methods, GodotFfi};
1313

1414
use crate::builtin::math::{GlamConv, GlamType};
15-
use crate::builtin::{inner, real, RVec3, Vector3, Vector3Axis};
15+
use crate::builtin::{inner, real, RVec3, Vector2i, Vector3, Vector3Axis};
1616

1717
/// Vector used for 3D math using integer coordinates.
1818
///
1919
/// 3-element structure that can be used to represent discrete positions or directions in 3D space,
2020
/// as well as any other triple of numeric values.
2121
///
22-
/// It uses integer coordinates and is therefore preferable to [`Vector3`] when exact precision is
22+
/// `Vector3i` uses integer coordinates and is therefore preferable to [`Vector3`] when exact precision is
2323
/// required. Note that the values are limited to 32 bits, and unlike `Vector3` this cannot be
2424
/// configured with an engine build option. Use `i64` or [`PackedInt64Array`][crate::builtin::PackedInt64Array]
2525
/// if 64-bit values are needed.
2626
///
27+
#[doc = shared_vector_docs!()]
28+
///
2729
/// ### Navigation to `impl` blocks within this page
2830
///
2931
/// - [Constants](#constants)
@@ -40,6 +42,8 @@ use crate::builtin::{inner, real, RVec3, Vector3, Vector3Axis};
4042
/// | 3D | [`Vector3`][crate::builtin::Vector3] | **`Vector3i`** |
4143
/// | 4D | [`Vector4`][crate::builtin::Vector4] | [`Vector4i`][crate::builtin::Vector4i] |
4244
///
45+
/// <br>You can convert to 2D vectors using [`to_2d()`][Self::to_2d], and to `Vector3` using [`cast_float()`][Self::cast_float].
46+
///
4347
/// # Godot docs
4448
///
4549
/// [`Vector3i` (stable)](https://docs.godotengine.org/en/stable/classes/class_vector3i.html)
@@ -68,17 +72,13 @@ impl_vector_fns!(Vector3i, glam::IVec3, i32, (x, y, z));
6872

6973
/// # Specialized `Vector3i` functions
7074
impl Vector3i {
71-
/// Constructs a new `Vector3i` from a [`Vector3`]. The floating point coordinates will be truncated.
75+
#[deprecated = "Moved to `Vector3::cast_int()`"]
7276
#[inline]
7377
pub const fn from_vector3(v: Vector3) -> Self {
74-
Self {
75-
x: v.x as i32,
76-
y: v.y as i32,
77-
z: v.z as i32,
78-
}
78+
v.cast_int()
7979
}
8080

81-
inline_impl_integer_vector_fns!(x, y, z);
81+
inline_impl_integer_vector_fns!(Vector3, x, y, z);
8282

8383
/// Converts `self` to the corresponding [`real`] `glam` type.
8484
#[doc(hidden)]
@@ -94,7 +94,7 @@ impl Vector3i {
9494
}
9595
}
9696

97-
impl_vector3x_fns!(Vector3i, i32);
97+
impl_vector3x_fns!(Vector3i, Vector2i, i32);
9898

9999
impl_vector_operators!(Vector3i, i32, (x, y, z));
100100

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use std::fmt;
2222
/// is always 64-bit. The engine can be compiled with the option `precision=double` to use 64-bit
2323
/// vectors; use the gdext library with the `double-precision` feature in that case.
2424
///
25+
#[doc = shared_vector_docs!()]
26+
///
2527
/// ### Navigation to `impl` blocks within this page
2628
///
2729
/// - [Constants](#constants)
@@ -70,14 +72,9 @@ impl_vector_fns!(Vector4, RVec4, real, (x, y, z, w));
7072

7173
/// # Specialized `Vector4` functions
7274
impl Vector4 {
73-
/// Constructs a new `Vector4` from a [`Vector4i`][crate::builtin::Vector4i].
75+
#[deprecated = "Moved to `Vector4i::cast_float()`"]
7476
pub const fn from_vector4i(v: Vector4i) -> Self {
75-
Self {
76-
x: v.x as real,
77-
y: v.y as real,
78-
z: v.z as real,
79-
w: v.w as real,
80-
}
77+
v.cast_float()
8178
}
8279

8380
#[doc(hidden)]
@@ -87,7 +84,7 @@ impl Vector4 {
8784
}
8885
}
8986

90-
impl_float_vector_fns!(Vector4, (x, y, z, w));
87+
impl_float_vector_fns!(Vector4, Vector4i, (x, y, z, w));
9188
impl_vector4x_fns!(Vector4, real);
9289
impl_vector3_vector4_fns!(Vector4, (x, y, z, w));
9390

0 commit comments

Comments
 (0)