2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <boost/cstdint.hpp>
42 /// Rectangle class, see swf defined rectangle record.
49 static const boost::int32_t rectNull
= 0x80000000;
50 static const boost::int32_t rectMax
= 0x7fffffff;
53 friend std::ostream
& operator<< (std::ostream
& os
, const SWFRect
& SWFRect
);
55 /// Construct a NULL rectangle
64 /// Construct a rectangle with given coordinates
65 SWFRect(int xmin
, int ymin
, int xmax
, int ymax
)
74 /// returns true if this is a NULL rectangle
77 return (_xMin
== rectNull
&& _xMax
== rectNull
);
80 /// set the rectangle to the NULL value
83 _xMin
= _yMin
= _xMax
= _yMax
= rectNull
;
86 /// TODO: deprecate this 'world' concept.
89 return _xMin
== (- rectMax
>> 9)
90 && _yMin
== (- rectMax
>> 9)
91 && _xMax
== (rectMax
>> 9)
92 && _yMax
== (rectMax
>> 9);
95 /// set the rectangle to the WORLD value
98 _xMin
= _yMin
= - rectMax
>> 9;
99 _xMax
= _yMax
= rectMax
>> 9;
102 /// Return width of this rectangle in TWIPS
103 boost::int32_t width() const
105 return _xMax
- _xMin
;
108 /// Return height of this rectangle in TWIPS
109 boost::int32_t height() const
111 return _yMax
- _yMin
;
114 /// Get the x coordinate of the left-up corner.
115 boost::int32_t get_x_min() const
121 /// Get the x coordinate of the right-down corner.
122 boost::int32_t get_x_max() const
128 /// Get the y coordinate of the left-up corner.
129 boost::int32_t get_y_min() const
135 /// Get the y coordinate of the right-down corner.
136 boost::int32_t get_y_max() const
142 /// Return true if the given point is inside this SWFRect.
143 bool point_test(boost::int32_t x
, boost::int32_t y
) const
145 if (is_null()) return false;
147 if (x
< _xMin
|| x
> _xMax
|| y
< _yMin
|| y
> _yMax
) {
153 /// Set ourself to bound the given point
154 void set_to_point(boost::int32_t x
, boost::int32_t y
)
161 void set_to_rect(boost::int32_t x1
, boost::int32_t y1
, boost::int32_t x2
,
170 /// Expand this rectangle to enclose the given point.
171 void expand_to_point(boost::int32_t x
, boost::int32_t y
)
180 /// Set ourself to bound a rectangle that has been transformed by m.
181 /// This is an axial bound of an oriented (and/or
182 /// sheared, scaled, etc) box.
183 void enclose_transformed_rect(const SWFMatrix
& m
, const SWFRect
& r
);
185 /// Expand this rectangle to enclose the given circle.
186 void expand_to_circle(boost::int32_t x
, boost::int32_t y
,
187 boost::int32_t radius
)
189 // I know it's easy to make code work for minus radius.
190 // would do that untill I see the requirement for a SWF RECTANGLE.
198 _xMin
= std::min(_xMin
, x
- radius
);
199 _yMin
= std::min(_yMin
, y
- radius
);
200 _xMax
= std::max(_xMax
, x
+ radius
);
201 _yMax
= std::max(_yMax
, y
+ radius
);
205 /// Same as enclose_transformed_rect but expanding the current SWFRect
206 /// instead of replacing it.
207 DSOEXPORT
void expand_to_transformed_rect(const SWFMatrix
& m
,
210 /// Makes union of the given and the current SWFRect
211 DSOEXPORT
void expand_to_rect(const SWFRect
& r
);
213 void set_lerp(const SWFRect
& a
, const SWFRect
& b
, float t
);
216 /// Make sure that the given point falls in this rectangle,
217 /// modifying it's coordinates if needed.
218 void clamp(geometry::Point2d
& p
) const;
220 /// Construct and return a Range2d object.
221 // TODO: deprecate this.
222 geometry::Range2d
<boost::int32_t> getRange() const
226 // Range2d has a differnt idea about what is a null SWFRect.
227 return geometry::Range2d
<boost::int32_t>(geometry::nullRange
);
229 else if( is_world() )
231 return geometry::Range2d
<boost::int32_t>(geometry::worldRange
);
235 return geometry::Range2d
<boost::int32_t>(_xMin
, _yMin
,
240 /// Return a string representation for this rectangle
241 std::string
toString() const;
245 // make ourself to enclose the given point.
246 void expand_to(boost::int32_t x
, boost::int32_t y
)
248 _xMin
= std::min(_xMin
, x
);
249 _yMin
= std::min(_yMin
, y
);
250 _xMax
= std::max(_xMax
, x
);
251 _yMax
= std::max(_yMax
, y
);
254 boost::int32_t _xMin
; // TWIPS
255 boost::int32_t _yMin
; // TWIPS
256 boost::int32_t _xMax
; // TWIPS
257 boost::int32_t _yMax
; // TWIPS
262 operator<<(std::ostream
& os
, const SWFRect
& r
)
266 << r
.get_x_min() << ","
267 << r
.get_y_min() << ","
268 << r
.get_x_max() << ","
269 << r
.get_y_max() << ")";
280 #endif // GNASH_RECT_H
285 // indent-tabs-mode: t