OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 5 #ifndef UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
6 #define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 6 #define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <deque> |
10 #include <vector> | |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "third_party/skia/include/core/SkScalar.h" | 14 #include "base/time.h" |
15 #include "third_party/skia/include/utils/SkMatrix44.h" | |
16 #include "ui/base/animation/animation_delegate.h" | |
17 #include "ui/gfx/compositor/compositor_export.h" | 15 #include "ui/gfx/compositor/compositor_export.h" |
16 #include "ui/gfx/compositor/layer_animation_element.h" | |
18 | 17 |
19 namespace gfx { | 18 namespace gfx { |
20 class Point; | 19 class Rect; |
21 } | 20 } |
22 | 21 |
23 namespace ui { | 22 namespace ui { |
24 | |
25 class Animation; | 23 class Animation; |
26 class Layer; | 24 class Layer; |
27 class LayerAnimatorDelegate; | 25 class LayerAnimationPreemptionStrategy; |
26 class LayerAnimationSequence; | |
28 class Transform; | 27 class Transform; |
29 | 28 |
30 // LayerAnimator manages animating various properties of a Layer. | 29 // When a property of layer needs to be changed it is set by way of |
31 class COMPOSITOR_EXPORT LayerAnimator : public ui::AnimationDelegate { | 30 // LayerAnimator. This enables LayerAnimator to animate property |
31 // changes. | |
32 class COMPOSITOR_EXPORT LayerAnimator { | |
32 public: | 33 public: |
33 // Types of properties that can be animated. | 34 // We need to keep track of the start time of every running animation. |
34 enum AnimationProperty { | 35 struct RunningAnimation { |
35 LOCATION, | 36 RunningAnimation(LayerAnimationSequence* sequence, |
36 OPACITY, | 37 base::TimeTicks start_time) |
37 TRANSFORM, | 38 : sequence(sequence), |
39 start_time(start_time) { | |
40 } | |
41 LayerAnimationSequence* sequence; | |
42 base::TimeTicks start_time; | |
38 }; | 43 }; |
44 typedef std::vector<RunningAnimation> RunningAnimations; | |
45 typedef std::deque<scoped_refptr<LayerAnimationSequence> > AnimationQueue; | |
39 | 46 |
40 explicit LayerAnimator(Layer* layer); | 47 // No implicit animations when properties are set. |
48 static LayerAnimator* CreateDefaultAnimator(); | |
49 | |
50 // Implicitly animates when properties are set. | |
51 static LayerAnimator* CreateImplicitAnimator(); | |
52 | |
53 explicit LayerAnimator(base::TimeDelta transition_duration); | |
41 virtual ~LayerAnimator(); | 54 virtual ~LayerAnimator(); |
42 | 55 |
43 // Sets the animation to use. LayerAnimator takes ownership of the animation. | 56 // Sets the transform on the Layer. |
44 void SetAnimation(Animation* animation); | 57 virtual void SetTransform(const Transform& transform); |
45 | 58 |
46 ui::Layer* layer() { return layer_; } | 59 // Sets the bounds of the layer. |
60 virtual void SetBounds(const gfx::Rect& bounds); | |
47 | 61 |
48 // Animates the layer to the specified point. The point is relative to the | 62 // Sets the opacity of the layer. |
49 // parent layer. | 63 virtual void SetOpacity(float opacity); |
50 void AnimateToPoint(const gfx::Point& target); | |
51 | 64 |
52 // Animates the transform from the current transform to |transform|. | 65 // Sets the layer the property setter is associated with. The property setter |
53 void AnimateTransform(const Transform& transform); | 66 // does not own the layer and can only be associated with a single layer. |
67 void SetLayer(Layer* layer); | |
54 | 68 |
55 // Animates the opacity from the current opacity to |target_opacity|. | 69 // Sets the animation preemption strategy. This determines the behaviour if |
56 void AnimateOpacity(float target_opacity); | 70 // a property is set during an animation. This class does not own the |
71 // strategy. | |
72 void set_preemption_strategy(LayerAnimationPreemptionStrategy* strategy) { | |
73 preemption_strategy_ = strategy; | |
74 } | |
57 | 75 |
58 // Returns the target value for the specified type. If the specified property | 76 // Set an animation sequence. If an animation for the same property is in |
59 // is not animating, the current value is returned. | 77 // progress, it needs to be interrupted with the new animation. |
60 gfx::Point GetTargetPoint(); | 78 void SetAnimation(LayerAnimationSequence* animation); |
sky
2011/10/14 16:39:52
The name 'Set' implies you can only have one. How
| |
61 float GetTargetOpacity(); | |
62 ui::Transform GetTargetTransform(); | |
63 | 79 |
64 // Returns true if animating |property|. | 80 // Enqueue an animation |
65 bool IsAnimating(AnimationProperty property) const; | 81 void EnqueueAnimation(LayerAnimationSequence* animation); |
sky
2011/10/14 16:39:52
How about Add.
| |
66 | 82 |
67 // Returns true if the animation is running. | 83 // Returns true if there is an animation in the queue (animations remain in |
68 bool IsRunning() const; | 84 // the queue until they complete). |
85 bool IsAnimating() const; | |
69 | 86 |
70 // Returns true if the animation has progressed at least once since | 87 // Stops animating the given property. No effect if there is no running |
71 // SetAnimation() was invoked. | 88 // animation for the given property. |
72 bool got_initial_tick() const { return got_initial_tick_; } | 89 void StopAnimatingProperty( |
90 LayerAnimationElement::AnimatableProperty property); | |
73 | 91 |
74 // AnimationDelegate: | 92 // Stops all animation and clears any queued animations. |
75 virtual void AnimationProgressed(const Animation* animation) OVERRIDE; | 93 void StopAnimating(); |
76 virtual void AnimationEnded(const Animation* animation) OVERRIDE; | 94 |
95 void Step(base::TimeTicks now); | |
96 | |
97 protected: | |
98 Layer* layer() { return layer_; } | |
77 | 99 |
78 private: | 100 private: |
79 // Parameters used when animating the location. | 101 void UpdateAnimationState(); |
80 struct LocationParams { | 102 void RemoveAnimation(LayerAnimationSequence* sequence); |
81 int start_x; | 103 void FinishAnimation(LayerAnimationSequence* sequence); |
82 int start_y; | 104 void FinishAnyAnimationWithZeroDuration(); |
83 int target_x; | 105 void ClearAnimations(); |
84 int target_y; | 106 RunningAnimation* GetRunningAnimation( |
85 }; | 107 LayerAnimationElement::AnimatableProperty property); |
108 void AddToQueueIfNotPresent(LayerAnimationSequence* sequence); | |
109 void ProcessQueue(); | |
110 bool StartSequence(LayerAnimationSequence* sequence); | |
86 | 111 |
87 // Parameters used when animating the transform. | 112 // This is the queue of animations to run |
88 struct TransformParams { | 113 AnimationQueue animation_queue_; |
89 SkMScalar start[16]; | |
90 SkMScalar target[16]; | |
91 }; | |
92 | 114 |
93 // Parameters used when animating the opacity. | 115 // The target of all layer animations |
94 struct OpacityParams { | |
95 float start; | |
96 float target; | |
97 }; | |
98 | |
99 union Params { | |
100 LocationParams location; | |
101 OpacityParams opacity; | |
102 TransformParams transform; | |
103 }; | |
104 | |
105 typedef std::map<AnimationProperty, Params> Elements; | |
106 | |
107 // Stops animating the specified property. This does not set the property | |
108 // being animated to its final value. | |
109 void StopAnimating(AnimationProperty property); | |
110 | |
111 LayerAnimatorDelegate* delegate(); | |
112 | |
113 // The layer. | |
114 Layer* layer_; | 116 Layer* layer_; |
115 | 117 |
116 // Properties being animated. | 118 // The currently running animations |
117 Elements elements_; | 119 RunningAnimations running_animations_; |
118 | 120 |
119 scoped_ptr<ui::Animation> animation_; | 121 // Determines how animations are replaced. |
122 LayerAnimationPreemptionStrategy* preemption_strategy_; | |
120 | 123 |
121 bool got_initial_tick_; | 124 // The default length of animations. |
125 base::TimeDelta transition_duration_; | |
126 | |
127 // Drives the animations | |
128 scoped_ptr<Animation> animation_; | |
122 | 129 |
123 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); | 130 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); |
124 }; | 131 }; |
125 | 132 |
126 } // namespace ui | 133 } // namespace ui |
127 | 134 |
128 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 135 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
OLD | NEW |