1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:47:44 +00:00

LibGfx: Templatize Point, Size, and Rect

This commit is contained in:
Matthew Olsson 2020-07-25 21:31:47 -07:00 committed by Andreas Kling
parent 7a1c328417
commit 335916d8db
33 changed files with 404 additions and 835 deletions

View file

@ -33,12 +33,13 @@
namespace Gfx {
void IntRect::intersect(const IntRect& other)
template<typename T>
void Rect<T>::intersect(const Rect<T>& other)
{
int l = max(left(), other.left());
int r = min(right(), other.right());
int t = max(top(), other.top());
int b = min(bottom(), other.bottom());
T l = max(left(), other.left());
T r = min(right(), other.right());
T t = max(top(), other.top());
T b = min(bottom(), other.bottom());
if (l > r || t > b) {
m_location = {};
@ -52,13 +53,14 @@ void IntRect::intersect(const IntRect& other)
m_size.set_height((b - t) + 1);
}
IntRect IntRect::united(const IntRect& other) const
template<typename T>
Rect<T> Rect<T>::united(const Rect<T>& other) const
{
if (is_null())
return other;
if (other.is_null())
return *this;
IntRect rect;
Rect<T> rect;
rect.set_left(min(left(), other.left()));
rect.set_top(min(top(), other.top()));
rect.set_right(max(right(), other.right()));
@ -66,32 +68,33 @@ IntRect IntRect::united(const IntRect& other) const
return rect;
}
Vector<IntRect, 4> IntRect::shatter(const IntRect& hammer) const
template<typename T>
Vector<Rect<T>, 4> Rect<T>::shatter(const Rect<T>& hammer) const
{
Vector<IntRect, 4> pieces;
Vector<Rect<T>, 4> pieces;
if (!intersects(hammer)) {
pieces.unchecked_append(*this);
return pieces;
}
IntRect top_shard {
Rect<T> top_shard {
x(),
y(),
width(),
hammer.y() - y()
};
IntRect bottom_shard {
Rect<T> bottom_shard {
x(),
hammer.y() + hammer.height(),
width(),
(y() + height()) - (hammer.y() + hammer.height())
};
IntRect left_shard {
Rect<T> left_shard {
x(),
max(hammer.y(), y()),
hammer.x() - x(),
min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
};
IntRect right_shard {
Rect<T> right_shard {
hammer.x() + hammer.width(),
max(hammer.y(), y()),
right() - hammer.right(),
@ -109,7 +112,8 @@ Vector<IntRect, 4> IntRect::shatter(const IntRect& hammer) const
return pieces;
}
void IntRect::align_within(const IntRect& other, TextAlignment alignment)
template<typename T>
void Rect<T>::align_within(const Rect<T>& other, TextAlignment alignment)
{
switch (alignment) {
case TextAlignment::Center:
@ -133,14 +137,16 @@ void IntRect::align_within(const IntRect& other, TextAlignment alignment)
}
}
template<>
String IntRect::to_string() const
{
return String::format("[%d,%d %dx%d]", x(), y(), width(), height());
}
const LogStream& operator<<(const LogStream& stream, const IntRect& value)
template<>
String FloatRect::to_string() const
{
return stream << value.to_string();
return String::format("[%f,%f %fx%f]", x(), y(), width(), height());
}
}
@ -166,3 +172,6 @@ bool decode(Decoder& decoder, Gfx::IntRect& rect)
}
}
template class Gfx::Rect<int>;
template class Gfx::Rect<float>;