From 2b3e9b778af397c4807c8c69c9cb745bae285a0f Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 4 Aug 2023 13:08:02 +0200 Subject: [PATCH] libui: Add Rect::relative --- libui/include/ui/Rect.h | 8 ++++++++ libui/src/Rect.cpp | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/libui/include/ui/Rect.h b/libui/include/ui/Rect.h index 774013ad..6503a6ed 100644 --- a/libui/include/ui/Rect.h +++ b/libui/include/ui/Rect.h @@ -29,6 +29,14 @@ namespace ui */ Point normalize(Point point); + /** + * @brief Transform an absolute position to a position relative to this rectangle. + * + * @param pos The original absolute position. + * @return Point The position relative to this rectangle. + */ + Point relative(Point pos); + /** * @brief Return a copy of this rectangle with no negative values (normalized to 0). * diff --git a/libui/src/Rect.cpp b/libui/src/Rect.cpp index 444157a0..448655ad 100644 --- a/libui/src/Rect.cpp +++ b/libui/src/Rect.cpp @@ -17,6 +17,14 @@ namespace ui return point; } + Point Rect::relative(Point point) + { + point = normalize(point); + point.x -= pos.x; + point.y -= pos.y; + return point; + } + Rect Rect::absolute() { return Rect { ui::Point { pos.x < 0 ? 0 : pos.x, pos.y < 0 ? 0 : pos.y }, width, height };