Blender  V2.59
GHOST_Rect.h
Go to the documentation of this file.
00001 /*
00002  * $Id: GHOST_Rect.h 35152 2011-02-25 11:28:33Z jesterking $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #ifndef _H_GHOST_Rect
00035 #define _H_GHOST_Rect
00036 
00037 #include "GHOST_Types.h"
00038 
00039 
00049 class GHOST_Rect {
00050 public:
00051 
00059         GHOST_Rect(GHOST_TInt32 l=0, GHOST_TInt32 t=0, GHOST_TInt32 r=0, GHOST_TInt32 b=0)
00060                 : m_l(l), m_t(t), m_r(r), m_b(b) {}
00061 
00066         GHOST_Rect(const GHOST_Rect& r)
00067                 : m_l(r.m_l), m_t(r.m_t), m_r(r.m_r), m_b(r.m_b) {}
00068         
00072         virtual ~GHOST_Rect() {};
00073 
00078         virtual inline GHOST_TInt32 getWidth() const;
00079 
00084         virtual inline GHOST_TInt32 getHeight() const;
00085 
00093         virtual inline void set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b);
00094 
00100         virtual inline bool isEmpty() const;
00101 
00107         virtual inline bool isValid() const;
00108 
00114         virtual void inset(GHOST_TInt32 i);
00115 
00121         virtual inline void unionRect(const GHOST_Rect& r);
00122 
00128         virtual inline void unionPoint(GHOST_TInt32 x, GHOST_TInt32 y);
00129 
00135         virtual inline void wrapPoint(GHOST_TInt32 &x, GHOST_TInt32 &y, GHOST_TInt32 ofs);
00136 
00144         virtual inline bool isInside(GHOST_TInt32 x, GHOST_TInt32 y) const;
00145 
00151         virtual GHOST_TVisibility getVisibility(GHOST_Rect& r) const;
00152 
00159         virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy);
00160 
00170         virtual void setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy, GHOST_TInt32 w, GHOST_TInt32 h);
00171 
00179         virtual bool clip(GHOST_Rect& r) const;
00180 
00182         GHOST_TInt32 m_l;
00184         GHOST_TInt32 m_t;
00186         GHOST_TInt32 m_r;
00188         GHOST_TInt32 m_b;
00189 
00190 #ifdef WITH_CXX_GUARDEDALLOC
00191 public:
00192         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GHOST:GHOST_Rect"); }
00193         void operator delete( void *mem ) { MEM_freeN(mem); }
00194 #endif
00195 };
00196 
00197 
00198 inline GHOST_TInt32 GHOST_Rect::getWidth() const
00199 {
00200         return m_r - m_l;
00201 }
00202 
00203 inline GHOST_TInt32 GHOST_Rect::getHeight() const
00204 {
00205         return m_b - m_t;
00206 }
00207 
00208 inline void GHOST_Rect::set(GHOST_TInt32 l, GHOST_TInt32 t, GHOST_TInt32 r, GHOST_TInt32 b)
00209 {
00210         m_l = l; m_t = t; m_r = r; m_b = b;
00211 }
00212 
00213 inline bool     GHOST_Rect::isEmpty() const
00214 {
00215         return (getWidth() == 0) || (getHeight() == 0);
00216 }
00217 
00218 inline bool GHOST_Rect::isValid() const
00219 {
00220         return (m_l <= m_r) && (m_t <= m_b);
00221 }
00222 
00223 inline void GHOST_Rect::unionRect(const GHOST_Rect& r)
00224 {
00225         if (r.m_l < m_l) m_l = r.m_l;
00226         if (r.m_r > m_r) m_r = r.m_r;
00227         if (r.m_t < m_t) m_t = r.m_t;
00228         if (r.m_b > m_b) m_b = r.m_b;
00229 }
00230 
00231 inline void GHOST_Rect::unionPoint(GHOST_TInt32 x, GHOST_TInt32 y)
00232 {
00233         if (x < m_l) m_l = x;
00234         if (x > m_r) m_r = x;
00235         if (y < m_t) m_t = y;
00236         if (y > m_b) m_b = y;
00237 }
00238 #include <stdio.h>
00239 inline void GHOST_Rect::wrapPoint(GHOST_TInt32 &x, GHOST_TInt32 &y, GHOST_TInt32 ofs)
00240 {
00241         GHOST_TInt32 w= getWidth();
00242         GHOST_TInt32 h= getHeight();
00243 
00244         /* highly unlikely but avoid eternal loop */
00245         if(w-ofs*2 <= 0 || h-ofs*2 <= 0)
00246                 return;
00247         while(x-ofs < m_l)              x+= w-(ofs*2);
00248         while(y-ofs < m_t)              y+= h-(ofs*2);
00249         while(x+ofs > m_r)              x-= w-(ofs*2);
00250         while(y+ofs > m_b)              y-= h-(ofs*2);
00251 }
00252 
00253 inline bool GHOST_Rect::isInside(GHOST_TInt32 x, GHOST_TInt32 y) const
00254 {
00255         return (x >= m_l) && (x <= m_r) && (y >= m_t) && (y <= m_b);
00256 }
00257 
00258 #endif // _H_GHOST_Rect
00259