2023-06-14 17:39:46 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-02-25 11:28:33 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup GHOST
|
2011-02-25 11:28:33 +00:00
|
|
|
* Macro's used in GHOST debug target.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#pragma once
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|
#include "GHOST_Types.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implements rectangle functionality.
|
|
|
|
|
* The four extreme coordinates are stored as left, top, right and bottom.
|
|
|
|
|
* To be valid, a rectangle should have a left coordinate smaller than or equal to right.
|
|
|
|
|
* To be valid, a rectangle should have a top coordinate smaller than or equal to bottom.
|
|
|
|
|
*/
|
|
|
|
|
class GHOST_Rect {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a rectangle with the given values.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \param l: requested left coordinate of the rectangle.
|
|
|
|
|
* \param t: requested top coordinate of the rectangle.
|
|
|
|
|
* \param r: requested right coordinate of the rectangle.
|
|
|
|
|
* \param b: requested bottom coordinate of the rectangle.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
GHOST_Rect(int32_t l = 0, int32_t t = 0, int32_t r = 0, int32_t b = 0)
|
2025-08-16 14:32:52 +10:00
|
|
|
: l_(l), t_(t), r_(r), b_(b)
|
2012-05-19 09:23:08 +00:00
|
|
|
{
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2025-01-26 20:08:09 +01:00
|
|
|
virtual ~GHOST_Rect() = default;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Access to rectangle width.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \return width of the rectangle.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual inline int32_t getWidth() const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Access to rectangle height.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \return height of the rectangle.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual inline int32_t getHeight() const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Sets all members of the rectangle.
|
2019-05-01 20:23:13 +10:00
|
|
|
* \param l: requested left coordinate of the rectangle.
|
|
|
|
|
* \param t: requested top coordinate of the rectangle.
|
|
|
|
|
* \param r: requested right coordinate of the rectangle.
|
|
|
|
|
* \param b: requested bottom coordinate of the rectangle.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual inline void set(int32_t l, int32_t t, int32_t r, int32_t b);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Returns whether this rectangle is empty.
|
|
|
|
|
* Empty rectangles are rectangles that have width==0 and/or height==0.
|
2019-05-01 20:23:13 +10:00
|
|
|
* \return boolean value (true==empty rectangle)
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
virtual inline bool isEmpty() const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Returns whether this rectangle is valid.
|
2025-08-16 14:32:52 +10:00
|
|
|
* Valid rectangles are rectangles that have l_ <= r_ and t_ <= b_.
|
2019-05-01 20:23:13 +10:00
|
|
|
* Thus, empty rectangles are valid.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \return boolean value (true==valid rectangle)
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
virtual inline bool isValid() const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Grows (or shrinks the rectangle).
|
|
|
|
|
* The method avoids negative insets making the rectangle invalid
|
2019-05-01 20:23:13 +10:00
|
|
|
* \param i: The amount of offset given to each extreme (negative values shrink the rectangle).
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual void inset(int32_t i);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Does a union of the rectangle given and this rectangle.
|
|
|
|
|
* The result is stored in this rectangle.
|
2019-05-01 20:23:13 +10:00
|
|
|
* \param r: The rectangle that is input for the union operation.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
virtual inline void unionRect(const GHOST_Rect &r);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Grows the rectangle to included a point.
|
2019-05-01 20:23:13 +10:00
|
|
|
* \param x: The x-coordinate of the point.
|
|
|
|
|
* \param y: The y-coordinate of the point.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual inline void unionPoint(int32_t x, int32_t y);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-10-17 14:08:01 +00:00
|
|
|
/**
|
|
|
|
|
* Grows the rectangle to included a point.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \param x: The x-coordinate of the point.
|
|
|
|
|
* \param y: The y-coordinate of the point.
|
2009-10-17 14:08:01 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual inline void wrapPoint(int32_t &x, int32_t &y, int32_t ofs, GHOST_TAxisFlag axis);
|
2022-06-18 14:33:50 +10:00
|
|
|
/**
|
|
|
|
|
* 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);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Returns whether the point is inside this rectangle.
|
|
|
|
|
* Point on the boundary is considered inside.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \param x: x-coordinate of point to test.
|
|
|
|
|
* \param y: y-coordinate of point to test.
|
2012-09-06 02:10:09 +00:00
|
|
|
* \return boolean value (true if point is inside).
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual inline bool isInside(int32_t x, int32_t y) const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Returns whether the rectangle is inside this rectangle.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \param r: rectangle to test.
|
|
|
|
|
* \return visibility (not, partially or fully visible).
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
virtual GHOST_TVisibility getVisibility(GHOST_Rect &r) const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Sets rectangle members.
|
|
|
|
|
* Sets rectangle members such that it is centered at the given location.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \param cx: requested center x-coordinate of the rectangle.
|
|
|
|
|
* \param cy: requested center y-coordinate of the rectangle.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual void setCenter(int32_t cx, int32_t cy);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Sets rectangle members.
|
|
|
|
|
* Sets rectangle members such that it is centered at the given location,
|
|
|
|
|
* with the width requested.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \param cx: requested center x-coordinate of the rectangle.
|
|
|
|
|
* \param cy: requested center y-coordinate of the rectangle.
|
|
|
|
|
* \param w: requested width of the rectangle.
|
|
|
|
|
* \param h: requested height of the rectangle.
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2021-07-05 10:10:20 -07:00
|
|
|
virtual void setCenter(int32_t cx, int32_t cy, int32_t w, int32_t h);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/**
|
|
|
|
|
* Clips a rectangle.
|
|
|
|
|
* Updates the rectangle given such that it will fit within this one.
|
|
|
|
|
* This can result in an empty rectangle.
|
2020-11-06 14:18:55 +11:00
|
|
|
* \param r: the rectangle to clip.
|
|
|
|
|
* \return whether clipping has occurred
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
2012-05-19 09:23:08 +00:00
|
|
|
virtual bool clip(GHOST_Rect &r) const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/** Left coordinate of the rectangle */
|
2025-08-16 14:32:52 +10:00
|
|
|
int32_t l_;
|
2002-10-12 11:37:38 +00:00
|
|
|
/** Top coordinate of the rectangle */
|
2025-08-16 14:32:52 +10:00
|
|
|
int32_t t_;
|
2002-10-12 11:37:38 +00:00
|
|
|
/** Right coordinate of the rectangle */
|
2025-08-16 14:32:52 +10:00
|
|
|
int32_t r_;
|
2002-10-12 11:37:38 +00:00
|
|
|
/** Bottom coordinate of the rectangle */
|
2025-08-16 14:32:52 +10:00
|
|
|
int32_t b_;
|
2010-12-07 11:57:34 +00:00
|
|
|
|
2012-06-25 09:14:37 +00:00
|
|
|
MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_Rect")
|
2002-10-12 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
inline int32_t GHOST_Rect::getWidth() const
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
return r_ - l_;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
inline int32_t GHOST_Rect::getHeight() const
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
return b_ - t_;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
inline void GHOST_Rect::set(int32_t l, int32_t t, int32_t r, int32_t b)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
l_ = l;
|
|
|
|
|
t_ = t;
|
|
|
|
|
r_ = r;
|
|
|
|
|
b_ = b;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-19 09:23:08 +00:00
|
|
|
inline bool GHOST_Rect::isEmpty() const
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
|
|
|
|
return (getWidth() == 0) || (getHeight() == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool GHOST_Rect::isValid() const
|
|
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
return (l_ <= r_) && (t_ <= b_);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void GHOST_Rect::unionRect(const GHOST_Rect &r)
|
|
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
if (r.l_ < l_) {
|
|
|
|
|
l_ = r.l_;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
if (r.r_ > r_) {
|
|
|
|
|
r_ = r.r_;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
if (r.t_ < t_) {
|
|
|
|
|
t_ = r.t_;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
if (r.b_ > b_) {
|
|
|
|
|
b_ = r.b_;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
inline void GHOST_Rect::unionPoint(int32_t x, int32_t y)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
if (x < l_) {
|
|
|
|
|
l_ = x;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
if (x > r_) {
|
|
|
|
|
r_ = x;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
if (y < t_) {
|
|
|
|
|
t_ = y;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
if (y > b_) {
|
|
|
|
|
b_ = y;
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-05-29 00:48:48 +10:00
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
inline void GHOST_Rect::wrapPoint(int32_t &x, int32_t &y, int32_t ofs, GHOST_TAxisFlag axis)
|
2009-10-17 14:08:01 +00:00
|
|
|
{
|
2021-07-05 10:10:20 -07:00
|
|
|
int32_t w = getWidth();
|
|
|
|
|
int32_t h = getHeight();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-10-17 14:08:01 +00:00
|
|
|
/* highly unlikely but avoid eternal loop */
|
2012-05-19 09:23:08 +00:00
|
|
|
if (w - ofs * 2 <= 0 || h - ofs * 2 <= 0) {
|
2009-10-17 14:08:01 +00:00
|
|
|
return;
|
2012-04-28 06:31:57 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-29 00:48:48 +10:00
|
|
|
if (axis & GHOST_kAxisX) {
|
2025-08-16 14:32:52 +10:00
|
|
|
while (x - ofs < l_) {
|
2019-05-29 00:48:48 +10:00
|
|
|
x += w - (ofs * 2);
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
while (x + ofs > r_) {
|
2019-05-29 00:48:48 +10:00
|
|
|
x -= w - (ofs * 2);
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2019-05-29 00:48:48 +10:00
|
|
|
}
|
2022-06-27 20:31:28 +10:00
|
|
|
if (axis & GHOST_kAxisY) {
|
2025-08-16 14:32:52 +10:00
|
|
|
while (y - ofs < t_) {
|
2019-05-29 00:48:48 +10:00
|
|
|
y += h - (ofs * 2);
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
while (y + ofs > b_) {
|
2019-05-29 00:48:48 +10:00
|
|
|
y -= h - (ofs * 2);
|
2022-06-17 17:14:04 +10:00
|
|
|
}
|
2019-05-29 00:48:48 +10:00
|
|
|
}
|
2009-10-17 14:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-18 14:33:50 +10:00
|
|
|
inline void GHOST_Rect::clampPoint(int32_t &x, int32_t &y)
|
|
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
if (x < l_) {
|
|
|
|
|
x = l_;
|
2022-06-18 14:33:50 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
else if (x > r_) {
|
|
|
|
|
x = r_;
|
2022-06-18 14:33:50 +10:00
|
|
|
}
|
|
|
|
|
|
2025-08-16 14:32:52 +10:00
|
|
|
if (y < t_) {
|
|
|
|
|
y = t_;
|
2022-06-18 14:33:50 +10:00
|
|
|
}
|
2025-08-16 14:32:52 +10:00
|
|
|
else if (y > b_) {
|
|
|
|
|
y = b_;
|
2022-06-18 14:33:50 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-05 10:10:20 -07:00
|
|
|
inline bool GHOST_Rect::isInside(int32_t x, int32_t y) const
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2025-08-16 14:32:52 +10:00
|
|
|
return (x >= l_) && (x <= r_) && (y >= t_) && (y <= b_);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|