Skip to content

Commit

Permalink
Apply rustfmt and minor review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Jan 20, 2023
1 parent 66a487a commit e7fa8a8
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 138 deletions.
61 changes: 32 additions & 29 deletions godot-core/src/builtin/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::builtin::Vector2i;
/// It uses floating-point coordinates of 32-bit precision, unlike the engine's `float` type which
/// is always 64-bit. The engine can be compiled with the option `precision=double` to use 64-bit
/// vectors, but this is not yet supported in the `gdextension` crate.
///
///
/// See [`Vector2i`] for its integer counterpart.
#[derive(Debug, Default, Clone, Copy, PartialEq)]
#[repr(C)]
Expand All @@ -30,48 +30,46 @@ pub struct Vector2 {
pub y: f32,
}

impl_vector_operators!(Vector2, f32, (x, y));
impl_vector_index!(Vector2, f32, (x, y), Vector2Axis, (X, Y));
impl_common_vector_fns!(Vector2, f32);
impl_float_vector_fns!(Vector2, f32);

impl Vector2 {
/// Constructs a new `Vector2` from the given `x` and `y`.
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}

/// Constructs a new `Vector2` with all components set to `v`.
pub const fn splat(v: f32) -> Self {
Self { x: v, y: v }
}

/// Constructs a new `Vector2` from a [`Vector2i`].
pub const fn from_vector2i(v: Vector2i) -> Self {
Self { x: v.x as f32, y: v.y as f32 }
}

/// Zero vector, a vector with all components set to `0.0`.
/// Vector with all components set to `0.0`.
pub const ZERO: Self = Self::splat(0.0);

/// One vector, a vector with all components set to `1.0`.
/// Vector with all components set to `1.0`.
pub const ONE: Self = Self::splat(1.0);

/// Infinity vector, a vector with all components set to `INFIINTY`.
/// Vector with all components set to `f32::INFINITY`.
pub const INF: Self = Self::splat(f32::INFINITY);

/// Left unit vector. Represents the direction of left.
/// Unit vector in -X direction (right in 2D coordinate system).
pub const LEFT: Self = Self::new(-1.0, 0.0);

/// Right unit vector. Represents the direction of right.
/// Unit vector in +X direction (right in 2D coordinate system).
pub const RIGHT: Self = Self::new(1.0, 0.0);

/// Up unit vector. Y is down in 2D, so this vector points -Y.
/// Unit vector in -Y direction (up in 2D coordinate system).
pub const UP: Self = Self::new(0.0, -1.0);

/// Down unit vector. Y is down in 2D, so this vector points +Y.
/// Unit vector in +Y direction (down in 2D coordinate system).
pub const DOWN: Self = Self::new(0.0, 1.0);

/// Constructs a new `Vector2` from the given `x` and `y`.
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}

/// Constructs a new `Vector2` with both components set to `v`.
pub const fn splat(v: f32) -> Self {
Self::new(v, v)
}

/// Constructs a new `Vector2` from a [`Vector2i`].
pub const fn from_vector2i(v: Vector2i) -> Self {
Self {
x: v.x as f32,
y: v.y as f32,
}
}

/// Returns the result of rotating this vector by `angle` (in radians).
pub fn rotated(self, angle: f32) -> Self {
Self::from_glam(glam::Affine2::from_angle(angle).transform_vector2(self.to_glam()))
Expand All @@ -88,13 +86,18 @@ impl Vector2 {
}
}

/// Formats this vector in the same way the Godot engine would.
/// Formats the vector like Godot: `(x, y)`.
impl fmt::Display for Vector2 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}

impl_common_vector_fns!(Vector2, f32);
impl_float_vector_fns!(Vector2, f32);
impl_vector_operators!(Vector2, f32, (x, y));
impl_vector_index!(Vector2, f32, (x, y), Vector2Axis, (X, Y));

