From 5c814e75f2e866b31834e03f37a5918b021a1c1d Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 18 Jun 2022 14:33:50 +1000 Subject: GHOST: add GHOST_Rect.clampPoint method Method for clamping a point inside a rectangle. --- intern/ghost/GHOST_Rect.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/intern/ghost/GHOST_Rect.h b/intern/ghost/GHOST_Rect.h index a8082bf015a..1cbc75fe60b 100644 --- a/intern/ghost/GHOST_Rect.h +++ b/intern/ghost/GHOST_Rect.h @@ -101,6 +101,12 @@ class GHOST_Rect { * \param y: The y-coordinate of the point. */ virtual inline void wrapPoint(int32_t &x, int32_t &y, int32_t ofs, GHOST_TAxisFlag axis); + /** + * Confine x & y within the rectangle (inclusive). + * \param x: The x-coordinate of the point. + * \param y: The y-coordinate of the point. + */ + virtual inline void clampPoint(int32_t &x, int32_t &y); /** * Returns whether the point is inside this rectangle. @@ -248,6 +254,23 @@ inline void GHOST_Rect::wrapPoint(int32_t &x, int32_t &y, int32_t ofs, GHOST_TAx } } +inline void GHOST_Rect::clampPoint(int32_t &x, int32_t &y) +{ + if (x < m_l) { + x = m_l; + } + else if (x > m_r) { + x = m_r; + } + + if (y < m_t) { + y = m_t; + } + else if (y > m_b) { + y = m_b; + } +} + inline bool GHOST_Rect::isInside(int32_t x, int32_t y) const { return (x >= m_l) && (x <= m_r) && (y >= m_t) && (y <= m_b); -- cgit v1.2.3