Home | History | Annotate | Download | only in animation
      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 
     49     bool equals(const AnimatableValue* value) const
     50     {
     51         return isSameType(value) && equalTo(value);
     52     }
     53     bool equals(const AnimatableValue& value) const
     54     {
     55         return equals(&value);
     56     }
     57 
     58     bool isClipPathOperation() const { return type() == TypeClipPathOperation; }
     59     bool isColor() const { return type() == TypeColor; }
     60     bool isDouble() const { return type() == TypeDouble; }
     61     bool isFilterOperations() const { return type() == TypeFilterOperations; }
     62     bool isImage() const { return type() == TypeImage; }
     63     bool isLength() const { return type() == TypeLength; }
     64     bool isLengthBox() const { return type() == TypeLengthBox; }
     65     bool isLengthBoxAndBool() const { return type() == TypeLengthBoxAndBool; }
     66     bool isLengthPoint() const { return type() == TypeLengthPoint; }
     67     bool isLengthSize() const { return type() == TypeLengthSize; }
     68     bool isNeutral() const { return type() == TypeNeutral; }
     69     bool isRepeatable() const { return type() == TypeRepeatable; }
     70     bool isSVGLength() const { return type() == TypeSVGLength; }
     71     bool isSVGPaint() const { return type() == TypeSVGPaint; }
     72     bool isShadow() const { return type() == TypeShadow; }
     73     bool isShapeValue() const { return type() == TypeShapeValue; }
     74     bool isStrokeDasharrayList() const { return type() == TypeStrokeDasharrayList; }
     75     bool isTransform() const { return type() == TypeTransform; }
     76     bool isUnknown() const { return type() == TypeUnknown; }
     77     bool isVisibility() const { return type() == TypeVisibility; }
     78 
     79     bool isSameType(const AnimatableValue* value) const
     80     {
     81         ASSERT(value);
     82         return value->type() == type();
     83     }
     84 
     85     bool usesNonDefaultInterpolationWith(const AnimatableValue* value) const
     86     {
     87         return isSameType(value) && !isUnknown();
     88     }
     89 
     90 protected:
     91     enum AnimatableType {
     92         TypeClipPathOperation,
     93         TypeColor,
     94         TypeDouble,
     95         TypeFilterOperations,
     96         TypeImage,
     97         TypeLength,
     98         TypeLengthBox,
     99         TypeLengthBoxAndBool,
    100         TypeLengthPoint,
    101         TypeLengthSize,
    102         TypeNeutral,
    103         TypeRepeatable,
    104         TypeSVGLength,
    105         TypeSVGPaint,
    106         TypeShadow,
    107         TypeShapeValue,
    108         TypeStrokeDasharrayList,
    109         TypeTransform,
    110         TypeUnknown,
    111         TypeVisibility,
    112     };
    113 
    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     template <class T>
    122     static PassRefPtr<T> takeConstRef(const T* value) { return PassRefPtr<T>(const_cast<T*>(value)); }
    123 
    124 private:
    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 
    130 #define DEFINE_ANIMATABLE_VALUE_TYPE_CASTS(thisType, predicate) \
    131     DEFINE_TYPE_CASTS(thisType, AnimatableValue, value, value->predicate, value.predicate)
    132 
    133 } // namespace WebCore
    134 
    135 #endif // AnimatableValue_h
    136