impl GodotFfi for Vector2 {
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
}
Expand Down
58 changes: 30 additions & 28 deletions godot-core/src/builtin/vector2i.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::builtin::Vector2;
///
/// 2-element structure that can be used to represent positions in 2D space or any other pair of
/// numeric values.
///
///
/// It uses integer coordinates and is therefore preferable to [`Vector2`] when exact precision is
/// required. Note that the values are limited to 32 bits, and unlike [`Vector2`] this cannot be
/// configured with an engine build option. Use `i64` or [`PackedInt64Array`] if 64-bit values are
Expand All @@ -29,45 +29,43 @@ pub struct Vector2i {
pub y: i32,
}

impl_vector_operators!(Vector2i, i32, (x, y));
impl_vector_index!(Vector2i, i32, (x, y), Vector2iAxis, (X, Y));
impl_common_vector_fns!(Vector2i, i32);

impl Vector2i {
/// Constructs a new `Vector2i` from the given `x` and `y`.
pub const fn new(x: i32, y: i32) -> Self {
Self { x, y }
}

/// Constructs a new `Vector2i` with all components set to `v`.
pub const fn splat(v: i32) -> Self {
Self { x: v, y: v }
}

/// Constructs a new `Vector2i` from a [`Vector2`]. The floating point coordinates will be
/// truncated.
pub const fn from_vector2(v: Vector2) -> Self {
Self { x: v.x as i32, y: v.y as i32 }
}

/// Zero vector, a vector with all components set to `0`.
/// Vector with all components set to `0`.
pub const ZERO: Self = Self::splat(0);

/// One vector, a vector with all components set to `1`.
/// Vector with all components set to `1`.
pub const ONE: Self = Self::splat(1);

/// Left unit vector. Represents the direction of left.
/// Unit vector in -X direction (right in 2D coordinate system).
pub const LEFT: Self = Self::new(-1, 0);

/// Right unit vector. Represents the direction of right.
/// Unit vector in +X direction (right in 2D coordinate system).
pub const RIGHT: Self = Self::new(1, 0);

/// Up unit vector. Y is down in 2D, so this vector points -Y.
/// Unit vector in -Y direction (up in 2D coordinate system).
pub const UP: Self = Self::new(0, -1);

/// Down unit vector. Y is down in 2D, so this vector points +Y.
/// Unit vector in +Y direction (down in 2D coordinate system).
pub const DOWN: Self = Self::new(0, 1);

/// Constructs a new `Vector2i` from the given `x` and `y`.
pub const fn new(x: i32, y: i32) -> Self {
Self { x, y }
}

/// Constructs a new `Vector2i` with both components set to `v`.
pub const fn splat(v: i32) -> Self {
Self::new(v, v)
}

/// Constructs a new `Vector2i` from a [`Vector2`]. The floating point coordinates will be truncated.
pub const fn from_vector2(v: Vector2) -> Self {
Self {
x: v.x as i32,
y: v.y as i32,
}
}

/// Converts the corresponding `glam` type to `Self`.
fn from_glam(v: glam::IVec2) -> Self {
Self::new(v.x, v.y)
Expand All @@ -79,13 +77,17 @@ impl Vector2i {
}
}

/// Formats this vector in the same way the Godot engine would.
/// Formats the vector like Godot: `(x, y)`.
impl fmt::Display for Vector2i {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}

impl_common_vector_fns!(Vector2i, i32);
impl_vector_operators!(Vector2i, i32, (x, y));
impl_vector_index!(Vector2i, i32, (x, y), Vector2iAxis, (X, Y));

impl GodotFfi for Vector2i {
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
}
Expand Down
69 changes: 35 additions & 34 deletions godot-core/src/builtin/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use crate::builtin::Vector3i;

/// Vector used for 3D math using floating point coordinates.
///
/// 2-element structure that can be used to represent positions in 2D space or any other pair of
/// 3-element structure that can be used to represent positions in 2D space or any other triple of
/// numeric values.
///
/// It uses floating-point coordinates of 32-bit precision, unlike the engine's `float` type which
/// is always 64-bit. The engine can be compiled with the option `precision=double` to use 64-bit
/// vectors, but this is not yet supported in the `gdextension` crate.
///
///
/// See [`Vector3i`] for its integer counterpart.
#[derive(Debug, Default, Clone, Copy, PartialEq)]
#[repr(C)]
Expand All @@ -32,54 +32,50 @@ pub struct Vector3 {
pub z: f32,
}

impl_vector_operators!(Vector3, f32, (x, y, z));
impl_vector_index!(Vector3, f32, (x, y, z), Vector3Axis, (X, Y, Z));
impl_common_vector_fns!(Vector3, f32);
impl_float_vector_fns!(Vector3, f32);

impl Vector3 {
/// Returns a `Vector3` with the given components.
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}

/// Returns a new `Vector3` with all components set to `v`.
pub const fn splat(v: f32) -> Self {
Self { x: v, y: v, z: v }
}

/// Constructs a new `Vector3` from a [`Vector3i`].
pub const fn from_vector3i(v: Vector3i) -> Self {
Self { x: v.x as f32, y: v.y as f32, z: v.z as f32 }
}

/// Zero vector, a vector with all components set to `0.0`.
/// Vector with all components set to `0.0`.
pub const ZERO: Self = Self::splat(0.0);

/// One vector, a vector with all components set to `1.0`.
/// Vector with all components set to `1.0`.
pub const ONE: Self = Self::splat(1.0);

/// Infinity vector, a vector with all components set to `INFIINTY`.
pub const INF: Self = Self::splat(f32::INFINITY);

/// Left unit vector. Represents the local direction of left, and the global direction of west.
/// Unit vector in -X direction. Can be interpreted as left in an untransformed 3D world.
pub const LEFT: Self = Self::new(-1.0, 0.0, 0.0);

/// Right unit vector. Represents the local direction of right, and the global direction of east.
/// Unit vector in +X direction. Can be interpreted as right in an untransformed 3D world.
pub const RIGHT: Self = Self::new(1.0, 0.0, 0.0);

/// Up unit vector.
/// Unit vector in -Y direction. Typically interpreted as down in a 3D world.
pub const UP: Self = Self::new(0.0, -1.0, 0.0);

/// Down unit vector.
/// Unit vector in +Y direction. Typically interpreted as up in a 3D world.
pub const DOWN: Self = Self::new(0.0, 1.0, 0.0);

/// Forward unit vector. Represents the local direction of forward, and the global direction of north.
/// Unit vector in -Z direction. Can be interpreted as "into the screen" in an untransformed 3D world.
pub const FORWARD: Self = Self::new(0.0, 0.0, -1.0);

/// Back unit vector. Represents the local direction of back, and the global direction of south.
/// Unit vector in +Z direction. Can be interpreted as "out of the screen" in an untransformed 3D world.
pub const BACK: Self = Self::new(0.0, 0.0, 1.0);

/// Returns a `Vector3` with the given components.
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}

/// Returns a new `Vector3` with all components set to `v`.
pub const fn splat(v: f32) -> Self {
Self::new(v, v, v)
}

/// Constructs a new `Vector3` from a [`Vector3i`].
pub const fn from_vector3i(v: Vector3i) -> Self {
Self {
x: v.x as f32,
y: v.y as f32,
z: v.z as f32,
}
}

/// Converts the corresponding `glam` type to `Self`.
fn from_glam(v: glam::Vec3) -> Self {
Self::new(v.x, v.y, v.z)
Expand All @@ -91,13 +87,18 @@ impl Vector3 {
}
}

/// Formats this vector in the same way the Godot engine would.
/// Formats the vector like Godot: `(x, y, z)`.
impl fmt::Display for Vector3 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {}, {})", self.x, self.y, self.z)
}
}

impl_common_vector_fns!(Vector3, f32);
impl_float_vector_fns!(Vector3, f32);
impl_vector_operators!(Vector3, f32, (x, y, z));
impl_vector_index!(Vector3, f32, (x, y, z), Vector3Axis, (X, Y, Z));

impl GodotFfi for Vector3 {
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
}
Expand Down
Loading

0 comments on commit e7fa8a8

Please sign in to comment.