Skip to content

Commit ceabbeb

Browse files
committed
implement Display trait for some built-in types
1 parent 11f070b commit ceabbeb

File tree

9 files changed

+114
-3
lines changed

9 files changed

+114
-3
lines changed

check.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ function findGodot() {
7676
echo "Found 'godot4.bat' script"
7777
godotBin="godot4.bat"
7878

79+
# This should come last: only use this as a last resort as usually `godot`
80+
# refers to a Godot 3.x installation.
81+
elif command -v godot &>/dev/null; then
82+
# Check if `godot` actually is Godot 4.x
83+
if godot --version | grep -qE "^4\\."; then
84+
echo "Found 'godot' executable with version $(godot --version)"
85+
godotBin="godot"
86+
else
87+
echo "Found 'godot' executable, but it has the incompatible version $(godot --version)"
88+
exit 2
89+
fi
90+
7991
# Error case
8092
else
8193
echo "Godot executable not found"
@@ -99,7 +111,7 @@ for arg in "${args[@]}"; do
99111
;;
100112
itest)
101113
findGodot
102-
114+
103115
cmds+=("cargo $toolchain build -p itest $extraArgs")
104116
cmds+=("$godotBin --path itest/godot --headless")
105117
;;

godot-core/src/builtin/aabb.rs

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

85+
impl std::fmt::Display for Aabb {
86+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87+
// Godot Output: [P: (_, _, _), S: (_, _, _)]
88+
write!(f, "[P: {}, S: {}]", self.position, self.size)
89+
}
90+
}
91+
8592
impl GodotFfi for Aabb {
8693
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
8794
}

godot-core/src/builtin/color.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,9 @@ 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+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
501+
write!(f, "({}, {}, {}, {})", self.r, self.g, self.b, self.a)
502+
}
503+
}

godot-core/src/builtin/plane.rs

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

141+
impl std::fmt::Display for Plane {
142+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
143+
// e.g.
144+
// [N: (0, 0, 0), D: 0]
145+
write!(f, "[N: {}, D: {}]", self.normal, self.d)
146+
}
147+
}
148+
141149
#[cfg(test)]
142150
mod test {
143151
use super::*;

godot-core/src/builtin/projection.rs

Lines changed: 54 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,56 @@ mod test {
959959
}
960960
}
961961
}
962+
963+
impl std::fmt::Display for Projection {
964+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
965+
// godot output:
966+
// ```gdscript
967+
// 1, 0, 0, 0
968+
// 0, 1, 0, 0
969+
// 0, 0, 1, 0
970+
// 0, 0, 0, 1
971+
// ```
972+
write!(
973+
f,
974+
"\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n{}, {}, {}, {}\n",
975+
// first row
976+
self.cols[0][Vector4Axis::X],
977+
self.cols[1][Vector4Axis::X],
978+
self.cols[2][Vector4Axis::X],
979+
self.cols[3][Vector4Axis::X],
980+
// second row
981+
self.cols[0][Vector4Axis::Y],
982+
self.cols[1][Vector4Axis::Y],
983+
self.cols[2][Vector4Axis::Y],
984+
self.cols[3][Vector4Axis::Y],
985+
// third row
986+
self.cols[0][Vector4Axis::Z],
987+
self.cols[1][Vector4Axis::Z],
988+
self.cols[2][Vector4Axis::Z],
989+
self.cols[3][Vector4Axis::Z],
990+
// forth row
991+
self.cols[0][Vector4Axis::W],
992+
self.cols[1][Vector4Axis::W],
993+
self.cols[2][Vector4Axis::W],
994+
self.cols[3][Vector4Axis::W],
995+
)
996+
}
997+
}
998+
999+
#[test]
1000+
fn display_test() {
1001+
const TEST_DATA: [Vector4; 4] = [
1002+
Vector4::new(1.0, 2.5, 1.0, 0.5),
1003+
Vector4::new(0.0, 1.5, 2.0, 0.5),
1004+
Vector4::new(0.0, 0.0, 3.0, 2.5),
1005+
Vector4::new(3.0, 1.0, 4.0, 1.5),
1006+
];
1007+
const EXPECTED_RESULT: &str = r"
1008+
1, 0, 0, 3
1009+
2.5, 1.5, 0, 1
1010+
1, 2, 3, 4
1011+
0.5, 0.5, 2.5, 1.5
1012+
";
1013+
assert_eq!(format!("{}", Projection::new(TEST_DATA)), EXPECTED_RESULT)
1014+
}

godot-core/src/builtin/rect2.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,11 @@ 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+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113+
// godot output be like:
114+
// [P: (0, 0), S: (0, 0)]
115+
write!(f, "[P: {}, S: {}]", self.position, self.size)
116+
}
117+
}

godot-core/src/builtin/rect2i.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,11 @@ 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+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102+
// godot output be like:
103+
// [P: (0, 0), S: (0, 0)]
104+
write!(f, "[P: {}, S: {}]", self.position, self.size)
105+
}
106+
}

godot-core/src/builtin/rid.rs

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

84+
impl std::fmt::Display for Rid {
85+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86+
// godot output: `RID(0)`
87+
match self {
88+
Rid::Valid(x) => write!(f, "RID({})", x),
89+
Rid::Invalid => write!(f, "RID(0)"),
90+
}
91+
}
92+
}
93+
8494
impl GodotFfi for Rid {
8595
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
8696
}

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)