From 5864aab87a1bd609cf5ebc492e37abd48ae8b88e Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Sat, 15 May 2021 01:31:28 +0200 Subject: [PATCH] LibGfx/Vector*: Implement Formatters --- Userland/Libraries/LibGfx/Vector2.h | 18 ++++++++++++++++++ Userland/Libraries/LibGfx/Vector3.h | 18 ++++++++++++++++++ Userland/Libraries/LibGfx/Vector4.h | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/Userland/Libraries/LibGfx/Vector2.h b/Userland/Libraries/LibGfx/Vector2.h index 2994bf1b42..a796f3c750 100644 --- a/Userland/Libraries/LibGfx/Vector2.h +++ b/Userland/Libraries/LibGfx/Vector2.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace Gfx { @@ -107,6 +108,11 @@ public: return sqrt(m_x * m_x + m_y * m_y); } + constexpr String to_string() const + { + return String::formatted("[{},{}]", x(), y()); + } + private: T m_x; T m_y; @@ -117,6 +123,18 @@ typedef Vector2 DoubleVector2; } +namespace AK { + +template +struct Formatter> : Formatter { + void format(FormatBuilder& builder, const Gfx::Vector2& value) + { + Formatter::format(builder, value.to_string()); + } +}; + +} + using Gfx::DoubleVector2; using Gfx::FloatVector2; using Gfx::Vector2; diff --git a/Userland/Libraries/LibGfx/Vector3.h b/Userland/Libraries/LibGfx/Vector3.h index 8e2d928acf..22c0eb425c 100644 --- a/Userland/Libraries/LibGfx/Vector3.h +++ b/Userland/Libraries/LibGfx/Vector3.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace Gfx { @@ -123,6 +124,11 @@ public: return sqrt(m_x * m_x + m_y * m_y + m_z * m_z); } + constexpr String to_string() const + { + return String::formatted("[{},{},{}]", x(), y(), z()); + } + private: T m_x; T m_y; @@ -134,6 +140,18 @@ typedef Vector3 DoubleVector3; } +namespace AK { + +template +struct Formatter> : Formatter { + void format(FormatBuilder& builder, const Gfx::Vector3& value) + { + Formatter::format(builder, value.to_string()); + } +}; + +} + using Gfx::DoubleVector3; using Gfx::FloatVector3; using Gfx::Vector3; diff --git a/Userland/Libraries/LibGfx/Vector4.h b/Userland/Libraries/LibGfx/Vector4.h index aacd0bf16c..ff9cdfb12a 100644 --- a/Userland/Libraries/LibGfx/Vector4.h +++ b/Userland/Libraries/LibGfx/Vector4.h @@ -6,6 +6,7 @@ #pragma once +#include #include namespace Gfx { @@ -123,6 +124,11 @@ public: return sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w); } + constexpr String to_string() const + { + return String::formatted("[{},{},{},{}]", x(), y(), z(), w()); + } + private: T m_x; T m_y; @@ -135,6 +141,18 @@ typedef Vector4 DoubleVector4; } +namespace AK { + +template +struct Formatter> : Formatter { + void format(FormatBuilder& builder, const Gfx::Vector4& value) + { + Formatter::format(builder, value.to_string()); + } +}; + +} + using Gfx::DoubleVector4; using Gfx::FloatVector4; using Gfx::Vector4;