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