Upstream version 5.34.104.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/animation/AnimationEffect.h"
35 #include "core/css/CSSValue.h"
36 #include "wtf/RefCounted.h"
37
38 namespace WebCore {
39
40 class AnimatableValue : public AnimationEffect::CompositableValue {
41 public:
42     virtual ~AnimatableValue() { }
43
44     static const AnimatableValue* neutralValue();
45
46     static PassRefPtr<AnimatableValue> interpolate(const AnimatableValue*, const AnimatableValue*, double fraction);
47     // For noncommutative values read add(A, B) to mean the value A with B composed onto it.
48     static PassRefPtr<AnimatableValue> add(const AnimatableValue*, const AnimatableValue*);
49     static bool usesDefaultInterpolation(const AnimatableValue* from, const AnimatableValue* to)
50     {
51         return !from->isSameType(to) || from->usesDefaultInterpolationWith(to);
52     }
53
54     bool equals(const AnimatableValue* value) const
55     {
56         return isSameType(value) && equalTo(value);
57     }
58     bool equals(const AnimatableValue& value) const
59     {
60         return equals(&value);
61     }
62
63     virtual bool dependsOnUnderlyingValue() const OVERRIDE FINAL { return false; }
64     virtual PassRefPtr<AnimatableValue> compositeOnto(const AnimatableValue*) const OVERRIDE FINAL { return takeConstRef(this); }
65
66     bool isClipPathOperation() const { return type() == TypeClipPathOperation; }
67     bool isColor() const { return type() == TypeColor; }
68     bool isDouble() const { return type() == TypeDouble; }
69     bool isFilterOperations() const { return type() == TypeFilterOperations; }
70     bool isImage() const { return type() == TypeImage; }
71     bool isLength() const { return type() == TypeLength; }
72     bool isLengthBox() const { return type() == TypeLengthBox; }
73     bool isLengthBoxAndBool() const { return type() == TypeLengthBoxAndBool; }
74     bool isLengthPoint() const { return type() == TypeLengthPoint; }
75     bool isLengthSize() const { return type() == TypeLengthSize; }
76     bool isNeutral() const { return type() == TypeNeutral; }
77     bool isRepeatable() const { return type() == TypeRepeatable; }
78     bool isSVGLength() const { return type() == TypeSVGLength; }
79     bool isSVGPaint() const { return type() == TypeSVGPaint; }
80     bool isShadow() const { return type() == TypeShadow; }
81     bool isShapeValue() const { return type() == TypeShapeValue; }
82     bool isStrokeDasharrayList() const { return type() == TypeStrokeDasharrayList; }
83     bool isTransform() const { return type() == TypeTransform; }
84     bool isUnknown() const { return type() == TypeUnknown; }
85     bool isVisibility() const { return type() == TypeVisibility; }
86
87     bool isSameType(const AnimatableValue* value) const
88     {
89         ASSERT(value);
90         return value->type() == type();
91     }
92
93 protected:
94     enum AnimatableType {
95         TypeClipPathOperation,
96         TypeColor,
97         TypeDouble,
98         TypeFilterOperations,
99         TypeImage,
100         TypeLength,
101         TypeLengthBox,
102         TypeLengthBoxAndBool,
103         TypeLengthPoint,
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 PassRefPtr<AnimatableValue> interpolateTo(const AnimatableValue*, double fraction) const = 0;
119     static PassRefPtr<AnimatableValue> defaultInterpolateTo(const AnimatableValue* left, const AnimatableValue* right, double fraction) { return takeConstRef((fraction < 0.5) ? left : right); }
120
121     // For noncommutative values read A->addWith(B) to mean the value A with B composed onto it.
122     virtual PassRefPtr<AnimatableValue> addWith(const AnimatableValue*) const;
123     static PassRefPtr<AnimatableValue> defaultAddWith(const AnimatableValue* left, const AnimatableValue* right) { return takeConstRef(right); }
124
125
126
127     template <class T>
128     static PassRefPtr<T> takeConstRef(const T* value) { return PassRefPtr<T>(const_cast<T*>(value)); }
129
130 private:
131     virtual AnimatableType type() const = 0;
132     // Implementations can assume that the object being compared has the same type as the object this is called on
133     virtual bool equalTo(const AnimatableValue*) const = 0;
134
135     friend class KeyframeEffectModel;
136 };
137
138 #define DEFINE_ANIMATABLE_VALUE_TYPE_CASTS(thisType, predicate) \
139     DEFINE_TYPE_CASTS(thisType, AnimatableValue, value, value->predicate, value.predicate)
140
141 } // namespace WebCore
142
143 #endif // AnimatableValue_h