LibGfx/Vector*: Implement Formatters

This commit is contained in:
Jelle Raaijmakers 2021-05-15 01:31:28 +02:00 committed by Linus Groh
parent 9459f3728c
commit 5864aab87a
3 changed files with 54 additions and 0 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/String.h>
#include <math.h>
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<double> DoubleVector2;
}
namespace AK {
template<typename T>
struct Formatter<Gfx::Vector2<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Vector2<T>& value)
{
Formatter<StringView>::format(builder, value.to_string());
}
};
}
using Gfx::DoubleVector2;
using Gfx::FloatVector2;
using Gfx::Vector2;

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/String.h>
#include <math.h>
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<double> DoubleVector3;
}
namespace AK {
template<typename T>
struct Formatter<Gfx::Vector3<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Vector3<T>& value)
{
Formatter<StringView>::format(builder, value.to_string());
}
};
}
using Gfx::DoubleVector3;
using Gfx::FloatVector3;
using Gfx::Vector3;

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/String.h>
#include <math.h>
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<double> DoubleVector4;
}
namespace AK {
template<typename T>
struct Formatter<Gfx::Vector4<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Vector4<T>& value)
{
Formatter<StringView>::format(builder, value.to_string());
}
};
}
using Gfx::DoubleVector4;
using Gfx::FloatVector4;
using Gfx::Vector4;