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 "SVGAnimatedProperty.h" |
29 | #include "SVGProperty.h" |
30 | |
31 | namespace WebCore { |
32 | |
33 | template<typename PropertyType> |
34 | class SVGAnimatedPrimitiveProperty : public SVGAnimatedProperty { |
35 | public: |
36 | using ValueType = PropertyType; |
37 | |
38 | static Ref<SVGAnimatedPrimitiveProperty> create(SVGElement* contextElement) |
39 | { |
40 | return adoptRef(*new SVGAnimatedPrimitiveProperty(contextElement)); |
41 | } |
42 | |
43 | static Ref<SVGAnimatedPrimitiveProperty> create(SVGElement* contextElement, const PropertyType& value) |
44 | { |
45 | return adoptRef(*new SVGAnimatedPrimitiveProperty(contextElement, value)); |
46 | } |
47 | |
48 | // Used by the DOM. |
49 | ExceptionOr<void> setBaseVal(const PropertyType& baseVal) |
50 | { |
51 | m_baseVal = baseVal; |
52 | commitPropertyChange(nullptr); |
53 | return { }; |
54 | } |
55 | |
56 | // Used by SVGElement::parseAttribute(). |
57 | void setBaseValInternal(const PropertyType& baseVal) { m_baseVal = baseVal; } |
58 | const PropertyType& baseVal() const { return m_baseVal; } |
59 | |
60 | // Used by SVGAttributeAnimator::progress. |
61 | void setAnimVal(const PropertyType& animVal) |
62 | { |
63 | ASSERT(isAnimating()); |
64 | m_animVal = animVal; |
65 | } |
66 | |
67 | const PropertyType& animVal() const |
68 | { |
69 | ASSERT_IMPLIES(isAnimating(), m_animVal); |
70 | return isAnimating() ? *m_animVal : m_baseVal; |
71 | } |
72 | |
73 | PropertyType& animVal() |
74 | { |
75 | ASSERT_IMPLIES(isAnimating(), m_animVal); |
76 | return isAnimating() ? *m_animVal : m_baseVal; |
77 | } |
78 | |
79 | // Used when committing a change from the SVGAnimatedProperty to the attribute. |
80 | String baseValAsString() const override { return SVGPropertyTraits<PropertyType>::toString(m_baseVal); } |
81 | |
82 | // Used to apply the SVGAttributeAnimator change to the target element. |
83 | String animValAsString() const override |
84 | { |
85 | ASSERT(isAnimating() && !!m_animVal); |
86 | return SVGPropertyTraits<PropertyType>::toString(*m_animVal); |
87 | } |
88 | |
89 | // Managing the relationship with the owner. |
90 | void setDirty() override { m_state = SVGPropertyState::Dirty; } |
91 | bool isDirty() const override { return m_state == SVGPropertyState::Dirty; } |
92 | Optional<String> synchronize() override |
93 | { |
94 | if (m_state == SVGPropertyState::Clean) |
95 | return WTF::nullopt; |
96 | m_state = SVGPropertyState::Clean; |
97 | return baseValAsString(); |
98 | } |
99 | |
100 | // Used by RenderSVGElements and DumpRenderTree. |
101 | const PropertyType& currentValue() const |
102 | { |
103 | ASSERT_IMPLIES(isAnimating(), m_animVal); |
104 | return isAnimating() ? *m_animVal : m_baseVal; |
105 | } |
106 | |
107 | // Controlling the animation. |
108 | void startAnimation() override |
109 | { |
110 | if (isAnimating()) |
111 | return; |
112 | m_animVal = m_baseVal; |
113 | SVGAnimatedProperty::startAnimation(); |
114 | } |
115 | |
116 | void stopAnimation() override |
117 | { |
118 | if (!isAnimating()) |
119 | return; |
120 | m_animVal = WTF::nullopt; |
121 | SVGAnimatedProperty::stopAnimation(); |
122 | } |
123 | |
124 | protected: |
125 | SVGAnimatedPrimitiveProperty(SVGElement* contextElement) |
126 | : SVGAnimatedProperty(contextElement) |
127 | , m_baseVal(SVGPropertyTraits<PropertyType>::initialValue()) |
128 | { |
129 | } |
130 | |
131 | SVGAnimatedPrimitiveProperty(SVGElement* contextElement, const PropertyType& value) |
132 | : SVGAnimatedProperty(contextElement) |
133 | , m_baseVal(value) |
134 | { |
135 | } |
136 | |
137 | PropertyType m_baseVal; |
138 | mutable Optional<PropertyType> m_animVal; |
139 | SVGPropertyState m_state { SVGPropertyState::Clean }; |
140 | }; |
141 | |
142 | } |
143 | |