initial import
[vuplus_webkit] / Source / WebCore / platform / animation / TimingFunction.h
1 /*
2  * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3  *           (C) 2000 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6  * Copyright (C) 2006 Graham Dennis (graham.dennis@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 TimingFunction_h
26 #define TimingFunction_h
27
28 #include <wtf/RefCounted.h>
29
30 namespace WebCore {
31
32 class TimingFunction : public RefCounted<TimingFunction> {
33 public:
34
35     enum TimingFunctionType {
36         LinearFunction, CubicBezierFunction, StepsFunction
37     };
38     
39     virtual ~TimingFunction() { }
40
41     TimingFunctionType type() const { return m_type; }
42     
43     bool isLinearTimingFunction() const { return m_type == LinearFunction; }
44     bool isCubicBezierTimingFunction() const { return m_type == CubicBezierFunction; }
45     bool isStepsTimingFunction() const { return m_type == StepsFunction; }
46     
47     virtual bool operator==(const TimingFunction& other) = 0;
48
49 protected:
50     TimingFunction(TimingFunctionType type)
51         : m_type(type)
52     {
53     }
54     
55     TimingFunctionType m_type;
56 };
57
58 class LinearTimingFunction : public TimingFunction {
59 public:
60     static PassRefPtr<LinearTimingFunction> create()
61     {
62         return adoptRef(new LinearTimingFunction);
63     }
64     
65     ~LinearTimingFunction() { }
66     
67     virtual bool operator==(const TimingFunction& other)
68     {
69         return other.isLinearTimingFunction();
70     }
71     
72 private:
73     LinearTimingFunction()
74         : TimingFunction(LinearFunction)
75     {
76     }
77 };
78     
79 class CubicBezierTimingFunction : public TimingFunction {
80 public:
81     static PassRefPtr<CubicBezierTimingFunction> create(double x1, double y1, double x2, double y2)
82     {
83         return adoptRef(new CubicBezierTimingFunction(x1, y1, x2, y2));
84     }
85
86     static PassRefPtr<CubicBezierTimingFunction> create()
87     {
88         return adoptRef(new CubicBezierTimingFunction());
89     }
90
91     ~CubicBezierTimingFunction() { }
92     
93     virtual bool operator==(const TimingFunction& other)
94     {
95         if (other.isCubicBezierTimingFunction()) {
96             const CubicBezierTimingFunction* ctf = static_cast<const CubicBezierTimingFunction*>(&other);
97             return m_x1 == ctf->m_x1 && m_y1 == ctf->m_y1 && m_x2 == ctf->m_x2 && m_y2 == ctf->m_y2;
98         }
99         return false;
100     }
101
102     double x1() const { return m_x1; }
103     double y1() const { return m_y1; }
104     double x2() const { return m_x2; }
105     double y2() const { return m_y2; }
106     
107     static const CubicBezierTimingFunction* defaultTimingFunction()
108     {
109         static const CubicBezierTimingFunction* dtf = create().leakRef();
110         return dtf;
111     }
112     
113 private:
114     CubicBezierTimingFunction(double x1 = 0.25, double y1 = 0.1, double x2 = 0.25, double y2 = 1.0)
115         : TimingFunction(CubicBezierFunction)
116         , m_x1(x1)
117         , m_y1(y1)
118         , m_x2(x2)
119         , m_y2(y2)
120     {
121     }
122
123     double m_x1;
124     double m_y1;
125     double m_x2;
126     double m_y2;
127 };
128
129 class StepsTimingFunction : public TimingFunction {
130 public:
131     static PassRefPtr<StepsTimingFunction> create(int steps, bool stepAtStart)
132     {
133         return adoptRef(new StepsTimingFunction(steps, stepAtStart));
134     }
135     
136     ~StepsTimingFunction() { }
137     
138     virtual bool operator==(const TimingFunction& other)
139     {
140         if (other.isStepsTimingFunction()) {
141             const StepsTimingFunction* stf = static_cast<const StepsTimingFunction*>(&other);
142             return m_steps == stf->m_steps && m_stepAtStart == stf->m_stepAtStart;
143         }
144         return false;
145     }
146     
147     int numberOfSteps() const { return m_steps; }
148     bool stepAtStart() const { return m_stepAtStart; }
149     
150 private:
151     StepsTimingFunction(int steps, bool stepAtStart)
152         : TimingFunction(StepsFunction)
153         , m_steps(steps)
154         , m_stepAtStart(stepAtStart)
155     {
156     }
157     
158     int m_steps;
159     bool m_stepAtStart;
160 };
161     
162 } // namespace WebCore
163
164 #endif // TimingFunction_h