Make automated FSCommand invocation tests show player-side output.
[gnash.git] / libcore / SWFRect.h
blob11f552b97d3063bd7625c0dea8fe8fa5ae617cff
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
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.
9 //
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.
14 //
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
20 #ifndef GNASH_RECT_H
21 #define GNASH_RECT_H
23 #include <string>
24 #include <cassert>
25 #include <ostream>
26 #include <cstdint>
28 #include "dsodefs.h"
29 #include "Range2d.h"
31 // Forward decl
32 namespace gnash {
33 class SWFMatrix;
34 namespace geometry {
35 class Point2d;
39 namespace gnash {
41 /// \brief
42 /// Rectangle class, see swf defined rectangle record.
43 ///
44 class SWFRect
47 public:
49 constexpr static std::int32_t rectNull = 0x80000000;
50 constexpr static std::int32_t rectMax = 0x7fffffff;
52 /// Ouput operator
53 friend std::ostream& operator<< (std::ostream& os, const SWFRect& SWFRect);
55 /// Construct a NULL rectangle
56 constexpr SWFRect()
58 _xMin(rectNull),
59 _yMin(rectNull),
60 _xMax(rectNull),
61 _yMax(rectNull)
64 /// Construct a rectangle with given coordinates
65 constexpr SWFRect(int xmin, int ymin, int xmax, int ymax)
67 _xMin(xmin),
68 _yMin(ymin),
69 _xMax(xmax),
70 _yMax(ymax)
74 /// returns true if this is a NULL rectangle
75 bool is_null() const
77 return (_xMin == rectNull && _xMax == rectNull);
80 /// set the rectangle to the NULL value
81 void set_null()
83 _xMin = _yMin = _xMax = _yMax = rectNull;
86 /// TODO: deprecate this 'world' concept.
87 bool is_world() const
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
96 void set_world()
98 _xMin = _yMin = - rectMax >> 9;
99 _xMax = _yMax = rectMax >> 9;
102 /// Return width of this rectangle in TWIPS
103 std::int32_t width() const
105 return _xMax - _xMin;
108 /// Return height of this rectangle in TWIPS
109 std::int32_t height() const
111 return _yMax - _yMin;
114 /// Get the x coordinate of the left-up corner.
115 std::int32_t get_x_min() const
117 assert(!is_null());
118 return _xMin;
121 /// Get the x coordinate of the right-down corner.
122 std::int32_t get_x_max() const
124 assert(!is_null());
125 return _xMax;
128 /// Get the y coordinate of the left-up corner.
129 std::int32_t get_y_min() const
131 assert(!is_null());
132 return _yMin;
135 /// Get the y coordinate of the right-down corner.
136 std::int32_t get_y_max() const
138 assert(!is_null());
139 return _yMax;
142 /// Return true if the given point is inside this SWFRect.
143 bool point_test(std::int32_t x, std::int32_t y) const
145 if (is_null()) return false;
147 if (x < _xMin || x > _xMax || y < _yMin || y > _yMax) {
148 return false;
150 return true;
153 /// Set ourself to bound the given point
154 void set_to_point(std::int32_t x, std::int32_t y)
156 _xMin = _xMax = x;
157 _yMin = _yMax = y;
161 void set_to_rect(std::int32_t x1, std::int32_t y1, std::int32_t x2,
162 std::int32_t y2)
164 _xMin = x1;
165 _yMin = y1;
166 _xMax = x2;
167 _yMax = y2;
170 /// Expand this rectangle to enclose the given point.
171 void expand_to_point(std::int32_t x, std::int32_t y)
173 if (is_null()) {
174 set_to_point(x, y);
175 } else {
176 expand_to(x, 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(std::int32_t x, std::int32_t y,
187 std::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.
191 assert(radius >= 0);
192 if (is_null()) {
193 _xMin = x - radius;
194 _yMin = y - radius;
195 _xMax = x + radius;
196 _yMax = y + radius;
197 } else {
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,
208 const SWFRect& r);
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);
215 /// \brief
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<std::int32_t> getRange() const
224 if (is_null())
226 // Range2d has a differnt idea about what is a null SWFRect.
227 return geometry::Range2d<std::int32_t>(geometry::nullRange);
229 else if( is_world() )
231 return geometry::Range2d<std::int32_t>(geometry::worldRange);
233 else
235 return geometry::Range2d<std::int32_t>(_xMin, _yMin,
236 _xMax, _yMax);
240 /// Return a string representation for this rectangle
241 std::string toString() const;
243 private:
245 // make ourself to enclose the given point.
246 void expand_to(std::int32_t x, std::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 std::int32_t _xMin; // TWIPS
255 std::int32_t _yMin; // TWIPS
256 std::int32_t _xMax; // TWIPS
257 std::int32_t _yMax; // TWIPS
261 inline std::ostream&
262 operator<<(std::ostream& os, const SWFRect& r)
264 if (!r.is_null()) {
265 os << "RECT("
266 << r.get_x_min() << ","
267 << r.get_y_min() << ","
268 << r.get_x_max() << ","
269 << r.get_y_max() << ")";
271 else {
272 os << "NULL RECT!";
275 return os;
278 } // namespace gnash
280 #endif // GNASH_RECT_H
283 // Local Variables:
284 // mode: C++
285 // indent-tabs-mode: t
286 // End: