Skip to content

Commit d4c35c6

Browse files
bors[bot]hapeniaBromeon
authored
Merge #225
225: Add a couple features for Vectors r=Bromeon a=Hapenia-Lans Utilities for vectors. This pull request allows you to write these code: ```rust let vec = Vector3i::new(1,2,3); let (x,y,_) = vec.xyz(); let vec_reverted = Vector3i::from(vec.zyx()); let vec2i = Vector2i::from((x,y)); ``` These functions' design still needs to be discussed. Another choice is something like this: ```rust let vec = Vector3i::new(1,2,3); let (x,y,z) = vec.tuplize(); // make it into tuple let vec_reverted = vec.zyx(); let vec2i = vec.xy(); // this needs large amounts of functions decl ``` Co-authored-by: ChenXi <[email protected]> Co-authored-by: Jan Haller <[email protected]>
2 parents 627d408 + 7dc76a1 commit d4c35c6

File tree

11 files changed

+367
-176
lines changed

11 files changed

+367
-176
lines changed

godot-core/src/builtin/mod.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ 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::*;
6358

6459
/// Meta-information about variant types, properties and class names.
6560
pub mod meta;
@@ -79,7 +74,6 @@ pub mod dictionary {
7974

8075
// Modules exporting declarative macros must appear first.
8176
mod macros;
82-
mod vector_macros;
8377

8478
// Rename imports because we re-export a subset of types under same module names.
8579
#[path = "array.rs"]
@@ -105,12 +99,7 @@ mod string;
10599
mod transform2d;
106100
mod transform3d;
107101
mod variant;
108-
mod vector2;
109-
mod vector2i;
110-
mod vector3;
111-
mod vector3i;
112-
mod vector4;
113-
mod vector4i;
102+
mod vectors;
114103

115104
#[doc(hidden)]
116105
pub mod inner {
@@ -200,7 +189,10 @@ mod real_mod {
200189
}
201190
}
202191

203-
pub use std::f32::consts;
192+
/// Re-export of [`std::f32::consts`] or [`std::f64::consts`], depending on precision config.
193+
pub mod real_consts {
194+
pub use std::f32::consts::*;
195+
}
204196

205197
/// A 2-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
206198
pub type RVec2 = glam::Vec2;
@@ -267,7 +259,10 @@ mod real_mod {
267259
}
268260
}
269261

270-
pub use std::f64::consts;
262+
/// Re-export of [`std::f32::consts`] or [`std::f64::consts`], depending on precision config.
263+
pub mod real_consts {
264+
pub use std::f64::consts::*;
265+
}
271266

272267
/// A 2-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
273268
pub type RVec2 = glam::DVec2;
@@ -297,26 +292,29 @@ mod real_mod {
297292

298293
pub use crate::real;
299294
pub(crate) use real_mod::*;
300-
pub use real_mod::{consts as real_consts, real};
295+
296+
pub use real_mod::{real, real_consts};
301297

302298
pub(crate) use glam::{IVec2, IVec3, IVec4};
303299

304-
/// A macro to coerce float-literals into the real type. Mainly used where
305-
/// you'd normally use a suffix to specity the type, such as `115.0f32`.
300+
/// A macro to coerce float-literals into the [`real`] type.
301+
///
302+
/// Mainly used where you'd normally use a suffix to specify the type, such as `115.0f32`.
303+
///
304+
/// # Examples
306305
///
307-
/// ### Examples
308-
/// Rust will not know how to infer the type of this call to `to_radians`:
306+
/// Rust is not able to infer the `self` type of this call to `to_radians`:
309307
/// ```compile_fail
310-
/// use godot_core::builtin::real;
308+
/// use godot::builtin::real;
311309
///
312310
/// let radians: real = 115.0.to_radians();
313311
/// ```
314-
/// But we can't add a suffix to the literal, since it may be either `f32` or
312+
/// But we cannot add a suffix to the literal, since it may be either `f32` or
315313
/// `f64` depending on the context. So instead we use our macro:
316314
/// ```
317-
/// use godot_core::builtin::real;
315+
/// use godot::builtin::real;
318316
///
319-
/// let radians: real = godot_core::real!(115.0).to_radians();
317+
/// let radians: real = real!(115.0).to_radians();
320318
/// ```
321319
#[macro_export]
322320
macro_rules! real {

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
mod vector2;
10+
mod vector2i;
11+
mod vector3;
12+
mod vector3i;
13+
mod vector4;
14+
mod vector4i;
15+
mod vector_axis;
16+
17+
pub use vector2::*;
18+
pub use vector2i::*;
19+
pub use vector3::*;
20+
pub use vector3i::*;
21+
pub use vector4::*;
22+
pub use vector4i::*;
23+
pub use vector_axis::*;
24+
25+
pub use crate::swizzle;

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_axis::*;
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_axis::*;
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)