Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / AnimatableValue.h
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 "wtf/RefCounted.h"
36
37 namespace WebCore {
38
39 class AnimatableValue : public RefCounted<AnimatableValue> {
40 public:
41     virtual ~AnimatableValue() { }
42
43     static const AnimatableValue* neutralValue();
44
45     static PassRefPtr<AnimatableValue> interpolate(const AnimatableValue*, const AnimatableValue*, double fraction);
46     // For noncommutative values read add(A, B) to mean the value A with B composed onto it.
47     static PassRefPtr<AnimatableValue> add(const AnimatableValue*, const AnimatableValue*);
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 isLengthSize() const { return type() == TypeLengthSize; }
72     bool isNeutral() const { return type() == TypeNeutral; }
73     bool isRepeatable() const { return type() == TypeRepeatable; }
74     bool isSVGLength() const { return type() == TypeSVGLength; }
75     bool isSVGPaint() const { return type() == TypeSVGPaint; }
76     bool isShadow() const { return type() == TypeShadow; }
77     bool isShapeValue() const { return type() == TypeShapeValue; }
78     bool isStrokeDasharrayList() const { return type() == TypeStrokeDasharrayList; }
79     bool isTransform() const { return type() == TypeTransform; }
80     bool isUnknown() const { return type() == TypeUnknown; }
81     bool isVisibility() const { return type() == TypeVisibility; }
82
83     bool isSameType(const AnimatableValue* value) const
84     {
85         ASSERT(value);
86         return value->type() == type();
87     }
88
89 protected:
90     enum AnimatableType {
91         TypeClipPathOperation,
92         TypeColor,
93         TypeDouble,
94         TypeFilterOperations,
95         TypeImage,
96         TypeLength,
97         TypeLengthBox,
98         TypeLengthBoxAndBool,
99         TypeLengthPoint,
100         TypeLengthSize,
101         TypeNeutral,
102         TypeRepeatable,
103         TypeSVGLength,
104         TypeSVGPaint,
105         TypeShadow,
106         TypeShapeValue,
107         TypeStrokeDasharrayList,
108         TypeTransform,
109         TypeUnknown,
110         TypeVisibility,
111     };
112
113     virtual bool usesDefaultInterpolationWith(const AnimatableValue* value) const { return false; }
114     virtual PassRefPtr<AnimatableValue> interpolateTo(const AnimatableValue*, double fraction) const = 0;
115     static PassRefPtr<AnimatableValue> defaultInterpolateTo(const AnimatableValue* left, const AnimatableValue* right, double fraction) { return takeConstRef((fraction < 0.5) ? left : right); }
116
117     // For noncommutative values read A->addWith(B) to mean the value A with B composed onto it.
118     virtual PassRefPtr<AnimatableValue> addWith(const AnimatableValue*) const;
119     static PassRefPtr<AnimatableValue> defaultAddWith(const AnimatableValue* left, const AnimatableValue* right) { return takeConstRef(right); }
120
121
122
123     template <class T>
124     static PassRefPtr<T> takeConstRef(const T* value) { return PassRefPtr<T>(const_cast<T*>(value)); }
125
126 private:
127     virtual AnimatableType type() const = 0;
128     // Implementations can assume that the object being compared has the same type as the object this is called on
129     virtual bool equalTo(const AnimatableValue*) const = 0;
130 };
131
132 #define DEFINE_ANIMATABLE_VALUE_TYPE_CASTS(thisType, predicate) \
133     DEFINE_TYPE_CASTS(thisType, AnimatableValue, value, value->predicate, value.predicate)
134
135 } // namespace WebCore
136
137 #endif // AnimatableValue_h