gfx/layers/apz/src/Axis.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set sw=2 ts=8 et tw=80 : */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla_layers_Axis_h
michael@0 8 #define mozilla_layers_Axis_h
michael@0 9
michael@0 10 #include <sys/types.h> // for int32_t
michael@0 11 #include "Units.h" // for CSSRect, CSSPoint
michael@0 12 #include "mozilla/TimeStamp.h" // for TimeDuration
michael@0 13 #include "nsTArray.h" // for nsTArray
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16 namespace layers {
michael@0 17
michael@0 18 const float EPSILON = 0.0001f;
michael@0 19
michael@0 20 class AsyncPanZoomController;
michael@0 21
michael@0 22 /**
michael@0 23 * Helper class to maintain each axis of movement (X,Y) for panning and zooming.
michael@0 24 * Note that everything here is specific to one axis; that is, the X axis knows
michael@0 25 * nothing about the Y axis and vice versa.
michael@0 26 */
michael@0 27 class Axis {
michael@0 28 public:
michael@0 29 Axis(AsyncPanZoomController* aAsyncPanZoomController);
michael@0 30
michael@0 31 enum Overscroll {
michael@0 32 // Overscroll is not happening at all.
michael@0 33 OVERSCROLL_NONE = 0,
michael@0 34 // Overscroll is happening in the negative direction. This means either to
michael@0 35 // the left or to the top depending on the axis.
michael@0 36 OVERSCROLL_MINUS,
michael@0 37 // Overscroll is happening in the positive direction. This means either to
michael@0 38 // the right or to the bottom depending on the axis.
michael@0 39 OVERSCROLL_PLUS,
michael@0 40 // Overscroll is happening both ways. This only means something when the
michael@0 41 // page is scaled out to a smaller size than the viewport.
michael@0 42 OVERSCROLL_BOTH
michael@0 43 };
michael@0 44
michael@0 45 /**
michael@0 46 * Notify this Axis that a new touch has been received, including a time delta
michael@0 47 * indicating how long it has been since the previous one. This triggers a
michael@0 48 * recalculation of velocity.
michael@0 49 */
michael@0 50 void UpdateWithTouchAtDevicePoint(int32_t aPos, const TimeDuration& aTimeDelta);
michael@0 51
michael@0 52 /**
michael@0 53 * Notify this Axis that a touch has begun, i.e. the user has put their finger
michael@0 54 * on the screen but has not yet tried to pan.
michael@0 55 */
michael@0 56 void StartTouch(int32_t aPos);
michael@0 57
michael@0 58 /**
michael@0 59 * Notify this Axis that a touch has ended gracefully. This may perform
michael@0 60 * recalculations of the axis velocity.
michael@0 61 */
michael@0 62 void EndTouch();
michael@0 63
michael@0 64 /**
michael@0 65 * Notify this Axis that a touch has ended forcefully. Useful for stopping
michael@0 66 * flings when a user puts their finger down in the middle of one (i.e. to
michael@0 67 * stop a previous touch including its fling so that a new one can take its
michael@0 68 * place).
michael@0 69 */
michael@0 70 void CancelTouch();
michael@0 71
michael@0 72 /**
michael@0 73 * Takes a requested displacement to the position of this axis, and adjusts it
michael@0 74 * to account for overscroll (which might decrease the displacement; this is
michael@0 75 * to prevent the viewport from overscrolling the page rect), and axis locking
michael@0 76 * (which might prevent any displacement from happening). If overscroll
michael@0 77 * ocurred, its amount is written to |aOverscrollAmountOut|.
michael@0 78 * The adjusted displacement is returned.
michael@0 79 */
michael@0 80 float AdjustDisplacement(float aDisplacement, float& aOverscrollAmountOut);
michael@0 81
michael@0 82 /**
michael@0 83 * Gets the distance between the starting position of the touch supplied in
michael@0 84 * startTouch() and the current touch from the last
michael@0 85 * updateWithTouchAtDevicePoint().
michael@0 86 */
michael@0 87 float PanDistance();
michael@0 88
michael@0 89 /**
michael@0 90 * Gets the distance between the starting position of the touch supplied in
michael@0 91 * startTouch() and the supplied position.
michael@0 92 */
michael@0 93 float PanDistance(float aPos);
michael@0 94
michael@0 95 /**
michael@0 96 * Applies friction during a fling, or cancels the fling if the velocity is
michael@0 97 * too low. Returns true if the fling should continue to another frame, or
michael@0 98 * false if it should end. |aDelta| is the amount of time that has passed
michael@0 99 * since the last time friction was applied.
michael@0 100 */
michael@0 101 bool FlingApplyFrictionOrCancel(const TimeDuration& aDelta);
michael@0 102
michael@0 103 /*
michael@0 104 * Returns true if the page is zoomed in to some degree along this axis such that scrolling is
michael@0 105 * possible and this axis has not been scroll locked while panning. Otherwise, returns false.
michael@0 106 */
michael@0 107 bool Scrollable();
michael@0 108
michael@0 109 void SetAxisLocked(bool aAxisLocked) { mAxisLocked = aAxisLocked; }
michael@0 110
michael@0 111 /**
michael@0 112 * Gets the overscroll state of the axis in its current position.
michael@0 113 */
michael@0 114 Overscroll GetOverscroll();
michael@0 115
michael@0 116 /**
michael@0 117 * If there is overscroll, returns the amount. Sign depends on in what
michael@0 118 * direction it is overscrolling. Positive excess means that it is
michael@0 119 * overscrolling in the positive direction, whereas negative excess means
michael@0 120 * that it is overscrolling in the negative direction. If there is overscroll
michael@0 121 * in both directions, this returns 0; it assumes that you check
michael@0 122 * GetOverscroll() first.
michael@0 123 */
michael@0 124 float GetExcess();
michael@0 125
michael@0 126 /**
michael@0 127 * Gets the raw velocity of this axis at this moment.
michael@0 128 */
michael@0 129 float GetVelocity();
michael@0 130
michael@0 131 /**
michael@0 132 * Sets the raw velocity of this axis at this moment.
michael@0 133 * Intended to be called only when the axis "takes over" a velocity from
michael@0 134 * another APZC, in which case there are no touch points available to call
michael@0 135 * UpdateWithTouchAtDevicePoint. In other circumstances,
michael@0 136 * UpdateWithTouchAtDevicePoint should be used and the velocity calculated
michael@0 137 * there.
michael@0 138 */
michael@0 139 void SetVelocity(float aVelocity);
michael@0 140
michael@0 141 /**
michael@0 142 * Gets the overscroll state of the axis given an additional displacement.
michael@0 143 * That is to say, if the given displacement is applied, this will tell you
michael@0 144 * whether or not it will overscroll, and in what direction.
michael@0 145 */
michael@0 146 Overscroll DisplacementWillOverscroll(float aDisplacement);
michael@0 147
michael@0 148 /**
michael@0 149 * If a displacement will overscroll the axis, this returns the amount and in
michael@0 150 * what direction. Similar to GetExcess() but takes a displacement to apply.
michael@0 151 */
michael@0 152 float DisplacementWillOverscrollAmount(float aDisplacement);
michael@0 153
michael@0 154 /**
michael@0 155 * If a scale will overscroll the axis, this returns the amount and in what
michael@0 156 * direction. Similar to GetExcess() but takes a displacement to apply.
michael@0 157 *
michael@0 158 * |aFocus| is the point at which the scale is focused at. We will offset the
michael@0 159 * scroll offset in such a way that it remains in the same place on the page
michael@0 160 * relative.
michael@0 161 */
michael@0 162 float ScaleWillOverscrollAmount(float aScale, float aFocus);
michael@0 163
michael@0 164 /**
michael@0 165 * Checks if an axis will overscroll in both directions by computing the
michael@0 166 * content rect and checking that its height/width (depending on the axis)
michael@0 167 * does not overextend past the viewport.
michael@0 168 *
michael@0 169 * This gets called by ScaleWillOverscroll().
michael@0 170 */
michael@0 171 bool ScaleWillOverscrollBothSides(float aScale);
michael@0 172
michael@0 173 /**
michael@0 174 * Returns whether there is room to pan on this axis in either direction.
michael@0 175 */
michael@0 176 bool HasRoomToPan() const;
michael@0 177
michael@0 178 float GetOrigin() const;
michael@0 179 float GetCompositionLength() const;
michael@0 180 float GetPageStart() const;
michael@0 181 float GetPageLength() const;
michael@0 182 float GetCompositionEnd() const;
michael@0 183 float GetPageEnd() const;
michael@0 184
michael@0 185 int32_t GetPos() const { return mPos; }
michael@0 186
michael@0 187 virtual float GetPointOffset(const CSSPoint& aPoint) const = 0;
michael@0 188 virtual float GetRectLength(const CSSRect& aRect) const = 0;
michael@0 189 virtual float GetRectOffset(const CSSRect& aRect) const = 0;
michael@0 190
michael@0 191 protected:
michael@0 192 int32_t mPos;
michael@0 193 int32_t mStartPos;
michael@0 194 float mVelocity;
michael@0 195 bool mAxisLocked; // Whether movement on this axis is locked.
michael@0 196 AsyncPanZoomController* mAsyncPanZoomController;
michael@0 197 nsTArray<float> mVelocityQueue;
michael@0 198 };
michael@0 199
michael@0 200 class AxisX : public Axis {
michael@0 201 public:
michael@0 202 AxisX(AsyncPanZoomController* mAsyncPanZoomController);
michael@0 203 virtual float GetPointOffset(const CSSPoint& aPoint) const;
michael@0 204 virtual float GetRectLength(const CSSRect& aRect) const;
michael@0 205 virtual float GetRectOffset(const CSSRect& aRect) const;
michael@0 206 };
michael@0 207
michael@0 208 class AxisY : public Axis {
michael@0 209 public:
michael@0 210 AxisY(AsyncPanZoomController* mAsyncPanZoomController);
michael@0 211 virtual float GetPointOffset(const CSSPoint& aPoint) const;
michael@0 212 virtual float GetRectLength(const CSSRect& aRect) const;
michael@0 213 virtual float GetRectOffset(const CSSRect& aRect) const;
michael@0 214 };
michael@0 215
michael@0 216 }
michael@0 217 }
michael@0 218
michael@0 219 #endif

mercurial