2002-08-24 15:31:37 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
2002-08-24 15:31:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-08-24 15:31:37 +00:00
|
|
|
*
|
2006-02-11 09:53:53 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-08-24 15:31:37 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-08-31 13:29:10 +00:00
|
|
|
#ifndef COMMON_RECT_H
|
|
|
|
#define COMMON_RECT_H
|
2002-08-24 15:31:37 +00:00
|
|
|
|
2003-08-01 12:21:04 +00:00
|
|
|
#include "common/scummsys.h"
|
2005-01-11 13:25:02 +00:00
|
|
|
#include "common/util.h"
|
2002-08-31 13:29:10 +00:00
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
namespace Common {
|
2002-08-24 15:31:37 +00:00
|
|
|
|
|
|
|
/*! @brief simple class for handling both 2D position and size
|
|
|
|
|
|
|
|
This small class is an helper for position and size values.
|
|
|
|
*/
|
2003-05-15 21:25:35 +00:00
|
|
|
struct Point {
|
2003-05-15 21:40:36 +00:00
|
|
|
int16 x; //!< The horizontal part of the point
|
|
|
|
int16 y; //!< The vertical part of the point
|
2003-05-15 21:25:35 +00:00
|
|
|
|
|
|
|
Point() : x(0), y(0) {};
|
2005-09-02 08:51:58 +00:00
|
|
|
Point(const Point &p) : x(p.x), y(p.y) {};
|
2003-05-15 21:40:36 +00:00
|
|
|
explicit Point(int16 x1, int16 y1) : x(x1), y(y1) {};
|
2003-05-15 21:25:35 +00:00
|
|
|
Point & operator=(const Point & p) { x = p.x; y = p.y; return *this; };
|
|
|
|
bool operator==(const Point & p) const { return x == p.x && y == p.y; };
|
2003-05-15 21:52:10 +00:00
|
|
|
bool operator!=(const Point & p) const { return x != p.x || y != p.y; };
|
2002-08-24 15:31:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*! @brief simple class for handling a rectangular zone.
|
|
|
|
|
|
|
|
This small class is an helper for rectangles.
|
2004-04-25 16:16:21 +00:00
|
|
|
Note: This implementation is built around the assumption that (top,left) is
|
2005-07-30 21:11:48 +00:00
|
|
|
part of the rectangle, but (bottom,right) is not! This is reflected in
|
2004-04-25 16:16:21 +00:00
|
|
|
various methods, including contains(), intersects() and others.
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-04-25 16:16:21 +00:00
|
|
|
Another very wide spread approach to rectangle classes treats (bottom,right)
|
|
|
|
also as a part of the rectangle.
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-04-25 16:16:21 +00:00
|
|
|
Coneptually, both are sound, but the approach we use saves many intermediate
|
|
|
|
computations (like computing the height in our case is done by doing this:
|
|
|
|
height = bottom - top;
|
|
|
|
while in the alternate system, it would be
|
|
|
|
height = bottom - top + 1;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-04-25 16:16:21 +00:00
|
|
|
When writing code using our Rect class, always keep this principle in mind!
|
2002-08-24 15:31:37 +00:00
|
|
|
*/
|
2003-05-15 21:25:35 +00:00
|
|
|
struct Rect {
|
2003-05-15 21:40:36 +00:00
|
|
|
int16 top, left; //!< The point at the top left of the rectangle (part of the rect).
|
|
|
|
int16 bottom, right; //!< The point at the bottom right of the rectangle (not part of the rect).
|
2003-05-15 21:25:35 +00:00
|
|
|
|
|
|
|
Rect() : top(0), left(0), bottom(0), right(0) {}
|
2004-04-06 11:11:10 +00:00
|
|
|
Rect(int16 w, int16 h) : top(0), left(0), bottom(h), right(w) {}
|
2004-01-18 21:41:38 +00:00
|
|
|
Rect(int16 x1, int16 y1, int16 x2, int16 y2) : top(y1), left(x1), bottom(y2), right(x2) {
|
|
|
|
assert(isValidRect());
|
|
|
|
}
|
2003-05-15 21:40:36 +00:00
|
|
|
int16 width() const { return right - left; }
|
2003-05-15 22:27:09 +00:00
|
|
|
int16 height() const { return bottom - top; }
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-05-28 12:53:11 +00:00
|
|
|
void setWidth(int16 aWidth) {
|
|
|
|
right = left + aWidth;
|
2005-05-28 12:28:23 +00:00
|
|
|
}
|
|
|
|
|
2005-05-28 12:53:11 +00:00
|
|
|
void setHeight(int16 aHeight) {
|
|
|
|
bottom = top + aHeight;
|
2005-05-28 12:28:23 +00:00
|
|
|
}
|
2002-08-24 15:31:37 +00:00
|
|
|
|
2003-05-15 21:52:10 +00:00
|
|
|
/*! @brief check if given position is inside this rectangle
|
2003-11-08 22:43:46 +00:00
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
@param x the horizontal position to check
|
2005-07-30 21:11:48 +00:00
|
|
|
@param y the vertical position to check
|
|
|
|
|
2003-05-15 21:52:10 +00:00
|
|
|
@return true if the given position is inside this rectangle, false otherwise
|
2002-08-24 15:31:37 +00:00
|
|
|
*/
|
2003-05-15 21:52:10 +00:00
|
|
|
bool contains(int16 x, int16 y) const {
|
2003-05-15 21:25:35 +00:00
|
|
|
return (left <= x) && (x < right) && (top <= y) && (y < bottom);
|
2002-08-25 11:38:40 +00:00
|
|
|
}
|
2003-05-15 21:52:10 +00:00
|
|
|
|
|
|
|
/*! @brief check if given point is inside this rectangle
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
@param p the point to check
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-05-15 21:52:10 +00:00
|
|
|
@return true if the given point is inside this rectangle, false otherwise
|
2002-08-24 15:31:37 +00:00
|
|
|
*/
|
2005-09-02 08:51:58 +00:00
|
|
|
bool contains(const Point &p) const {
|
2004-03-04 22:26:31 +00:00
|
|
|
return contains(p.x, p.y);
|
2002-08-25 11:38:40 +00:00
|
|
|
}
|
2003-05-15 21:52:10 +00:00
|
|
|
|
2003-06-01 17:06:07 +00:00
|
|
|
/*! @brief check if given rectangle intersects with this rectangle
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-05-29 16:37:49 +00:00
|
|
|
@param r the rectangle to check
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-05-15 21:52:10 +00:00
|
|
|
@return true if the given rectangle is inside the rectangle, false otherwise
|
|
|
|
*/
|
2005-09-02 08:51:58 +00:00
|
|
|
bool intersects(const Rect &r) const {
|
2003-06-01 17:06:07 +00:00
|
|
|
return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! @brief extend this rectangle so that it contains r
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2003-06-01 17:06:07 +00:00
|
|
|
@param r the rectangle to extend by
|
|
|
|
*/
|
2005-09-02 08:51:58 +00:00
|
|
|
void extend(const Rect &r) {
|
2003-06-01 17:06:07 +00:00
|
|
|
left = MIN(left, r.left);
|
|
|
|
right = MAX(right, r.right);
|
|
|
|
top = MIN(top, r.top);
|
|
|
|
bottom = MAX(bottom, r.bottom);
|
2003-05-15 21:52:10 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-01-08 03:24:01 +00:00
|
|
|
/*! @brief extend this rectangle in all four directions by the given number of pixels
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-01-08 03:24:01 +00:00
|
|
|
@param offset the size to grow by
|
|
|
|
*/
|
2003-05-15 22:15:24 +00:00
|
|
|
void grow(int16 offset) {
|
|
|
|
top -= offset;
|
|
|
|
left -= offset;
|
|
|
|
bottom += offset;
|
|
|
|
right += offset;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-09-02 08:51:58 +00:00
|
|
|
void clip(const Rect &r) {
|
2004-01-18 21:41:38 +00:00
|
|
|
assert(isValidRect());
|
|
|
|
assert(r.isValidRect());
|
|
|
|
|
2004-01-08 03:24:01 +00:00
|
|
|
if (top < r.top) top = r.top;
|
2004-01-18 21:41:38 +00:00
|
|
|
else if (top > r.bottom) top = r.bottom;
|
|
|
|
|
2004-01-08 03:24:01 +00:00
|
|
|
if (left < r.left) left = r.left;
|
2004-01-18 21:41:38 +00:00
|
|
|
else if (left > r.right) left = r.right;
|
|
|
|
|
2004-01-08 03:24:01 +00:00
|
|
|
if (bottom > r.bottom) bottom = r.bottom;
|
2004-01-18 21:41:38 +00:00
|
|
|
else if (bottom < r.top) bottom = r.top;
|
|
|
|
|
2004-01-08 03:24:01 +00:00
|
|
|
if (right > r.right) right = r.right;
|
2004-01-18 21:41:38 +00:00
|
|
|
else if (right < r.left) right = r.left;
|
2004-01-08 03:24:01 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-01-08 03:24:01 +00:00
|
|
|
void clip(int maxw, int maxh) {
|
|
|
|
clip(Rect(0, 0, maxw, maxh));
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-01-18 21:41:38 +00:00
|
|
|
bool isValidRect() const {
|
|
|
|
return (left <= right && top <= bottom);
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-03-04 22:26:31 +00:00
|
|
|
void moveTo(int16 x, int16 y) {
|
|
|
|
bottom += y - top;
|
|
|
|
right += x - left;
|
|
|
|
top = y;
|
|
|
|
left = x;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-09-02 08:51:58 +00:00
|
|
|
void moveTo(const Point &p) {
|
2004-03-04 22:26:31 +00:00
|
|
|
moveTo(p.x, p.y);
|
|
|
|
}
|
2005-09-02 08:51:58 +00:00
|
|
|
|
|
|
|
void debugPrint(int debuglevel = 0, const char *caption = "Rect:") const {
|
|
|
|
debug(debuglevel, "%s %d, %d, %d, %d", caption, left, top, right, bottom);
|
|
|
|
}
|
2002-08-24 15:31:37 +00:00
|
|
|
};
|
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
} // End of namespace Common
|
2002-08-31 13:29:10 +00:00
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
#endif
|