2020-02-14 20:58:14 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <andreas@ladybird.org>
|
2020-02-14 20:58:14 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-14 20:58:14 -03:00
|
|
|
*/
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
#include <AK/ByteString.h>
|
2020-02-14 20:58:14 -03:00
|
|
|
#include <LibGfx/Point.h>
|
2020-07-26 01:31:47 -03:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-03-29 14:03:13 -03:00
|
|
|
#include <LibIPC/Decoder.h>
|
2020-05-12 13:05:43 -03:00
|
|
|
#include <LibIPC/Encoder.h>
|
2020-02-14 20:58:14 -03:00
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
2020-07-26 01:31:47 -03:00
|
|
|
template<typename T>
|
2021-06-16 14:24:27 -03:00
|
|
|
void Point<T>::constrain(Rect<T> const& rect)
|
2020-07-22 03:46:15 -03:00
|
|
|
{
|
2023-05-21 19:41:18 -03:00
|
|
|
m_x = AK::clamp<T>(x(), rect.left(), rect.right() - 1);
|
|
|
|
|
m_y = AK::clamp<T>(y(), rect.top(), rect.bottom() - 1);
|
2020-07-22 03:46:15 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-24 18:39:07 -03:00
|
|
|
template<typename T>
|
2021-09-15 20:34:50 -03:00
|
|
|
[[nodiscard]] Point<T> Point<T>::end_point_for_aspect_ratio(Point<T> const& previous_end_point, float aspect_ratio) const
|
2021-08-24 18:39:07 -03:00
|
|
|
{
|
2021-09-15 20:34:50 -03:00
|
|
|
VERIFY(aspect_ratio > 0);
|
2024-04-18 16:32:56 -03:00
|
|
|
T const x_sign = previous_end_point.x() >= x() ? 1 : -1;
|
|
|
|
|
T const y_sign = previous_end_point.y() >= y() ? 1 : -1;
|
2021-09-15 20:34:50 -03:00
|
|
|
T dx = AK::abs(previous_end_point.x() - x());
|
|
|
|
|
T dy = AK::abs(previous_end_point.y() - y());
|
|
|
|
|
if (dx > dy) {
|
|
|
|
|
dy = (T)((float)dx / aspect_ratio);
|
|
|
|
|
} else {
|
|
|
|
|
dx = (T)((float)dy * aspect_ratio);
|
|
|
|
|
}
|
|
|
|
|
return { x() + x_sign * dx, y() + y_sign * dy };
|
2021-08-24 18:39:07 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-26 01:31:47 -03:00
|
|
|
template<>
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString IntPoint::to_byte_string() const
|
2020-02-14 20:58:14 -03:00
|
|
|
{
|
2023-12-16 11:19:34 -03:00
|
|
|
return ByteString::formatted("[{},{}]", x(), y());
|
2020-02-14 20:58:14 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-26 01:31:47 -03:00
|
|
|
template<>
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString FloatPoint::to_byte_string() const
|
2020-02-14 20:58:14 -03:00
|
|
|
{
|
2023-12-16 11:19:34 -03:00
|
|
|
return ByteString::formatted("[{},{}]", x(), y());
|
2020-02-14 20:58:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-02-15 08:04:35 -03:00
|
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
|
2024-04-13 02:21:20 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<void> encode(Encoder& encoder, Gfx::IntPoint const& point)
|
2020-05-12 13:05:43 -03:00
|
|
|
{
|
2023-01-02 01:37:35 -03:00
|
|
|
TRY(encoder.encode(point.x()));
|
|
|
|
|
TRY(encoder.encode(point.y()));
|
|
|
|
|
return {};
|
2020-05-12 13:05:43 -03:00
|
|
|
}
|
|
|
|
|
|
2024-04-13 02:21:20 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<void> encode(Encoder& encoder, Gfx::FloatPoint const& point)
|
|
|
|
|
{
|
|
|
|
|
TRY(encoder.encode(point.x()));
|
|
|
|
|
TRY(encoder.encode(point.y()));
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2024-02-14 03:07:55 -03:00
|
|
|
|
2022-11-15 13:24:59 -03:00
|
|
|
template<>
|
2022-12-22 22:40:33 -03:00
|
|
|
ErrorOr<Gfx::IntPoint> decode(Decoder& decoder)
|
2020-02-15 08:04:35 -03:00
|
|
|
{
|
2022-12-22 22:40:33 -03:00
|
|
|
auto x = TRY(decoder.decode<int>());
|
|
|
|
|
auto y = TRY(decoder.decode<int>());
|
|
|
|
|
return Gfx::IntPoint { x, y };
|
2020-02-15 08:04:35 -03:00
|
|
|
}
|
|
|
|
|
|
2024-02-14 03:07:55 -03:00
|
|
|
template<>
|
|
|
|
|
ErrorOr<Gfx::FloatPoint> decode(Decoder& decoder)
|
|
|
|
|
{
|
|
|
|
|
auto x = TRY(decoder.decode<float>());
|
|
|
|
|
auto y = TRY(decoder.decode<float>());
|
|
|
|
|
return Gfx::FloatPoint { x, y };
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 08:04:35 -03:00
|
|
|
}
|
2020-07-26 01:31:47 -03:00
|
|
|
|
|
|
|
|
template class Gfx::Point<int>;
|
|
|
|
|
template class Gfx::Point<float>;
|