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 <vector> |
10 | 10 |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
14 #include "third_party/skia/include/core/SkScalar.h" | 12 #include "base/memory/scoped_vector.h" |
15 #include "third_party/skia/include/utils/SkMatrix44.h" | 13 #include "base/time.h" |
16 #include "ui/base/animation/animation_delegate.h" | |
17 #include "ui/gfx/compositor/compositor_export.h" | 14 #include "ui/gfx/compositor/compositor_export.h" |
15 #include "ui/gfx/compositor/layer_animation_element.h" | |
18 | 16 |
19 namespace gfx { | 17 namespace gfx { |
20 class Point; | 18 class Rect; |
21 } | 19 } |
22 | 20 |
23 namespace ui { | 21 namespace ui { |
24 | |
25 class Animation; | 22 class Animation; |
26 class Layer; | 23 class Layer; |
27 class LayerAnimatorDelegate; | 24 class LayerAnimationSequence; |
28 class Transform; | 25 class Transform; |
29 | 26 |
30 // LayerAnimator manages animating various properties of a Layer. | 27 // When a property of layer needs to be changed it is set by way of |
31 class COMPOSITOR_EXPORT LayerAnimator : public ui::AnimationDelegate { | 28 // LayerAnimator. This enables LayerAnimator to animate property |
29 // changes. | |
30 class COMPOSITOR_EXPORT LayerAnimator { | |
32 public: | 31 public: |
33 // Types of properties that can be animated. | 32 // We need to keep track of the start time of every running animation. |
sky
2011/10/19 00:06:33
Make these (and typedefs private).
| |
34 enum AnimationProperty { | 33 struct RunningAnimation { |
35 LOCATION, | 34 RunningAnimation(LayerAnimationSequence* sequence, |
36 OPACITY, | 35 base::TimeTicks start_time) |
37 TRANSFORM, | 36 : sequence(sequence), |
37 start_time(start_time) { | |
38 } | |
39 LayerAnimationSequence* sequence; | |
40 base::TimeTicks start_time; | |
38 }; | 41 }; |
39 | 42 |
40 explicit LayerAnimator(Layer* layer); | 43 typedef std::vector<RunningAnimation> RunningAnimations; |
44 typedef ScopedVector<LayerAnimationSequence> AnimationQueue; | |
45 | |
46 enum PreemptionStrategy { | |
47 IMMEDIATELY_SET_NEW_TARGET, | |
48 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, | |
49 ENQUEUE_NEW_ANIMATION, | |
50 REPLACE_QUEUED_ANIMATIONS, | |
51 BLEND_WITH_CURRENT_ANIMATION | |
52 }; | |
53 | |
54 // No implicit animations when properties are set. | |
55 static LayerAnimator* CreateDefaultAnimator(); | |
56 | |
57 // Implicitly animates when properties are set. | |
58 static LayerAnimator* CreateImplicitAnimator(); | |
59 | |
60 explicit LayerAnimator(base::TimeDelta transition_duration); | |
41 virtual ~LayerAnimator(); | 61 virtual ~LayerAnimator(); |
sky
2011/10/19 00:06:33
constructors before other methods
| |
42 | 62 |
43 // Sets the animation to use. LayerAnimator takes ownership of the animation. | 63 // Sets the transform on the Layer. |
sky
2011/10/19 15:43:25
Document what these does (the 3 setters). Also, do
| |
44 void SetAnimation(Animation* animation); | 64 virtual void SetTransform(const Transform& transform); |
45 | 65 |
46 ui::Layer* layer() { return layer_; } | 66 // Sets the bounds of the layer. |
67 virtual void SetBounds(const gfx::Rect& bounds); | |
47 | 68 |
48 // Animates the layer to the specified point. The point is relative to the | 69 // Sets the opacity of the layer. |
49 // parent layer. | 70 virtual void SetOpacity(float opacity); |
50 void AnimateToPoint(const gfx::Point& target); | |
51 | 71 |
52 // Animates the transform from the current transform to |transform|. | 72 // Sets the layer animation delegate the animator is associated with. The |
53 void AnimateTransform(const Transform& transform); | 73 // animator does not own the delegate. |
74 void SetDelegate(LayerAnimationDelegate* delegate); | |
54 | 75 |
55 // Animates the opacity from the current opacity to |target_opacity|. | 76 // Sets the animation preemption strategy. This determines the behaviour if |
56 void AnimateOpacity(float target_opacity); | 77 // a property is set during an animation. This class does not own the |
sky
2011/10/19 15:43:25
Remove last sentence and document what the default
| |
78 // strategy. | |
79 void set_preemption_strategy(PreemptionStrategy strategy) { | |
80 preemption_strategy_ = strategy; | |
81 } | |
57 | 82 |
58 // Returns the target value for the specified type. If the specified property | 83 // Start an animation sequence. If an animation for the same property is in |
59 // is not animating, the current value is returned. | 84 // progress, it needs to be interrupted with the new animation. The animator |
60 gfx::Point GetTargetPoint(); | 85 // takes ownership of this animation sequence. |
61 float GetTargetOpacity(); | 86 void StartAnimation(LayerAnimationSequence* animation); |
62 ui::Transform GetTargetTransform(); | |
63 | 87 |
64 // Returns true if animating |property|. | 88 // Schedule an animation to be run when possible. The animator takes ownership |
65 bool IsAnimating(AnimationProperty property) const; | 89 // of this animation sequence. |
90 void ScheduleAnimation(LayerAnimationSequence* animation); | |
66 | 91 |
67 // Returns true if the animation is running. | 92 // Schedules the animations to be run together. Obviously will no work if |
68 bool IsRunning() const; | 93 // they animate any common properties. The animator takes ownership of the |
94 // animation sequences. | |
95 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); | |
69 | 96 |
70 // Returns true if the animation has progressed at least once since | 97 // Returns true if there is an animation in the queue (animations remain in |
71 // SetAnimation() was invoked. | 98 // the queue until they complete). |
72 bool got_initial_tick() const { return got_initial_tick_; } | 99 bool IsAnimating() const; |
73 | 100 |
74 // AnimationDelegate: | 101 // Stops animating the given property. No effect if there is no running |
75 virtual void AnimationProgressed(const Animation* animation) OVERRIDE; | 102 // animation for the given property. Skips to the final state of the |
76 virtual void AnimationEnded(const Animation* animation) OVERRIDE; | 103 // animation. |
104 void StopAnimatingProperty( | |
105 LayerAnimationElement::AnimatableProperty property); | |
106 | |
107 // Stops all animation and clears any queued animations. | |
108 void StopAnimating(); | |
109 | |
110 void Step(base::TimeTicks now); | |
sky
2011/10/19 15:43:25
This shouldn't be public.
| |
111 | |
112 // For testing purposes only. | |
113 void SetAnimationForTest(Animation* animation); | |
114 base::TimeTicks get_last_step_time_for_test() { return last_step_time_; } | |
115 | |
116 protected: | |
117 LayerAnimationDelegate* delegate() { return delegate_; } | |
77 | 118 |
78 private: | 119 private: |
79 // Parameters used when animating the location. | 120 void UpdateAnimationState(); |
sky
2011/10/19 15:43:25
Add descriptions for these methods.
| |
80 struct LocationParams { | 121 void RemoveAnimation(LayerAnimationSequence* sequence); |
81 int start_x; | 122 void FinishAnimation(LayerAnimationSequence* sequence); |
82 int start_y; | 123 void FinishAnyAnimationWithZeroDuration(); |
83 int target_x; | 124 void ClearAnimations(); |
84 int target_y; | 125 RunningAnimation* GetRunningAnimation( |
85 }; | 126 LayerAnimationElement::AnimatableProperty property); |
127 void AddToQueueIfNotPresent(LayerAnimationSequence* sequence); | |
128 void RemoveAllAnimationsWithACommonProperty(LayerAnimationSequence* sequence, | |
129 bool abort); | |
130 void ImmediatelySetNewTarget(LayerAnimationSequence* sequence); | |
131 void ImmediatelyAnimateToNewTarget(LayerAnimationSequence* sequence); | |
132 void EnqueueNewAnimation(LayerAnimationSequence* sequence); | |
133 void ReplaceQueuedAnimations(LayerAnimationSequence* sequence); | |
86 | 134 |
87 // Parameters used when animating the transform. | 135 // If there's an animation in the queue that doesn't animate the same property |
88 struct TransformParams { | 136 // as a running animation, or an animation schedule to run before it, start it |
89 SkMScalar start[16]; | 137 // up. Repeat until there are no such animations. |
90 SkMScalar target[16]; | 138 void ProcessQueue(); |
91 }; | |
92 | 139 |
93 // Parameters used when animating the opacity. | 140 bool StartSequenceImmediately(LayerAnimationSequence* sequence); |
94 struct OpacityParams { | |
95 float start; | |
96 float target; | |
97 }; | |
98 | 141 |
99 union Params { | 142 // This is the queue of animations to run |
sky
2011/10/19 15:43:25
end sentences with periods.
| |
100 LocationParams location; | 143 AnimationQueue animation_queue_; |
101 OpacityParams opacity; | |
102 TransformParams transform; | |
103 }; | |
104 | 144 |
105 typedef std::map<AnimationProperty, Params> Elements; | 145 // The target of all layer animations |
146 LayerAnimationDelegate* delegate_; | |
106 | 147 |
107 // Stops animating the specified property. This does not set the property | 148 // The currently running animations |
108 // being animated to its final value. | 149 RunningAnimations running_animations_; |
109 void StopAnimating(AnimationProperty property); | |
110 | 150 |
111 LayerAnimatorDelegate* delegate(); | 151 // Determines how animations are replaced. |
152 PreemptionStrategy preemption_strategy_; | |
112 | 153 |
113 // The layer. | 154 // The default length of animations. |
114 Layer* layer_; | 155 base::TimeDelta transition_duration_; |
115 | 156 |
116 // Properties being animated. | 157 // Used for coordinating the starting of animations. |
117 Elements elements_; | 158 base::TimeTicks last_step_time_; |
118 | 159 |
119 scoped_ptr<ui::Animation> animation_; | 160 // Drives the animations |
120 | 161 scoped_ptr<Animation> animation_; |
121 bool got_initial_tick_; | |
122 | 162 |
123 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); | 163 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); |
124 }; | 164 }; |
125 | 165 |
126 } // namespace ui | 166 } // namespace ui |
127 | 167 |
128 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 168 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
OLD | NEW |