Luna/libui/src/Rect.cpp

63 lines
1.5 KiB
C++
Raw Normal View History

2023-08-14 10:34:27 +00:00
/**
* @file Rect.cpp
* @author apio (cloudapio.eu)
* @brief A simple 2D rectangle representation.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
2023-08-03 10:19:45 +00:00
#include <ui/Rect.h>
namespace ui
{
bool Rect::contains(Point point)
{
2023-08-03 15:38:49 +00:00
return (point.x >= pos.x) && (point.y >= pos.y) && (point.x <= (pos.x + width)) &&
(point.y <= (pos.y + height));
2023-08-03 10:19:45 +00:00
}
2023-08-06 10:44:16 +00:00
bool Rect::contains(Rect rect)
{
if (!contains(rect.pos)) return false;
Point rel = relative(rect.pos);
if ((rel.x + rect.width) > width) return false;
if ((rel.y + rect.height) > height) return false;
return true;
}
2023-08-03 10:19:45 +00:00
Point Rect::normalize(Point point)
{
2023-08-03 15:38:49 +00:00
if (point.x < pos.x) point.x = pos.x;
if (point.y < pos.y) point.y = pos.y;
if (point.x > pos.x + width) point.x = pos.x + width;
if (point.y > pos.y + height) point.y = pos.y + height;
2023-08-03 10:19:45 +00:00
return point;
}
2023-08-03 15:38:49 +00:00
2023-08-04 11:08:02 +00:00
Point Rect::relative(Point point)
{
point = normalize(point);
point.x -= pos.x;
point.y -= pos.y;
return point;
}
Point Rect::absolute(Point point)
{
point.x += pos.x;
point.y += pos.y;
return point;
}
Rect Rect::absolute(Rect rect)
{
return Rect { absolute(rect.pos), rect.width, rect.height };
}
Rect Rect::normalized()
2023-08-03 15:38:49 +00:00
{
return Rect { ui::Point { pos.x < 0 ? 0 : pos.x, pos.y < 0 ? 0 : pos.y }, width, height };
}
2023-08-03 10:19:45 +00:00
};