GHOST: add GHOST_Rect.clampPoint method

Method for clamping a point inside a rectangle.
This commit is contained in:
Campbell Barton 2022-06-18 14:33:50 +10:00
parent 600c391a65
commit 5c814e75f2
1 changed files with 23 additions and 0 deletions

View File

@ -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);