2020-02-15 00:58:14 +01:00
|
|
|
/*
|
2021-06-16 19:24:27 +02:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-02-15 00:58:14 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-15 00:58:14 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-02-15 00:58:14 +01:00
|
|
|
#include <LibGfx/Point.h>
|
2020-07-25 21:31:47 -07:00
|
|
|
#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>
|
2020-02-15 00:58:14 +01:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<typename T>
|
2021-06-16 19:24:27 +02:00
|
|
|
void Point<T>::constrain(Rect<T> const& rect)
|
2020-07-21 23:46:15 -07:00
|
|
|
{
|
2023-05-06 20:54:38 +02:00
|
|
|
m_x = AK::clamp<T>(x(), rect.left(), rect.right());
|
|
|
|
m_y = AK::clamp<T>(y(), rect.top(), rect.bottom());
|
2020-07-21 23:46:15 -07:00
|
|
|
}
|
|
|
|
|
2021-08-24 23:39:07 +02:00
|
|
|
template<typename T>
|
2021-09-15 19:34:50 -04:00
|
|
|
[[nodiscard]] Point<T> Point<T>::end_point_for_aspect_ratio(Point<T> const& previous_end_point, float aspect_ratio) const
|
2021-08-24 23:39:07 +02:00
|
|
|
{
|
2021-09-15 19:34:50 -04:00
|
|
|
VERIFY(aspect_ratio > 0);
|
|
|
|
const T x_sign = previous_end_point.x() >= x() ? 1 : -1;
|
|
|
|
const T y_sign = previous_end_point.y() >= y() ? 1 : -1;
|
|
|
|
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 23:39:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<>
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString IntPoint::to_deprecated_string() const
|
2020-02-15 00:58:14 +01:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::formatted("[{},{}]", x(), y());
|
2020-02-15 00:58:14 +01:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<>
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString FloatPoint::to_deprecated_string() const
|
2020-02-15 00:58:14 +01:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::formatted("[{},{}]", x(), y());
|
2020-02-15 00:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-02-15 12:04:35 +01:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2022-11-15 11:24:59 -05:00
|
|
|
template<>
|
2023-01-01 23:37:35 -05:00
|
|
|
ErrorOr<void> encode(Encoder& encoder, Gfx::IntPoint const& point)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
2023-01-01 23:37:35 -05:00
|
|
|
TRY(encoder.encode(point.x()));
|
|
|
|
TRY(encoder.encode(point.y()));
|
|
|
|
return {};
|
2020-05-12 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
2022-11-15 11:24:59 -05:00
|
|
|
template<>
|
2022-12-22 20:40:33 -05:00
|
|
|
ErrorOr<Gfx::IntPoint> decode(Decoder& decoder)
|
2020-02-15 12:04:35 +01:00
|
|
|
{
|
2022-12-22 20:40:33 -05:00
|
|
|
auto x = TRY(decoder.decode<int>());
|
|
|
|
auto y = TRY(decoder.decode<int>());
|
|
|
|
return Gfx::IntPoint { x, y };
|
2020-02-15 12:04:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-07-25 21:31:47 -07:00
|
|
|
|
|
|
|
template class Gfx::Point<int>;
|
|
|
|
template class Gfx::Point<float>;
|