• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "platform/animation/AnimationUtilities.h" // For blend()
29 #include "platform/animation/UnitBezier.h"
30 #include "wtf/OwnPtr.h"
31 #include "wtf/PassOwnPtr.h"
32 #include "wtf/PassRefPtr.h"
33 #include "wtf/RefCounted.h"
34 #include "wtf/StdLibExtras.h"
35 #include "wtf/Vector.h"
36 #include "wtf/text/StringBuilder.h"
37 #include "wtf/text/WTFString.h"
38 #include <algorithm>
39 
40 namespace WebCore {
41 
42 class PLATFORM_EXPORT TimingFunction : public RefCounted<TimingFunction> {
43 public:
44 
45     enum Type {
46         LinearFunction, CubicBezierFunction, StepsFunction
47     };
48 
~TimingFunction()49     virtual ~TimingFunction() { }
50 
type()51     Type type() const { return m_type; }
52 
53     virtual String toString() const = 0;
54 
55     // Evaluates the timing function at the given fraction. The accuracy parameter provides a hint as to the required
56     // accuracy and is not guaranteed.
57     virtual double evaluate(double fraction, double accuracy) const = 0;
58 
59 protected:
TimingFunction(Type type)60     TimingFunction(Type type)
61         : m_type(type)
62     {
63     }
64 
65 private:
66     Type m_type;
67 };
68 
69 class PLATFORM_EXPORT LinearTimingFunction FINAL : public TimingFunction {
70 public:
shared()71     static LinearTimingFunction* shared()
72     {
73         DEFINE_STATIC_REF(LinearTimingFunction, linear, (adoptRef(new LinearTimingFunction())));
74         return linear;
75     }
76 
~LinearTimingFunction()77     virtual ~LinearTimingFunction() { }
78 
79     virtual String toString() const OVERRIDE;
80 
81     virtual double evaluate(double fraction, double) const OVERRIDE;
82 
83 private:
LinearTimingFunction()84     LinearTimingFunction()
85         : TimingFunction(LinearFunction)
86     {
87     }
88 };
89 
90 class PLATFORM_EXPORT CubicBezierTimingFunction FINAL : public TimingFunction {
91 public:
92     enum SubType {
93         Ease,
94         EaseIn,
95         EaseOut,
96         EaseInOut,
97         Custom
98     };
99 
create(double x1,double y1,double x2,double y2)100     static PassRefPtr<CubicBezierTimingFunction> create(double x1, double y1, double x2, double y2)
101     {
102         return adoptRef(new CubicBezierTimingFunction(Custom, x1, y1, x2, y2));
103     }
104 
preset(SubType subType)105     static CubicBezierTimingFunction* preset(SubType subType)
106     {
107         switch (subType) {
108         case Ease:
109             {
110                 DEFINE_STATIC_REF(CubicBezierTimingFunction, ease, (adoptRef(new CubicBezierTimingFunction(Ease, 0.25, 0.1, 0.25, 1.0))));
111                 return ease;
112             }
113         case EaseIn:
114             {
115                 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeIn, (adoptRef(new CubicBezierTimingFunction(EaseIn, 0.42, 0.0, 1.0, 1.0))));
116                 return easeIn;
117             }
118         case EaseOut:
119             {
120                 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeOut, (adoptRef(new CubicBezierTimingFunction(EaseOut, 0.0, 0.0, 0.58, 1.0))));
121                 return easeOut;
122             }
123         case EaseInOut:
124             {
125                 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeInOut, (adoptRef(new CubicBezierTimingFunction(EaseInOut, 0.42, 0.0, 0.58, 1.0))));
126                 return easeInOut;
127             }
128         default:
129             ASSERT_NOT_REACHED();
130             return 0;
131         }
132     }
133 
~CubicBezierTimingFunction()134     virtual ~CubicBezierTimingFunction() { }
135 
136     virtual String toString() const OVERRIDE;
137 
138     virtual double evaluate(double fraction, double accuracy) const OVERRIDE;
139 
x1()140     double x1() const { return m_x1; }
y1()141     double y1() const { return m_y1; }
x2()142     double x2() const { return m_x2; }
y2()143     double y2() const { return m_y2; }
144 
subType()145     SubType subType() const { return m_subType; }
146 
147 private:
CubicBezierTimingFunction(SubType subType,double x1,double y1,double x2,double y2)148     explicit CubicBezierTimingFunction(SubType subType, double x1, double y1, double x2, double y2)
149         : TimingFunction(CubicBezierFunction)
150         , m_x1(x1)
151         , m_y1(y1)
152         , m_x2(x2)
153         , m_y2(y2)
154         , m_subType(subType)
155     {
156     }
157 
158     double m_x1;
159     double m_y1;
160     double m_x2;
161     double m_y2;
162     SubType m_subType;
163     mutable OwnPtr<UnitBezier> m_bezier;
164 };
165 
166 class PLATFORM_EXPORT StepsTimingFunction FINAL : public TimingFunction {
167 public:
168     enum SubType {
169         Start,
170         End,
171         Middle,
172         Custom
173     };
174 
175     enum StepAtPosition {
176         StepAtStart,
177         StepAtMiddle,
178         StepAtEnd
179     };
180 
create(int steps,StepAtPosition stepAtPosition)181     static PassRefPtr<StepsTimingFunction> create(int steps, StepAtPosition stepAtPosition)
182     {
183         return adoptRef(new StepsTimingFunction(Custom, steps, stepAtPosition));
184     }
185 
preset(SubType subType)186     static StepsTimingFunction* preset(SubType subType)
187     {
188         switch (subType) {
189         case Start:
190             {
191                 DEFINE_STATIC_REF(StepsTimingFunction, start, (adoptRef(new StepsTimingFunction(Start, 1, StepAtStart))));
192                 return start;
193             }
194         case Middle:
195             {
196                 DEFINE_STATIC_REF(StepsTimingFunction, middle, (adoptRef(new StepsTimingFunction(Middle, 1, StepAtMiddle))));
197                 return middle;
198             }
199         case End:
200             {
201                 DEFINE_STATIC_REF(StepsTimingFunction, end, (adoptRef(new StepsTimingFunction(End, 1, StepAtEnd))));
202                 return end;
203             }
204         default:
205             ASSERT_NOT_REACHED();
206             return 0;
207         }
208     }
209 
210 
~StepsTimingFunction()211     virtual ~StepsTimingFunction() { }
212 
213     virtual String toString() const OVERRIDE;
214 
215     virtual double evaluate(double fraction, double) const OVERRIDE;
216 
numberOfSteps()217     int numberOfSteps() const { return m_steps; }
stepAtPosition()218     StepAtPosition stepAtPosition() const { return m_stepAtPosition; }
219 
subType()220     SubType subType() const { return m_subType; }
221 
222 private:
StepsTimingFunction(SubType subType,int steps,StepAtPosition stepAtPosition)223     StepsTimingFunction(SubType subType, int steps, StepAtPosition stepAtPosition)
224         : TimingFunction(StepsFunction)
225         , m_steps(steps)
226         , m_stepAtPosition(stepAtPosition)
227         , m_subType(subType)
228     {
229     }
230 
231     int m_steps;
232     StepAtPosition m_stepAtPosition;
233     SubType m_subType;
234 };
235 
236 PLATFORM_EXPORT bool operator==(const LinearTimingFunction&, const TimingFunction&);
237 PLATFORM_EXPORT bool operator==(const CubicBezierTimingFunction&, const TimingFunction&);
238 PLATFORM_EXPORT bool operator==(const StepsTimingFunction&, const TimingFunction&);
239 
240 PLATFORM_EXPORT bool operator==(const TimingFunction&, const TimingFunction&);
241 PLATFORM_EXPORT bool operator!=(const TimingFunction&, const TimingFunction&);
242 
243 #define DEFINE_TIMING_FUNCTION_TYPE_CASTS(typeName) \
244     DEFINE_TYPE_CASTS( \
245         typeName##TimingFunction, TimingFunction, value, \
246         value->type() == TimingFunction::typeName##Function, \
247         value.type() == TimingFunction::typeName##Function)
248 
249 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Linear);
250 DEFINE_TIMING_FUNCTION_TYPE_CASTS(CubicBezier);
251 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Steps);
252 
253 } // namespace WebCore
254 
255 #endif // TimingFunction_h
256