blob: ef5d2b27f84bf779fa02023db8a283fc62e37785 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
2 * Copyright (C) 2004, 2005 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2008 Cameron McCormack <cam@mcc.id.au>
7 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#ifndef SVGAnimationElement_h
26#define SVGAnimationElement_h
27
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010028#include "core/svg/SVGAnimatedBoolean.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010029#include "core/svg/SVGTests.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010030#include "core/svg/animation/SVGSMILElement.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010031#include "platform/animation/UnitBezier.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000032#include "wtf/Functional.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010033
34namespace WebCore {
35
36enum AnimationMode {
37 NoAnimation,
38 FromToAnimation,
39 FromByAnimation,
40 ToAnimation,
41 ByAnimation,
42 ValuesAnimation,
43 PathAnimation // Used by AnimateMotion.
44};
45
Torne (Richard Coles)09380292014-02-21 12:17:33 +000046// If we have 'inherit' as animation value, we need to grab the value
47// during the animation since the value can be animated itself.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010048enum AnimatedPropertyValueType {
49 RegularPropertyValue,
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010050 InheritValue
51};
52
53enum CalcMode {
54 CalcModeDiscrete,
55 CalcModeLinear,
56 CalcModePaced,
57 CalcModeSpline
58};
59
60class ConditionEventListener;
61class TimeContainer;
62class SVGAnimatedType;
63
64class SVGAnimationElement : public SVGSMILElement,
Torne (Richard Coles)09380292014-02-21 12:17:33 +000065 public SVGTests {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010066public:
67 // SVGAnimationElement
68 float getStartTime() const;
69 float getCurrentTime() const;
Torne (Richard Coles)93ac45c2013-05-29 14:40:20 +010070 float getSimpleDuration() const;
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010071
Ben Murdoch591b9582013-07-10 11:41:44 +010072 void beginElement();
73 void beginElementAt(float offset);
74 void endElement();
75 void endElementAt(float offset);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010076
Ben Murdoch07a852d2014-03-31 11:51:52 +010077 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(begin, beginEvent);
78 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(end, endEvent);
79 DEFINE_MAPPED_ATTRIBUTE_EVENT_LISTENER(repeat, repeatEvent);
80
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010081 static bool isTargetAttributeCSSProperty(SVGElement*, const QualifiedName&);
82
83 virtual bool isAdditive() const;
84 bool isAccumulated() const;
85 AnimationMode animationMode() const { return m_animationMode; }
86 CalcMode calcMode() const { return m_calcMode; }
87
88 enum ShouldApplyAnimation {
89 DontApplyAnimation,
90 ApplyCSSAnimation,
91 ApplyXMLAnimation
92 };
93
94 ShouldApplyAnimation shouldApplyAnimation(SVGElement* targetElement, const QualifiedName& attributeName);
95
96 AnimatedPropertyValueType fromPropertyValueType() const { return m_fromPropertyValueType; }
97 AnimatedPropertyValueType toPropertyValueType() const { return m_toPropertyValueType; }
98
Torne (Richard Coles)09380292014-02-21 12:17:33 +000099 template<typename AnimatedType, typename ParseTypeFromStringType>
100 void adjustForInheritance(ParseTypeFromStringType parseTypeFromString, AnimatedPropertyValueType valueType, AnimatedType& animatedType, SVGElement* contextElement)
101 {
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100102 if (valueType != InheritValue)
103 return;
104 // Replace 'inherit' by its computed property value.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100105 String typeString;
106 adjustForInheritance(contextElement, attributeName(), typeString);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000107 animatedType = parseTypeFromString(this, typeString);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100108 }
109
110 template<typename AnimatedType>
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100111 void animateDiscreteType(float percentage, const AnimatedType& fromType, const AnimatedType& toType, AnimatedType& animatedType)
112 {
113 if ((animationMode() == FromToAnimation && percentage > 0.5) || animationMode() == ToAnimation || percentage == 1) {
114 animatedType = AnimatedType(toType);
115 return;
116 }
117 animatedType = AnimatedType(fromType);
118 }
119
120 void animateAdditiveNumber(float percentage, unsigned repeatCount, float fromNumber, float toNumber, float toAtEndOfDurationNumber, float& animatedNumber)
121 {
122 float number;
123 if (calcMode() == CalcModeDiscrete)
124 number = percentage < 0.5 ? fromNumber : toNumber;
125 else
126 number = (toNumber - fromNumber) * percentage + fromNumber;
127
128 if (isAccumulated() && repeatCount)
129 number += toAtEndOfDurationNumber * repeatCount;
130
131 if (isAdditive() && animationMode() != ToAnimation)
132 animatedNumber += number;
133 else
134 animatedNumber = number;
135 }
136
137protected:
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100138 SVGAnimationElement(const QualifiedName&, Document&);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100139
140 void computeCSSPropertyValue(SVGElement*, CSSPropertyID, String& value);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000141 void determinePropertyValueTypes(const String& from, const String& to);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100142
143 bool isSupportedAttribute(const QualifiedName&);
144 virtual void parseAttribute(const QualifiedName&, const AtomicString&) OVERRIDE;
145 virtual void svgAttributeChanged(const QualifiedName&) OVERRIDE;
146
147 enum AttributeType {
148 AttributeTypeCSS,
149 AttributeTypeXML,
150 AttributeTypeAuto
151 };
152 AttributeType attributeType() const { return m_attributeType; }
153
154 String toValue() const;
155 String byValue() const;
156 String fromValue() const;
157
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100158 // from SVGSMILElement
159 virtual void startedActiveInterval() OVERRIDE;
160 virtual void updateAnimation(float percent, unsigned repeat, SVGSMILElement* resultElement) OVERRIDE;
161
162 AnimatedPropertyValueType m_fromPropertyValueType;
163 AnimatedPropertyValueType m_toPropertyValueType;
164
165 virtual void setTargetElement(SVGElement*) OVERRIDE;
166 virtual void setAttributeName(const QualifiedName&) OVERRIDE;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000167 AnimatedPropertyType determineAnimatedPropertyType() const;
168
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100169 bool hasInvalidCSSAttributeType() const { return m_hasInvalidCSSAttributeType; }
170
171 virtual void updateAnimationMode();
172 void setAnimationMode(AnimationMode animationMode) { m_animationMode = animationMode; }
173 void setCalcMode(CalcMode calcMode) { m_calcMode = calcMode; }
174
175private:
176 virtual void animationAttributeChanged() OVERRIDE;
177 void setAttributeType(const AtomicString&);
178
179 void checkInvalidCSSAttributeType(SVGElement*);
180
181 virtual bool calculateToAtEndOfDurationValue(const String& toAtEndOfDurationString) = 0;
182 virtual bool calculateFromAndToValues(const String& fromString, const String& toString) = 0;
183 virtual bool calculateFromAndByValues(const String& fromString, const String& byString) = 0;
184 virtual void calculateAnimatedValue(float percent, unsigned repeatCount, SVGSMILElement* resultElement) = 0;
185 virtual float calculateDistance(const String& /*fromString*/, const String& /*toString*/) { return -1.f; }
186
187 void currentValuesForValuesAnimation(float percent, float& effectivePercent, String& from, String& to);
188 void calculateKeyTimesForCalcModePaced();
189 float calculatePercentFromKeyPoints(float percent) const;
190 void currentValuesFromKeyPoints(float percent, float& effectivePercent, String& from, String& to) const;
191 float calculatePercentForSpline(float percent, unsigned splineIndex) const;
192 float calculatePercentForFromTo(float percent) const;
193 unsigned calculateKeyTimesIndex(float percent) const;
194
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100195 void adjustForInheritance(SVGElement* targetElement, const QualifiedName& attributeName, String&);
196
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100197 void setCalcMode(const AtomicString&);
198
199 bool m_animationValid;
200
201 AttributeType m_attributeType;
202 Vector<String> m_values;
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100203 // FIXME: We should probably use doubles for this, but there's no point
204 // making such a change unless all SVG logic for sampling animations is
205 // changed to use doubles.
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100206 Vector<float> m_keyTimes;
207 Vector<float> m_keyPoints;
208 Vector<UnitBezier> m_keySplines;
209 String m_lastValuesAnimationFrom;
210 String m_lastValuesAnimationTo;
211 bool m_hasInvalidCSSAttributeType;
212 CalcMode m_calcMode;
213 AnimationMode m_animationMode;
214};
215
216} // namespace WebCore
217
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100218#endif // SVGAnimationElement_h