Home | History | Annotate | Download | only in scroll
      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 #include "platform/Timer.h"
     35 #include "platform/geometry/FloatPoint.h"
     36 #include "platform/scroll/ScrollAnimator.h"
     37 
     38 class ScrollAnimatorNoneTest;
     39 
     40 namespace WebCore {
     41 
     42 class IntPoint;
     43 class ActivePlatformGestureAnimation;
     44 struct ScrollAnimatorParameters;
     45 
     46 class PLATFORM_EXPORT ScrollAnimatorNone : public ScrollAnimator {
     47 public:
     48     explicit ScrollAnimatorNone(ScrollableArea*);
     49     virtual ~ScrollAnimatorNone();
     50 
     51     virtual bool scroll(ScrollbarOrientation, ScrollGranularity, float step, float delta) OVERRIDE;
     52     virtual void scrollToOffsetWithoutAnimation(const FloatPoint&) OVERRIDE;
     53 
     54     virtual void cancelAnimations() OVERRIDE;
     55     virtual void serviceScrollAnimations() OVERRIDE;
     56 
     57     virtual void willEndLiveResize() OVERRIDE;
     58     virtual void didAddVerticalScrollbar(Scrollbar*) OVERRIDE;
     59     virtual void didAddHorizontalScrollbar(Scrollbar*) OVERRIDE;
     60 
     61     enum Curve {
     62         Linear,
     63         Quadratic,
     64         Cubic,
     65         Quartic,
     66         Bounce
     67     };
     68 
     69     struct PLATFORM_EXPORT Parameters {
     70         Parameters();
     71         Parameters(bool isEnabled, double animationTime, double repeatMinimumSustainTime, Curve attackCurve, double attackTime, Curve releaseCurve, double releaseTime, Curve coastTimeCurve, double maximumCoastTime);
     72 
     73         // Note that the times can be overspecified such that releaseTime or releaseTime and attackTime are greater
     74         // than animationTime. animationTime takes priority over releaseTime, capping it. attackTime is capped at
     75         // whatever time remains, or zero if none.
     76         bool m_isEnabled;
     77         double m_animationTime;
     78         double m_repeatMinimumSustainTime;
     79 
     80         Curve m_attackCurve;
     81         double m_attackTime;
     82 
     83         Curve m_releaseCurve;
     84         double m_releaseTime;
     85 
     86         Curve m_coastTimeCurve;
     87         double m_maximumCoastTime;
     88     };
     89 
     90 protected:
     91     virtual void animationWillStart() { }
     92     virtual void animationDidFinish() { }
     93 
     94     Parameters parametersForScrollGranularity(ScrollGranularity) const;
     95 
     96     friend class ::ScrollAnimatorNoneTest;
     97 
     98     struct PLATFORM_EXPORT PerAxisData {
     99         PerAxisData(ScrollAnimatorNone* parent, float* currentPos, int visibleLength);
    100         void reset();
    101         bool updateDataFromParameters(float step, float delta, float scrollableSize, double currentTime, Parameters*);
    102         bool animateScroll(double currentTime);
    103         void updateVisibleLength(int visibleLength);
    104 
    105         static double curveAt(Curve, double t);
    106         static double attackCurve(Curve, double deltaT, double curveT, double startPos, double attackPos);
    107         static double releaseCurve(Curve, double deltaT, double curveT, double releasePos, double desiredPos);
    108         static double coastCurve(Curve, double factor);
    109 
    110         static double curveIntegralAt(Curve, double t);
    111         static double attackArea(Curve, double startT, double endT);
    112         static double releaseArea(Curve, double startT, double endT);
    113 
    114         double newScrollAnimationPosition(double deltaTime);
    115 
    116         float* m_currentPosition;
    117         double m_currentVelocity;
    118 
    119         double m_desiredPosition;
    120         double m_desiredVelocity;
    121 
    122         double m_startPosition;
    123         double m_startTime;
    124         double m_startVelocity;
    125 
    126         double m_animationTime;
    127         double m_lastAnimationTime;
    128 
    129         double m_attackPosition;
    130         double m_attackTime;
    131         Curve m_attackCurve;
    132 
    133         double m_releasePosition;
    134         double m_releaseTime;
    135         Curve m_releaseCurve;
    136 
    137         int m_visibleLength;
    138     };
    139 
    140     void startNextTimer();
    141     void animationTimerFired();
    142 
    143     void stopAnimationTimerIfNeeded();
    144     bool animationTimerActive();
    145     void updateVisibleLengths();
    146 
    147     PerAxisData m_horizontalData;
    148     PerAxisData m_verticalData;
    149 
    150     double m_startTime;
    151     bool m_animationActive;
    152 };
    153 
    154 } // namespace WebCore
    155 
    156 #endif // ScrollAnimatorNone_h
    157