2020-02-15 02:58:14 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-15 02:58:14 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <LibGfx/Point.h>
|
2020-07-26 07:31:47 +03:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-03-29 20:03:13 +03:00
|
|
|
#include <LibIPC/Decoder.h>
|
2020-05-12 19:05:43 +03:00
|
|
|
#include <LibIPC/Encoder.h>
|
2020-02-15 02:58:14 +03:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2020-07-26 07:31:47 +03:00
|
|
|
template<typename T>
|
|
|
|
void Point<T>::constrain(const Rect<T>& rect)
|
2020-07-22 09:46:15 +03:00
|
|
|
{
|
2020-07-26 07:31:47 +03:00
|
|
|
if (x() < rect.left()) {
|
|
|
|
set_x(rect.left());
|
|
|
|
} else if (x() > rect.right()) {
|
|
|
|
set_x(rect.right());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y() < rect.top()) {
|
|
|
|
set_y(rect.top());
|
|
|
|
} else if (y() > rect.bottom()) {
|
|
|
|
set_y(rect.bottom());
|
|
|
|
}
|
2020-07-22 09:46:15 +03:00
|
|
|
}
|
|
|
|
|
2020-07-26 07:31:47 +03:00
|
|
|
template<>
|
2020-06-10 11:57:59 +03:00
|
|
|
String IntPoint::to_string() const
|
2020-02-15 02:58:14 +03:00
|
|
|
{
|
2021-01-03 17:26:47 +03:00
|
|
|
return String::formatted("[{},{}]", x(), y());
|
2020-02-15 02:58:14 +03:00
|
|
|
}
|
|
|
|
|
2020-07-26 07:31:47 +03:00
|
|
|
template<>
|
|
|
|
String FloatPoint::to_string() const
|
2020-02-15 02:58:14 +03:00
|
|
|
{
|
2021-01-03 17:26:47 +03:00
|
|
|
return String::formatted("[{},{}]", x(), y());
|
2020-02-15 02:58:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-02-15 14:04:35 +03:00
|
|
|
|
|
|
|
namespace IPC {
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
bool encode(Encoder& encoder, const Gfx::IntPoint& point)
|
2020-05-12 19:05:43 +03:00
|
|
|
{
|
|
|
|
encoder << point.x() << point.y();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
bool decode(Decoder& decoder, Gfx::IntPoint& point)
|
2020-02-15 14:04:35 +03:00
|
|
|
{
|
2020-02-23 03:32:00 +03:00
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
2020-03-29 20:03:13 +03:00
|
|
|
if (!decoder.decode(x))
|
|
|
|
return false;
|
|
|
|
if (!decoder.decode(y))
|
2020-02-15 14:04:35 +03:00
|
|
|
return false;
|
|
|
|
point = { x, y };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-07-26 07:31:47 +03:00
|
|
|
|
|
|
|
template class Gfx::Point<int>;
|
|
|
|
template class Gfx::Point<float>;
|