2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-06-16 19:18:01 +02:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-01-09 02:06:04 +01:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-02-15 00:58:14 +01:00
|
|
|
#include <AK/String.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
#include <LibGfx/Rect.h>
|
2020-03-29 19:03:13 +02:00
|
|
|
#include <LibIPC/Decoder.h>
|
2020-05-12 18:05:43 +02:00
|
|
|
#include <LibIPC/Encoder.h>
|
2019-01-09 02:06:04 +01:00
|
|
|
|
2020-02-06 13:02:38 +01:00
|
|
|
namespace Gfx {
|
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<typename T>
|
2021-06-16 19:18:01 +02:00
|
|
|
void Rect<T>::intersect(Rect<T> const& other)
|
2019-01-09 02:06:04 +01:00
|
|
|
{
|
2020-07-25 21:31:47 -07:00
|
|
|
T l = max(left(), other.left());
|
|
|
|
|
T r = min(right(), other.right());
|
|
|
|
|
T t = max(top(), other.top());
|
|
|
|
|
T b = min(bottom(), other.bottom());
|
2019-01-09 02:06:04 +01:00
|
|
|
|
2019-01-20 23:42:36 +01:00
|
|
|
if (l > r || t > b) {
|
2019-06-07 11:46:55 +02:00
|
|
|
m_location = {};
|
|
|
|
|
m_size = {};
|
2019-01-09 02:06:04 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 17:54:06 +01:00
|
|
|
m_location.set_x(l);
|
|
|
|
|
m_location.set_y(t);
|
|
|
|
|
m_size.set_width((r - l) + 1);
|
|
|
|
|
m_size.set_height((b - t) + 1);
|
2019-01-09 02:06:04 +01:00
|
|
|
}
|
2019-01-13 00:04:23 +01:00
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<typename T>
|
2021-06-16 19:18:01 +02:00
|
|
|
Rect<T> Rect<T>::united(Rect<T> const& other) const
|
2019-01-13 00:04:23 +01:00
|
|
|
{
|
2019-01-18 04:37:49 +01:00
|
|
|
if (is_null())
|
|
|
|
|
return other;
|
|
|
|
|
if (other.is_null())
|
|
|
|
|
return *this;
|
2020-07-25 21:31:47 -07:00
|
|
|
Rect<T> rect;
|
2019-01-13 00:04:23 +01:00
|
|
|
rect.set_left(min(left(), other.left()));
|
|
|
|
|
rect.set_top(min(top(), other.top()));
|
|
|
|
|
rect.set_right(max(right(), other.right()));
|
|
|
|
|
rect.set_bottom(max(bottom(), other.bottom()));
|
|
|
|
|
return rect;
|
|
|
|
|
}
|
2019-02-19 14:49:23 +01:00
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<typename T>
|
2021-06-16 19:18:01 +02:00
|
|
|
Vector<Rect<T>, 4> Rect<T>::shatter(Rect<T> const& hammer) const
|
2019-02-19 14:49:23 +01:00
|
|
|
{
|
2020-07-25 21:31:47 -07:00
|
|
|
Vector<Rect<T>, 4> pieces;
|
2019-02-19 14:49:23 +01:00
|
|
|
if (!intersects(hammer)) {
|
2019-05-27 12:50:18 +02:00
|
|
|
pieces.unchecked_append(*this);
|
2019-02-19 14:49:23 +01:00
|
|
|
return pieces;
|
|
|
|
|
}
|
2020-07-25 21:31:47 -07:00
|
|
|
Rect<T> top_shard {
|
2019-02-19 14:49:23 +01:00
|
|
|
x(),
|
|
|
|
|
y(),
|
|
|
|
|
width(),
|
|
|
|
|
hammer.y() - y()
|
|
|
|
|
};
|
2020-07-25 21:31:47 -07:00
|
|
|
Rect<T> bottom_shard {
|
2019-02-19 14:49:23 +01:00
|
|
|
x(),
|
|
|
|
|
hammer.y() + hammer.height(),
|
|
|
|
|
width(),
|
|
|
|
|
(y() + height()) - (hammer.y() + hammer.height())
|
|
|
|
|
};
|
2020-07-25 21:31:47 -07:00
|
|
|
Rect<T> left_shard {
|
2019-02-19 14:49:23 +01:00
|
|
|
x(),
|
|
|
|
|
max(hammer.y(), y()),
|
|
|
|
|
hammer.x() - x(),
|
|
|
|
|
min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
|
|
|
|
|
};
|
2020-07-25 21:31:47 -07:00
|
|
|
Rect<T> right_shard {
|
2019-02-19 14:49:23 +01:00
|
|
|
hammer.x() + hammer.width(),
|
|
|
|
|
max(hammer.y(), y()),
|
2019-02-19 16:37:12 +01:00
|
|
|
right() - hammer.right(),
|
2019-02-19 14:49:23 +01:00
|
|
|
min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
|
|
|
|
|
};
|
2020-07-14 10:15:56 -06:00
|
|
|
if (!top_shard.is_empty())
|
2019-05-27 12:50:18 +02:00
|
|
|
pieces.unchecked_append(top_shard);
|
2020-07-14 10:15:56 -06:00
|
|
|
if (!bottom_shard.is_empty())
|
2019-05-27 12:50:18 +02:00
|
|
|
pieces.unchecked_append(bottom_shard);
|
2020-07-14 10:15:56 -06:00
|
|
|
if (!left_shard.is_empty())
|
2019-05-27 12:50:18 +02:00
|
|
|
pieces.unchecked_append(left_shard);
|
2020-07-14 10:15:56 -06:00
|
|
|
if (!right_shard.is_empty())
|
2019-05-27 12:50:18 +02:00
|
|
|
pieces.unchecked_append(right_shard);
|
2019-02-19 14:49:23 +01:00
|
|
|
|
|
|
|
|
return pieces;
|
|
|
|
|
}
|
2019-05-25 20:15:52 +02:00
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<typename T>
|
2021-06-16 19:18:01 +02:00
|
|
|
void Rect<T>::align_within(Rect<T> const& other, TextAlignment alignment)
|
2019-05-25 20:15:52 +02:00
|
|
|
{
|
|
|
|
|
switch (alignment) {
|
|
|
|
|
case TextAlignment::Center:
|
|
|
|
|
center_within(other);
|
|
|
|
|
return;
|
|
|
|
|
case TextAlignment::TopLeft:
|
|
|
|
|
set_location(other.location());
|
|
|
|
|
return;
|
2019-09-06 19:23:36 +02:00
|
|
|
case TextAlignment::TopRight:
|
|
|
|
|
set_x(other.x() + other.width() - width());
|
|
|
|
|
set_y(other.y());
|
|
|
|
|
return;
|
2019-05-25 20:15:52 +02:00
|
|
|
case TextAlignment::CenterLeft:
|
|
|
|
|
set_x(other.x());
|
|
|
|
|
center_vertically_within(other);
|
|
|
|
|
return;
|
|
|
|
|
case TextAlignment::CenterRight:
|
|
|
|
|
set_x(other.x() + other.width() - width());
|
|
|
|
|
center_vertically_within(other);
|
|
|
|
|
return;
|
2021-05-21 01:03:02 +01:00
|
|
|
case TextAlignment::BottomLeft:
|
|
|
|
|
set_x(other.x());
|
|
|
|
|
set_y(other.y() + other.height() - height());
|
|
|
|
|
return;
|
2020-08-21 08:52:39 -06:00
|
|
|
case TextAlignment::BottomRight:
|
|
|
|
|
set_x(other.x() + other.width() - width());
|
|
|
|
|
set_y(other.y() + other.height() - height());
|
|
|
|
|
return;
|
2019-05-25 20:15:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-02-06 13:02:38 +01:00
|
|
|
|
2021-01-23 01:16:06 +01:00
|
|
|
template<typename T>
|
2021-06-16 19:18:01 +02:00
|
|
|
void Rect<T>::set_size_around(Size<T> const& new_size, Point<T> const& fixed_point)
|
2021-01-23 01:16:06 +01:00
|
|
|
{
|
|
|
|
|
const T new_x = fixed_point.x() - (T)(new_size.width() * ((float)(fixed_point.x() - x()) / width()));
|
|
|
|
|
const T new_y = fixed_point.y() - (T)(new_size.height() * ((float)(fixed_point.y() - y()) / height()));
|
|
|
|
|
|
|
|
|
|
set_location({ new_x, new_y });
|
|
|
|
|
set_size(new_size);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<>
|
2020-06-10 10:57:59 +02:00
|
|
|
String IntRect::to_string() const
|
2020-02-15 00:58:14 +01:00
|
|
|
{
|
2021-04-21 21:11:16 +02:00
|
|
|
return String::formatted("[{},{} {}x{}]", x(), y(), width(), height());
|
2020-02-15 00:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<>
|
|
|
|
|
String FloatRect::to_string() const
|
2020-02-15 00:58:14 +01:00
|
|
|
{
|
2021-04-15 00:36:14 -07:00
|
|
|
return String::formatted("[{},{} {}x{}]", x(), y(), width(), height());
|
2020-02-15 00:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 13:02:38 +01:00
|
|
|
}
|
2020-02-15 12:04:35 +01:00
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
2021-06-16 19:18:01 +02:00
|
|
|
bool encode(Encoder& encoder, Gfx::IntRect const& rect)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
|
|
|
|
encoder << rect.location() << rect.size();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
bool decode(Decoder& decoder, Gfx::IntRect& rect)
|
2020-02-15 12:04:35 +01:00
|
|
|
{
|
2020-06-10 10:57:59 +02:00
|
|
|
Gfx::IntPoint point;
|
|
|
|
|
Gfx::IntSize size;
|
2020-03-29 19:03:13 +02:00
|
|
|
if (!decoder.decode(point))
|
2020-02-15 12:04:35 +01:00
|
|
|
return false;
|
2020-03-29 19:03:13 +02:00
|
|
|
if (!decoder.decode(size))
|
2020-02-15 12:04:35 +01:00
|
|
|
return false;
|
|
|
|
|
rect = { point, size };
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-07-25 21:31:47 -07:00
|
|
|
|
|
|
|
|
template class Gfx::Rect<int>;
|
|
|
|
|
template class Gfx::Rect<float>;
|