Gnash  0.8.11dev
SWFRect.h
Go to the documentation of this file.
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
18 
19 
20 #ifndef GNASH_RECT_H
21 #define GNASH_RECT_H
22 
23 #include <string>
24 #include <cassert>
25 #include <ostream>
26 #include <cstdint>
27 
28 #include "dsodefs.h"
29 #include "Range2d.h"
30 
31 // Forward decl
32 namespace gnash {
33  class SWFMatrix;
34  namespace geometry {
35  class Point2d;
36  }
37 }
38 
39 namespace gnash {
40 
44 class SWFRect
45 {
46 
47 public:
48 
49  constexpr static std::int32_t rectNull = 0x80000000;
50  constexpr static std::int32_t rectMax = 0x7fffffff;
51 
53  friend std::ostream& operator<< (std::ostream& os, const SWFRect& SWFRect);
54 
56  constexpr SWFRect()
57  :
58  _xMin(rectNull),
59  _yMin(rectNull),
60  _xMax(rectNull),
61  _yMax(rectNull)
62  {}
63 
65  constexpr SWFRect(int xmin, int ymin, int xmax, int ymax)
66  :
67  _xMin(xmin),
68  _yMin(ymin),
69  _xMax(xmax),
70  _yMax(ymax)
71  {
72  }
73 
75  bool is_null() const
76  {
77  return (_xMin == rectNull && _xMax == rectNull);
78  }
79 
81  void set_null()
82  {
83  _xMin = _yMin = _xMax = _yMax = rectNull;
84  }
85 
87  bool is_world() const
88  {
89  return _xMin == (- rectMax >> 9)
90  && _yMin == (- rectMax >> 9)
91  && _xMax == (rectMax >> 9)
92  && _yMax == (rectMax >> 9);
93  }
94 
96  void set_world()
97  {
98  _xMin = _yMin = - rectMax >> 9;
99  _xMax = _yMax = rectMax >> 9;
100  }
101 
103  std::int32_t width() const
104  {
105  return _xMax - _xMin;
106  }
107 
109  std::int32_t height() const
110  {
111  return _yMax - _yMin;
112  }
113 
115  std::int32_t get_x_min() const
116  {
117  assert(!is_null());
118  return _xMin;
119  }
120 
122  std::int32_t get_x_max() const
123  {
124  assert(!is_null());
125  return _xMax;
126  }
127 
129  std::int32_t get_y_min() const
130  {
131  assert(!is_null());
132  return _yMin;
133  }
134 
136  std::int32_t get_y_max() const
137  {
138  assert(!is_null());
139  return _yMax;
140  }
141 
143  bool point_test(std::int32_t x, std::int32_t y) const
144  {
145  if (is_null()) return false;
146 
147  if (x < _xMin || x > _xMax || y < _yMin || y > _yMax) {
148  return false;
149  }
150  return true;
151  }
152 
154  void set_to_point(std::int32_t x, std::int32_t y)
155  {
156  _xMin = _xMax = x;
157  _yMin = _yMax = y;
158  }
159 
160 
161  void set_to_rect(std::int32_t x1, std::int32_t y1, std::int32_t x2,
162  std::int32_t y2)
163  {
164  _xMin = x1;
165  _yMin = y1;
166  _xMax = x2;
167  _yMax = y2;
168  }
169 
171  void expand_to_point(std::int32_t x, std::int32_t y)
172  {
173  if (is_null()) {
174  set_to_point(x, y);
175  } else {
176  expand_to(x, y);
177  }
178  }
179 
183  void enclose_transformed_rect(const SWFMatrix& m, const SWFRect& r);
184 
186  void expand_to_circle(std::int32_t x, std::int32_t y,
187  std::int32_t radius)
188  {
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);
202  }
203  }
204 
207  DSOEXPORT void expand_to_transformed_rect(const SWFMatrix& m,
208  const SWFRect& r);
209 
211  DSOEXPORT void expand_to_rect(const SWFRect& r);
212 
213  void set_lerp(const SWFRect& a, const SWFRect& b, float t);
214 
218  void clamp(geometry::Point2d& p) const;
219 
221  // TODO: deprecate this.
223  {
224  if (is_null())
225  {
226  // Range2d has a differnt idea about what is a null SWFRect.
228  }
229  else if( is_world() )
230  {
232  }
233  else
234  {
235  return geometry::Range2d<std::int32_t>(_xMin, _yMin,
236  _xMax, _yMax);
237  }
238  }
239 
241  std::string toString() const;
242 
243 private:
244 
245  // make ourself to enclose the given point.
246  void expand_to(std::int32_t x, std::int32_t y)
247  {
248  _xMin = std::min(_xMin, x);
249  _yMin = std::min(_yMin, y);
250  _xMax = std::max(_xMax, x);
251  _yMax = std::max(_yMax, y);
252  }
253 
254  std::int32_t _xMin; // TWIPS
255  std::int32_t _yMin; // TWIPS
256  std::int32_t _xMax; // TWIPS
257  std::int32_t _yMax; // TWIPS
258 };
259 
260 
261 inline std::ostream&
262 operator<<(std::ostream& os, const SWFRect& r)
263 {
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() << ")";
270  }
271  else {
272  os << "NULL RECT!";
273  }
274 
275  return os;
276 }
277 
278 } // namespace gnash
279 
280 #endif // GNASH_RECT_H
281 
282 
283 // Local Variables:
284 // mode: C++
285 // indent-tabs-mode: t
286 // End:
Definition: GnashKey.h:147
A NULL range is a range enclosing NO points.
Definition: Range2d.h:43
void set_to_rect(std::int32_t x1, std::int32_t y1, std::int32_t x2, std::int32_t y2)
Definition: SWFRect.h:161
A WORLD range2d is a range including all points on the plane.
Definition: Range2d.h:52
constexpr SWFRect(int xmin, int ymin, int xmax, int ymax)
Construct a rectangle with given coordinates.
Definition: SWFRect.h:65
Definition: SWFMatrix.h:53
std::int32_t get_y_min() const
Get the y coordinate of the left-up corner.
Definition: SWFRect.h:129
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
std::int32_t get_x_max() const
Get the x coordinate of the right-down corner.
Definition: SWFRect.h:122
geometry::Range2d< std::int32_t > getRange() const
Construct and return a Range2d object.
Definition: SWFRect.h:222
2D Point class
Definition: Point2d.h:38
constexpr SWFRect()
Construct a NULL rectangle.
Definition: SWFRect.h:56
void set_world()
set the rectangle to the WORLD value
Definition: SWFRect.h:96
void expand_to_point(std::int32_t x, std::int32_t y)
Expand this rectangle to enclose the given point.
Definition: SWFRect.h:171
2d Range template class
Definition: Range2d.h:77
Definition: GnashKey.h:164
std::ostream & operator<<(std::ostream &os, const Point2d &p)
Output operator.
Definition: Point2d.h:136
Definition: GnashKey.h:166
const std::string & toString(VM &vm, const ObjectURI &uri)
Definition: VM.h:308
std::int32_t x
Definition: BitmapData_as.cpp:434
std::int32_t width() const
Return width of this rectangle in TWIPS.
Definition: SWFRect.h:103
Definition: GnashKey.h:148
#define DSOEXPORT
Definition: dsodefs.h:55
T clamp(T i, T min, T max)
Definition: GnashNumeric.h:77
void set_to_point(std::int32_t x, std::int32_t y)
Set ourself to bound the given point.
Definition: SWFRect.h:154
std::int32_t get_y_max() const
Get the y coordinate of the right-down corner.
Definition: SWFRect.h:136
void set_null()
set the rectangle to the NULL value
Definition: SWFRect.h:81
std::int32_t y
Definition: BitmapData_as.cpp:435
Rectangle class, see swf defined rectangle record.
Definition: SWFRect.h:44
Definition: GnashKey.h:162
std::int32_t height() const
Return height of this rectangle in TWIPS.
Definition: SWFRect.h:109
void expand_to_circle(std::int32_t x, std::int32_t y, std::int32_t radius)
Expand this rectangle to enclose the given circle.
Definition: SWFRect.h:186
bool is_world() const
TODO: deprecate this &#39;world&#39; concept.
Definition: SWFRect.h:87
Definition: GnashKey.h:159
std::int32_t get_x_min() const
Get the x coordinate of the left-up corner.
Definition: SWFRect.h:115
bool point_test(std::int32_t x, std::int32_t y) const
Return true if the given point is inside this SWFRect.
Definition: SWFRect.h:143
bool is_null() const
returns true if this is a NULL rectangle
Definition: SWFRect.h:75