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)
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.
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.
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.
25 #ifndef TimingFunction_h
26 #define TimingFunction_h
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/text/StringBuilder.h"
36 #include "wtf/text/WTFString.h"
41 class PLATFORM_EXPORT TimingFunction : public RefCounted<TimingFunction> {
45 LinearFunction, CubicBezierFunction, StepsFunction
48 virtual ~TimingFunction() { }
50 Type type() const { return m_type; }
52 virtual String toString() const = 0;
54 // Evaluates the timing function at the given fraction. The accuracy parameter provides a hint as to the required
55 // accuracy and is not guaranteed.
56 virtual double evaluate(double fraction, double accuracy) const = 0;
58 // This function returns the minimum and maximum values obtainable when
59 // calling evaluate();
60 virtual void range(double* minValue, double* maxValue) const = 0;
63 TimingFunction(Type type)
72 class PLATFORM_EXPORT LinearTimingFunction final : public TimingFunction {
74 static LinearTimingFunction* shared()
76 DEFINE_STATIC_REF(LinearTimingFunction, linear, (adoptRef(new LinearTimingFunction())));
80 virtual ~LinearTimingFunction() { }
82 virtual String toString() const override;
84 virtual double evaluate(double fraction, double) const override;
86 virtual void range(double* minValue, double* maxValue) const override;
88 LinearTimingFunction()
89 : TimingFunction(LinearFunction)
94 class PLATFORM_EXPORT CubicBezierTimingFunction final : public TimingFunction {
104 static PassRefPtr<CubicBezierTimingFunction> create(double x1, double y1, double x2, double y2)
106 return adoptRef(new CubicBezierTimingFunction(Custom, x1, y1, x2, y2));
109 static CubicBezierTimingFunction* preset(SubType subType)
114 DEFINE_STATIC_REF(CubicBezierTimingFunction, ease, (adoptRef(new CubicBezierTimingFunction(Ease, 0.25, 0.1, 0.25, 1.0))));
119 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeIn, (adoptRef(new CubicBezierTimingFunction(EaseIn, 0.42, 0.0, 1.0, 1.0))));
124 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeOut, (adoptRef(new CubicBezierTimingFunction(EaseOut, 0.0, 0.0, 0.58, 1.0))));
129 DEFINE_STATIC_REF(CubicBezierTimingFunction, easeInOut, (adoptRef(new CubicBezierTimingFunction(EaseInOut, 0.42, 0.0, 0.58, 1.0))));
133 ASSERT_NOT_REACHED();
138 virtual ~CubicBezierTimingFunction() { }
140 virtual String toString() const override;
142 virtual double evaluate(double fraction, double accuracy) const override;
143 virtual void range(double* minValue, double* maxValue) const override;
145 double x1() const { return m_x1; }
146 double y1() const { return m_y1; }
147 double x2() const { return m_x2; }
148 double y2() const { return m_y2; }
150 SubType subType() const { return m_subType; }
153 explicit CubicBezierTimingFunction(SubType subType, double x1, double y1, double x2, double y2)
154 : TimingFunction(CubicBezierFunction)
168 mutable OwnPtr<UnitBezier> m_bezier;
171 class PLATFORM_EXPORT StepsTimingFunction final : public TimingFunction {
173 enum StepAtPosition {
179 static PassRefPtr<StepsTimingFunction> create(int steps, StepAtPosition stepAtPosition)
181 return adoptRef(new StepsTimingFunction(steps, stepAtPosition));
184 static StepsTimingFunction* preset(StepAtPosition position)
186 DEFINE_STATIC_REF(StepsTimingFunction, start, create(1, Start));
187 DEFINE_STATIC_REF(StepsTimingFunction, middle, create(1, Middle));
188 DEFINE_STATIC_REF(StepsTimingFunction, end, create(1, End));
197 ASSERT_NOT_REACHED();
203 virtual ~StepsTimingFunction() { }
205 virtual String toString() const override;
207 virtual double evaluate(double fraction, double) const override;
209 virtual void range(double* minValue, double* maxValue) const override;
210 int numberOfSteps() const { return m_steps; }
211 StepAtPosition stepAtPosition() const { return m_stepAtPosition; }
214 StepsTimingFunction(int steps, StepAtPosition stepAtPosition)
215 : TimingFunction(StepsFunction)
217 , m_stepAtPosition(stepAtPosition)
222 StepAtPosition m_stepAtPosition;
225 PLATFORM_EXPORT bool operator==(const LinearTimingFunction&, const TimingFunction&);
226 PLATFORM_EXPORT bool operator==(const CubicBezierTimingFunction&, const TimingFunction&);
227 PLATFORM_EXPORT bool operator==(const StepsTimingFunction&, const TimingFunction&);
229 PLATFORM_EXPORT bool operator==(const TimingFunction&, const TimingFunction&);
230 PLATFORM_EXPORT bool operator!=(const TimingFunction&, const TimingFunction&);
232 #define DEFINE_TIMING_FUNCTION_TYPE_CASTS(typeName) \
234 typeName##TimingFunction, TimingFunction, value, \
235 value->type() == TimingFunction::typeName##Function, \
236 value.type() == TimingFunction::typeName##Function)
238 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Linear);
239 DEFINE_TIMING_FUNCTION_TYPE_CASTS(CubicBezier);
240 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Steps);
244 #endif // TimingFunction_h