From d9669b0ed0afe4913b991f2eb0ee386b15706cf3 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 16 Aug 2022 22:46:24 +0200 Subject: [PATCH] Ported form godot4: Reformat structure string operators The order of numbers is not changed except for Transform2D. All logic is done inside of their structures (and not in Variant). -aaronfranke https://github.com/godotengine/godot/commit/554c776e08c9ee35fa9e2677e02f4005c11ddbc0 --- core/color.cpp | 2 +- core/math/aabb.cpp | 2 +- core/math/basis.cpp | 15 +++------------ core/math/plane.cpp | 2 +- core/math/projection.cpp | 12 ++++-------- core/math/transform.cpp | 6 +++--- core/math/transform_2d.cpp | 4 +++- core/variant.cpp | 30 +++--------------------------- 8 files changed, 19 insertions(+), 54 deletions(-) diff --git a/core/color.cpp b/core/color.cpp index e1f414833..cf03ff303 100644 --- a/core/color.cpp +++ b/core/color.cpp @@ -463,7 +463,7 @@ float Color::gray() const { } Color::operator String() const { - return rtos(r) + ", " + rtos(g) + ", " + rtos(b) + ", " + rtos(a); + return "(" + String::num(r, 4) + ", " + String::num(g, 4) + ", " + String::num(b, 4) + ", " + String::num(a, 4) + ")"; } Color Color::operator+(const Color &p_color) const { diff --git a/core/math/aabb.cpp b/core/math/aabb.cpp index 18693bc12..a52c755d3 100644 --- a/core/math/aabb.cpp +++ b/core/math/aabb.cpp @@ -426,5 +426,5 @@ Variant AABB::intersects_rayv(const Vector3 &p_from, const Vector3 &p_dir) const } AABB::operator String() const { - return String() + position + " - " + size; + return "[P: " + position.operator String() + ", S: " + size + "]"; } diff --git a/core/math/basis.cpp b/core/math/basis.cpp index e76815b87..392208f24 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -803,18 +803,9 @@ bool Basis::operator!=(const Basis &p_matrix) const { } Basis::operator String() const { - String mtx; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (i != 0 || j != 0) { - mtx += ", "; - } - - mtx += rtos(rows[i][j]); - } - } - - return mtx; + return "[X: " + get_axis(0).operator String() + + ", Y: " + get_axis(1).operator String() + + ", Z: " + get_axis(2).operator String() + "]"; } Quaternion Basis::get_quaternion() const { diff --git a/core/math/plane.cpp b/core/math/plane.cpp index a588c0dfb..f259f7835 100644 --- a/core/math/plane.cpp +++ b/core/math/plane.cpp @@ -154,5 +154,5 @@ bool Plane::is_equal_approx_any_side(const Plane &p_plane) const { } Plane::operator String() const { - return normal.operator String() + ", " + rtos(d); + return "[N: " + normal.operator String() + ", D: " + String::num_real(d) + "]"; } diff --git a/core/math/projection.cpp b/core/math/projection.cpp index e9ec7080f..c6fea62d4 100644 --- a/core/math/projection.cpp +++ b/core/math/projection.cpp @@ -866,14 +866,10 @@ Plane Projection::xform(const Plane &p_vec4) const { } Projection::operator String() const { - String str; - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - str += String((j > 0) ? ", " : "\n") + rtos(matrix[i][j]); - } - } - - return str; + return "[ X: " + matrix[0].operator String() + + ", Y: " + matrix[1].operator String() + + ", Z: " + matrix[2].operator String() + + ", W: " + matrix[3].operator String() + " ]"; } real_t Projection::get_aspect() const { diff --git a/core/math/transform.cpp b/core/math/transform.cpp index daf930284..2c00bc991 100644 --- a/core/math/transform.cpp +++ b/core/math/transform.cpp @@ -258,9 +258,9 @@ Transform Transform::interpolate_with(const Transform &p_transform, real_t p_c) } Transform::operator String() const { - return "[X: " + basis.get_column(0).operator String() + - ", Y: " + basis.get_column(1).operator String() + - ", Z: " + basis.get_column(2).operator String() + + return "[X: " + basis.get_axis(0).operator String() + + ", Y: " + basis.get_axis(1).operator String() + + ", Z: " + basis.get_axis(2).operator String() + ", O: " + origin.operator String() + "]"; } diff --git a/core/math/transform_2d.cpp b/core/math/transform_2d.cpp index 33dc8307d..e07a24c8b 100644 --- a/core/math/transform_2d.cpp +++ b/core/math/transform_2d.cpp @@ -330,5 +330,7 @@ Transform2D Transform2D::interpolate_with(const Transform2D &p_transform, real_t } Transform2D::operator String() const { - return String(String() + columns[0] + ", " + columns[1] + ", " + columns[2]); + return "[X: " + columns[0].operator String() + + ", Y: " + columns[1].operator String() + + ", O: " + columns[2].operator String() + "]"; } diff --git a/core/variant.cpp b/core/variant.cpp index c3375270e..12e3fbb44 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -1729,7 +1729,6 @@ String Variant::stringify(List &stack) const { return operator Vector4(); case VECTOR4I: return operator Vector4i(); - case PLANE: return operator Plane(); case QUATERNION: @@ -1737,40 +1736,17 @@ String Variant::stringify(List &stack) const { case AABB: return operator ::AABB(); case BASIS: { - Basis mat3 = operator Basis(); - - String mtx("("); - for (int i = 0; i < 3; i++) { - if (i != 0) { - mtx += ", "; - } - - mtx += "("; - - for (int j = 0; j < 3; j++) { - if (j != 0) { - mtx += ", "; - } - - mtx += Variant(mat3.rows[i][j]).operator String(); - } - - mtx += ")"; - } - - return mtx + ")"; + return operator Basis(); } break; case TRANSFORM: return operator Transform(); case TRANSFORM2D: { - Transform2D mat32 = operator Transform2D(); - return "(" + Variant(mat32.columns[0]).operator String() + ", " + Variant(mat32.columns[1]).operator String() + ", " + Variant(mat32.columns[2]).operator String() + ")"; + return operator Transform2D(); } break; case PROJECTION: return operator Projection(); - case COLOR: - return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a); + return operator Color(); case NODE_PATH: return operator NodePath(); //RID