1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef AnimatableValue_h
32#define AnimatableValue_h
33
34#include "core/css/CSSValue.h"
35#include "platform/heap/Handle.h"
36#include "wtf/RefCounted.h"
37
38namespace blink {
39
40class AnimatableValue : public RefCountedWillBeGarbageCollectedFinalized<AnimatableValue> {
41public:
42    virtual ~AnimatableValue() { }
43
44    static const AnimatableValue* neutralValue();
45
46    static PassRefPtrWillBeRawPtr<AnimatableValue> interpolate(const AnimatableValue*, const AnimatableValue*, double fraction);
47    static double distance(const AnimatableValue* from, const AnimatableValue* to);
48    static bool usesDefaultInterpolation(const AnimatableValue* from, const AnimatableValue* to)
49    {
50        return !from->isSameType(to) || from->usesDefaultInterpolationWith(to);
51    }
52
53    bool equals(const AnimatableValue* value) const
54    {
55        return isSameType(value) && equalTo(value);
56    }
57    bool equals(const AnimatableValue& value) const
58    {
59        return equals(&value);
60    }
61
62    bool isClipPathOperation() const { return type() == TypeClipPathOperation; }
63    bool isColor() const { return type() == TypeColor; }
64    bool isDouble() const { return type() == TypeDouble; }
65    bool isFilterOperations() const { return type() == TypeFilterOperations; }
66    bool isImage() const { return type() == TypeImage; }
67    bool isLength() const { return type() == TypeLength; }
68    bool isLengthBox() const { return type() == TypeLengthBox; }
69    bool isLengthBoxAndBool() const { return type() == TypeLengthBoxAndBool; }
70    bool isLengthPoint() const { return type() == TypeLengthPoint; }
71    bool isLengthPoint3D() const { return type() == TypeLengthPoint3D; }
72    bool isLengthSize() const { return type() == TypeLengthSize; }
73    bool isNeutral() const { return type() == TypeNeutral; }
74    bool isRepeatable() const { return type() == TypeRepeatable; }
75    bool isSVGLength() const { return type() == TypeSVGLength; }
76    bool isSVGPaint() const { return type() == TypeSVGPaint; }
77    bool isShadow() const { return type() == TypeShadow; }
78    bool isShapeValue() const { return type() == TypeShapeValue; }
79    bool isStrokeDasharrayList() const { return type() == TypeStrokeDasharrayList; }
80    bool isTransform() const { return type() == TypeTransform; }
81    bool isUnknown() const { return type() == TypeUnknown; }
82    bool isVisibility() const { return type() == TypeVisibility; }
83
84    bool isSameType(const AnimatableValue* value) const
85    {
86        ASSERT(value);
87        return value->type() == type();
88    }
89
90    virtual void trace(Visitor*) { }
91
92protected:
93    enum AnimatableType {
94        TypeClipPathOperation,
95        TypeColor,
96        TypeDouble,
97        TypeFilterOperations,
98        TypeImage,
99        TypeLength,
100        TypeLengthBox,
101        TypeLengthBoxAndBool,
102        TypeLengthPoint,
103        TypeLengthPoint3D,
104        TypeLengthSize,
105        TypeNeutral,
106        TypeRepeatable,
107        TypeSVGLength,
108        TypeSVGPaint,
109        TypeShadow,
110        TypeShapeValue,
111        TypeStrokeDasharrayList,
112        TypeTransform,
113        TypeUnknown,
114        TypeVisibility,
115    };
116
117    virtual bool usesDefaultInterpolationWith(const AnimatableValue* value) const { return false; }
118    virtual PassRefPtrWillBeRawPtr<AnimatableValue> interpolateTo(const AnimatableValue*, double fraction) const = 0;
119    static PassRefPtrWillBeRawPtr<AnimatableValue> defaultInterpolateTo(const AnimatableValue* left, const AnimatableValue* right, double fraction) { return takeConstRef((fraction < 0.5) ? left : right); }
120
121    template <class T>
122    static PassRefPtrWillBeRawPtr<T> takeConstRef(const T* value) { return PassRefPtrWillBeRawPtr<T>(const_cast<T*>(value)); }
123
124private:
125    virtual AnimatableType type() const = 0;
126    // Implementations can assume that the object being compared has the same type as the object this is called on
127    virtual bool equalTo(const AnimatableValue*) const = 0;
128
129    virtual double distanceTo(const AnimatableValue*) const;
130
131    template <class Keyframe> friend class KeyframeEffectModel;
132};
133
134#define DEFINE_ANIMATABLE_VALUE_TYPE_CASTS(thisType, predicate) \
135    DEFINE_TYPE_CASTS(thisType, AnimatableValue, value, value->predicate, value.predicate)
136
137} // namespace blink
138
139#endif // AnimatableValue_h
140