2 * Copyright (C) 2013 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef AnimationNode_h
32 #define AnimationNode_h
34 #include "bindings/core/v8/ScriptWrappable.h"
35 #include "core/animation/Timing.h"
36 #include "platform/heap/Handle.h"
37 #include "wtf/OwnPtr.h"
38 #include "wtf/PassOwnPtr.h"
39 #include "wtf/RefCounted.h"
43 class AnimationPlayer;
45 class AnimationNodeTiming;
47 enum TimingUpdateReason {
49 TimingUpdateForAnimationFrame
52 static inline bool isNull(double value)
54 return std::isnan(value);
57 static inline double nullValue()
59 return std::numeric_limits<double>::quiet_NaN();
62 class AnimationNode : public RefCountedWillBeGarbageCollectedFinalized<AnimationNode>, public ScriptWrappable {
63 DEFINE_WRAPPERTYPEINFO();
64 friend class AnimationPlayer; // Calls attach/detach, updateInheritedTime.
66 // Note that logic in CSSAnimations depends on the order of these values.
74 class EventDelegate : public NoBaseWillBeGarbageCollectedFinalized<EventDelegate> {
76 virtual ~EventDelegate() { }
77 virtual void onEventCondition(const AnimationNode*) = 0;
78 virtual void trace(Visitor*) { }
81 virtual ~AnimationNode() { }
83 virtual bool isAnimation() const { return false; }
85 Phase phase() const { return ensureCalculated().phase; }
86 bool isCurrent() const { return ensureCalculated().isCurrent; }
87 bool isInEffect() const { return ensureCalculated().isInEffect; }
88 bool isInPlay() const { return ensureCalculated().isInPlay; }
89 double timeToForwardsEffectChange() const { return ensureCalculated().timeToForwardsEffectChange; }
90 double timeToReverseEffectChange() const { return ensureCalculated().timeToReverseEffectChange; }
92 double currentIteration() const { return ensureCalculated().currentIteration; }
93 double iterationDuration() const;
95 // This method returns time in ms as it is unused except via the API.
96 double duration() const { return iterationDuration() * 1000; }
98 double activeDuration() const { return activeDurationInternal() * 1000; }
99 double activeDurationInternal() const;
100 double timeFraction() const { return ensureCalculated().timeFraction; }
101 double startTime() const { return m_startTime * 1000; }
102 double startTimeInternal() const { return m_startTime; }
103 double endTime() const { return endTimeInternal() * 1000; }
104 double endTimeInternal() const { return startTime() + specifiedTiming().startDelay + activeDurationInternal() + specifiedTiming().endDelay; }
106 const AnimationPlayer* player() const { return m_player; }
107 AnimationPlayer* player() { return m_player; }
108 const Timing& specifiedTiming() const { return m_timing; }
109 PassRefPtrWillBeRawPtr<AnimationNodeTiming> timing();
110 void updateSpecifiedTiming(const Timing&);
112 // This method returns time in ms as it is unused except via the API.
113 double localTime(bool& isNull) const { isNull = !m_player; return ensureCalculated().localTime * 1000; }
114 double currentIteration(bool& isNull) const { isNull = !ensureCalculated().isInEffect; return ensureCalculated().currentIteration; }
116 void setName(const String& name) { m_name = name; }
117 const String& name() const { return m_name; }
119 virtual void trace(Visitor*);
122 explicit AnimationNode(const Timing&, PassOwnPtrWillBeRawPtr<EventDelegate> = nullptr);
124 // When AnimationNode receives a new inherited time via updateInheritedTime
125 // it will (if necessary) recalculate timings and (if necessary) call
126 // updateChildrenAndEffects.
127 void updateInheritedTime(double inheritedTime, TimingUpdateReason) const;
128 void invalidate() const { m_needsUpdate = true; };
129 bool hasEvents() const { return m_eventDelegate; }
130 void clearEventDelegate() { m_eventDelegate = nullptr; }
132 virtual void attach(AnimationPlayer* player)
137 virtual void detach()
143 double repeatedDuration() const;
145 virtual void updateChildrenAndEffects() const = 0;
146 virtual double intrinsicIterationDuration() const { return 0; };
147 virtual double calculateTimeToEffectChange(bool forwards, double localTime, double timeToNextIteration) const = 0;
148 virtual void specifiedTimingChanged() { }
150 // FIXME: m_parent and m_startTime are placeholders, they depend on timing groups.
151 RawPtrWillBeMember<AnimationNode> m_parent;
152 const double m_startTime;
153 RawPtrWillBeMember<AnimationPlayer> m_player;
155 OwnPtrWillBeMember<EventDelegate> m_eventDelegate;
157 mutable struct CalculatedTiming {
159 double currentIteration;
165 double timeToForwardsEffectChange;
166 double timeToReverseEffectChange;
168 mutable bool m_needsUpdate;
169 mutable double m_lastUpdateTime;
172 const CalculatedTiming& ensureCalculated() const;