diff --git a/godot-core/src/builtin/aabb.rs b/godot-core/src/builtin/aabb.rs index e6d7505bc..26dc97892 100644 --- a/godot-core/src/builtin/aabb.rs +++ b/godot-core/src/builtin/aabb.rs @@ -82,6 +82,20 @@ impl Aabb { */ } +impl std::fmt::Display for Aabb { + /// Formats `Aabb` to match godot's display style. + /// + /// Example: + /// ``` + /// use godot::prelude::*; + /// let aabb = Aabb::new(Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 1.0, 1.0)); + /// assert_eq!(format!("{}", aabb), "[P: (0, 0, 0), S: (1, 1, 1)]"); + /// ``` + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[P: {}, S: {}]", self.position, self.size) + } +} + // SAFETY: // This type is represented as `Self` in Godot, so `*mut Self` is sound. unsafe impl GodotFfi for Aabb { diff --git a/godot-core/src/builtin/color.rs b/godot-core/src/builtin/color.rs index c93d97489..31966d70e 100644 --- a/godot-core/src/builtin/color.rs +++ b/godot-core/src/builtin/color.rs @@ -497,3 +497,17 @@ fn to_be_words(mut u: u64) -> [u16; 4] { let x = (u & 0xffff) as u16; [x, y, z, w] } + +impl std::fmt::Display for Color { + /// Formats `Color` to match Godot's string representation. + /// + /// Example: + /// ``` + /// use godot::prelude::*; + /// let color = Color::from_rgba(1.0,1.0,1.0,1.0); + /// assert_eq!(format!("{}", color), "(1, 1, 1, 1)"); + /// ``` + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({}, {}, {}, {})", self.r, self.g, self.b, self.a) + } +} diff --git a/godot-core/src/builtin/plane.rs b/godot-core/src/builtin/plane.rs index 83209fa4d..bd59fb2ec 100644 --- a/godot-core/src/builtin/plane.rs +++ b/godot-core/src/builtin/plane.rs @@ -140,6 +140,20 @@ unsafe impl GodotFfi for Plane { ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } } +impl std::fmt::Display for Plane { + /// Formats `Plane` to match Godot's string representation. + /// + /// Example: + /// ``` + /// use godot::prelude::*; + /// let plane = Plane::new(Vector3::new(1.0, 0.0, 0.0), 1.0); + /// assert_eq!(format!("{}", plane), "[N: (1, 0, 0), D: 1]"); + /// ``` + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[N: {}, D: {}]", self.normal, self.d) + } +} + #[cfg(test)] mod test { use super::*; diff --git a/godot-core/src/builtin/projection.rs b/godot-core/src/builtin/projection.rs index 8625152e0..20f184753 100644 --- a/godot-core/src/builtin/projection.rs +++ b/godot-core/src/builtin/projection.rs @@ -9,7 +9,7 @@ use godot_ffi as sys; use sys::{ffi_methods, GodotFfi}; use super::glam_helpers::{GlamConv, GlamType}; -use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4}; +use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4, Vector4Axis}; use super::{real, RMat4, RealConv}; /// A 4x4 matrix used for 3D projective transformations. It can represent @@ -961,3 +961,51 @@ mod test { } } } + +impl std::fmt::Display for Projection { + /// Formats `Projection` to match Godot's string representation. + /// + /// Example: + /// ``` + /// use godot::prelude::*; + /// let proj = Projection::new([ + /// Vector4::new(1.0, 2.5, 1.0, 0.5), + /// Vector4::new(0.0, 1.5, 2.0, 0.5), + /// Vector4::new(0.0, 0.0, 3.0, 2.5), + /// Vector4::new(3.0, 1.0, 4.0, 1.5), + /// ]); + /// const FMT_RESULT: &str = r" + /// 1, 0, 0, 3 + /// 2.5, 1.5, 0, 1 + /// 1, 2, 3, 4 + /// 0.5, 0.5, 2.5, 1.5 + /// "; + /// assert_eq!(format!("{}", proj), FMT_RESULT); + /// ``` + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n", + // first row + self.cols[0][Vector4Axis::X], + self.cols[1][Vector4Axis::X], + self.cols[2][Vector4Axis::X], + self.cols[3][Vector4Axis::X], + // second row + self.cols[0][Vector4Axis::Y], + self.cols[1][Vector4Axis::Y], + self.cols[2][Vector4Axis::Y], + self.cols[3][Vector4Axis::Y], + // third row + self.cols[0][Vector4Axis::Z], + self.cols[1][Vector4Axis::Z], + self.cols[2][Vector4Axis::Z], + self.cols[3][Vector4Axis::Z], + // forth row + self.cols[0][Vector4Axis::W], + self.cols[1][Vector4Axis::W], + self.cols[2][Vector4Axis::W], + self.cols[3][Vector4Axis::W], + ) + } +} diff --git a/godot-core/src/builtin/rect2.rs b/godot-core/src/builtin/rect2.rs index 492a86e1a..aa5973c4b 100644 --- a/godot-core/src/builtin/rect2.rs +++ b/godot-core/src/builtin/rect2.rs @@ -109,3 +109,19 @@ impl Rect2 { unsafe impl GodotFfi for Rect2 { ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } } + +impl std::fmt::Display for Rect2 { + /// Formats `Rect2` to match Godot's string representation. + /// + /// Example: + /// ``` + /// use godot::prelude::*; + /// let rect = Rect2::new(Vector2::new(0.0, 0.0), Vector2::new(1.0, 1.0)); + /// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]"); + /// ``` + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // godot output be like: + // [P: (0, 0), S: (0, 0)] + write!(f, "[P: {}, S: {}]", self.position, self.size) + } +} diff --git a/godot-core/src/builtin/rect2i.rs b/godot-core/src/builtin/rect2i.rs index ff6f994ed..cab59889b 100644 --- a/godot-core/src/builtin/rect2i.rs +++ b/godot-core/src/builtin/rect2i.rs @@ -267,6 +267,20 @@ unsafe impl GodotFfi for Rect2i { ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } } +impl std::fmt::Display for Rect2i { + /// Formats `Rect2i` to match Godot's string representation. + /// + /// Example: + /// ``` + /// use godot::prelude::*; + /// let rect = Rect2i::new(Vector2i::new(0, 0), Vector2i::new(1, 1)); + /// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]"); + /// ``` + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[P: {}, S: {}]", self.position, self.size) + } +} + #[cfg(test)] mod test { use super::*; diff --git a/godot-core/src/builtin/rid.rs b/godot-core/src/builtin/rid.rs index 57eeaf394..29d18edfb 100644 --- a/godot-core/src/builtin/rid.rs +++ b/godot-core/src/builtin/rid.rs @@ -81,6 +81,24 @@ impl Rid { } } +impl std::fmt::Display for Rid { + /// Formats `Rid` to match Godot's string representation. + /// + /// Example: + /// ``` + /// use godot::prelude::*; + /// let id = Rid::new(1); + /// assert_eq!(format!("{}", id), "RID(1)"); + /// ``` + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // godot output: `RID(0)` + match self { + Rid::Valid(x) => write!(f, "RID({})", x), + Rid::Invalid => write!(f, "RID(0)"), + } + } +} + // SAFETY: // This type is represented as `Self` in Godot, so `*mut Self` is sound. unsafe impl GodotFfi for Rid { diff --git a/godot-core/src/builtin/transform2d.rs b/godot-core/src/builtin/transform2d.rs index e5a8ff89f..12185298a 100644 --- a/godot-core/src/builtin/transform2d.rs +++ b/godot-core/src/builtin/transform2d.rs @@ -496,7 +496,6 @@ impl Default for Basis2D { impl Display for Basis2D { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let [a, b] = self.cols; - write!(f, "[a: {a}, b: {b})]") } }