diff --git a/libui/include/ui/Rect.h b/libui/include/ui/Rect.h index 6503a6ed..da6a2603 100644 --- a/libui/include/ui/Rect.h +++ b/libui/include/ui/Rect.h @@ -37,11 +37,27 @@ namespace ui */ Point relative(Point pos); + /** + * @brief Transform a position relative to this rectangle to an absolute position. + * + * @param pos The original relative position. + * @return Point The absolute position. + */ + Point absolute(Point pos); + + /** + * @brief Transform another rectangle relative to this one to an absolute rectangle. + * + * @param rect The original relative rectangle. + * @return Point The absolute rectangle. + */ + Rect absolute(Rect rect); + /** * @brief Return a copy of this rectangle with no negative values (normalized to 0). * * @return Rect The new rectangle. */ - Rect absolute(); + Rect normalized(); }; } diff --git a/libui/src/Rect.cpp b/libui/src/Rect.cpp index 448655ad..e96eb7e9 100644 --- a/libui/src/Rect.cpp +++ b/libui/src/Rect.cpp @@ -25,7 +25,19 @@ namespace ui return point; } - Rect Rect::absolute() + 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() { return Rect { ui::Point { pos.x < 0 ? 0 : pos.x, pos.y < 0 ? 0 : pos.y }, width, height }; }