1/*
2 * Copyright (c) 2011, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef ScrollAnimatorNone_h
32#define ScrollAnimatorNone_h
33
34#if ENABLE(SMOOTH_SCROLLING)
35
36#if !ENABLE(REQUEST_ANIMATION_FRAME)
37#error "SMOOTH_SCROLLING requires REQUEST_ANIMATION_FRAME to be enabled."
38#endif
39
40#include "FloatPoint.h"
41#include "ScrollAnimator.h"
42#include "Timer.h"
43#include <wtf/OwnPtr.h>
44
45class ScrollAnimatorNoneTest;
46
47namespace WebCore {
48
49class IntPoint;
50class ActivePlatformGestureAnimation;
51struct ScrollAnimatorParameters;
52
53class ScrollAnimatorNone : public ScrollAnimator {
54public:
55    explicit ScrollAnimatorNone(ScrollableArea*);
56    virtual ~ScrollAnimatorNone();
57
58    virtual bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float multiplier);
59    virtual void scrollToOffsetWithoutAnimation(const FloatPoint&);
60
61#if !USE(REQUEST_ANIMATION_FRAME_TIMER)
62    virtual void cancelAnimations();
63    virtual void serviceScrollAnimations();
64#endif
65
66    virtual void willEndLiveResize();
67    virtual void didAddVerticalScrollbar(Scrollbar*);
68    virtual void didAddHorizontalScrollbar(Scrollbar*);
69
70    enum Curve {
71        Linear,
72        Quadratic,
73        Cubic,
74        Quartic,
75        Bounce
76    };
77
78    struct Parameters {
79        Parameters();
80        Parameters(bool isEnabled, double animationTime, double repeatMinimumSustainTime, Curve attackCurve, double attackTime, Curve releaseCurve, double releaseTime, Curve coastTimeCurve, double maximumCoastTime);
81
82        // Note that the times can be overspecified such that releaseTime or releaseTime and attackTime are greater
83        // than animationTime. animationTime takes priority over releaseTime, capping it. attackTime is capped at
84        // whatever time remains, or zero if none.
85        bool m_isEnabled;
86        double m_animationTime;
87        double m_repeatMinimumSustainTime;
88
89        Curve m_attackCurve;
90        double m_attackTime;
91
92        Curve m_releaseCurve;
93        double m_releaseTime;
94
95        Curve m_coastTimeCurve;
96        double m_maximumCoastTime;
97    };
98
99protected:
100    virtual void animationWillStart() { }
101    virtual void animationDidFinish() { }
102
103    Parameters parametersForScrollGranularity(ScrollGranularity) const;
104
105    friend class ::ScrollAnimatorNoneTest;
106
107    struct PerAxisData {
108        PerAxisData(ScrollAnimatorNone* parent, float* currentPos, int visibleLength);
109        void reset();
110        bool updateDataFromParameters(float step, float multiplier, float scrollableSize, double currentTime, Parameters*);
111        bool animateScroll(double currentTime);
112        void updateVisibleLength(int visibleLength);
113
114        static double curveAt(Curve, double t);
115        static double attackCurve(Curve, double deltaT, double curveT, double startPos, double attackPos);
116        static double releaseCurve(Curve, double deltaT, double curveT, double releasePos, double desiredPos);
117        static double coastCurve(Curve, double factor);
118
119        static double curveIntegralAt(Curve, double t);
120        static double attackArea(Curve, double startT, double endT);
121        static double releaseArea(Curve, double startT, double endT);
122
123        float* m_currentPosition;
124        double m_currentVelocity;
125
126        double m_desiredPosition;
127        double m_desiredVelocity;
128
129        double m_startPosition;
130        double m_startTime;
131        double m_startVelocity;
132
133        double m_animationTime;
134        double m_lastAnimationTime;
135
136        double m_attackPosition;
137        double m_attackTime;
138        Curve m_attackCurve;
139
140        double m_releasePosition;
141        double m_releaseTime;
142        Curve m_releaseCurve;
143
144        int m_visibleLength;
145    };
146
147#if USE(REQUEST_ANIMATION_FRAME_TIMER)
148    void animationTimerFired(Timer<ScrollAnimatorNone>*);
149    void startNextTimer(double delay);
150#else
151    void startNextTimer();
152#endif
153    void animationTimerFired();
154
155    void stopAnimationTimerIfNeeded();
156    bool animationTimerActive();
157    void updateVisibleLengths();
158
159    PerAxisData m_horizontalData;
160    PerAxisData m_verticalData;
161
162    double m_startTime;
163#if USE(REQUEST_ANIMATION_FRAME_TIMER)
164    Timer<ScrollAnimatorNone> m_animationTimer;
165#else
166    bool m_animationActive;
167#endif
168};
169
170} // namespace WebCore
171
172#endif // ENABLE(SMOOTH_SCROLLING)
173
174#endif // ScrollAnimatorNone_h
175