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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
#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
|
|
|
{
|
2021-07-16 22:47:21 +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>
|
|
|
|
[[nodiscard]] Point<T> Point<T>::end_point_for_square_aspect_ratio(Point<T> const& previous_end_point) const
|
|
|
|
{
|
|
|
|
const T dx = previous_end_point.x() - x();
|
|
|
|
const T dy = previous_end_point.y() - y();
|
|
|
|
const T x_sign = dx >= 0 ? 1 : -1;
|
|
|
|
const T y_sign = dy >= 0 ? 1 : -1;
|
|
|
|
const T abs_size = AK::max(AK::abs(dx), AK::abs(dy));
|
|
|
|
return { x() + x_sign * abs_size, y() + y_sign * abs_size };
|
|
|
|
}
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<>
|
2020-06-10 10:57:59 +02:00
|
|
|
String IntPoint::to_string() const
|
2020-02-15 00:58:14 +01:00
|
|
|
{
|
2021-01-03 15:26:47 +01:00
|
|
|
return String::formatted("[{},{}]", x(), y());
|
2020-02-15 00:58:14 +01:00
|
|
|
}
|
|
|
|
|
2020-07-25 21:31:47 -07:00
|
|
|
template<>
|
|
|
|
String FloatPoint::to_string() const
|
2020-02-15 00:58:14 +01:00
|
|
|
{
|
2021-01-03 15:26:47 +01:00
|
|
|
return String::formatted("[{},{}]", x(), y());
|
2020-02-15 00:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-02-15 12:04:35 +01:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2021-06-16 19:24:27 +02:00
|
|
|
bool encode(Encoder& encoder, Gfx::IntPoint const& point)
|
2020-05-12 18:05:43 +02:00
|
|
|
{
|
|
|
|
encoder << point.x() << point.y();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-10 10:57:59 +02:00
|
|
|
bool decode(Decoder& decoder, Gfx::IntPoint& point)
|
2020-02-15 12:04:35 +01:00
|
|
|
{
|
2020-02-23 01:32:00 +01:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2020-03-29 19:03:13 +02:00
|
|
|
if (!decoder.decode(x))
|
|
|
|
return false;
|
|
|
|
if (!decoder.decode(y))
|
2020-02-15 12:04:35 +01:00
|
|
|
return false;
|
|
|
|
point = { x, y };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-07-25 21:31:47 -07:00
|
|
|
|
|
|
|
template class Gfx::Point<int>;
|
|
|
|
template class Gfx::Point<float>;
|