add support for rbg565 color oled
[enigma2.git] / lib / gdi / erect.cpp
1 #include <lib/gdi/erect.h>
2 #include <lib/base/eerror.h>
3
4 /*****************************************************************************
5   eRect member functions
6  *****************************************************************************/
7
8 eRect::eRect( const ePoint &topLeft, const ePoint &bottomRight )
9 {
10         x1 = topLeft.x();
11         y1 = topLeft.y();
12         x2 = bottomRight.x();
13         y2 = bottomRight.y();
14 }
15
16 eRect eRect::normalize() const
17 {
18         eRect r;
19         if ( x2 < x1 ) {                                // swap bad x values
20         r.x1 = x2;
21         r.x2 = x1;
22         } else {
23         r.x1 = x1;
24         r.x2 = x2;
25         }
26         if ( y2 < y1 ) {                                // swap bad y values
27         r.y1 = y2;
28         r.y2 = y1;
29         } else {
30         r.y1 = y1;
31         r.y2 = y2;
32         }
33         return r;
34 }
35
36 void eRect::rect( int *x, int *y, int *w, int *h ) const
37 {
38         *x = x1;
39         *y = y1;
40         *w = x2-x1;
41         *h = y2-y1;
42 }
43
44 void eRect::coords( int *xp1, int *yp1, int *xp2, int *yp2 ) const
45 {
46         *xp1 = x1;
47         *yp1 = y1;
48         *xp2 = x2;
49         *yp2 = y2;
50 }
51
52 void eRect::moveTopLeft( const ePoint &p )
53 {
54         x2 += (p.x() - x1);
55         y2 += (p.y() - y1);
56         x1 = p.x();
57         y1 = p.y();
58 }
59
60 void eRect::moveBottomRight( const ePoint &p )
61 {
62         x1 += (p.x() - x2);
63         y1 += (p.y() - y2);
64         x2 = p.x();
65         y2 = p.y();
66 }
67
68 void eRect::moveTopRight( const ePoint &p )
69 {
70         x1 += (p.x() - x2);
71         y2 += (p.y() - y1);
72         x2 = p.x();
73         y1 = p.y();
74 }
75
76 void eRect::moveBottomLeft( const ePoint &p )
77 {
78         x2 += (p.x() - x1);
79         y1 += (p.y() - y2);
80         x1 = p.x();
81         y2 = p.y();
82 }
83
84 void eRect::moveCenter( const ePoint &p )
85 {
86         int w = x2 - x1;
87         int h = y2 - y1;
88         x1 = (p.x() - w/2);
89         y1 = (p.y() - h/2);
90         x2 = x1 + w;
91         y2 = y1 + h;
92 }
93
94 void eRect::setRect( int x, int y, int w, int h )
95 {
96         x1 = x;
97         y1 = y;
98         x2 = (x+w);
99         y2 = (y+h);
100 }
101
102 void eRect::setCoords( int xp1, int yp1, int xp2, int yp2 )
103 {
104         x1 = xp1;
105         y1 = yp1;
106         x2 = xp2;
107         y2 = yp2;
108 }
109
110 void eRect::setWidth( int w )
111 {
112         x2 = x1 + w;
113 }
114
115 void eRect::setHeight( int h )
116 {
117         y2 = y1 + h;
118 }
119
120 void eRect::setSize( const eSize &s )
121 {
122         x2 = s.width() +x1;
123         y2 = s.height()+y1;
124 }
125
126 bool eRect::contains( const ePoint &p) const
127 {
128         return p.x() >= x1 && p.x() < x2 &&
129                    p.y() >= y1 && p.y() < y2;
130 }
131
132 bool eRect::contains( const eRect &r) const
133 {
134         return r.x1 >= x1 &&
135                                  r.x2 <= x2 &&
136                                  r.y1 >= y1 &&
137                                  r.y2 <= y2;
138 }
139
140 eRect& eRect::operator|=(const eRect &r)
141 {
142         *this = *this | r;
143         return *this;
144 }
145
146 eRect& eRect::operator&=(const eRect &r)
147 {
148         *this = *this & r;
149         return *this;
150 }
151
152 eRect eRect::operator|(const eRect &r) const
153 {
154         if ( valid() ) {
155         if ( r.valid() ) {
156                 eRect tmp;
157                 tmp.setLeft(   MIN( x1, r.x1 ) );
158                 tmp.setRight(  MAX( x2, r.x2 ) );
159                 tmp.setTop(        MIN( y1, r.y1 ) );
160                 tmp.setBottom( MAX( y2, r.y2 ) );
161                 return tmp;
162         } else {
163                 return *this;
164         }
165         } else {
166         return r;
167         }
168 }
169
170 eRect eRect::unite( const eRect &r ) const
171 {
172         return *this | r;
173 }
174
175 eRect eRect::operator&( const eRect &r ) const
176 {
177         eRect tmp;
178         tmp.x1 = MAX( x1, r.x1 );
179         tmp.x2 = MIN( x2, r.x2 );
180         tmp.y1 = MAX( y1, r.y1 );
181         tmp.y2 = MIN( y2, r.y2 );
182         return tmp;
183 }
184
185 eRect eRect::intersect( const eRect &r ) const
186 {
187         return *this & r;
188 }
189
190 bool eRect::intersects( const eRect &r ) const
191 {
192         return ( MAX( x1, r.x1 ) < MIN( x2, r.x2 ) &&
193                  MAX( y1, r.y1 ) < MIN( y2, r.y2 ) );
194 }
195
196 bool operator==( const eRect &r1, const eRect &r2 )
197 {
198         return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2;
199 }
200
201 bool operator!=( const eRect &r1, const eRect &r2 )
202 {
203         return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2;
204 }
205
206 void eRect::scale(int x_n, int x_d, int y_n, int y_d) 
207 {
208         ASSERT(x_d); ASSERT(y_d);
209         x1 *= x_n; x1 /= x_d; 
210         x2 *= x_n; x2 /= x_d; 
211         y1 *= y_n; y1 /= y_d; 
212         y2 *= y_n; y2 /= y_d; 
213 }
214