- Your Widget Set For OpenGL
Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | Related Pages

urectangle.hpp

00001 /*************************************************************************** 00002 LibUFO - UI For OpenGL 00003 copyright : (C) 2001-2005 by Johannes Schmidt 00004 email : schmidtjf at users.sourceforge.net 00005 ------------------- 00006 00007 file : include/ufo/util/urectangle.hpp 00008 begin : Wed May 16 2001 00009 $Id: urectangle.hpp,v 1.18 2005/09/30 12:36:48 schmidtjf Exp $ 00010 ***************************************************************************/ 00011 00012 /*************************************************************************** 00013 * This library is free software; you can redistribute it and/or * 00014 * modify it under the terms of the GNU Lesser General Public * 00015 * License as published by the Free Software Foundation; either * 00016 * version 2.1 of the License, or (at your option) any later version. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00021 * Lesser General Public License for more details. * 00022 * * 00023 * You should have received a copy of the GNU Lesser General Public * 00024 * License along with this library; if not, write to the Free Software * 00025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 00026 ***************************************************************************/ 00027 00028 #ifndef URECTANGLE_HPP 00029 #define URECTANGLE_HPP 00030 00031 #include "../uobject.hpp" 00032 00033 #include "upoint.hpp" 00034 #include "udimension.hpp" 00035 #include "uinsets.hpp" 00036 00037 namespace ufo { 00038 00049 class UFO_EXPORT URectangle { 00050 public: 00051 inline URectangle(); 00052 inline URectangle(int x, int y, int w, int h); 00053 inline URectangle(const UPoint & p, const UDimension & d); 00055 inline URectangle(const UPoint & p1, const UPoint & p2); 00057 inline URectangle(const UDimension & d); 00058 00059 inline UPoint getLocation() const; 00060 inline UDimension getSize() const; 00061 00062 inline void setBounds(int x, int y, int w, int h); 00063 inline void setBounds(const URectangle & rect); 00064 00068 inline bool contains(const UPoint & pos) const; 00069 00073 inline bool isValid() const; 00077 inline bool isInvalid() const; 00079 inline bool isEmpty() const; 00080 00085 inline void clamp(const UDimension & maxDim); 00090 inline void expand(const UDimension & minDim); 00091 00094 inline void intersect(const URectangle & rect); 00098 inline void unite(const URectangle & rect); 00099 00102 inline URectangle computeUnion(const URectangle & src) const; 00103 00110 inline static URectangle * computeUnion(const URectangle & src1, 00111 const URectangle & src2, URectangle * dest); 00112 00113 public: // Public operators 00117 inline bool operator()() const; 00121 inline bool operator!() const; 00122 00123 00127 inline URectangle & operator+=(const UPoint & p); 00131 inline URectangle & operator-=(const UPoint & p); 00132 00136 inline URectangle & operator+=(const UDimension & dim); 00140 inline URectangle & operator-=(const UDimension & dim); 00141 00145 inline URectangle & operator+=(const UInsets & insets); 00149 inline URectangle & operator-=(const UInsets & insets); 00150 00151 inline friend std::ostream & operator<<(std::ostream & os, const URectangle & o); 00152 public: // Public attributes 00153 int x; 00154 int y; 00155 int w; 00156 int h; 00157 public: // Public static attributes 00158 static URectangle invalid; 00159 }; 00160 00161 00162 // 00163 // public operators 00164 // 00165 inline URectangle operator+(const URectangle & rect, const UPoint & p); 00166 inline URectangle operator-(const URectangle & rect, const UPoint & p); 00167 00168 inline URectangle operator+(const URectangle & rect, const UDimension & dim); 00169 inline URectangle operator-(const URectangle & rect, const UDimension & dim); 00170 00171 inline URectangle operator+(const URectangle & rect, const UInsets & in); 00172 inline URectangle operator-(const URectangle & rect, const UInsets & in); 00173 00175 inline bool operator==(const URectangle & r1,const URectangle & r2); 00176 inline bool operator!=(const URectangle & r1,const URectangle & r2); 00177 00181 class UFO_EXPORT URectangleObject : public URectangle, public UObject { 00182 UFO_DECLARE_DYNAMIC_CLASS(URectangleObject) 00183 public: 00184 inline URectangleObject(); 00185 inline URectangleObject(const URectangle & rect); 00186 inline URectangleObject(int x, int y, int w, int h); 00187 inline URectangleObject(const UPoint & p, const UDimension & d); 00189 inline URectangleObject(const UPoint & p1, const UPoint & p2); 00190 00191 // 00192 // overrides UObject 00193 // 00194 virtual unsigned int hashCode() const; 00195 virtual bool equals(const UObject * obj); 00196 virtual bool equals(const URectangle * obj); 00197 virtual UObject * clone() const; 00198 00199 protected: // Protected methods 00200 virtual std::ostream & paramString(std::ostream & os) const; 00201 }; 00202 00203 // 00204 // URectangle 00205 // inline implementation 00206 // 00207 00208 inline URectangle::URectangle() : x(0), y(0), w(0), h(0) {} 00209 00210 inline URectangle::URectangle(int x, int y, int w, int h) 00211 : x(x), y(y), w(w), h(h) {} 00212 00213 inline URectangle::URectangle(const UPoint & p, const UDimension & d) 00214 : x(p.x) 00215 , y(p.y) 00216 , w(d.w) 00217 , h(d.h) 00218 {} 00219 00220 inline URectangle::URectangle(const UPoint & p1, const UPoint & p2) { 00221 x = std::min(p1.x, p2.x); 00222 y = std::min(p1.y, p2.y); 00223 w = std::abs(p2.x - p1.x); 00224 h = std::abs(p2.y - p1.y); 00225 } 00226 00227 inline URectangle::URectangle(const UDimension & d) 00228 : x(0) 00229 , y(0) 00230 , w(d.w) 00231 , h(d.h) 00232 {} 00233 00234 00235 inline UPoint 00236 URectangle::getLocation() const { 00237 return UPoint(x, y); 00238 } 00239 00240 inline UDimension 00241 URectangle::getSize() const { 00242 return UDimension(w, h); 00243 } 00244 00245 00246 inline void 00247 URectangle::setBounds(int x, int y, int w, int h) { 00248 this->x = x; 00249 this->y = y; 00250 this->w = w; 00251 this->h = h; 00252 } 00253 00254 inline void 00255 URectangle::setBounds(const URectangle & rect) { 00256 x = rect.x; 00257 y = rect.y; 00258 w = rect.w; 00259 h = rect.h; 00260 } 00261 00262 inline void 00263 URectangle::clamp(const UDimension & maxDim) { 00264 w = std::min(w, maxDim.w); 00265 h = std::min(h, maxDim.h); 00266 } 00267 00268 inline void 00269 URectangle::expand(const UDimension & minDim) { 00270 w = std::max(w, minDim.w); 00271 h = std::max(h, minDim.h); 00272 } 00273 00274 inline void 00275 URectangle::intersect(const URectangle & rect) { 00276 int x1 = std::max(x, rect.x); 00277 int y1 = std::max(y, rect.y); 00278 int x2 = std::min(x + w, rect.x + rect.w); 00279 int y2 = std::min(y + h, rect.y + rect.h); 00280 00281 setBounds(x1, y1, x2 - x1, y2 - y1); 00282 w = std::max(w, 0); 00283 h = std::max(h, 0); 00284 } 00285 00286 inline void 00287 URectangle::unite(const URectangle & rect) { 00288 int x1 = std::min(x, rect.x); 00289 int y1 = std::min(y, rect.y); 00290 int x2 = std::max(x + w, rect.x + rect.w); 00291 int y2 = std::max(y + h, rect.y + rect.h); 00292 00293 setBounds(x1, y1, x2 - x1 + 1, y2 - y1 + 1); 00294 } 00295 00296 inline bool 00297 URectangle::contains(const UPoint & pos) const { 00298 return ( 00299 pos.x >= x && pos.x < x + w && 00300 pos.y >= y && pos.y < y + h 00301 ); 00302 } 00303 00304 inline URectangle 00305 URectangle::computeUnion(const URectangle & src) const { 00306 URectangle ret; 00307 URectangle::computeUnion(*this, src, &ret); 00308 return ret; 00309 } 00310 00311 inline URectangle * 00312 URectangle::computeUnion(const URectangle & src1, 00313 const URectangle & src2, URectangle * dest) { 00314 if (dest) { 00315 // allow using src rectangle as dest rectangle 00316 int x = std::min(src1.x, src2.x); 00317 int y = std::min(src1.y, src2.y); 00318 dest->w = std::max(src1.x + src1.w, src2.x + src2.w) - x; 00319 dest->h = std::max(src1.y + src1.h, src2.y + src2.h) - y; 00320 dest->x = x; 00321 dest->y = y; 00322 } 00323 return dest; 00324 } 00325 00326 inline bool 00327 URectangle::isInvalid() const { 00328 return (*this == URectangle::invalid); 00329 } 00330 00331 inline bool 00332 URectangle::isValid() const { 00333 return (x != URectangle::invalid.x && 00334 y != URectangle::invalid.y && 00335 w != URectangle::invalid.w && 00336 h != URectangle::invalid.h); 00337 } 00338 00339 inline bool 00340 URectangle::isEmpty() const { 00341 return (!(w && h)); 00342 } 00343 00344 inline std::ostream & 00345 operator<<(std::ostream & os, const URectangle & o) { 00346 return os << "URectangle[" << o.x << "," << o.y 00347 << "," << o.w << "x" << o.h << "]"; 00348 } 00349 00350 inline bool 00351 URectangle::operator()() const { 00352 return isValid(); 00353 } 00354 00355 inline bool 00356 URectangle::operator!() const { 00357 return !isValid(); 00358 } 00359 00360 inline URectangle & 00361 URectangle::operator+=(const UPoint & p) { 00362 this->x += p.x; 00363 this->y += p.y; 00364 return *this; 00365 } 00366 00367 inline URectangle & 00368 URectangle::operator-=(const UPoint & p) { 00369 this->x -= p.x; 00370 this->y -= p.y; 00371 return *this; 00372 } 00373 00374 00375 inline URectangle & 00376 URectangle::operator+=(const UDimension & dim) { 00377 this->w += dim.w; 00378 this->h += dim.h; 00379 return *this; 00380 } 00381 00382 inline URectangle & 00383 URectangle::operator-=(const UDimension & dim) { 00384 this->w -= dim.w; 00385 this->h -= dim.h; 00386 return *this; 00387 } 00388 00389 inline URectangle & 00390 URectangle::operator+=(const UInsets & insets) { 00391 x -= insets.left; 00392 y -= insets.top; 00393 w += insets.getHorizontal(); 00394 h += insets.getVertical(); 00395 return *this; 00396 } 00397 00398 inline URectangle & 00399 URectangle::operator-=(const UInsets & insets) { 00400 x += insets.left; 00401 y += insets.top; 00402 w -= insets.getHorizontal(); 00403 h -= insets.getVertical(); 00404 return *this; 00405 } 00406 00407 00408 // 00409 // public operators 00410 // inline implementation 00411 // 00412 inline URectangle operator+(const URectangle & rect, const UPoint & p) { 00413 URectangle ret(rect); 00414 return ret += p; 00415 } 00416 00417 inline URectangle operator-(const URectangle & rect, const UPoint & p) { 00418 URectangle ret(rect); 00419 return ret -= p; 00420 } 00421 00422 inline URectangle operator+(const URectangle & rect, const UDimension & dim) { 00423 URectangle ret(rect); 00424 return ret += dim; 00425 } 00426 00427 inline URectangle operator-(const URectangle & rect, const UDimension & dim) { 00428 URectangle ret(rect); 00429 return ret -= dim; 00430 } 00431 00432 inline URectangle operator+(const URectangle & rect, const UInsets & in) { 00433 URectangle ret(rect); 00434 return ret += in; 00435 } 00436 00437 inline URectangle operator-(const URectangle & rect, const UInsets & in) { 00438 URectangle ret(rect); 00439 return ret -= in; 00440 } 00441 00442 00443 inline bool operator==(const URectangle & r1,const URectangle & r2) { 00444 return ((r1.x == r2.x) && 00445 (r1.y == r2.y) && 00446 (r1.w == r2.w) && 00447 (r1.h == r2.w)); 00448 } 00449 00450 inline bool operator!=(const URectangle & r1,const URectangle & r2) { 00451 return !(operator==(r1, r2)); 00452 } 00453 00454 // 00455 // URectangleObject 00456 // inline implementation 00457 // 00458 00459 00460 inline 00461 URectangleObject::URectangleObject() {} 00462 00463 inline 00464 URectangleObject::URectangleObject(const URectangle & rect) 00465 : URectangle(rect) {} 00466 00467 inline 00468 URectangleObject::URectangleObject(int x, int y, int w, int h) 00469 : URectangle(x, y, w, h) {} 00470 00471 inline 00472 URectangleObject::URectangleObject(const UPoint & p, const UDimension & d) 00473 : URectangle(p, d) {} 00474 00475 inline 00476 URectangleObject::URectangleObject(const UPoint & p1, const UPoint & p2) 00477 : URectangle(p1, p2) {} 00478 00479 // 00480 // overrides UObject 00481 // 00482 inline unsigned int 00483 URectangleObject::hashCode() const { 00484 // FIXME 00485 // doh, too lazy 00486 int temp = ((w << 16) | h); 00487 temp &= ~(x | y); 00488 return temp; 00489 } 00490 00491 inline bool 00492 URectangleObject::equals(const UObject * obj) { 00493 if (const URectangle * rect = dynamic_cast<const URectangle*>(obj)) { 00494 return ((x == rect->x) && 00495 (y == rect->y) && 00496 (w == rect->w) && 00497 (h == rect->h)); 00498 } 00499 return false; 00500 } 00501 00502 inline bool 00503 URectangleObject::equals(const URectangle * obj) { 00504 return ((x == obj->x) && 00505 (y == obj->y) && 00506 (w == obj->w) && 00507 (h == obj->h)); 00508 } 00509 00510 inline UObject * 00511 URectangleObject::clone() const { 00512 return new URectangleObject(x, y, w, h); 00513 } 00514 00515 00516 // 00517 // Protected methods 00518 // 00519 00520 inline std::ostream & 00521 URectangleObject::paramString(std::ostream & os) const { 00522 return os << x << "," << y << "," << w << "x" << h; 00523 } 00524 00525 } // namespace ufo 00526 00527 #endif // URECTANGLE_HPP

The libUFO Project - written by Johannes Schmidt