2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef AnimationBase_h
30 #define AnimationBase_h
32 #include "CSSPropertyNames.h"
33 #include "core/animation/css/CSSAnimationData.h"
34 #include "core/rendering/style/RenderStyleConstants.h"
35 #include "wtf/RefCounted.h"
40 class AnimationController;
41 class CompositeAnimation;
48 class AnimationBase : public RefCounted<AnimationBase> {
49 friend class CompositeAnimation;
50 friend class CSSPropertyAnimation;
53 AnimationBase(const CSSAnimationData* transition, RenderObject& renderer, CompositeAnimation* compAnim);
54 virtual ~AnimationBase() { }
56 RenderObject* renderer() const { return m_object; }
64 double duration() const;
66 // Animations and Transitions go through the states below. When entering the STARTED state
67 // the animation is started. This may or may not require deferred response from the animator.
68 // If so, we stay in this state until that response is received (and it returns the start time).
69 // Otherwise, we use the current time as the start time and go immediately to AnimationStateLooping
70 // or AnimationStateEnding.
72 AnimationStateNew, // animation just created, animation not running yet
73 AnimationStateStartWaitTimer, // start timer running, waiting for fire
74 AnimationStateStartWaitStyleAvailable, // waiting for style setup so we can start animations
75 AnimationStateStartWaitResponse, // animation started, waiting for response
76 AnimationStateLooping, // response received, animation running, loop timer running, waiting for fire
77 AnimationStateEnding, // received, animation running, end timer running, waiting for fire
78 AnimationStatePausedWaitTimer, // in pause mode when animation started
79 AnimationStatePausedWaitStyleAvailable, // in pause mode when waiting for style setup
80 AnimationStatePausedWaitResponse, // animation paused when in STARTING state
81 AnimationStatePausedRun, // animation paused when in LOOPING or ENDING state
82 AnimationStateDone, // end timer fired, animation finished and removed
83 AnimationStateFillingForwards // animation has ended and is retaining its final value
87 AnimationStateInputMakeNew, // reset back to new from any state
88 AnimationStateInputStartAnimation, // animation requests a start
89 AnimationStateInputRestartAnimation, // force a restart from any state
90 AnimationStateInputStartTimerFired, // start timer fired
91 AnimationStateInputStyleAvailable, // style is setup, ready to start animating
92 AnimationStateInputStartTimeSet, // m_startTime was set
93 AnimationStateInputLoopTimerFired, // loop timer fired
94 AnimationStateInputEndTimerFired, // end timer fired
95 AnimationStateInputPauseOverride, // pause an animation due to override
96 AnimationStateInputResumeOverride, // resume an overridden animation
97 AnimationStateInputPlayStateRunning, // play state paused -> running
98 AnimationStateInputPlayStatePaused, // play state running -> paused
99 AnimationStateInputEndAnimation // force an end from any state
102 // Called when animation is in AnimationStateNew to start animation
103 void updateStateMachine(AnimStateInput, double param);
105 // Animation has actually started, at passed time
106 void onAnimationStartResponse(double startTime)
108 updateStateMachine(AnimationBase::AnimationStateInputStartTimeSet, startTime);
111 // Called to change to or from paused state
112 void updatePlayState(EAnimPlayState);
113 bool playStatePlaying() const;
115 bool waitingToStart() const { return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer; }
116 bool preActive() const
118 return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer || m_animState == AnimationStateStartWaitStyleAvailable || m_animState == AnimationStateStartWaitResponse;
121 bool postActive() const { return m_animState == AnimationStateDone; }
122 bool active() const { return !postActive() && !preActive(); }
123 bool running() const { return !isNew() && !postActive(); }
124 bool paused() const { return m_pauseTime >= 0; }
125 bool isNew() const { return m_animState == AnimationStateNew; }
126 bool waitingForStartTime() const { return m_animState == AnimationStateStartWaitResponse; }
127 bool waitingForStyleAvailable() const { return m_animState == AnimationStateStartWaitStyleAvailable; }
129 virtual double timeToNextService();
131 double progress(double scale, double offset, const TimingFunction*) const;
133 virtual void animate(CompositeAnimation*, RenderObject*, const RenderStyle* /*currentStyle*/, RenderStyle* /*targetStyle*/, RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
134 virtual void getAnimatedStyle(RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
136 virtual bool shouldFireEvents() const { return false; }
138 void fireAnimationEventsIfNeeded();
140 void setAnimation(const CSSAnimationData* anim) { m_animation = const_cast<CSSAnimationData*>(anim); }
142 // Return true if this animation is overridden. This will only be the case for
143 // ImplicitAnimations and is used to determine whether or not we should force
144 // set the start time. If an animation is overridden, it will probably not get
145 // back the AnimationStateInputStartTimeSet input.
146 virtual bool overridden() const { return false; }
148 // Does this animation/transition involve the given property?
149 virtual bool affectsProperty(CSSPropertyID /*property*/) const { return false; }
151 bool isAnimatingProperty(CSSPropertyID property, bool acceleratedOnly, bool isRunningNow) const
153 if (acceleratedOnly && !m_isAccelerated)
157 return (!waitingToStart() && !postActive()) && affectsProperty(property);
159 return !postActive() && affectsProperty(property);
162 // FIXME: rename this using the "lists match" terminology.
163 bool isTransformFunctionListValid() const { return m_transformFunctionListValid; }
164 bool filterFunctionListsMatch() const { return m_filterFunctionListsMatch; }
166 // Freeze the animation; used by DumpRenderTree.
167 void freezeAtTime(double t);
169 double beginAnimationUpdateTime() const;
171 double getElapsedTime() const;
173 void styleAvailable()
175 ASSERT(waitingForStyleAvailable());
176 updateStateMachine(AnimationBase::AnimationStateInputStyleAvailable, -1);
179 const CSSAnimationData* animation() const { return m_animation.get(); }
182 virtual void overrideAnimations() { }
183 virtual void resumeOverriddenAnimations() { }
185 CompositeAnimation* compositeAnimation() { return m_compAnim; }
187 // These are called when the corresponding timer fires so subclasses can do any extra work
188 virtual void onAnimationStart(double /*elapsedTime*/) { }
189 virtual void onAnimationIteration(double /*elapsedTime*/) { }
190 virtual void onAnimationEnd(double /*elapsedTime*/) { }
192 // timeOffset is an offset from the current time when the animation should start. Negative values are OK.
193 virtual void startAnimation(double /*timeOffset*/) { }
194 // timeOffset is the time at which the animation is being paused.
195 virtual void pauseAnimation(double /*timeOffset*/) { }
196 virtual void endAnimation() { }
198 void goIntoEndingOrLoopingState();
200 bool isAccelerated() const { return m_isAccelerated; }
202 static void setNeedsStyleRecalc(Node*);
204 void getTimeToNextEvent(double& time, bool& isLooping) const;
206 double fractionalTime(double scale, double elapsedTime, double offset) const;
208 AnimState m_animState;
210 bool m_isAccelerated;
211 bool m_transformFunctionListValid;
212 bool m_filterFunctionListsMatch;
215 double m_requestedStartTime;
217 double m_totalDuration;
218 double m_nextIterationDuration;
220 RenderObject* m_object;
222 RefPtr<CSSAnimationData> m_animation;
223 CompositeAnimation* m_compAnim;
226 } // namespace WebCore
228 #endif // AnimationBase_h