SVGAnimatedIntegerPair.h (4306B)
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_SVGANIMATEDINTEGERPAIR_H_ 8 #define DOM_SVG_SVGANIMATEDINTEGERPAIR_H_ 9 10 #include "DOMSVGAnimatedInteger.h" 11 #include "mozilla/SMILAttr.h" 12 #include "mozilla/UniquePtr.h" 13 #include "nsCycleCollectionParticipant.h" 14 #include "nsError.h" 15 16 namespace mozilla { 17 18 class SMILValue; 19 20 namespace dom { 21 class SVGAnimationElement; 22 class SVGElement; 23 } // namespace dom 24 25 class SVGAnimatedIntegerPair { 26 public: 27 friend class AutoChangeIntegerPairNotifier; 28 using SVGElement = dom::SVGElement; 29 30 enum PairIndex { eFirst, eSecond }; 31 32 void Init(uint8_t aAttrEnum = 0xff, int32_t aValue1 = 0, 33 int32_t 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(int32_t aValue, PairIndex aPairIndex, 45 SVGElement* aSVGElement); 46 void SetBaseValues(int32_t aValue1, int32_t aValue2, SVGElement* aSVGElement); 47 int32_t GetBaseValue(PairIndex aIndex) const { 48 return mBaseVal[aIndex == eFirst ? 0 : 1]; 49 } 50 void SetAnimValue(const int32_t aValue[2], SVGElement* aSVGElement); 51 int32_t GetAnimValue(PairIndex aIndex) const { 52 return mAnimVal[aIndex == eFirst ? 0 : 1]; 53 } 54 55 // Returns true if the animated value of this integer 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::DOMSVGAnimatedInteger> ToDOMAnimatedInteger( 63 PairIndex aIndex, SVGElement* aSVGElement); 64 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement); 65 66 private: 67 int32_t mAnimVal[2]; 68 int32_t mBaseVal[2]; 69 uint8_t mAttrEnum; // element specified tracking for attribute 70 bool mIsAnimated; 71 bool mIsBaseSet; 72 73 public: 74 struct DOMAnimatedInteger final : public dom::DOMSVGAnimatedInteger { 75 DOMAnimatedInteger(SVGAnimatedIntegerPair* aVal, PairIndex aIndex, 76 SVGElement* aSVGElement) 77 : dom::DOMSVGAnimatedInteger(aSVGElement), mVal(aVal), mIndex(aIndex) {} 78 virtual ~DOMAnimatedInteger(); 79 80 SVGAnimatedIntegerPair* mVal; // kept alive because it belongs to content 81 PairIndex mIndex; // are we the first or second integer 82 83 int32_t BaseVal() override { return mVal->GetBaseValue(mIndex); } 84 void SetBaseVal(int32_t aValue) override { 85 mVal->SetBaseValue(aValue, mIndex, mSVGElement); 86 } 87 88 // Script may have modified animation parameters or timeline -- DOM getters 89 // need to flush any resample requests to reflect these modifications. 90 int32_t AnimVal() override { 91 mSVGElement->FlushAnimations(); 92 return mVal->GetAnimValue(mIndex); 93 } 94 }; 95 96 struct SMILIntegerPair : public SMILAttr { 97 public: 98 SMILIntegerPair(SVGAnimatedIntegerPair* aVal, SVGElement* aSVGElement) 99 : mVal(aVal), mSVGElement(aSVGElement) {} 100 101 // These will stay alive because a SMILAttr only lives as long 102 // as the Compositing step, and DOM elements don't get a chance to 103 // die during that. 104 SVGAnimatedIntegerPair* mVal; 105 SVGElement* mSVGElement; 106 107 // SMILAttr methods 108 nsresult ValueFromString(const nsAString& aStr, 109 const dom::SVGAnimationElement* aSrcElement, 110 SMILValue& aValue, 111 bool& aPreventCachingOfSandwich) const override; 112 SMILValue GetBaseValue() const override; 113 void ClearAnimValue() override; 114 nsresult SetAnimValue(const SMILValue& aValue) override; 115 }; 116 }; 117 118 } // namespace mozilla 119 120 #endif // DOM_SVG_SVGANIMATEDINTEGERPAIR_H_