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 #include "SWFRect.h"
20 
21 #include <sstream>
22 
23 #include "SWFMatrix.h"
24 #include "Point2d.h"
25 #include "GnashNumeric.h" // for flerp, clamp...
26 
27 
28 namespace gnash {
29 
30 // Set ourself to bound a rectangle that has been transformed by m.
31 void
enclose_transformed_rect(const SWFMatrix & m,const SWFRect & r)32 SWFRect::enclose_transformed_rect(const SWFMatrix& m, const SWFRect& r)
33 {
34     std::int32_t  x1 = r.get_x_min();
35     std::int32_t  y1 = r.get_y_min();
36     std::int32_t  x2 = r.get_x_max();
37     std::int32_t  y2 = r.get_y_max();
38 
39     point  p0(x1, y1);
40     point  p1(x2, y1);
41     point  p2(x2, y2);
42     point  p3(x1, y2);
43 
44     m.transform(p0);
45     m.transform(p1);
46     m.transform(p2);
47     m.transform(p3);
48 
49     set_to_point(p0.x, p0.y);
50     expand_to(p1.x, p1.y);
51     expand_to(p2.x, p2.y);
52     expand_to(p3.x, p3.y);
53 }
54 
55 void
expand_to_rect(const SWFRect & r)56 SWFRect::expand_to_rect(const SWFRect& r)
57 // Expand ourself to enclose the given SWFRect.
58 {
59     if( r.is_null() ) {
60         return;
61     }
62 
63     if( is_null() ) {
64         *this = r;
65     }else {
66         _xMin = std::min(_xMin, r.get_x_min());
67         _yMin = std::min(_yMin, r.get_y_min());
68         _xMax = std::max(_xMax, r.get_x_max());
69         _yMax = std::max(_yMax, r.get_y_max());
70     }
71 }
72 
73 void
expand_to_transformed_rect(const SWFMatrix & m,const SWFRect & r)74 SWFRect::expand_to_transformed_rect(const SWFMatrix& m, const SWFRect& r)
75 {
76     if (r.is_null()) {
77          return;
78     }
79 
80     const std::int32_t x1 = r.get_x_min();
81     const std::int32_t y1 = r.get_y_min();
82     const std::int32_t x2 = r.get_x_max();
83     const std::int32_t y2 = r.get_y_max();
84 
85     point p0(x1, y1);
86     point p1(x2, y1);
87     point p2(x2, y2);
88     point p3(x1, y2);
89 
90     m.transform(p0);
91     m.transform(p1);
92     m.transform(p2);
93     m.transform(p3);
94 
95     if (is_null()) {
96         set_to_point(p0.x, p0.y);
97     }
98     else {
99         expand_to(p0.x, p0.y);
100     }
101     expand_to(p1.x, p1.y);
102     expand_to(p2.x, p2.y);
103     expand_to(p3.x, p3.y);
104 }
105 
106 void
set_lerp(const SWFRect & a,const SWFRect & b,float t)107 SWFRect::set_lerp(const SWFRect& a, const SWFRect& b, float t)
108 // Set this to the lerp of a and b.
109 {
110     assert( !a.is_null() );
111     assert( !b.is_null() );
112 
113     _xMin = lerp<float>(a.get_x_min(), b.get_x_min(), t);
114     _yMin = lerp<float>(a.get_y_min(), b.get_y_min(), t);
115     _xMax = lerp<float>(a.get_x_max(), b.get_x_max(), t);
116     _yMax = lerp<float>(a.get_y_max(), b.get_y_max(), t);
117 }
118 
119 void
clamp(point & p) const120 SWFRect::clamp(point& p) const
121 {
122     assert( !is_null() );
123     p.x = gnash::clamp<std::int32_t>(p.x, _xMin, _xMax);
124     p.y = gnash::clamp<std::int32_t>(p.y, _yMin, _yMax);
125 }
126 
127 std::string
toString() const128 SWFRect::toString() const
129 {
130     std::stringstream ss;
131     ss << *this;
132     return ss.str();
133 }
134 
135 }   // end namespace gnash
136 
137 
138 // Local Variables:
139 // mode: C++
140 // c-basic-offset: 8
141 // tab-width: 8
142 // indent-tabs-mode: t
143 // End:
144