SVGAnimatedNumberPair.h (4430B)
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/. */ 6 7 #ifndef DOM_SVG_SVGANIMATEDNUMBERPAIR_H_ 8 #define DOM_SVG_SVGANIMATEDNUMBERPAIR_H_ 9 10 #include "DOMSVGAnimatedNumber.h" 11 #include "mozilla/SMILAttr.h" 12 #include "mozilla/UniquePtr.h" 13 #include "nsCycleCollectionParticipant.h" 14 #include "nsError.h" 15 #include "nsMathUtils.h" 16 17 namespace mozilla { 18 19 class SMILValue; 20 21 namespace dom { 22 class SVGAnimationElement; 23 class SVGElement; 24 } // namespace dom 25 26 class SVGAnimatedNumberPair { 27 public: 28 friend class AutoChangeNumberPairNotifier; 29 using SVGElement = dom::SVGElement; 30 31 enum PairIndex { eFirst, eSecond }; 32 33 void Init(uint8_t aAttrEnum = 0xff, float aValue1 = 0, float aValue2 = 0) { 34 mAnimVal[0] = mBaseVal[0] = aValue1; 35 mAnimVal[1] = mBaseVal[1] = aValue2; 36 mAttrEnum = aAttrEnum; 37 mIsAnimated = false; 38 mIsBaseSet = false; 39 } 40 41 nsresult SetBaseValueString(const nsAString& aValue, SVGElement* aSVGElement); 42 void GetBaseValueString(nsAString& aValue) const; 43 44 void SetBaseValue(float aValue, PairIndex aPairIndex, 45 SVGElement* aSVGElement); 46 void SetBaseValues(float aValue1, float aValue2, SVGElement* aSVGElement); 47 float GetBaseValue(PairIndex aIndex) const { 48 return mBaseVal[aIndex == eFirst ? 0 : 1]; 49 } 50 void SetAnimValue(const float aValue[2], SVGElement* aSVGElement); 51 float GetAnimValue(PairIndex aIndex) const { 52 return mAnimVal[aIndex == eFirst ? 0 : 1]; 53 } 54 55 // Returns true if the animated value of this number has been explicitly 56 // set (either by animation, or by taking on the base value which has been 57 // explicitly set by markup or a DOM call), false otherwise. 58 // If this returns false, the animated value is still valid, that is, 59 // usable, and represents the default base value of the attribute. 60 bool IsExplicitlySet() const { return mIsAnimated || mIsBaseSet; } 61 62 already_AddRefed<dom::DOMSVGAnimatedNumber> ToDOMAnimatedNumber( 63 PairIndex aIndex, SVGElement* aSVGElement); 64 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement); 65 66 private: 67 float mAnimVal[2]; 68 float mBaseVal[2]; 69 uint8_t mAttrEnum; // element specified tracking for attribute 70 bool mIsAnimated; 71 bool mIsBaseSet; 72 73 public: 74 // DOM wrapper class for the (DOM)SVGAnimatedNumber interface where the 75 // wrapped class is SVGAnimatedNumberPair. 76 struct DOMAnimatedNumber final : public dom::DOMSVGAnimatedNumber { 77 DOMAnimatedNumber(SVGAnimatedNumberPair* aVal, PairIndex aIndex, 78 SVGElement* aSVGElement) 79 : dom::DOMSVGAnimatedNumber(aSVGElement), mVal(aVal), mIndex(aIndex) {} 80 virtual ~DOMAnimatedNumber(); 81 82 SVGAnimatedNumberPair* mVal; // kept alive because it belongs to content 83 PairIndex mIndex; // are we the first or second number 84 85 float BaseVal() override { return mVal->GetBaseValue(mIndex); } 86 void SetBaseVal(float aValue) override { 87 MOZ_ASSERT(std::isfinite(aValue)); 88 mVal->SetBaseValue(aValue, mIndex, mSVGElement); 89 } 90 91 // Script may have modified animation parameters or timeline -- DOM getters 92 // need to flush any resample requests to reflect these modifications. 93 float AnimVal() override { 94 mSVGElement->FlushAnimations(); 95 return mVal->GetAnimValue(mIndex); 96 } 97 }; 98 99 struct SMILNumberPair : public SMILAttr { 100 public: 101 SMILNumberPair(SVGAnimatedNumberPair* aVal, SVGElement* aSVGElement) 102 : mVal(aVal), mSVGElement(aSVGElement) {} 103 104 // These will stay alive because a SMILAttr only lives as long 105 // as the Compositing step, and DOM elements don't get a chance to 106 // die during that. 107 SVGAnimatedNumberPair* mVal; 108 SVGElement* mSVGElement; 109 110 // SMILAttr methods 111 nsresult ValueFromString(const nsAString& aStr, 112 const dom::SVGAnimationElement* aSrcElement, 113 SMILValue& aValue, 114 bool& aPreventCachingOfSandwich) const override; 115 SMILValue GetBaseValue() const override; 116 void ClearAnimValue() override; 117 nsresult SetAnimValue(const SMILValue& aValue) override; 118 }; 119 }; 120 121 } // namespace mozilla 122 123 #endif // DOM_SVG_SVGANIMATEDNUMBERPAIR_H_