3 * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
20 Vector_2d_base(int x, int y) : _x(x), _y(y) {}
23 template< typename R >
24 class Vector_2d : public Vector_2d_base
27 Vector_2d(int x, int y) : Vector_2d_base(x, y) {}
28 explicit Vector_2d(Vector_2d_base const &o) : Vector_2d_base(o) {}
29 // Point() : _x(-1), _y(-1) {}
31 //int x() const { return _x; }
32 //int y() const { return _y; }
34 R operator + (R const &o) const
35 { return R(_x + o._x, _y + o._y); }
37 R operator - (R const &o) const
38 { return R(_x - o._x, _y - o._y); }
40 bool operator == (R const &o) const
41 { return _x == o._x && _y == o._y; }
43 R &operator += (R const &o)
44 { _x += o._x; _y += o._y; return *static_cast<R*>(this); }
46 R &operator -= (R const &o)
47 { _x -= o._x; _y -= o._y; return *static_cast<R*>(this); }
49 bool operator != (R const &o) const
50 { return !operator == (o); }
52 bool operator >= (R const &o) const
53 { return _x >= o._x && _y >= o._y; }
55 bool operator <= (R const &o) const
56 { return _x <= o._x && _y <= o._y; }
58 bool operator < (R const &o) const
59 { return _x < o._x && _y < o._y; }
61 R max(R const &o) const
62 { return R(std::max<int>(_x, o._x), std::max<int>(_y, o._y)); }
64 R min(R const &o) const
65 { return R(std::min<int>(_x, o._x), std::min<int>(_y, o._y)); }
67 R operator / (int d) const
68 { return R(_x / d, _y / d); }
70 R operator * (int d) const
71 { return R(_x * d, _y * d); }
73 R operator / (R const &o) const
74 { return R(_x / o._x, _y / o._y); }
76 R operator * (R const &o) const
77 { return R(_x * o._x, _y * o._y); }
80 class Point : public Vector_2d<Point>
83 typedef Vector_2d<Point> B;
85 Point(int x, int y) : B(x, y) {}
88 explicit Point(Vector_2d_base const &o) : B(o) {}
90 int x() const { return _x; }
91 int y() const { return _y; }
93 void x(int x) { _x = x; }
94 void y(int y) { _y = y; }
98 class Area : public Vector_2d<Area>
101 typedef Vector_2d<Area> B;
104 enum { Max_w = 0x100000, Max_h = Max_w };
106 Area(int w, int h) : B(w, h) {}
109 int w() const { return _x; }
110 int h() const { return _y; }
112 void w(int w) { _x = w; }
113 void h(int h) { _y = h; }
115 bool valid() const { return _x > 0 && _y > 0; }
116 int pixels() const { return _x * _y; }
118 Area grow(Point const &diff) const
119 { return Area(_x + diff.x(), _y + diff.y()); }
128 Rect(Point const &p1, Point const &p2) : _p1(p1), _p2(p2) {}
129 Rect(Point const &p1, Area const &a)
130 : _p1(p1), _p2(p1.x() + a.w() - 1, p1.y() + a.h() - 1)
133 explicit Rect(Area const &a) : _p1(0, 0), _p2(a.w() - 1, a.h() - 1) {}
137 Rect operator = (Rect const &o) { _p1 = o._p1; _p2 = o._p2; return *this; }
138 bool operator == (Rect const &o) const
139 { return _p1 == o._p1 && _p2 == o._p2; }
141 bool operator != (Rect const &o) const
142 { return !operator == (o); }
144 Point const &p1() const { return _p1; }
145 Point const &p2() const { return _p2; }
147 int x1() const { return _p1.x(); }
148 int y1() const { return _p1.y(); }
149 int x2() const { return _p2.x(); }
150 int y2() const { return _p2.y(); }
151 int w() const { return x2() - x1() + 1; }
152 int h() const { return y2() - y1() + 1; }
154 Area area() const { return Area(w(), h()); }
156 bool valid() const { return x1() <= x2() && y1() <= y2(); }
157 bool fits(Area const &a) const { return w() >= a.w() && h() >= a.h(); }
160 Rect operator & (Rect const &o) const
162 return Rect(Point(std::max(x1(), o.x1()), std::max(y1(), o.y1())),
163 Point(std::min(x2(), o.x2()), std::min(y2(), o.y2())));
166 Rect operator | (Rect const &o) const
168 return Rect(Point(std::min(x1(), o.x1()), std::min(y1(), o.y1())),
169 Point(std::max(x2(), o.x2()), std::max(y2(), o.y2())));
172 Rect &operator |= (Rect const &o)
178 Rect grow(int x) const
179 { return Rect(Point(x1() - x, y1() - x), Point(x2() + x, y2() + x)); }
181 Rect top(int h) const
184 n._p2 = Point(_p2.x(), _p1.y() + h - 1);
188 Rect left(int w) const
191 n._p2 = Point(_p1.x() + w - 1, _p2.y());
195 Rect bottom(int h) const
198 n._p1 = Point(_p1.x(), _p2.y() - h + 1);
202 Rect right(int w) const
205 n._p1 = Point(_p2.x() - w + 1, _p1.y());
209 Rect offset(int _x1, int _y1, int _x2, int _y2) const
210 { return Rect(Point(x1() + _x1, y1() + _y1), Point(x2() + _x2, y2() + _y2)); }
212 Point center(Area const &a) const
213 { return Point((w() - a.w()) / 2, (h() - a.h()) / 2); }
215 bool contains(Point const &p) const
216 { return p >= p1() && p <= p2(); }
218 Rect operator - (Point const &p) const
219 { return Rect(p1() - p, p2() - p); }
221 Rect operator + (Point const &p) const
222 { return Rect(p1() + p, p2() + p); }
224 Rect move_to(Point const &p1) const
225 { return Rect(p1, area()); }
233 Rect_tuple(Rect const &t, Rect const &l, Rect const &r, Rect const &b)
234 { _r[0] = t; _r[1] = l; _r[2] = r; _r[3] = b; }
236 Rect const &operator [] (unsigned i) const { return _r[i]; }
237 Rect const &t() const { return _r[0]; }
238 Rect const &l() const { return _r[1]; }
239 Rect const &r() const { return _r[2]; }
240 Rect const &b() const { return _r[3]; }
244 Rect_tuple operator - (Rect const &lh, Rect const &rh)
248 Rect(Point(lh.x1(), lh.y1()), Point(lh.x2(), re.y1() - 1)),
249 Rect(Point(lh.x1(), re.y1()), Point(re.x1() - 1, re.y2())),
250 Rect(Point(re.x2() + 1, re.y1()), Point(lh.x2(), re.y2())),
251 Rect(Point(lh.x1(), re.y2() + 1), Point(lh.x2(), lh.y2())));
255 template< typename E >
261 Flags(unsigned v, bool) : _v(v) {}
268 Flags(int z = 0) : _v(z) {} //Private_bool const * = 0) : _v(0) {}
269 Flags(E e) : _v(e) {}
270 Flags(Flags const &o) : _v(o._v) {}
272 operator Private_bool * () const { return (Private_bool *)_v; }
273 bool operator ! () const { return !_v; }
274 Flags operator | (Flags const &o) const { return Flags(_v | o._v, true); }
275 Flags operator | (Enum e) const { return Flags(_v | (unsigned)e, true); }
276 Flags &operator |= (Flags const &o) { _v |= o._v; return *this; }
277 Flags &operator |= (Enum e) { _v |= (unsigned)e; return *this; }
278 Flags operator & (Flags const &o) const { return Flags(_v & o._v, true); }
279 Flags operator & (Enum e) const { return Flags(_v & (unsigned)e, true); }
280 Flags &operator &= (Flags const &o) { _v &= o._v; return *this; }
281 Flags &operator &= (Enum e) { _v &= (unsigned)e; return *this; }
282 Flags operator ~ () const { return Flags(~_v, true); }
284 unsigned value() const { return _v; }
295 typedef Flags<Orientation> Orientations;
301 Align_right = 0x0002,
302 Align_h_center = 0x0004,
303 Align_justify = 0x0008,
305 Align_horizontal_m = Align_left | Align_right | Align_h_center,
308 Align_bottom = 0x0040,
309 Align_v_center = 0x0080,
311 Align_vertical_m = Align_top | Align_bottom | Align_v_center,
313 Align_center = Align_h_center | Align_v_center,
316 typedef Flags<Alignment_flag> Alignment;