merge total remake

This commit is contained in:
Eduardo Bart
2011-08-13 23:09:11 -03:00
parent 0af7856475
commit 55862b07ad
253 changed files with 6777 additions and 8463 deletions

View File

@@ -2,89 +2,89 @@
#define RECT_H
#include "types.h"
#include <ostream>
#include <sstream>
template <class T>
template<class T>
class TPoint;
template <class T>
template<class T>
class TSize;
template <class T>
template<class T>
class TRect
{
public:
inline TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
inline TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
inline TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
inline TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
inline TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
inline TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x1+size.width()-1), y2(y1+size.height()-1) { }
TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x1+size.width()-1), y2(y1+size.height()-1) { }
inline bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
inline bool isEmpty() const { return x1 > x2 || y1 > y2; }
inline bool isValid() const { return x1 <= x2 && y1 <= y2; }
bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
bool isEmpty() const { return x1 > x2 || y1 > y2; }
bool isValid() const { return x1 <= x2 && y1 <= y2; }
inline T left() const { return x1; }
inline T top() const { return y1; }
inline T right() const { return x2; }
inline T bottom() const { return y2; }
inline T horizontalCenter() const { return x1 + (x2 - x1)/2; }
inline T verticalCenter() const { return y1 + (y2 - y1)/2; }
inline T x() const { return x1; }
inline T y() const { return y1; }
inline TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
inline TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
inline TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
inline TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
inline TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
inline T width() const { return x2 - x1 + 1; }
inline T height() const { return y2 - y1 + 1; }
inline TSize<T> size() const { return TSize<T>(width(), height()); }
inline void reset() { x1 = y1 = 0; x2 = y2 = -1; }
inline void clear() { x2 = x1 - 1; y2 = y1 - 1; }
T left() const { return x1; }
T top() const { return y1; }
T right() const { return x2; }
T bottom() const { return y2; }
T horizontalCenter() const { return x1 + (x2 - x1)/2; }
T verticalCenter() const { return y1 + (y2 - y1)/2; }
T x() const { return x1; }
T y() const { return y1; }
TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
T width() const { return x2 - x1 + 1; }
T height() const { return y2 - y1 + 1; }
TSize<T> size() const { return TSize<T>(width(), height()); }
void reset() { x1 = y1 = 0; x2 = y2 = -1; }
void clear() { x2 = x1 - 1; y2 = y1 - 1; }
inline void setLeft(T pos) { x1 = pos; }
inline void setTop(T pos) { y1 = pos; }
inline void setRight(T pos) { x2 = pos; }
inline void setBottom(T pos) { y2 = pos; }
inline void setX(T x) { x1 = x; }
inline void setY(T y) { y1 = y; }
inline void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
inline void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
inline void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
inline void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
inline void setWidth(T width) { x2 = x1 + width - 1; }
inline void setHeight(T height) { y2 = y1 + height- 1; }
inline void setSize(T width, T height) { x2 = x1 + width - 1; y2 = y1 + height - 1; }
inline void setSize(const TSize<T>& size) { x2 = x1 + size.width() - 1; y2 = y1 + size.height() - 1; }
inline void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
inline void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
void setLeft(T pos) { x1 = pos; }
void setTop(T pos) { y1 = pos; }
void setRight(T pos) { x2 = pos; }
void setBottom(T pos) { y2 = pos; }
void setX(T x) { x1 = x; }
void setY(T y) { y1 = y; }
void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
void setWidth(T width) { x2 = x1 + width - 1; }
void setHeight(T height) { y2 = y1 + height- 1; }
void setSize(T width, T height) { x2 = x1 + width - 1; y2 = y1 + height - 1; }
void setSize(const TSize<T>& size) { x2 = x1 + size.width() - 1; y2 = y1 + size.height() - 1; }
void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
inline void addLeft(T add) { x1 -= add; }
inline void addTop(T add) { y1 -= add; }
inline void addRight(T add) { x2 += add; }
inline void addBottom(T add) { y2 += add; }
void addLeft(T add) { x1 -= add; }
void addTop(T add) { y1 -= add; }
void addRight(T add) { x2 += add; }
void addBottom(T add) { y2 += add; }
inline void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
inline void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
inline void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
inline void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
inline void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
inline void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
inline void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
inline void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
inline void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
inline void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
inline void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
inline void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
inline TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
inline TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x, y1 + p.y), TPoint<T>(x2 + p.x, y2 + p.y)); }
TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x, y1 + p.y), TPoint<T>(x2 + p.x, y2 + p.y)); }
inline TRect<T> expanded(T pixels) const { return TRect<T>(TPoint<T>(x1 - pixels, y1 - pixels), TPoint<T>(x2 + pixels, y2 + pixels)); }
TRect<T> expanded(T pixels) const { return TRect<T>(TPoint<T>(x1 - pixels, y1 - pixels), TPoint<T>(x2 + pixels, y2 + pixels)); }
inline void moveCenter(const TPoint<T> &p) {
void moveCenter(const TPoint<T> &p) {
T w = x2 - x1;
T h = y2 - y1;
x1 = p.x - w/2;
@@ -92,18 +92,18 @@ public:
x2 = x1 + w;
y2 = y1 + h;
}
inline void moveHorizontalCenter(T x) {
void moveHorizontalCenter(T x) {
T w = x2 - x1;
x1 = x - w/2;
x2 = x1 + w;
}
inline void moveVerticalCenter(T y) {
void moveVerticalCenter(T y) {
T h = y2 - y1;
y1 = y - h/2;
y2 = y1 + h;
}
inline bool contains(const TPoint<T> &p, bool insideOnly = false) const {
bool contains(const TPoint<T> &p, bool insideOnly = false) const {
T l, r;
if(x2 < x1 - 1) {
l = x2;
@@ -137,7 +137,7 @@ public:
return true;
}
inline bool intersects(const TRect<T> &r) const {
bool intersects(const TRect<T> &r) const {
if(isNull() || r.isNull())
return false;
@@ -178,7 +178,7 @@ public:
return true;
}
inline TRect<T> united(const TRect<T> &r) const {
TRect<T> united(const TRect<T> &r) const {
TRect<T> tmp;
tmp.x1 = std::min(x1, r.x1);
tmp.x2 = std::max(x2, r.x2);
@@ -187,7 +187,7 @@ public:
return tmp;
}
inline TRect<T> intersection(const TRect<T> &r) const {
TRect<T> intersection(const TRect<T> &r) const {
if(isNull())
return r;
if(r.isNull())
@@ -229,12 +229,12 @@ public:
return tmp;
}
inline TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
inline bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
inline bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this; }
bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
inline TRect<T>& operator|=(const TRect<T>& other) { *this = united(other); return *this; }
inline TRect<T>& operator&=(const TRect<T>& other) { *this = intersection(other); return *this; }
TRect<T>& operator|=(const TRect<T>& other) { *this = united(other); return *this; }
TRect<T>& operator&=(const TRect<T>& other) { *this = intersection(other); return *this; }
private:
T x1, y1, x2, y2;
@@ -243,14 +243,20 @@ private:
typedef TRect<int> Rect;
typedef TRect<float> RectF;
template <class T>
inline std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
template<class T>
std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
{
out << "Rect(" << rect.left() << ","
<< rect.top() << ","
<< rect.width() << ","
<< rect.height() << ")";
out << rect.left() << " " << rect.top() << " " << rect.width() << " " << rect.height();
return out;
}
#endif // RECT_H
template<class T>
std::istream& operator>>(std::istream& in, TRect<T>& rect)
{
T x, y , w, h;
in >> x >> y >> w >> h;
rect.setRect(x,y,w,h);
return in;
}
#endif