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); }
71 class Point : public Vector_2d<Point>
74 typedef Vector_2d<Point> B;
76 Point(int x, int y) : B(x, y) {}
77 Point() : B(-1, -1) {}
79 explicit Point(Vector_2d_base const &o) : B(o) {}
81 int x() const { return _x; }
82 int y() const { return _y; }
84 void x(int x) { _x = x; }
85 void y(int y) { _y = y; }
89 class Area : public Vector_2d<Area>
92 typedef Vector_2d<Area> B;
95 enum { Max_w = 0x100000, Max_h = Max_w };
97 Area(int w, int h) : B(w, h) {}
100 int w() const { return _x; }
101 int h() const { return _y; }
103 void w(int w) { _x = w; }
104 void h(int h) { _y = h; }
106 bool valid() const { return _x > 0 && _y > 0; }
107 int pixels() const { return _x * _y; }
109 Area grow(Point const &diff) const
110 { return Area(_x + diff.x(), _y + diff.y()); }
119 Rect(Point const &p1, Point const &p2) : _p1(p1), _p2(p2) {}
120 Rect(Point const &p1, Area const &a)
121 : _p1(p1), _p2(p1.x() + a.w() - 1, p1.y() + a.h() - 1)
124 explicit Rect(Area const &a) : _p1(0, 0), _p2(a.w() - 1, a.h() - 1) {}
128 Rect operator = (Rect const &o) { _p1 = o._p1; _p2 = o._p2; return *this; }
129 bool operator == (Rect const &o) const
130 { return _p1 == o._p1 && _p2 == o._p2; }
132 bool operator != (Rect const &o) const
133 { return !operator == (o); }
135 Point const &p1() const { return _p1; }
136 Point const &p2() const { return _p2; }
138 int x1() const { return _p1.x(); }
139 int y1() const { return _p1.y(); }
140 int x2() const { return _p2.x(); }
141 int y2() const { return _p2.y(); }
142 int w() const { return x2() - x1() + 1; }
143 int h() const { return y2() - y1() + 1; }
145 Area area() const { return Area(w(), h()); }
147 bool valid() const { return x1() <= x2() && y1() <= y2(); }
148 bool fits(Area const &a) const { return w() >= a.w() && h() >= a.h(); }
151 Rect operator & (Rect const &o) const
153 return Rect(Point(std::max(x1(), o.x1()), std::max(y1(), o.y1())),
154 Point(std::min(x2(), o.x2()), std::min(y2(), o.y2())));
157 Rect operator | (Rect const &o) const
159 return Rect(Point(std::min(x1(), o.x1()), std::min(y1(), o.y1())),
160 Point(std::max(x2(), o.x2()), std::max(y2(), o.y2())));
163 Rect &operator |= (Rect const &o)
169 Rect grow(int x) const
170 { return Rect(Point(x1() - x, y1() - x), Point(x2() + x, y2() + x)); }
172 Rect top(int h) const
175 n._p2 = Point(_p2.x(), _p1.y() + h - 1);
179 Rect left(int w) const
182 n._p2 = Point(_p1.x() + w - 1, _p2.y());
186 Rect bottom(int h) const
189 n._p1 = Point(_p1.x(), _p2.y() - h + 1);
193 Rect right(int w) const
196 n._p1 = Point(_p2.x() - w + 1, _p1.y());
200 Rect offset(int _x1, int _y1, int _x2, int _y2) const
201 { return Rect(Point(x1() + _x1, y1() + _y1), Point(x2() + _x2, y2() + _y2)); }
203 Point center(Area const &a) const
204 { return Point((w() - a.w()) / 2, (h() - a.h()) / 2); }
206 bool contains(Point const &p) const
207 { return p >= p1() && p <= p2(); }
209 Rect operator - (Point const &p) const
210 { return Rect(p1() - p, p2() - p); }
212 Rect operator + (Point const &p) const
213 { return Rect(p1() + p, p2() + p); }
215 Rect move_to(Point const &p1) const
216 { return Rect(p1, area()); }
223 Rect_tuple(Rect const &t, Rect const &l, Rect const &r, Rect const &b)
224 : t(t), l(l), r(r), b(b)
229 Rect_tuple operator - (Rect const &lh, Rect const &rh)
233 Rect(Point(lh.x1(), lh.y1()), Point(lh.x2(), re.y1() - 1)),
234 Rect(Point(lh.x1(), re.y1()), Point(re.x1() - 1, re.y2())),
235 Rect(Point(re.x2() + 1, re.y1()), Point(lh.x2(), re.y2())),
236 Rect(Point(lh.x1(), re.y2() + 1), Point(lh.x2(), lh.y2())));
240 template< typename E >
246 Flags(unsigned v, bool) : _v(v) {}
253 Flags(int z = 0) : _v(z) {} //Private_bool const * = 0) : _v(0) {}
254 Flags(E e) : _v(e) {}
255 Flags(Flags const &o) : _v(o._v) {}
257 operator Private_bool * () const { return (Private_bool *)_v; }
258 bool operator ! () const { return !_v; }
259 Flags operator | (Flags const &o) const { return Flags(_v | o._v, true); }
260 Flags operator | (Enum e) const { return Flags(_v | (unsigned)e, true); }
261 Flags &operator |= (Flags const &o) { _v |= o._v; return *this; }
262 Flags &operator |= (Enum e) { _v |= (unsigned)e; return *this; }
263 Flags operator & (Flags const &o) const { return Flags(_v & o._v, true); }
264 Flags operator & (Enum e) const { return Flags(_v & (unsigned)e, true); }
265 Flags &operator &= (Flags const &o) { _v &= o._v; return *this; }
266 Flags &operator &= (Enum e) { _v &= (unsigned)e; return *this; }
267 Flags operator ~ () const { return Flags(~_v, true); }
269 unsigned value() const { return _v; }
280 typedef Flags<Orientation> Orientations;
286 Align_right = 0x0002,
287 Align_h_center = 0x0004,
288 Align_justify = 0x0008,
290 Align_horizontal_m = Align_left | Align_right | Align_h_center,
293 Align_bottom = 0x0040,
294 Align_v_center = 0x0080,
296 Align_vertical_m = Align_top | Align_bottom | Align_v_center,
298 Align_center = Align_h_center | Align_v_center,
301 typedef Flags<Alignment_flag> Alignment;