1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_GFX_INTERPOLATED_TRANSFORM_H_
6 #define UI_GFX_INTERPOLATED_TRANSFORM_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "ui/gfx/point.h"
11 #include "ui/gfx/point3_f.h"
12 #include "ui/gfx/transform.h"
13 #include "ui/gfx/transform_util.h"
14 #include "ui/gfx/vector3d_f.h"
18 ///////////////////////////////////////////////////////////////////////////////
19 // class InterpolatedTransform
21 // Abstract base class for transforms that animate over time. These
22 // interpolated transforms can be combined to allow for more sophisticated
23 // animations. For example, you might combine a rotation of 90 degrees between
24 // times 0 and 1, with a scale from 1 to 0.3 between times 0 and 0.25 and a
25 // scale from 0.3 to 1 from between times 0.75 and 1.
27 ///////////////////////////////////////////////////////////////////////////////
28 class GFX_EXPORT InterpolatedTransform
{
30 InterpolatedTransform();
31 // The interpolated transform varies only when t in (start_time, end_time).
32 // If t <= start_time, Interpolate(t) will return the initial transform, and
33 // if t >= end_time, Interpolate(t) will return the final transform.
34 InterpolatedTransform(float start_time
, float end_time
);
35 virtual ~InterpolatedTransform();
37 // Returns the interpolated transform at time t. Note: not virtual.
38 gfx::Transform
Interpolate(float t
) const;
40 // The Intepolate ultimately returns the product of our transform at time t
41 // and our child's transform at time t (if we have one).
43 // This function takes ownership of the passed InterpolatedTransform.
44 void SetChild(InterpolatedTransform
* child
);
46 // If the interpolated transform is reversed, Interpolate(t) will return
48 void SetReversed(bool reversed
) { reversed_
= reversed
; }
49 bool Reversed() const { return reversed_
; }
52 // Calculates the interpolated transform without considering our child.
53 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const = 0;
55 // If time in (start_time_, end_time_], this function linearly interpolates
56 // between start_value and end_value. More precisely it returns
57 // (1 - t) * start_value + t * end_value where
58 // t = (start_time_ - time) / (end_time_ - start_time_).
59 // If time < start_time_ it returns start_value, and if time >= end_time_
60 // it returns end_value.
61 float ValueBetween(float time
, float start_value
, float end_value
) const;
63 float start_time() const { return start_time_
; }
64 float end_time() const { return end_time_
; }
67 const float start_time_
;
68 const float end_time_
;
70 // The child transform. If you consider an interpolated transform as a
71 // function of t. If, without a child, we are f(t), and our child is
72 // g(t), then with a child we become f'(t) = f(t) * g(t). Using a child
73 // transform, we can chain collections of transforms together.
74 scoped_ptr
<InterpolatedTransform
> child_
;
78 DISALLOW_COPY_AND_ASSIGN(InterpolatedTransform
);
81 ///////////////////////////////////////////////////////////////////////////////
82 // class InterpolatedRotation
84 // Represents an animated rotation.
86 ///////////////////////////////////////////////////////////////////////////////
87 class GFX_EXPORT InterpolatedRotation
: public InterpolatedTransform
{
89 InterpolatedRotation(float start_degrees
, float end_degrees
);
90 InterpolatedRotation(float start_degrees
,
94 virtual ~InterpolatedRotation();
97 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const override
;
100 const float start_degrees_
;
101 const float end_degrees_
;
103 DISALLOW_COPY_AND_ASSIGN(InterpolatedRotation
);
106 ///////////////////////////////////////////////////////////////////////////////
107 // class InterpolatedAxisAngleRotation
109 // Represents an animated rotation.
111 ///////////////////////////////////////////////////////////////////////////////
112 class GFX_EXPORT InterpolatedAxisAngleRotation
: public InterpolatedTransform
{
114 InterpolatedAxisAngleRotation(const gfx::Vector3dF
& axis
,
117 InterpolatedAxisAngleRotation(const gfx::Vector3dF
& axis
,
122 virtual ~InterpolatedAxisAngleRotation();
125 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const override
;
128 gfx::Vector3dF axis_
;
129 const float start_degrees_
;
130 const float end_degrees_
;
132 DISALLOW_COPY_AND_ASSIGN(InterpolatedAxisAngleRotation
);
135 ///////////////////////////////////////////////////////////////////////////////
136 // class InterpolatedScale
138 // Represents an animated scale.
140 ///////////////////////////////////////////////////////////////////////////////
141 class GFX_EXPORT InterpolatedScale
: public InterpolatedTransform
{
143 InterpolatedScale(float start_scale
, float end_scale
);
144 InterpolatedScale(float start_scale
, float end_scale
,
145 float start_time
, float end_time
);
146 InterpolatedScale(const gfx::Point3F
& start_scale
,
147 const gfx::Point3F
& end_scale
);
148 InterpolatedScale(const gfx::Point3F
& start_scale
,
149 const gfx::Point3F
& end_scale
,
152 virtual ~InterpolatedScale();
155 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const override
;
158 const gfx::Point3F start_scale_
;
159 const gfx::Point3F end_scale_
;
161 DISALLOW_COPY_AND_ASSIGN(InterpolatedScale
);
164 class GFX_EXPORT InterpolatedTranslation
: public InterpolatedTransform
{
166 InterpolatedTranslation(const gfx::Point
& start_pos
,
167 const gfx::Point
& end_pos
);
168 InterpolatedTranslation(const gfx::Point
& start_pos
,
169 const gfx::Point
& end_pos
,
172 InterpolatedTranslation(const gfx::Point3F
& start_pos
,
173 const gfx::Point3F
& end_pos
);
174 InterpolatedTranslation(const gfx::Point3F
& start_pos
,
175 const gfx::Point3F
& end_pos
,
178 virtual ~InterpolatedTranslation();
181 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const override
;
184 const gfx::Point3F start_pos_
;
185 const gfx::Point3F end_pos_
;
187 DISALLOW_COPY_AND_ASSIGN(InterpolatedTranslation
);
190 ///////////////////////////////////////////////////////////////////////////////
191 // class InterpolatedConstantTransform
193 // Represents a transform that is constant over time. This is only useful when
194 // composed with other interpolated transforms.
196 // See InterpolatedTransformAboutPivot for an example of its usage.
198 ///////////////////////////////////////////////////////////////////////////////
199 class GFX_EXPORT InterpolatedConstantTransform
: public InterpolatedTransform
{
201 explicit InterpolatedConstantTransform(const gfx::Transform
& transform
);
202 virtual ~InterpolatedConstantTransform();
205 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const override
;
208 const gfx::Transform transform_
;
210 DISALLOW_COPY_AND_ASSIGN(InterpolatedConstantTransform
);
213 ///////////////////////////////////////////////////////////////////////////////
214 // class InterpolatedTransformAboutPivot
216 // Represents an animated transform with a transformed origin. Essentially,
217 // at each time, t, the interpolated transform is created by composing
218 // P * T * P^-1 where P is a constant transform to the new origin.
220 ///////////////////////////////////////////////////////////////////////////////
221 class GFX_EXPORT InterpolatedTransformAboutPivot
222 : public InterpolatedTransform
{
224 // Takes ownership of the passed transform.
225 InterpolatedTransformAboutPivot(const gfx::Point
& pivot
,
226 InterpolatedTransform
* transform
);
228 // Takes ownership of the passed transform.
229 InterpolatedTransformAboutPivot(const gfx::Point
& pivot
,
230 InterpolatedTransform
* transform
,
233 virtual ~InterpolatedTransformAboutPivot();
236 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const override
;
239 void Init(const gfx::Point
& pivot
, InterpolatedTransform
* transform
);
241 scoped_ptr
<InterpolatedTransform
> transform_
;
243 DISALLOW_COPY_AND_ASSIGN(InterpolatedTransformAboutPivot
);
246 class GFX_EXPORT InterpolatedMatrixTransform
: public InterpolatedTransform
{
248 InterpolatedMatrixTransform(const gfx::Transform
& start_transform
,
249 const gfx::Transform
& end_transform
);
251 InterpolatedMatrixTransform(const gfx::Transform
& start_transform
,
252 const gfx::Transform
& end_transform
,
256 virtual ~InterpolatedMatrixTransform();
259 virtual gfx::Transform
InterpolateButDoNotCompose(float t
) const override
;
262 void Init(const gfx::Transform
& start_transform
,
263 const gfx::Transform
& end_transform
);
265 gfx::DecomposedTransform start_decomp_
;
266 gfx::DecomposedTransform end_decomp_
;
271 #endif // UI_GFX_INTERPOLATED_TRANSFORM_H_