| 1 |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
| 2 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
package org.mozilla.gecko.gfx; |
| 7 |
|
| 8 |
import android.view.View; |
| 9 |
|
| 10 |
import org.mozilla.gecko.util.FloatUtils; |
| 11 |
|
| 12 |
/** |
| 13 |
* This class represents the physics for one axis of movement (i.e. either |
| 14 |
* horizontal or vertical). It tracks the different properties of movement |
| 15 |
* like displacement, velocity, viewport dimensions, etc. pertaining to |
| 16 |
* a particular axis. |
| 17 |
*/ |
| 18 |
|
| 19 |
abstract class Axis { |
| 20 |
|
| 21 |
// This fraction of velocity remains after every animation frame when the velocity is low. |
| 22 |
private static final float FRICTION_SLOW = getFrameAdjustedFriction(0.85f); |
| 23 |
// This fraction of velocity remains after every animation frame when the velocity is high. |
| 24 |
private static final float FRICTION_FAST = getFrameAdjustedFriction(0.97f); |
| 25 |
// Below this velocity (in pixels per frame), the friction starts increasing from FRICTION_FAST |
| 26 |
// to FRICTION_SLOW. |
| 27 |
|
| 28 |
static final float MS_PER_FRAME = 4.0f; |
| 29 |
private static final float FRAMERATE_MULTIPLIER = (1000f/60f) / MS_PER_FRAME; |
| 30 |
private static final float VELOCITY_THRESHOLD = 10 / FRAMERATE_MULTIPLIER; |
| 31 |
// The maximum velocity change factor between events, per ms, in %. |
| 32 |
// Direction changes are excluded. |
| 33 |
private static final float MAX_EVENT_ACCELERATION = 0.012f; |
| 34 |
|
| 35 |
// The rate of deceleration when the surface has overscrolled. |
| 36 |
private static final float OVERSCROLL_DECEL_RATE = getFrameAdjustedFriction(0.04f); |
| 37 |
// The percentage of the surface which can be overscrolled before it must snap back. |
| 38 |
private static final float SNAP_LIMIT = 0.3f; |
| 39 |
|
| 40 |
// The minimum amount of space that must be present for an axis to be considered scrollable, |
| 41 |
// in pixels. |
| 42 |
private static final float MIN_SCROLLABLE_DISTANCE = 0.5f; |
| 43 |
|
| 44 |
|
| 45 |
// The values we use for friction are based on a 16.6ms frame, adjust them to MS_PER_FRAME: |
| 46 |
// FRICTION^1 = FRICTION_ADJUSTED^(16/MS_PER_FRAME) |
| 47 |
// FRICTION_ADJUSTED = e ^ ((ln(FRICTION))/FRAMERATE_MULTIPLIER) |
| 48 |
static float getFrameAdjustedFriction(float baseFriction) { |
| 49 |
return (float)Math.pow(Math.E, (Math.log(baseFriction) / FRAMERATE_MULTIPLIER)); |
| 50 |
} |
| 51 |
|
| 52 |
private enum FlingStates { |
| 53 |
STOPPED, |
| 54 |
PANNING, |
| 55 |
FLINGING, |
| 56 |
} |
| 57 |
|
| 58 |
private enum Overscroll { |
| 59 |
NONE, |
| 60 |
MINUS, // Overscrolled in the negative direction |
| 61 |
PLUS, // Overscrolled in the positive direction |
| 62 |
BOTH, // Overscrolled in both directions (page is zoomed to smaller than screen) |
| 63 |
} |
| 64 |
|
| 65 |
private int mOverscrollMode; /* Default to only overscrolling if we're allowed to scroll in a direction */ |
| 66 |
private float mFirstTouchPos; /* Position of the first touch event on the current drag. */ |
| 67 |
private float mTouchPos; /* Position of the most recent touch event on the current drag. */ |
| 68 |
private float mLastTouchPos; /* Position of the touch event before touchPos. */ |
| 69 |
private float mVelocity; /* Velocity in this direction; pixels per animation frame. */ |
| 70 |
private boolean mScrollingDisabled; /* Whether movement on this axis is locked. */ |
| 71 |
private boolean mDisableSnap; /* Whether overscroll snapping is disabled. */ |
| 72 |
private float mDisplacement; |
| 73 |
|
| 74 |
private FlingStates mFlingState; /* The fling state we're in on this axis. */ |
| 75 |
|
| 76 |
protected abstract float getOrigin(); |
| 77 |
protected abstract float getViewportLength(); |
| 78 |
protected abstract float getPageStart(); |
| 79 |
protected abstract float getPageLength(); |
| 80 |
|
| 81 |
Axis() { |
| 82 |
mOverscrollMode = View.OVER_SCROLL_IF_CONTENT_SCROLLS; |
| 83 |
} |
| 84 |
|
| 85 |
public int getOverScrollMode() { |
| 86 |
return mOverscrollMode; |
| 87 |
} |
| 88 |
|
| 89 |
private float getViewportEnd() { |
| 90 |
return getOrigin() + getViewportLength(); |
| 91 |
} |
| 92 |
|
| 93 |
private float getPageEnd() { |
| 94 |
return getPageStart() + getPageLength(); |
| 95 |
} |
| 96 |
|
| 97 |
void startTouch(float pos) { |
| 98 |
mVelocity = 0.0f; |
| 99 |
mScrollingDisabled = false; |
| 100 |
mFirstTouchPos = mTouchPos = mLastTouchPos = pos; |
| 101 |
} |
| 102 |
|
| 103 |
float panDistance(float currentPos) { |
| 104 |
return currentPos - mFirstTouchPos; |
| 105 |
} |
| 106 |
|
| 107 |
void setScrollingDisabled(boolean disabled) { |
| 108 |
mScrollingDisabled = disabled; |
| 109 |
} |
| 110 |
|
| 111 |
void saveTouchPos() { |
| 112 |
mLastTouchPos = mTouchPos; |
| 113 |
} |
| 114 |
|
| 115 |
void updateWithTouchAt(float pos, float timeDelta) { |
| 116 |
float newVelocity = (mTouchPos - pos) / timeDelta * MS_PER_FRAME; |
| 117 |
|
| 118 |
// If there's a direction change, or current velocity is very low, |
| 119 |
// allow setting of the velocity outright. Otherwise, use the current |
| 120 |
// velocity and a maximum change factor to set the new velocity. |
| 121 |
boolean curVelocityIsLow = Math.abs(mVelocity) < 1.0f / FRAMERATE_MULTIPLIER; |
| 122 |
boolean directionChange = (mVelocity > 0) != (newVelocity > 0); |
| 123 |
if (curVelocityIsLow || (directionChange && !FloatUtils.fuzzyEquals(newVelocity, 0.0f))) { |
| 124 |
mVelocity = newVelocity; |
| 125 |
} else { |
| 126 |
float maxChange = Math.abs(mVelocity * timeDelta * MAX_EVENT_ACCELERATION); |
| 127 |
mVelocity = Math.min(mVelocity + maxChange, Math.max(mVelocity - maxChange, newVelocity)); |
| 128 |
} |
| 129 |
|
| 130 |
mTouchPos = pos; |
| 131 |
} |
| 132 |
|
| 133 |
boolean overscrolled() { |
| 134 |
return getOverscroll() != Overscroll.NONE; |
| 135 |
} |
| 136 |
|
| 137 |
private Overscroll getOverscroll() { |
| 138 |
boolean minus = (getOrigin() < getPageStart()); |
| 139 |
boolean plus = (getViewportEnd() > getPageEnd()); |
| 140 |
if (minus && plus) { |
| 141 |
return Overscroll.BOTH; |
| 142 |
} else if (minus) { |
| 143 |
return Overscroll.MINUS; |
| 144 |
} else if (plus) { |
| 145 |
return Overscroll.PLUS; |
| 146 |
} else { |
| 147 |
return Overscroll.NONE; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// Returns the amount that the page has been overscrolled. If the page hasn't been |
| 152 |
// overscrolled on this axis, returns 0. |
| 153 |
private float getExcess() { |
| 154 |
switch (getOverscroll()) { |
| 155 |
case MINUS: return getPageStart() - getOrigin(); |
| 156 |
case PLUS: return getViewportEnd() - getPageEnd(); |
| 157 |
case BOTH: return (getViewportEnd() - getPageEnd()) + (getPageStart() - getOrigin()); |
| 158 |
default: return 0.0f; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/* |
| 163 |
* Returns true if the page is zoomed in to some degree along this axis such that scrolling is |
| 164 |
* possible and this axis has not been scroll locked while panning. Otherwise, returns false. |
| 165 |
*/ |
| 166 |
boolean scrollable() { |
| 167 |
// if we are axis locked, return false |
| 168 |
if (mScrollingDisabled) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
// there is scrollable space, and we're not disabled, or the document fits the viewport |
| 173 |
// but we always allow overscroll anyway |
| 174 |
return getViewportLength() <= getPageLength() - MIN_SCROLLABLE_DISTANCE || |
| 175 |
getOverScrollMode() == View.OVER_SCROLL_ALWAYS; |
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* Returns the resistance, as a multiplier, that should be taken into account when |
| 180 |
* tracking or pinching. |
| 181 |
*/ |
| 182 |
float getEdgeResistance(boolean forPinching) { |
| 183 |
float excess = getExcess(); |
| 184 |
if (excess > 0.0f && (getOverscroll() == Overscroll.BOTH || !forPinching)) { |
| 185 |
// excess can be greater than viewport length, but the resistance |
| 186 |
// must never drop below 0.0 |
| 187 |
return Math.max(0.0f, SNAP_LIMIT - excess / getViewportLength()); |
| 188 |
} |
| 189 |
return 1.0f; |
| 190 |
} |
| 191 |
|
| 192 |
/* Returns the velocity. If the axis is locked, returns 0. */ |
| 193 |
float getRealVelocity() { |
| 194 |
return scrollable() ? mVelocity : 0f; |
| 195 |
} |
| 196 |
|
| 197 |
void startPan() { |
| 198 |
mFlingState = FlingStates.PANNING; |
| 199 |
} |
| 200 |
|
| 201 |
void startFling(boolean stopped) { |
| 202 |
mDisableSnap = false; |
| 203 |
|
| 204 |
if (stopped) { |
| 205 |
mFlingState = FlingStates.STOPPED; |
| 206 |
} else { |
| 207 |
mFlingState = FlingStates.FLINGING; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/* Advances a fling animation by one step. */ |
| 212 |
boolean advanceFling() { |
| 213 |
if (mFlingState != FlingStates.FLINGING) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
float excess = getExcess(); |
| 218 |
Overscroll overscroll = getOverscroll(); |
| 219 |
boolean decreasingOverscroll = false; |
| 220 |
if ((overscroll == Overscroll.MINUS && mVelocity > 0) || |
| 221 |
(overscroll == Overscroll.PLUS && mVelocity < 0)) |
| 222 |
{ |
| 223 |
decreasingOverscroll = true; |
| 224 |
} |
| 225 |
|
| 226 |
if (mDisableSnap || FloatUtils.fuzzyEquals(excess, 0.0f) || decreasingOverscroll) { |
| 227 |
// If we aren't overscrolled, just apply friction. |
| 228 |
if (Math.abs(mVelocity) >= VELOCITY_THRESHOLD) { |
| 229 |
mVelocity *= FRICTION_FAST; |
| 230 |
} else { |
| 231 |
float t = mVelocity / VELOCITY_THRESHOLD; |
| 232 |
mVelocity *= FloatUtils.interpolate(FRICTION_SLOW, FRICTION_FAST, t); |
| 233 |
} |
| 234 |
} else { |
| 235 |
// Otherwise, decrease the velocity linearly. |
| 236 |
float elasticity = 1.0f - excess / (getViewportLength() * SNAP_LIMIT); |
| 237 |
if (overscroll == Overscroll.MINUS) { |
| 238 |
mVelocity = Math.min((mVelocity + OVERSCROLL_DECEL_RATE) * elasticity, 0.0f); |
| 239 |
} else { // must be Overscroll.PLUS |
| 240 |
mVelocity = Math.max((mVelocity - OVERSCROLL_DECEL_RATE) * elasticity, 0.0f); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
void stopFling() { |
| 248 |
mVelocity = 0.0f; |
| 249 |
mFlingState = FlingStates.STOPPED; |
| 250 |
} |
| 251 |
|
| 252 |
// Performs displacement of the viewport position according to the current velocity. |
| 253 |
void displace() { |
| 254 |
// if this isn't scrollable just return |
| 255 |
if (!scrollable()) |
| 256 |
return; |
| 257 |
|
| 258 |
if (mFlingState == FlingStates.PANNING) |
| 259 |
mDisplacement += (mLastTouchPos - mTouchPos) * getEdgeResistance(false); |
| 260 |
else |
| 261 |
mDisplacement += mVelocity; |
| 262 |
|
| 263 |
// if overscroll is disabled and we're trying to overscroll, reset the displacement |
| 264 |
// to remove any excess. Using getExcess alone isn't enough here since it relies on |
| 265 |
// getOverscroll which doesn't take into account any new displacement being applied |
| 266 |
if (getOverScrollMode() == View.OVER_SCROLL_NEVER) { |
| 267 |
if (mDisplacement + getOrigin() < getPageStart()) { |
| 268 |
mDisplacement = getPageStart() - getOrigin(); |
| 269 |
stopFling(); |
| 270 |
} else if (mDisplacement + getViewportEnd() > getPageEnd()) { |
| 271 |
mDisplacement = getPageEnd() - getViewportEnd(); |
| 272 |
stopFling(); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
float resetDisplacement() { |
| 278 |
float d = mDisplacement; |
| 279 |
mDisplacement = 0.0f; |
| 280 |
return d; |
| 281 |
} |
| 282 |
} |