Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2000 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 2000 Antti Koivisto (koivisto (at) kde.org)
      4  *           (C) 2000 Dirk Mueller (mueller (at) kde.org)
      5  * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
      6  * Copyright (C) 2006 Graham Dennis (graham.dennis (at) gmail.com)
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Library General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Library General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Library General Public License
     19  * along with this library; see the file COPYING.LIB.  If not, write to
     20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     21  * Boston, MA 02110-1301, USA.
     22  *
     23  */
     24 
     25 #ifndef Animation_h
     26 #define Animation_h
     27 
     28 #include "PlatformString.h"
     29 #include "RenderStyleConstants.h"
     30 #include "TimingFunction.h"
     31 #include <wtf/PassRefPtr.h>
     32 #include <wtf/RefCounted.h>
     33 
     34 namespace WebCore {
     35 
     36 const int cAnimateNone = 0;
     37 const int cAnimateAll = -2;
     38 
     39 class Animation : public RefCounted<Animation> {
     40 public:
     41     ~Animation();
     42 
     43     static PassRefPtr<Animation> create() { return adoptRef(new Animation); }
     44     static PassRefPtr<Animation> create(const Animation* o) { return adoptRef(new Animation(*o)); }
     45 
     46     bool isDelaySet() const { return m_delaySet; }
     47     bool isDirectionSet() const { return m_directionSet; }
     48     bool isDurationSet() const { return m_durationSet; }
     49     bool isFillModeSet() const { return m_fillModeSet; }
     50     bool isIterationCountSet() const { return m_iterationCountSet; }
     51     bool isNameSet() const { return m_nameSet; }
     52     bool isPlayStateSet() const { return m_playStateSet; }
     53     bool isPropertySet() const { return m_propertySet; }
     54     bool isTimingFunctionSet() const { return m_timingFunctionSet; }
     55 
     56     // Flags this to be the special "none" animation (animation-name: none)
     57     bool isNoneAnimation() const { return m_isNone; }
     58     // We can make placeholder Animation objects to keep the comma-separated lists
     59     // of properties in sync. isValidAnimation means this is not a placeholder.
     60     bool isValidAnimation() const { return !m_isNone && !m_name.isEmpty(); }
     61 
     62     bool isEmpty() const
     63     {
     64         return (!m_directionSet && !m_durationSet && !m_fillModeSet
     65                 && !m_nameSet && !m_playStateSet && !m_iterationCountSet
     66                 && !m_delaySet && !m_timingFunctionSet && !m_propertySet);
     67     }
     68 
     69     bool isEmptyOrZeroDuration() const
     70     {
     71         return isEmpty() || (m_duration == 0 && m_delay <= 0);
     72     }
     73 
     74     void clearDelay() { m_delaySet = false; }
     75     void clearDirection() { m_directionSet = false; }
     76     void clearDuration() { m_durationSet = false; }
     77     void clearFillMode() { m_fillModeSet = false; }
     78     void clearIterationCount() { m_iterationCountSet = false; }
     79     void clearName() { m_nameSet = false; }
     80     void clearPlayState() { m_playStateSet = AnimPlayStatePlaying; }
     81     void clearProperty() { m_propertySet = false; }
     82     void clearTimingFunction() { m_timingFunctionSet = false; }
     83 
     84     double delay() const { return m_delay; }
     85 
     86     enum AnimationDirection { AnimationDirectionNormal, AnimationDirectionAlternate };
     87     AnimationDirection direction() const { return static_cast<AnimationDirection>(m_direction); }
     88 
     89     unsigned fillMode() const { return m_fillMode; }
     90 
     91     double duration() const { return m_duration; }
     92 
     93     enum { IterationCountInfinite = -1 };
     94     int iterationCount() const { return m_iterationCount; }
     95     const String& name() const { return m_name; }
     96     EAnimPlayState playState() const { return static_cast<EAnimPlayState>(m_playState); }
     97     int property() const { return m_property; }
     98     const PassRefPtr<TimingFunction> timingFunction() const { return m_timingFunction; }
     99 
    100     void setDelay(double c) { m_delay = c; m_delaySet = true; }
    101     void setDirection(AnimationDirection d) { m_direction = d; m_directionSet = true; }
    102     void setDuration(double d) { ASSERT(d >= 0); m_duration = d; m_durationSet = true; }
    103     void setFillMode(unsigned f) { m_fillMode = f; m_fillModeSet = true; }
    104     void setIterationCount(int c) { m_iterationCount = c; m_iterationCountSet = true; }
    105     void setName(const String& n) { m_name = n; m_nameSet = true; }
    106     void setPlayState(EAnimPlayState d) { m_playState = d; m_playStateSet = true; }
    107     void setProperty(int t) { m_property = t; m_propertySet = true; }
    108     void setTimingFunction(PassRefPtr<TimingFunction> f) { m_timingFunction = f; m_timingFunctionSet = true; }
    109 
    110     void setIsNoneAnimation(bool n) { m_isNone = n; }
    111 
    112     Animation& operator=(const Animation& o);
    113 
    114     // return true if all members of this class match (excluding m_next)
    115     bool animationsMatch(const Animation*, bool matchPlayStates = true) const;
    116 
    117     // return true every Animation in the chain (defined by m_next) match
    118     bool operator==(const Animation& o) const { return animationsMatch(&o); }
    119     bool operator!=(const Animation& o) const { return !(*this == o); }
    120 
    121     bool fillsBackwards() const { return m_fillModeSet && (m_fillMode == AnimationFillModeBackwards || m_fillMode == AnimationFillModeBoth); }
    122     bool fillsForwards() const { return m_fillModeSet && (m_fillMode == AnimationFillModeForwards || m_fillMode == AnimationFillModeBoth); }
    123 
    124 private:
    125     Animation();
    126     Animation(const Animation& o);
    127 
    128     String m_name;
    129     int m_property;
    130     int m_iterationCount;
    131     double m_delay;
    132     double m_duration;
    133     RefPtr<TimingFunction> m_timingFunction;
    134     unsigned m_direction : 1; // AnimationDirection
    135     unsigned m_fillMode : 2;
    136 
    137     unsigned m_playState     : 2;
    138 
    139     bool m_delaySet          : 1;
    140     bool m_directionSet      : 1;
    141     bool m_durationSet       : 1;
    142     bool m_fillModeSet       : 1;
    143     bool m_iterationCountSet : 1;
    144     bool m_nameSet           : 1;
    145     bool m_playStateSet      : 1;
    146     bool m_propertySet       : 1;
    147     bool m_timingFunctionSet : 1;
    148 
    149     bool m_isNone            : 1;
    150 
    151 public:
    152     static float initialAnimationDelay() { return 0; }
    153     static AnimationDirection initialAnimationDirection() { return AnimationDirectionNormal; }
    154     static double initialAnimationDuration() { return 0; }
    155     static unsigned initialAnimationFillMode() { return AnimationFillModeNone; }
    156     static int initialAnimationIterationCount() { return 1; }
    157     static String initialAnimationName() { return String("none"); }
    158     static EAnimPlayState initialAnimationPlayState() { return AnimPlayStatePlaying; }
    159     static int initialAnimationProperty() { return cAnimateAll; }
    160     static PassRefPtr<TimingFunction> initialAnimationTimingFunction() { return CubicBezierTimingFunction::create(); }
    161 };
    162 
    163 } // namespace WebCore
    164 
    165 #endif // Animation_h
    166