Blender  V2.59
GHOST_Rect.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: GHOST_Rect.cpp 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 #include "GHOST_Rect.h"
00035 
00036 
00037 
00038 void GHOST_Rect::inset(GHOST_TInt32 i)
00039 {
00040         if (i > 0) {
00041                 // Grow the rectangle
00042                 m_l -= i;
00043                 m_r += i;
00044                 m_t -= i;
00045                 m_b += i;
00046         }
00047         else if (i < 0) {
00048                 // Shrink the rectangle, check for insets larger than half the size
00049                 GHOST_TInt32 i2 = i * 2;
00050                 if (getWidth() > i2) {
00051                         m_l += i;
00052                         m_r -= i;
00053                 }
00054                 else {
00055                         m_l = m_l + ((m_r - m_l) / 2);
00056                         m_r = m_l;
00057                 }
00058                 if (getHeight() > i2) {
00059                         m_t += i;
00060                         m_b -= i;
00061                 }
00062                 else {
00063                         m_t = m_t + ((m_b - m_t) / 2);
00064                         m_b = m_t;
00065                 }
00066         }
00067 }
00068 
00069 
00070 GHOST_TVisibility GHOST_Rect::getVisibility(GHOST_Rect& r) const
00071 {
00072         bool lt = isInside(r.m_l, r.m_t);
00073         bool rt = isInside(r.m_r, r.m_t);
00074         bool lb = isInside(r.m_l, r.m_b);
00075         bool rb = isInside(r.m_r, r.m_b);
00076         GHOST_TVisibility v;
00077         if (lt && rt && lb && rb) {
00078                 // All points inside, rectangle is inside this
00079                 v = GHOST_kFullyVisible;                
00080         }
00081         else if (!(lt || rt || lb || rb)) {
00082                 // None of the points inside
00083                 // Check to see whether the rectangle is larger than this one
00084                 if ((r.m_l < m_l) && (r.m_t < m_t) && (r.m_r > m_r) && (r.m_b > m_b)) {
00085                         v = GHOST_kPartiallyVisible;
00086                 }
00087                 else {
00088                         v = GHOST_kNotVisible;
00089                 }
00090         }
00091         else {
00092                 // Some of the points inside, rectangle is partially inside
00093                 v = GHOST_kPartiallyVisible;
00094         }
00095         return v;
00096 }
00097 
00098 
00099 void GHOST_Rect::setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy)
00100 {
00101         GHOST_TInt32 offset = cx - (m_l + (m_r - m_l)/2);
00102         m_l += offset;
00103         m_r += offset;
00104         offset = cy - (m_t + (m_b - m_t)/2);
00105         m_t += offset;
00106         m_b += offset;
00107 }
00108 
00109 void GHOST_Rect::setCenter(GHOST_TInt32 cx, GHOST_TInt32 cy, GHOST_TInt32 w, GHOST_TInt32 h)
00110 {
00111         long w_2, h_2;
00112         
00113         w_2 = w >> 1;
00114         h_2 = h >> 1;
00115         m_l = cx - w_2;
00116         m_t = cy - h_2;
00117         m_r = m_l + w;
00118         m_b = m_t + h;
00119 }
00120 
00121 bool GHOST_Rect::clip(GHOST_Rect& r) const
00122 {
00123         bool clipped = false;
00124         if (r.m_l < m_l) {
00125                 r.m_l = m_l;
00126                 clipped = true;
00127         }
00128         if (r.m_t < m_t) {
00129                 r.m_t = m_t;
00130                 clipped = true;
00131         }
00132         if (r.m_r > m_r) {
00133                 r.m_r = m_r;
00134                 clipped = true;
00135         }
00136         if (r.m_b > m_b) {
00137                 r.m_b = m_b;
00138                 clipped = true;
00139         }
00140         return clipped;
00141 }
00142