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