2012-07-19 23:48:25 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2013-10-02 18:03:04 -07:00
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-07-19 23:48:25 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_layers_Axis_h
|
|
|
|
#define mozilla_layers_Axis_h
|
|
|
|
|
2013-08-11 16:17:23 -07:00
|
|
|
#include <sys/types.h> // for int32_t
|
|
|
|
#include "Units.h" // for CSSRect, CSSPoint
|
|
|
|
#include "mozilla/TimeStamp.h" // for TimeDuration
|
|
|
|
#include "nsTArray.h" // for nsTArray
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-11-17 18:07:08 -08:00
|
|
|
const float EPSILON = 0.0001f;
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
class AsyncPanZoomController;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to maintain each axis of movement (X,Y) for panning and zooming.
|
|
|
|
* Note that everything here is specific to one axis; that is, the X axis knows
|
|
|
|
* nothing about the Y axis and vice versa.
|
|
|
|
*/
|
|
|
|
class Axis {
|
|
|
|
public:
|
|
|
|
Axis(AsyncPanZoomController* aAsyncPanZoomController);
|
|
|
|
|
|
|
|
enum Overscroll {
|
|
|
|
// Overscroll is not happening at all.
|
|
|
|
OVERSCROLL_NONE = 0,
|
|
|
|
// Overscroll is happening in the negative direction. This means either to
|
|
|
|
// the left or to the top depending on the axis.
|
|
|
|
OVERSCROLL_MINUS,
|
|
|
|
// Overscroll is happening in the positive direction. This means either to
|
|
|
|
// the right or to the bottom depending on the axis.
|
|
|
|
OVERSCROLL_PLUS,
|
|
|
|
// Overscroll is happening both ways. This only means something when the
|
|
|
|
// page is scaled out to a smaller size than the viewport.
|
|
|
|
OVERSCROLL_BOTH
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify this Axis that a new touch has been received, including a time delta
|
|
|
|
* indicating how long it has been since the previous one. This triggers a
|
|
|
|
* recalculation of velocity.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
void UpdateWithTouchAtDevicePoint(int32_t aPos, const TimeDuration& aTimeDelta);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify this Axis that a touch has begun, i.e. the user has put their finger
|
|
|
|
* on the screen but has not yet tried to pan.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
void StartTouch(int32_t aPos);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
2012-08-13 21:08:38 -07:00
|
|
|
* Notify this Axis that a touch has ended gracefully. This may perform
|
|
|
|
* recalculations of the axis velocity.
|
2012-07-19 23:48:25 -07:00
|
|
|
*/
|
2012-08-13 21:08:38 -07:00
|
|
|
void EndTouch();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify this Axis that a touch has ended forcefully. Useful for stopping
|
|
|
|
* flings when a user puts their finger down in the middle of one (i.e. to
|
|
|
|
* stop a previous touch including its fling so that a new one can take its
|
|
|
|
* place).
|
|
|
|
*/
|
|
|
|
void CancelTouch();
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
2013-08-20 16:00:57 -07:00
|
|
|
* Takes a requested displacement to the position of this axis, and adjusts
|
2013-10-02 18:03:04 -07:00
|
|
|
* it to account for acceleration (which might increase the displacement),
|
|
|
|
* overscroll (which might decrease the displacement; this is to prevent the
|
|
|
|
* viewport from overscrolling the page rect), and axis locking (which might
|
|
|
|
* prevent any displacement from happening). If overscroll ocurred, its amount
|
|
|
|
* is written to |aOverscrollAmountOut|.
|
2013-08-20 16:00:57 -07:00
|
|
|
* The adjusted displacement is returned.
|
2014-01-21 09:33:52 -08:00
|
|
|
*
|
|
|
|
* aScrollingDisabled is used to indicate that no scrolling should happen
|
|
|
|
* in this axis. This is used to implement overflow: hidden;
|
2012-07-19 23:48:25 -07:00
|
|
|
*/
|
2014-01-21 09:33:52 -08:00
|
|
|
float AdjustDisplacement(float aDisplacement, float& aOverscrollAmountOut,
|
|
|
|
bool aScrollingDisabled);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the distance between the starting position of the touch supplied in
|
|
|
|
* startTouch() and the current touch from the last
|
|
|
|
* updateWithTouchAtDevicePoint().
|
|
|
|
*/
|
|
|
|
float PanDistance();
|
|
|
|
|
2013-10-02 18:03:04 -07:00
|
|
|
/**
|
|
|
|
* Gets the distance between the starting position of the touch supplied in
|
|
|
|
* startTouch() and the supplied position.
|
|
|
|
*/
|
|
|
|
float PanDistance(float aPos);
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
/**
|
|
|
|
* Applies friction during a fling, or cancels the fling if the velocity is
|
|
|
|
* too low. Returns true if the fling should continue to another frame, or
|
|
|
|
* false if it should end. |aDelta| is the amount of time that has passed
|
|
|
|
* since the last time friction was applied.
|
|
|
|
*/
|
|
|
|
bool FlingApplyFrictionOrCancel(const TimeDuration& aDelta);
|
|
|
|
|
2013-10-02 18:03:04 -07:00
|
|
|
/*
|
|
|
|
* Returns true if the page is zoomed in to some degree along this axis such that scrolling is
|
|
|
|
* possible and this axis has not been scroll locked while panning. Otherwise, returns false.
|
|
|
|
*/
|
|
|
|
bool Scrollable();
|
|
|
|
|
2014-01-21 09:33:52 -08:00
|
|
|
void SetAxisLocked(bool aAxisLocked) { mAxisLocked = aAxisLocked; }
|
2013-10-02 18:03:04 -07:00
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
/**
|
|
|
|
* Gets the overscroll state of the axis in its current position.
|
|
|
|
*/
|
|
|
|
Overscroll GetOverscroll();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there is overscroll, returns the amount. Sign depends on in what
|
|
|
|
* direction it is overscrolling. Positive excess means that it is
|
|
|
|
* overscrolling in the positive direction, whereas negative excess means
|
|
|
|
* that it is overscrolling in the negative direction. If there is overscroll
|
|
|
|
* in both directions, this returns 0; it assumes that you check
|
|
|
|
* GetOverscroll() first.
|
|
|
|
*/
|
2012-08-21 21:37:15 -07:00
|
|
|
float GetExcess();
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-09-28 21:02:45 -07:00
|
|
|
/**
|
|
|
|
* Gets the factor of acceleration applied to the velocity, based on the
|
|
|
|
* amount of flings that have been done successively.
|
|
|
|
*/
|
|
|
|
float GetAccelerationFactor();
|
|
|
|
|
2012-07-19 23:48:25 -07:00
|
|
|
/**
|
|
|
|
* Gets the raw velocity of this axis at this moment.
|
|
|
|
*/
|
|
|
|
float GetVelocity();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the overscroll state of the axis given an additional displacement.
|
|
|
|
* That is to say, if the given displacement is applied, this will tell you
|
|
|
|
* whether or not it will overscroll, and in what direction.
|
|
|
|
*/
|
2013-05-02 06:24:16 -07:00
|
|
|
Overscroll DisplacementWillOverscroll(float aDisplacement);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a displacement will overscroll the axis, this returns the amount and in
|
2013-10-10 14:02:25 -07:00
|
|
|
* what direction. Similar to GetExcess() but takes a displacement to apply.
|
2012-07-19 23:48:25 -07:00
|
|
|
*/
|
2013-05-02 06:24:16 -07:00
|
|
|
float DisplacementWillOverscrollAmount(float aDisplacement);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a scale will overscroll the axis, this returns the amount and in what
|
2013-10-10 14:02:25 -07:00
|
|
|
* direction. Similar to GetExcess() but takes a displacement to apply.
|
2012-07-19 23:48:25 -07:00
|
|
|
*
|
|
|
|
* |aFocus| is the point at which the scale is focused at. We will offset the
|
|
|
|
* scroll offset in such a way that it remains in the same place on the page
|
|
|
|
* relative.
|
|
|
|
*/
|
2013-10-10 09:21:50 -07:00
|
|
|
float ScaleWillOverscrollAmount(float aScale, float aFocus);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if an axis will overscroll in both directions by computing the
|
|
|
|
* content rect and checking that its height/width (depending on the axis)
|
|
|
|
* does not overextend past the viewport.
|
|
|
|
*
|
|
|
|
* This gets called by ScaleWillOverscroll().
|
|
|
|
*/
|
2013-10-10 09:21:50 -07:00
|
|
|
bool ScaleWillOverscrollBothSides(float aScale);
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2012-08-21 21:37:15 -07:00
|
|
|
float GetOrigin();
|
2012-09-28 19:16:34 -07:00
|
|
|
float GetCompositionLength();
|
2012-08-21 21:37:15 -07:00
|
|
|
float GetPageStart();
|
|
|
|
float GetPageLength();
|
2012-09-28 19:16:34 -07:00
|
|
|
float GetCompositionEnd();
|
2012-08-21 21:37:15 -07:00
|
|
|
float GetPageEnd();
|
2012-07-19 23:48:25 -07:00
|
|
|
|
2013-08-20 16:00:57 -07:00
|
|
|
int32_t GetPos() const { return mPos; }
|
|
|
|
|
2013-05-30 18:30:13 -07:00
|
|
|
virtual float GetPointOffset(const CSSPoint& aPoint) = 0;
|
2013-06-03 06:52:44 -07:00
|
|
|
virtual float GetRectLength(const CSSRect& aRect) = 0;
|
|
|
|
virtual float GetRectOffset(const CSSRect& aRect) = 0;
|
2012-07-19 23:48:25 -07:00
|
|
|
|
|
|
|
protected:
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mPos;
|
|
|
|
int32_t mStartPos;
|
2012-07-19 23:48:25 -07:00
|
|
|
float mVelocity;
|
2012-08-13 21:08:38 -07:00
|
|
|
// Acceleration is represented by an int, which is the power we raise a
|
|
|
|
// constant to and then multiply the velocity by whenever it is sampled. We do
|
|
|
|
// this only when we detect that the user wants to do a fast fling; that is,
|
|
|
|
// they are flinging multiple times in a row very quickly, probably trying to
|
|
|
|
// reach one of the extremes of the page.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mAcceleration;
|
2014-01-21 09:33:52 -08:00
|
|
|
bool mAxisLocked; // Whether movement on this axis is locked.
|
2013-01-24 19:59:10 -08:00
|
|
|
AsyncPanZoomController* mAsyncPanZoomController;
|
2013-02-20 14:59:15 -08:00
|
|
|
nsTArray<float> mVelocityQueue;
|
2012-07-19 23:48:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class AxisX : public Axis {
|
|
|
|
public:
|
|
|
|
AxisX(AsyncPanZoomController* mAsyncPanZoomController);
|
2013-05-30 18:30:13 -07:00
|
|
|
virtual float GetPointOffset(const CSSPoint& aPoint);
|
2013-06-03 06:52:44 -07:00
|
|
|
virtual float GetRectLength(const CSSRect& aRect);
|
|
|
|
virtual float GetRectOffset(const CSSRect& aRect);
|
2012-07-19 23:48:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class AxisY : public Axis {
|
|
|
|
public:
|
|
|
|
AxisY(AsyncPanZoomController* mAsyncPanZoomController);
|
2013-05-30 18:30:13 -07:00
|
|
|
virtual float GetPointOffset(const CSSPoint& aPoint);
|
2013-06-03 06:52:44 -07:00
|
|
|
virtual float GetRectLength(const CSSRect& aRect);
|
|
|
|
virtual float GetRectOffset(const CSSRect& aRect);
|
2012-07-19 23:48:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|