OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #ifndef UI_GFX_INTERPOLATED_TRANSFORM_H_ |
| 6 #define UI_GFX_INTERPOLATED_TRANSFORM_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "ui/gfx/transform.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "ui/gfx/point.h" |
| 12 |
| 13 namespace ui { |
| 14 |
| 15 /////////////////////////////////////////////////////////////////////////////// |
| 16 // class InterpolatedTransform |
| 17 // |
| 18 // Abstract base class for transforms that animate over time. These |
| 19 // interpolated transforms can be combined to allow for more sophisticated |
| 20 // animations. For example, you might combine a rotation of 90 degrees between |
| 21 // times 0 and 1, with a scale from 1 to 0.3 between times 0 and 0.25 and a |
| 22 // scale from 0.3 to 1 from between times 0.75 and 1. |
| 23 // |
| 24 /////////////////////////////////////////////////////////////////////////////// |
| 25 class InterpolatedTransform { |
| 26 public: |
| 27 InterpolatedTransform(); |
| 28 // The interpolated transform varies only when t in (start_time, end_time). |
| 29 // If t <= start_time, Interpolate(t) will return the initial transform, and |
| 30 // if t >= end_time, Interpolate(t) will return the final transform. |
| 31 InterpolatedTransform(float start_time, float end_time); |
| 32 virtual ~InterpolatedTransform(); |
| 33 |
| 34 // Returns the interpolated transform at time t. Note: not virtual. |
| 35 ui::Transform Interpolate(float t) const; |
| 36 |
| 37 // The Intepolate ultimately returns the product of our transform at time t |
| 38 // and our child's transform at time t (if we have one). |
| 39 // |
| 40 // This function takes ownership of the passed InterpolatedTransform. |
| 41 void SetChild(InterpolatedTransform* child); |
| 42 |
| 43 protected: |
| 44 // Calculates the interpolated transform without considering our child. |
| 45 virtual ui::Transform InterpolateButDoNotCompose(float t) const = 0; |
| 46 |
| 47 // If time in (start_time_, end_time_], this function linearly interpolates |
| 48 // between start_value and end_value. More precisely it returns |
| 49 // (1 - t) * start_value + t * end_value where |
| 50 // t = (start_time_ - time) / (end_time_ - start_time_). |
| 51 // If time < start_time_ it returns start_value, and if time >= end_time_ |
| 52 // it returns end_value. |
| 53 float ValueBetween(float time, float start_value, float end_value) const; |
| 54 |
| 55 private: |
| 56 const float start_time_; |
| 57 const float end_time_; |
| 58 |
| 59 // The child transform. If you consider an interpolated transform as a |
| 60 // function of t. If, without a child, we are f(t), and our child is |
| 61 // g(t), then with a child we become f'(t) = f(t) * g(t). Using a child |
| 62 // transform, we can chain collections of transforms together. |
| 63 scoped_ptr<InterpolatedTransform> child_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(InterpolatedTransform); |
| 66 }; |
| 67 |
| 68 /////////////////////////////////////////////////////////////////////////////// |
| 69 // class InterpolatedRotation |
| 70 // |
| 71 // Represents an animated rotation. |
| 72 // |
| 73 /////////////////////////////////////////////////////////////////////////////// |
| 74 class InterpolatedRotation : public InterpolatedTransform { |
| 75 public: |
| 76 InterpolatedRotation(float start_degrees, float end_degrees); |
| 77 InterpolatedRotation(float start_degrees, |
| 78 float end_degrees, |
| 79 float start_time, |
| 80 float end_time); |
| 81 virtual ~InterpolatedRotation(); |
| 82 |
| 83 protected: |
| 84 virtual ui::Transform InterpolateButDoNotCompose(float t) const OVERRIDE; |
| 85 |
| 86 private: |
| 87 const float start_degrees_; |
| 88 const float end_degrees_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(InterpolatedRotation); |
| 91 }; |
| 92 |
| 93 /////////////////////////////////////////////////////////////////////////////// |
| 94 // class InterpolatedScale |
| 95 // |
| 96 // Represents an animated scale. |
| 97 // |
| 98 /////////////////////////////////////////////////////////////////////////////// |
| 99 class InterpolatedScale : public InterpolatedTransform { |
| 100 public: |
| 101 InterpolatedScale(float start_scale, float end_scale); |
| 102 InterpolatedScale(float start_scale, |
| 103 float end_scale, |
| 104 float start_time, |
| 105 float end_time); |
| 106 virtual ~InterpolatedScale(); |
| 107 |
| 108 protected: |
| 109 virtual ui::Transform InterpolateButDoNotCompose(float t) const OVERRIDE; |
| 110 |
| 111 private: |
| 112 const float start_scale_; |
| 113 const float end_scale_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(InterpolatedScale); |
| 116 }; |
| 117 |
| 118 class InterpolatedTranslation : public InterpolatedTransform { |
| 119 public: |
| 120 InterpolatedTranslation(const gfx::Point& start_pos, |
| 121 const gfx::Point& end_pos); |
| 122 InterpolatedTranslation(const gfx::Point& start_pos, |
| 123 const gfx::Point& end_pos, |
| 124 float start_time, |
| 125 float end_time); |
| 126 virtual ~InterpolatedTranslation(); |
| 127 |
| 128 protected: |
| 129 virtual ui::Transform InterpolateButDoNotCompose(float t) const OVERRIDE; |
| 130 |
| 131 private: |
| 132 const gfx::Point start_pos_; |
| 133 const gfx::Point end_pos_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(InterpolatedTranslation); |
| 136 }; |
| 137 |
| 138 /////////////////////////////////////////////////////////////////////////////// |
| 139 // class InterpolatedConstantTransform |
| 140 // |
| 141 // Represents a transform that is constant over time. This is only useful when |
| 142 // composed with other interpolated transforms. |
| 143 // |
| 144 // See InterpolatedTransformAboutPivot for an example of its usage. |
| 145 // |
| 146 /////////////////////////////////////////////////////////////////////////////// |
| 147 class InterpolatedConstantTransform : public InterpolatedTransform { |
| 148 public: |
| 149 InterpolatedConstantTransform(const ui::Transform& transform); |
| 150 virtual ~InterpolatedConstantTransform(); |
| 151 |
| 152 protected: |
| 153 virtual ui::Transform InterpolateButDoNotCompose(float t) const OVERRIDE; |
| 154 |
| 155 private: |
| 156 const ui::Transform transform_; |
| 157 |
| 158 DISALLOW_COPY_AND_ASSIGN(InterpolatedConstantTransform); |
| 159 }; |
| 160 |
| 161 /////////////////////////////////////////////////////////////////////////////// |
| 162 // class InterpolatedTransformAboutPivot |
| 163 // |
| 164 // Represents an animated transform with a transformed origin. Essentially, |
| 165 // at each time, t, the interpolated transform is created by composing |
| 166 // P * T * P^-1 where P is a constant transform to the new origin. |
| 167 // |
| 168 /////////////////////////////////////////////////////////////////////////////// |
| 169 class InterpolatedTransformAboutPivot : public InterpolatedTransform { |
| 170 public: |
| 171 // Takes ownership of the passed transform. |
| 172 InterpolatedTransformAboutPivot(const gfx::Point& pivot, |
| 173 InterpolatedTransform* transform); |
| 174 |
| 175 // Takes ownership of the passed transform. |
| 176 InterpolatedTransformAboutPivot(const gfx::Point& pivot, |
| 177 InterpolatedTransform* transform, |
| 178 float start_time, |
| 179 float end_time); |
| 180 virtual ~InterpolatedTransformAboutPivot(); |
| 181 |
| 182 protected: |
| 183 virtual ui::Transform InterpolateButDoNotCompose(float t) const OVERRIDE; |
| 184 |
| 185 private: |
| 186 void Init(const gfx::Point& pivot, InterpolatedTransform* transform); |
| 187 |
| 188 scoped_ptr<InterpolatedTransform> transform_; |
| 189 |
| 190 DISALLOW_COPY_AND_ASSIGN(InterpolatedTransformAboutPivot); |
| 191 }; |
| 192 |
| 193 } // namespace ui |
| 194 |
| 195 #endif // UI_GFX_INTERPOLATED_TRANSFORM_H_ |
OLD | NEW |