1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_SVG_SVGANIMATEDNUMBERPAIR_H_
8 #define DOM_SVG_SVGANIMATEDNUMBERPAIR_H_
10 #include "DOMSVGAnimatedNumber.h"
11 #include "nsCycleCollectionParticipant.h"
13 #include "nsMathUtils.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/FloatingPoint.h"
16 #include "mozilla/SMILAttr.h"
17 #include "mozilla/UniquePtr.h"
24 class SVGAnimationElement
;
28 class SVGAnimatedNumberPair
{
30 friend class AutoChangeNumberPairNotifier
;
31 using SVGElement
= dom::SVGElement
;
33 enum PairIndex
{ eFirst
, eSecond
};
35 void Init(uint8_t aAttrEnum
= 0xff, float aValue1
= 0, float aValue2
= 0) {
36 mAnimVal
[0] = mBaseVal
[0] = aValue1
;
37 mAnimVal
[1] = mBaseVal
[1] = aValue2
;
38 mAttrEnum
= aAttrEnum
;
43 nsresult
SetBaseValueString(const nsAString
& aValue
, SVGElement
* aSVGElement
);
44 void GetBaseValueString(nsAString
& aValue
) const;
46 void SetBaseValue(float aValue
, PairIndex aPairIndex
,
47 SVGElement
* aSVGElement
);
48 void SetBaseValues(float aValue1
, float aValue2
, SVGElement
* aSVGElement
);
49 float GetBaseValue(PairIndex aIndex
) const {
50 return mBaseVal
[aIndex
== eFirst
? 0 : 1];
52 void SetAnimValue(const float aValue
[2], SVGElement
* aSVGElement
);
53 float GetAnimValue(PairIndex aIndex
) const {
54 return mAnimVal
[aIndex
== eFirst
? 0 : 1];
57 // Returns true if the animated value of this number has been explicitly
58 // set (either by animation, or by taking on the base value which has been
59 // explicitly set by markup or a DOM call), false otherwise.
60 // If this returns false, the animated value is still valid, that is,
61 // usable, and represents the default base value of the attribute.
62 bool IsExplicitlySet() const { return mIsAnimated
|| mIsBaseSet
; }
64 already_AddRefed
<dom::DOMSVGAnimatedNumber
> ToDOMAnimatedNumber(
65 PairIndex aIndex
, SVGElement
* aSVGElement
);
66 UniquePtr
<SMILAttr
> ToSMILAttr(SVGElement
* aSVGElement
);
71 uint8_t mAttrEnum
; // element specified tracking for attribute
76 // DOM wrapper class for the (DOM)SVGAnimatedNumber interface where the
77 // wrapped class is SVGAnimatedNumberPair.
78 struct DOMAnimatedNumber final
: public dom::DOMSVGAnimatedNumber
{
79 DOMAnimatedNumber(SVGAnimatedNumberPair
* aVal
, PairIndex aIndex
,
80 SVGElement
* aSVGElement
)
81 : dom::DOMSVGAnimatedNumber(aSVGElement
), mVal(aVal
), mIndex(aIndex
) {}
82 virtual ~DOMAnimatedNumber();
84 SVGAnimatedNumberPair
* mVal
; // kept alive because it belongs to content
85 PairIndex mIndex
; // are we the first or second number
87 float BaseVal() override
{ return mVal
->GetBaseValue(mIndex
); }
88 void SetBaseVal(float aValue
) override
{
89 MOZ_ASSERT(IsFinite(aValue
));
90 mVal
->SetBaseValue(aValue
, mIndex
, mSVGElement
);
93 // Script may have modified animation parameters or timeline -- DOM getters
94 // need to flush any resample requests to reflect these modifications.
95 float AnimVal() override
{
96 mSVGElement
->FlushAnimations();
97 return mVal
->GetAnimValue(mIndex
);
101 struct SMILNumberPair
: public SMILAttr
{
103 SMILNumberPair(SVGAnimatedNumberPair
* aVal
, SVGElement
* aSVGElement
)
104 : mVal(aVal
), mSVGElement(aSVGElement
) {}
106 // These will stay alive because a SMILAttr only lives as long
107 // as the Compositing step, and DOM elements don't get a chance to
109 SVGAnimatedNumberPair
* mVal
;
110 SVGElement
* mSVGElement
;
113 nsresult
ValueFromString(const nsAString
& aStr
,
114 const dom::SVGAnimationElement
* aSrcElement
,
116 bool& aPreventCachingOfSandwich
) const override
;
117 SMILValue
GetBaseValue() const override
;
118 void ClearAnimValue() override
;
119 nsresult
SetAnimValue(const SMILValue
& aValue
) override
;
123 } // namespace mozilla
125 #endif // DOM_SVG_SVGANIMATEDNUMBERPAIR_H_