00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef RECT_H_INCLUDED
00020 #define RECT_H_INCLUDED
00021
00022 namespace Pentagram {
00023
00024 struct Rect {
00025 sint32 x, y;
00026 sint32 w, h;
00027
00028 Rect() : x(0), y(0), w(0), h(0) {}
00029 Rect(int nx, int ny, int nw, int nh) : x(nx), y(ny), w(nw), h(nh) {}
00030 Rect(const Rect& o) : x(o.x), y(o.y), w(o.w), h(o.h) {}
00031
00032 void Set(int nx, int ny, int nw, int nh) { x=nx; y=ny; w=nw; h=nh; }
00033 void Set(Rect &o) { *this = o; }
00034
00035
00036 bool IsValid() const { return w > 0 && h > 0; }
00037
00038
00039 bool InRect(int px, int py) const { return px >= x && py >= y && px < (x+w) && py < (y+h); }
00040
00041
00042 void MoveRel(sint32 dx, sint32 dy) { x=x+dx; y=y+dy; }
00043
00044
00045 void MoveAbs(sint32 nx, sint32 ny) { x=nx; y=ny; }
00046
00047
00048 void ResizeRel(sint32 dw, sint32 dh) { w=w+dw; h=h+dh; }
00049
00050
00051 void ResizeAbs(sint32 nw, sint32 nh) { w=nw; h=nh; }
00052
00053
00054 void Intersect(int ox, int oy, int ow, int oh)
00055 {
00056 int x2 = x + w, y2 = y + h;
00057 int ox2 = ox + ow, oy2 = oy + oh;
00058
00059 if (x < ox) x = ox;
00060 else if (x > ox2) x = ox2;
00061
00062 if (x2 < ox) x2 = ox;
00063 else if (x2 > ox2) x2 = ox2;
00064
00065 if (y < oy) y = oy;
00066 else if (y > oy2) y = oy2;
00067
00068 if (y2 < oy) y2 = oy;
00069 else if (y2 > oy2) y2 = oy2;
00070
00071 w = x2 - x;
00072 h = y2 - y;
00073
00074 }
00075
00076
00077 void IntersectOther(int &ox, int &oy, int &ow, int &oh) const
00078 {
00079 int x2 = x + w, y2 = y + h;
00080 int ox2 = ox + ow, oy2 = oy + oh;
00081
00082 if (ox < x) ox = x;
00083 else if (ox > x2) ox = x2;
00084
00085 if (ox2 < x) ox2 = x;
00086 else if (ox2 > x2) ox2 = x2;
00087
00088 if (oy < y) oy = y;
00089 else if (oy > y2) oy = y2;
00090
00091 if (oy2 < y) oy2 = y;
00092 else if (oy2 > y2) oy2 = y2;
00093
00094 ow = ox2 - ox;
00095 oh = oy2 - oy;
00096 }
00097
00098
00099 void Intersect(const Rect& o)
00100 {
00101 Intersect(o.x, o.y, o.w, o.h);
00102 }
00103
00104
00105 void Union(int ox, int oy, int ow, int oh)
00106 {
00107 int x2 = x + w, y2 = y + h;
00108 int ox2 = ox + ow, oy2 = oy + oh;
00109
00110 if (ox < x) x = ox;
00111 else if (ox2 > x2) x2= ox2;
00112
00113 if (oy < y) y = ox;
00114 else if (oy2 > y2) y2 = ox2;
00115
00116 w = x2 - x;
00117 h = y2 - y;
00118 }
00119
00120
00121 void Union(const Rect& o)
00122 {
00123 Union(o.x, o.y, o.w, o.h);
00124 }
00125
00126 bool Overlaps(const Rect& o) const
00127 {
00128 if (x+w <= o.x || o.x+o.w <= x) return false;
00129 if (y+h <= o.y || o.y+o.h <= y) return false;
00130 return true;
00131 }
00132
00133
00134 Rect &operator += (const Rect& o)
00135 {
00136 Union(o.x, o.y, o.w, o.h);
00137 return *(this);
00138 }
00139
00140
00141 Rect &operator + (const Rect& o) const
00142 {
00143 Rect result(*this);
00144 return (result+=o);
00145 }
00146
00147 bool operator == (const Rect& o) const
00148 {
00149 return x == o.x && y == o.y && w == o.w && h == o.h;
00150 }
00151
00152 };
00153
00154 }
00155
00156 #endif // RECT_H_INCLUDED