• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

SWFRect.h

Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
00003 //   Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (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
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 
00020 #ifndef GNASH_RECT_H
00021 #define GNASH_RECT_H
00022 
00023 #include "dsodefs.h"
00024 #include "Range2d.h"
00025 
00026 #include <string>
00027 #include <cassert> // for inlines
00028 #include <iostream> // for output operator
00029 #include <boost/cstdint.hpp>
00030 
00031 // Forward decl
00032 namespace gnash {
00033     class SWFMatrix;
00034     class SWFStream;
00035     namespace geometry {
00036         class Point2d;
00037     }
00038 }
00039 
00040 namespace gnash {
00041 
00045 class SWFRect
00046 {
00047 
00048 public:
00050     //
00066     void    read(SWFStream& in);
00067 
00068     static const boost::int32_t rectNull = 0x80000000;
00069     static const boost::int32_t rectMax = 0x7fffffff;
00070     
00072     friend std::ostream& operator<< (std::ostream& os, const SWFRect& SWFRect);
00073 
00075     SWFRect()
00076         :
00077        _xMin(rectNull),
00078        _yMin(rectNull),
00079        _xMax(rectNull),
00080        _yMax(rectNull)
00081     {}
00082 
00084     SWFRect(int xmin, int ymin, int xmax, int ymax)
00085         :
00086         _xMin(xmin),
00087         _yMin(ymin),
00088         _xMax(xmax),
00089         _yMax(ymax)
00090     {
00091     }
00092 
00094     bool is_null() const
00095     {
00096         return (_xMin == rectNull && _xMax == rectNull);
00097     }
00098 
00100     void set_null()
00101     {
00102         _xMin = _yMin = _xMax = _yMax = rectNull;
00103     }
00104 
00106     bool is_world() const
00107     {
00108         return _xMin == (- rectMax >> 9) 
00109             && _yMin == (- rectMax >> 9) 
00110             && _xMax == (rectMax >> 9)
00111             && _yMax == (rectMax >> 9);
00112     }
00113 
00115     void set_world()
00116     {
00117         _xMin = _yMin = - rectMax >> 9;
00118         _xMax = _yMax = rectMax >> 9;
00119     }
00120 
00122     boost::int32_t width() const
00123     {
00124         return _xMax - _xMin;
00125     }
00126 
00128     boost::int32_t height() const
00129     {
00130         return _yMax - _yMin;
00131     }
00132 
00134     boost::int32_t get_x_min() const
00135     {
00136         assert(!is_null());
00137         return _xMin;
00138     }
00139 
00141     boost::int32_t get_x_max() const
00142     {
00143         assert(!is_null());
00144         return _xMax;
00145     }
00146 
00148     boost::int32_t get_y_min() const
00149     {
00150         assert(!is_null());
00151         return _yMin;
00152     }
00153 
00155     boost::int32_t get_y_max() const
00156     {
00157         assert(!is_null());
00158         return _yMax;
00159     }
00160     
00162     bool point_test(boost::int32_t x, boost::int32_t y) const
00163     {
00164         if (is_null()) return false;
00165         
00166         if (x < _xMin || x > _xMax || y < _yMin || y > _yMax) {
00167             return false;
00168         } 
00169         return true;
00170     }
00171 
00173     void set_to_point(boost::int32_t x, boost::int32_t y)
00174     {
00175         _xMin = _xMax = x;
00176         _yMin = _yMax = y;
00177     }
00178 
00179     
00180     void set_to_rect(boost::int32_t x1, boost::int32_t y1, boost::int32_t x2,
00181             boost::int32_t y2)
00182     {
00183         _xMin = x1;
00184         _yMin = y1;
00185         _xMax = x2;
00186         _yMax = y2;
00187     }
00188     
00190     void expand_to_point(boost::int32_t x, boost::int32_t y)
00191     {
00192         if (is_null()) {
00193             set_to_point(x, y);
00194         } else {
00195             expand_to(x, y);
00196         }
00197     }
00198 
00202     void enclose_transformed_rect(const SWFMatrix& m, const SWFRect& r);
00203     
00205     void expand_to_circle(boost::int32_t x, boost::int32_t y,
00206             boost::int32_t radius)
00207     {
00208         // I know it's easy to make code work for minus radius.
00209         // would do that untill I see the requirement for a SWF RECTANGLE.
00210         assert(radius >= 0); 
00211         if (is_null()) {
00212             _xMin = x - radius;
00213             _yMin = y - radius;
00214             _xMax = x + radius;
00215             _yMax = y + radius;
00216         } else {
00217             _xMin = std::min(_xMin, x - radius);
00218             _yMin = std::min(_yMin, y - radius);
00219             _xMax = std::max(_xMax, x + radius);
00220             _yMax = std::max(_yMax, y + radius);
00221         }
00222     }
00223       
00226     DSOEXPORT void expand_to_transformed_rect(const SWFMatrix& m,
00227             const SWFRect& r);
00228     
00230     DSOEXPORT void expand_to_rect(const SWFRect& r);
00231     
00232     void set_lerp(const SWFRect& a, const SWFRect& b, float t);
00233 
00237     void clamp(geometry::Point2d& p) const;
00238 
00240     // TODO: deprecate this.
00241     geometry::Range2d<boost::int32_t> getRange() const
00242     {
00243         if (is_null())
00244         {
00245            // Range2d has a differnt idea about what is a null SWFRect.
00246            return geometry::Range2d<boost::int32_t>(geometry::nullRange); 
00247         }
00248         else if( is_world() ) 
00249         {
00250             return geometry::Range2d<boost::int32_t>(geometry::worldRange);
00251         }
00252         else
00253         {
00254             return geometry::Range2d<boost::int32_t>(_xMin, _yMin,
00255                     _xMax, _yMax);
00256         }
00257     }
00258 
00260     std::string toString() const;
00261 
00262 private:
00263 
00264     // make ourself to enclose the given point.
00265     void expand_to(boost::int32_t x, boost::int32_t y)
00266     {
00267         _xMin = std::min(_xMin, x);
00268         _yMin = std::min(_yMin, y);
00269         _xMax = std::max(_xMax, x);
00270         _yMax = std::max(_yMax, y);
00271     }
00272 
00273     boost::int32_t _xMin; // TWIPS
00274     boost::int32_t _yMin; // TWIPS
00275     boost::int32_t _xMax; // TWIPS
00276     boost::int32_t _yMax; // TWIPS
00277 };
00278 
00279 
00280 inline std::ostream&
00281 operator<< (std::ostream& os, const SWFRect& r)
00282 {
00283     if (!r.is_null()) {
00284         os << "RECT(" 
00285            << r.get_x_min() << "," 
00286            << r.get_y_min() << "," 
00287            << r.get_x_max() << "," 
00288            << r.get_y_max() << ")";
00289     }
00290     else {
00291         os << "NULL RECT!";
00292     }
00293 
00294     return os;
00295 }
00296 
00297 }   // namespace gnash
00298 
00299 #endif // GNASH_RECT_H
00300 
00301 
00302 // Local Variables:
00303 // mode: C++
00304 // indent-tabs-mode: t
00305 // End:

Generated on Fri Mar 16 2012 15:46:12 for Gnash by  doxygen 1.7.1