1 | /* |
2 | * Copyright (C) 2018-2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "SVGAnimatedPropertyImpl.h" |
29 | #include "SVGAnimatedPropertyPairAnimator.h" |
30 | #include "SVGMarkerTypes.h" |
31 | |
32 | namespace WebCore { |
33 | |
34 | class SVGElement; |
35 | |
36 | class SVGAnimatedAngleOrientAnimator final : public SVGAnimatedPropertyPairAnimator<SVGAnimatedAngleAnimator, SVGAnimatedOrientTypeAnimator> { |
37 | using Base = SVGAnimatedPropertyPairAnimator<SVGAnimatedAngleAnimator, SVGAnimatedOrientTypeAnimator>; |
38 | using Base::Base; |
39 | |
40 | public: |
41 | static auto create(const QualifiedName& attributeName, Ref<SVGAnimatedAngle>& animated1, Ref<SVGAnimatedOrientType>& animated2, AnimationMode animationMode, CalcMode calcMode, bool isAccumulated, bool isAdditive) |
42 | { |
43 | return std::make_unique<SVGAnimatedAngleOrientAnimator>(attributeName, animated1, animated2, animationMode, calcMode, isAccumulated, isAdditive); |
44 | } |
45 | |
46 | private: |
47 | void setFromAndToValues(SVGElement*, const String& from, const String& to) final |
48 | { |
49 | auto pairFrom = SVGPropertyTraits<std::pair<SVGAngleValue, SVGMarkerOrientType>>::fromString(from); |
50 | auto pairTo = SVGPropertyTraits<std::pair<SVGAngleValue, SVGMarkerOrientType>>::fromString(to); |
51 | |
52 | m_animatedPropertyAnimator1->m_function.m_from = pairFrom.first; |
53 | m_animatedPropertyAnimator1->m_function.m_to = pairTo.first; |
54 | |
55 | m_animatedPropertyAnimator2->m_function.m_from = pairFrom.second; |
56 | m_animatedPropertyAnimator2->m_function.m_to = pairTo.second; |
57 | } |
58 | |
59 | void setFromAndByValues(SVGElement* targetElement, const String& from, const String& by) final |
60 | { |
61 | setFromAndToValues(targetElement, from, by); |
62 | if (m_animatedPropertyAnimator2->m_function.m_from != SVGMarkerOrientAngle || m_animatedPropertyAnimator2->m_function.m_to != SVGMarkerOrientAngle) |
63 | return; |
64 | m_animatedPropertyAnimator1->m_function.addFromAndToValues(targetElement); |
65 | } |
66 | |
67 | void animate(SVGElement* targetElement, float progress, unsigned repeatCount) final |
68 | { |
69 | if (m_animatedPropertyAnimator2->m_function.m_from != m_animatedPropertyAnimator2->m_function.m_to) { |
70 | // Discrete animation - no linear interpolation possible between values (e.g. auto to angle). |
71 | m_animatedPropertyAnimator2->animate(targetElement, progress, repeatCount); |
72 | |
73 | SVGAngleValue animatedAngle; |
74 | if (progress < 0.5f && m_animatedPropertyAnimator2->m_function.m_from == SVGMarkerOrientAngle) |
75 | animatedAngle = m_animatedPropertyAnimator1->m_function.m_from; |
76 | else if (progress >= 0.5f && m_animatedPropertyAnimator2->m_function.m_to == SVGMarkerOrientAngle) |
77 | animatedAngle = m_animatedPropertyAnimator1->m_function.m_to; |
78 | |
79 | m_animatedPropertyAnimator1->m_animated->setAnimVal(animatedAngle); |
80 | return; |
81 | } |
82 | |
83 | if (m_animatedPropertyAnimator2->m_function.m_from == SVGMarkerOrientAngle) { |
84 | // Regular from- toangle animation, with support for smooth interpolation, and additive and accumulated animation. |
85 | m_animatedPropertyAnimator2->m_animated->setAnimVal(SVGMarkerOrientAngle); |
86 | |
87 | m_animatedPropertyAnimator1->animate(targetElement, progress, repeatCount); |
88 | return; |
89 | } |
90 | |
91 | // auto, auto-start-reverse, or unknown. |
92 | m_animatedPropertyAnimator1->m_animated->animVal()->value().setValue(0); |
93 | |
94 | if (m_animatedPropertyAnimator2->m_function.m_from == SVGMarkerOrientAuto || m_animatedPropertyAnimator2->m_function.m_from == SVGMarkerOrientAutoStartReverse) |
95 | m_animatedPropertyAnimator2->m_animated->setAnimVal(m_animatedPropertyAnimator2->m_function.m_from); |
96 | else |
97 | m_animatedPropertyAnimator2->m_animated->setAnimVal(SVGMarkerOrientUnknown); |
98 | } |
99 | |
100 | void stop(SVGElement* targetElement) final |
101 | { |
102 | if (!m_animatedPropertyAnimator1->m_animated->isAnimating()) |
103 | return; |
104 | apply(targetElement); |
105 | Base::stop(targetElement); |
106 | } |
107 | }; |
108 | |
109 | class SVGAnimatedIntegerPairAnimator final : public SVGAnimatedPropertyPairAnimator<SVGAnimatedIntegerAnimator, SVGAnimatedIntegerAnimator> { |
110 | using Base = SVGAnimatedPropertyPairAnimator<SVGAnimatedIntegerAnimator, SVGAnimatedIntegerAnimator>; |
111 | using Base::Base; |
112 | |
113 | public: |
114 | static auto create(const QualifiedName& attributeName, Ref<SVGAnimatedInteger>& animated1, Ref<SVGAnimatedInteger>& animated2, AnimationMode animationMode, CalcMode calcMode, bool isAccumulated, bool isAdditive) |
115 | { |
116 | return std::make_unique<SVGAnimatedIntegerPairAnimator>(attributeName, animated1, animated2, animationMode, calcMode, isAccumulated, isAdditive); |
117 | } |
118 | |
119 | private: |
120 | void setFromAndToValues(SVGElement*, const String& from, const String& to) final |
121 | { |
122 | auto pairFrom = SVGPropertyTraits<std::pair<int, int>>::fromString(from); |
123 | auto pairTo = SVGPropertyTraits<std::pair<int, int>>::fromString(to); |
124 | |
125 | m_animatedPropertyAnimator1->m_function.m_from = pairFrom.first; |
126 | m_animatedPropertyAnimator1->m_function.m_to = pairTo.first; |
127 | |
128 | m_animatedPropertyAnimator2->m_function.m_from = pairFrom.second; |
129 | m_animatedPropertyAnimator2->m_function.m_to = pairTo.second; |
130 | } |
131 | |
132 | void setFromAndByValues(SVGElement*, const String& from, const String& by) final |
133 | { |
134 | auto pairFrom = SVGPropertyTraits<std::pair<int, int>>::fromString(from); |
135 | auto pairBy = SVGPropertyTraits<std::pair<int, int>>::fromString(by); |
136 | |
137 | m_animatedPropertyAnimator1->m_function.m_from = pairFrom.first; |
138 | m_animatedPropertyAnimator1->m_function.m_to = pairFrom.first + pairBy.first; |
139 | |
140 | m_animatedPropertyAnimator2->m_function.m_from = pairFrom.second; |
141 | m_animatedPropertyAnimator2->m_function.m_to = pairFrom.second + pairBy.second; |
142 | } |
143 | |
144 | void setToAtEndOfDurationValue(const String& toAtEndOfDuration) final |
145 | { |
146 | auto pairToAtEndOfDuration = SVGPropertyTraits<std::pair<int, int>>::fromString(toAtEndOfDuration); |
147 | m_animatedPropertyAnimator1->m_function.m_toAtEndOfDuration = pairToAtEndOfDuration.first; |
148 | m_animatedPropertyAnimator2->m_function.m_toAtEndOfDuration = pairToAtEndOfDuration.second; |
149 | } |
150 | }; |
151 | |
152 | class SVGAnimatedNumberPairAnimator final : public SVGAnimatedPropertyPairAnimator<SVGAnimatedNumberAnimator, SVGAnimatedNumberAnimator> { |
153 | using Base = SVGAnimatedPropertyPairAnimator<SVGAnimatedNumberAnimator, SVGAnimatedNumberAnimator>; |
154 | using Base::Base; |
155 | |
156 | public: |
157 | static auto create(const QualifiedName& attributeName, Ref<SVGAnimatedNumber>& animated1, Ref<SVGAnimatedNumber>& animated2, AnimationMode animationMode, CalcMode calcMode, bool isAccumulated, bool isAdditive) |
158 | { |
159 | return std::make_unique<SVGAnimatedNumberPairAnimator>(attributeName, animated1, animated2, animationMode, calcMode, isAccumulated, isAdditive); |
160 | } |
161 | |
162 | private: |
163 | void setFromAndToValues(SVGElement*, const String& from, const String& to) final |
164 | { |
165 | auto pairFrom = SVGPropertyTraits<std::pair<float, float>>::fromString(from); |
166 | auto pairTo = SVGPropertyTraits<std::pair<float, float>>::fromString(to); |
167 | |
168 | m_animatedPropertyAnimator1->m_function.m_from = pairFrom.first; |
169 | m_animatedPropertyAnimator1->m_function.m_to = pairTo.first; |
170 | |
171 | m_animatedPropertyAnimator2->m_function.m_from = pairFrom.second; |
172 | m_animatedPropertyAnimator2->m_function.m_to = pairTo.second; |
173 | } |
174 | |
175 | void setFromAndByValues(SVGElement*, const String& from, const String& by) final |
176 | { |
177 | auto pairFrom = SVGPropertyTraits<std::pair<float, float>>::fromString(from); |
178 | auto pairBy = SVGPropertyTraits<std::pair<float, float>>::fromString(by); |
179 | |
180 | m_animatedPropertyAnimator1->m_function.m_from = pairFrom.first; |
181 | m_animatedPropertyAnimator1->m_function.m_to = pairFrom.first + pairBy.first; |
182 | |
183 | m_animatedPropertyAnimator2->m_function.m_from = pairFrom.second; |
184 | m_animatedPropertyAnimator2->m_function.m_to = pairFrom.second + pairBy.second; |
185 | } |
186 | |
187 | void setToAtEndOfDurationValue(const String& toAtEndOfDuration) final |
188 | { |
189 | auto pairToAtEndOfDuration = SVGPropertyTraits<std::pair<float, float>>::fromString(toAtEndOfDuration); |
190 | m_animatedPropertyAnimator1->m_function.m_toAtEndOfDuration = pairToAtEndOfDuration.first; |
191 | m_animatedPropertyAnimator2->m_function.m_toAtEndOfDuration = pairToAtEndOfDuration.second; |
192 | } |
193 | }; |
194 | |
195 | } |
196 | |