Просмотр исходного кода

LibGfx: Make formatting of spatial types work with non-int/floats

Line, Point, Rect, and Size now all have Formatters that will work with
any type that itself has a Formatter.
Sam Atkins 2 лет назад
Родитель
Сommit
07bceb861a

+ 13 - 0
Userland/Libraries/LibGfx/Line.h

@@ -6,6 +6,7 @@
 
 
 #pragma once
 #pragma once
 
 
+#include <AK/Format.h>
 #include <AK/Optional.h>
 #include <AK/Optional.h>
 #include <AK/StdLibExtras.h>
 #include <AK/StdLibExtras.h>
 #include <AK/String.h>
 #include <AK/String.h>
@@ -153,3 +154,15 @@ inline String FloatLine::to_string() const
 }
 }
 
 
 }
 }
+
+namespace AK {
+
+template<typename T>
+struct Formatter<Gfx::Line<T>> : Formatter<FormatString> {
+    ErrorOr<void> format(FormatBuilder& builder, Gfx::Line<T> const& value)
+    {
+        return Formatter<FormatString>::format(builder, "[{},{} -> {},{}]"sv, value.a().x(), value.a().y(), value.b().x(), value.b().y());
+    }
+};
+
+}

+ 2 - 2
Userland/Libraries/LibGfx/Point.h

@@ -285,10 +285,10 @@ inline Point<T> cubic_interpolate(Point<T> const& p1, Point<T> const& p2, Point<
 namespace AK {
 namespace AK {
 
 
 template<typename T>
 template<typename T>
-struct Formatter<Gfx::Point<T>> : Formatter<StringView> {
+struct Formatter<Gfx::Point<T>> : Formatter<FormatString> {
     ErrorOr<void> format(FormatBuilder& builder, Gfx::Point<T> const& value)
     ErrorOr<void> format(FormatBuilder& builder, Gfx::Point<T> const& value)
     {
     {
-        return Formatter<StringView>::format(builder, value.to_string());
+        return Formatter<FormatString>::format(builder, "[{},{}]"sv, value.x(), value.y());
     }
     }
 };
 };
 
 

+ 2 - 2
Userland/Libraries/LibGfx/Rect.h

@@ -1026,10 +1026,10 @@ using FloatRect = Rect<float>;
 namespace AK {
 namespace AK {
 
 
 template<typename T>
 template<typename T>
-struct Formatter<Gfx::Rect<T>> : Formatter<StringView> {
+struct Formatter<Gfx::Rect<T>> : Formatter<FormatString> {
     ErrorOr<void> format(FormatBuilder& builder, Gfx::Rect<T> const& value)
     ErrorOr<void> format(FormatBuilder& builder, Gfx::Rect<T> const& value)
     {
     {
-        return Formatter<StringView>::format(builder, value.to_string());
+        return Formatter<FormatString>::format(builder, "[{},{} {}x{}]"sv, value.x(), value.y(), value.width(), value.height());
     }
     }
 };
 };
 
 

+ 2 - 2
Userland/Libraries/LibGfx/Size.h

@@ -183,10 +183,10 @@ using FloatSize = Size<float>;
 namespace AK {
 namespace AK {
 
 
 template<typename T>
 template<typename T>
-struct Formatter<Gfx::Size<T>> : Formatter<StringView> {
+struct Formatter<Gfx::Size<T>> : Formatter<FormatString> {
     ErrorOr<void> format(FormatBuilder& builder, Gfx::Size<T> const& value)
     ErrorOr<void> format(FormatBuilder& builder, Gfx::Size<T> const& value)
     {
     {
-        return Formatter<StringView>::format(builder, value.to_string());
+        return Formatter<FormatString>::format(builder, "[{}x{}]"sv, value.width(), value.height());
     }
     }
 };
 };