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 "SVGAttributeAnimator.h"
29
30namespace WebCore {
31
32class SVGElement;
33
34template<typename AnimatedProperty, typename AnimationFunction>
35class SVGAnimatedPropertyAnimator : public SVGAttributeAnimator {
36public:
37 using AnimatorAnimatedProperty = AnimatedProperty;
38
39 template<typename... Arguments>
40 SVGAnimatedPropertyAnimator(const QualifiedName& attributeName, Ref<AnimatedProperty>& animated, Arguments&&... arguments)
41 : SVGAttributeAnimator(attributeName)
42 , m_animated(animated.copyRef())
43 , m_function(std::forward<Arguments>(arguments)...)
44 {
45 }
46
47 void appendAnimatedInstance(Ref<AnimatedProperty>& animated)
48 {
49 m_animatedInstances.append(animated.copyRef());
50 }
51
52 bool isDiscrete() const override { return m_function.isDiscrete(); }
53
54 void setFromAndToValues(SVGElement* targetElement, const String& from, const String& to) override
55 {
56 m_function.setFromAndToValues(targetElement, from, to);
57 }
58
59 void setFromAndByValues(SVGElement* targetElement, const String& from, const String& by) override
60 {
61 m_function.setFromAndByValues(targetElement, from, by);
62 }
63
64 void setToAtEndOfDurationValue(const String& toAtEndOfDuration) override
65 {
66 m_function.setToAtEndOfDurationValue(toAtEndOfDuration);
67 }
68
69 void start(SVGElement*) override
70 {
71 m_animated->startAnimation();
72 for (auto& instance : m_animatedInstances)
73 instance->instanceStartAnimation(m_animated);
74 }
75
76 void apply(SVGElement* targetElement) override
77 {
78 if (isAnimatedStylePropertyAniamtor(targetElement))
79 applyAnimatedStylePropertyChange(targetElement, m_animated->animValAsString());
80 applyAnimatedPropertyChange(targetElement);
81 }
82
83 void stop(SVGElement* targetElement) override
84 {
85 if (!m_animated->isAnimating())
86 return;
87
88 m_animated->stopAnimation();
89 for (auto& instance : m_animatedInstances)
90 instance->instanceStopAnimation();
91
92 applyAnimatedPropertyChange(targetElement);
93 if (isAnimatedStylePropertyAniamtor(targetElement))
94 removeAnimatedStyleProperty(targetElement);
95 }
96
97 Optional<float> calculateDistance(SVGElement* targetElement, const String& from, const String& to) const override
98 {
99 return m_function.calculateDistance(targetElement, from, to);
100 }
101
102protected:
103 Ref<AnimatedProperty> m_animated;
104 Vector<Ref<AnimatedProperty>> m_animatedInstances;
105 AnimationFunction m_function;
106};
107
108}
109