SVGAnimatedPrimitiveProperty.h [plain text]
#pragma once
#include "SVGAnimatedProperty.h"
#include "SVGSharedPrimitiveProperty.h"
namespace WebCore {
template<typename PropertyType>
class SVGAnimatedPrimitiveProperty : public SVGAnimatedProperty {
public:
using ValueType = PropertyType;
static Ref<SVGAnimatedPrimitiveProperty> create(SVGElement* contextElement)
{
return adoptRef(*new SVGAnimatedPrimitiveProperty(contextElement));
}
static Ref<SVGAnimatedPrimitiveProperty> create(SVGElement* contextElement, const PropertyType& value)
{
return adoptRef(*new SVGAnimatedPrimitiveProperty(contextElement, value));
}
ExceptionOr<void> setBaseVal(const PropertyType& baseVal)
{
m_baseVal->setValue(baseVal);
commitPropertyChange(nullptr);
return { };
}
void setBaseValInternal(const PropertyType& baseVal) { m_baseVal->setValue(baseVal); }
const PropertyType& baseVal() const { return m_baseVal->value(); }
void setAnimVal(const PropertyType& animVal)
{
ASSERT(isAnimating() && m_animVal);
m_animVal->setValue(animVal);
}
const PropertyType& animVal() const
{
ASSERT_IMPLIES(isAnimating(), m_animVal);
return isAnimating() ? m_animVal->value() : m_baseVal->value();
}
PropertyType& animVal()
{
ASSERT_IMPLIES(isAnimating(), m_animVal);
return isAnimating() ? m_animVal->value() : m_baseVal->value();
}
String baseValAsString() const override { return m_baseVal->valueAsString(); }
String animValAsString() const override
{
ASSERT(isAnimating() && m_animVal);
return m_animVal->valueAsString();
}
void setDirty() override { m_baseVal->setDirty(); }
bool isDirty() const override { return m_baseVal->isDirty(); }
Optional<String> synchronize() override { return m_baseVal->synchronize(); }
const PropertyType& currentValue() const
{
ASSERT_IMPLIES(isAnimating(), m_animVal);
return isAnimating() ? m_animVal->value() : m_baseVal->value();
}
void startAnimation(SVGAttributeAnimator& animator) override
{
if (m_animVal)
m_animVal->setValue(m_baseVal->value());
else
ensureAnimVal();
SVGAnimatedProperty::startAnimation(animator);
}
void stopAnimation(SVGAttributeAnimator& animator) override
{
SVGAnimatedProperty::stopAnimation(animator);
if (!isAnimating())
m_animVal = nullptr;
else if (m_animVal)
m_animVal->setValue(m_baseVal->value());
}
void instanceStartAnimation(SVGAttributeAnimator& animator, SVGAnimatedProperty& animated) override
{
if (isAnimating())
return;
m_animVal = static_cast<SVGAnimatedPrimitiveProperty&>(animated).m_animVal;
SVGAnimatedProperty::instanceStartAnimation(animator, animated);
}
void instanceStopAnimation(SVGAttributeAnimator& animator) override
{
if (!isAnimating())
return;
m_animVal = nullptr;
SVGAnimatedProperty::instanceStopAnimation(animator);
}
protected:
SVGAnimatedPrimitiveProperty(SVGElement* contextElement)
: SVGAnimatedProperty(contextElement)
, m_baseVal(SVGSharedPrimitiveProperty<PropertyType>::create())
{
}
SVGAnimatedPrimitiveProperty(SVGElement* contextElement, const PropertyType& value)
: SVGAnimatedProperty(contextElement)
, m_baseVal(SVGSharedPrimitiveProperty<PropertyType>::create(value))
{
}
RefPtr<SVGSharedPrimitiveProperty<PropertyType>>& ensureAnimVal()
{
if (!m_animVal)
m_animVal = SVGSharedPrimitiveProperty<PropertyType>::create(m_baseVal->value());
return m_animVal;
}
Ref<SVGSharedPrimitiveProperty<PropertyType>> m_baseVal;
mutable RefPtr<SVGSharedPrimitiveProperty<PropertyType>> m_animVal;
};
}