Skip to content

Commit 1817a79

Browse files
authored
Try #224:
2 parents 558a6d3 + 180acce commit 1817a79

File tree

8 files changed

+146
-2
lines changed

8 files changed

+146
-2
lines changed

godot-core/src/builtin/aabb.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ impl Aabb {
8282
*/
8383
}
8484

85+
impl std::fmt::Display for Aabb {
86+
/// Formats `Aabb` to match godot's display style.
87+
///
88+
/// # Example
89+
///
90+
/// ```
91+
/// use godot::prelude::*;
92+
/// let aabb = Aabb::new(Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 1.0, 1.0));
93+
/// assert_eq!(format!("{}", aabb), "[P: (0, 0, 0), S: (1, 1, 1)]");
94+
/// ```
95+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96+
write!(f, "[P: {}, S: {}]", self.position, self.size)
97+
}
98+
}
99+
85100
impl GodotFfi for Aabb {
86101
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
87102
}

godot-core/src/builtin/color.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,18 @@ fn to_be_words(mut u: u64) -> [u16; 4] {
495495
let x = (u & 0xffff) as u16;
496496
[x, y, z, w]
497497
}
498+
499+
impl std::fmt::Display for Color {
500+
/// Formats `Color` to match godot's display style.
501+
///
502+
/// # Example
503+
///
504+
/// ```
505+
/// use godot::prelude::*;
506+
/// let color = Color::from_rgba(1.0,1.0,1.0,1.0);
507+
/// assert_eq!(format!("{}", color), "(1, 1, 1, 1)");
508+
/// ```
509+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
510+
write!(f, "({}, {}, {}, {})", self.r, self.g, self.b, self.a)
511+
}
512+
}

godot-core/src/builtin/plane.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ impl GodotFfi for Plane {
138138
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
139139
}
140140

141+
impl std::fmt::Display for Plane {
142+
/// Formats `Plane` to match godot's display style.
143+
///
144+
/// # Example
145+
///
146+
/// ```
147+
/// use godot::prelude::*;
148+
/// let plane = Plane::new(Vector3::new(1.0, 0.0, 0.0), 1.0);
149+
/// assert_eq!(format!("{}", plane), "[N: (1, 0, 0), D: 1]");
150+
/// ```
151+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
152+
write!(f, "[N: {}, D: {}]", self.normal, self.d)
153+
}
154+
}
155+
141156
#[cfg(test)]
142157
mod test {
143158
use super::*;

godot-core/src/builtin/projection.rs

Lines changed: 50 additions & 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 super::glam_helpers::{GlamConv, GlamType};
12-
use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4};
12+
use super::{inner::InnerProjection, Plane, Transform3D, Vector2, Vector4, Vector4Axis};
1313
use super::{real, RMat4, RealConv};
1414

1515
/// A 4x4 matrix used for 3D projective transformations. It can represent
@@ -959,3 +959,52 @@ mod test {
959959
}
960960
}
961961
}
962+
963+
impl std::fmt::Display for Projection {
964+
/// Formats `Projection` to match godot's display style.
965+
///
966+
/// # Example
967+
///
968+
/// ```
969+
/// use godot::prelude::*;
970+
/// let proj = Projection::new([
971+
/// Vector4::new(1.0, 2.5, 1.0, 0.5),
972+
/// Vector4::new(0.0, 1.5, 2.0, 0.5),
973+
/// Vector4::new(0.0, 0.0, 3.0, 2.5),
974+
/// Vector4::new(3.0, 1.0, 4.0, 1.5),
975+
/// ]);
976+
/// const FMT_RESULT: &str = r"
977+
/// 1, 0, 0, 3
978+
/// 2.5, 1.5, 0, 1
979+
/// 1, 2, 3, 4
980+
/// 0.5, 0.5, 2.5, 1.5
981+
/// ";
982+
/// assert_eq!(format!("{}", proj), FMT_RESULT);
983+
/// ```
984+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
985+
write!(
986+
f,
987+
"\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n",
988+
// first row
989+
self.cols[0][Vector4Axis::X],
990+
self.cols[1][Vector4Axis::X],
991+
self.cols[2][Vector4Axis::X],
992+
self.cols[3][Vector4Axis::X],
993+
// second row
994+
self.cols[0][Vector4Axis::Y],
995+
self.cols[1][Vector4Axis::Y],
996+
self.cols[2][Vector4Axis::Y],
997+
self.cols[3][Vector4Axis::Y],
998+
// third row
999+
self.cols[0][Vector4Axis::Z],
1000+
self.cols[1][Vector4Axis::Z],
1001+
self.cols[2][Vector4Axis::Z],
1002+
self.cols[3][Vector4Axis::Z],
1003+
// forth row
1004+
self.cols[0][Vector4Axis::W],
1005+
self.cols[1][Vector4Axis::W],
1006+
self.cols[2][Vector4Axis::W],
1007+
self.cols[3][Vector4Axis::W],
1008+
)
1009+
}
1010+
}

godot-core/src/builtin/rect2.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,20 @@ impl Rect2 {
107107
impl GodotFfi for Rect2 {
108108
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
109109
}
110+
111+
impl std::fmt::Display for Rect2 {
112+
/// Formats `Rect2` to match godot's display style.
113+
///
114+
/// # Example
115+
///
116+
/// ```
117+
/// use godot::prelude::*;
118+
/// let rect = Rect2::new(Vector2::new(0.0, 0.0), Vector2::new(1.0, 1.0));
119+
/// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]");
120+
/// ```
121+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122+
// godot output be like:
123+
// [P: (0, 0), S: (0, 0)]
124+
write!(f, "[P: {}, S: {}]", self.position, self.size)
125+
}
126+
}

godot-core/src/builtin/rect2i.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,18 @@ impl Rect2i {
9696
impl GodotFfi for Rect2i {
9797
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
9898
}
99+
100+
impl std::fmt::Display for Rect2i {
101+
/// Formats `Rect2i` to match godot's display style.
102+
///
103+
/// # Example
104+
///
105+
/// ```
106+
/// use godot::prelude::*;
107+
/// let rect = Rect2i::new(Vector2i::new(0, 0), Vector2i::new(1, 1));
108+
/// assert_eq!(format!("{}", rect), "[P: (0, 0), S: (1, 1)]");
109+
/// ```
110+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111+
write!(f, "[P: {}, S: {}]", self.position, self.size)
112+
}
113+
}

godot-core/src/builtin/rid.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ impl Rid {
8181
}
8282
}
8383

84+
impl std::fmt::Display for Rid {
85+
/// Formats `Rid` to match godot's display style.
86+
///
87+
/// # Example
88+
///
89+
/// ```
90+
/// use godot::prelude::*;
91+
/// let id = Rid::new(1);
92+
/// assert_eq!(format!("{}", id), "RID(1)");
93+
/// ```
94+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95+
// godot output: `RID(0)`
96+
match self {
97+
Rid::Valid(x) => write!(f, "RID({})", x),
98+
Rid::Invalid => write!(f, "RID(0)"),
99+
}
100+
}
101+
}
102+
84103
impl GodotFfi for Rid {
85104
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
86105
}

godot-core/src/builtin/transform2d.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ impl Default for Basis2D {
494494
impl Display for Basis2D {
495495
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
496496
let [a, b] = self.cols;
497-
498497
write!(f, "[a: {a}, b: {b})]")
499498
}
500499
}

0 commit comments

Comments
 (0)