Bug 716673 - Encapsulate the velocity, locked, and disableSnap variables in Axis. r=pcwalton a=akeybl
authorKartikaya Gupta <kgupta@mozilla.com>
Tue, 10 Jan 2012 10:06:01 -0500
changeset 84994 5d9ada0705dfa58e12a3b1e14a187c9aaad5b32f
parent 84993 6ce2cc8bfbea88b1fe99e4630aa6c7db8fe1336a
child 84995 b4ec0b5d9b5a9ca9992336aead4ac3733b30c042
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspcwalton, akeybl
bugs716673
milestone11.0a2
Bug 716673 - Encapsulate the velocity, locked, and disableSnap variables in Axis. r=pcwalton a=akeybl
mobile/android/base/ui/PanZoomController.java
--- a/mobile/android/base/ui/PanZoomController.java
+++ b/mobile/android/base/ui/PanZoomController.java
@@ -231,17 +231,18 @@ public class PanZoomController
     @SuppressWarnings("fallthrough")
     public void abortAnimation() {
         // this happens when gecko changes the viewport on us or if the device is rotated.
         // if that's the case, abort any animation in progress and re-zoom so that the page
         // snaps to edges. for other cases (where the user's finger(s) are down) don't do
         // anything special.
         switch (mState) {
         case FLING:
-            mX.velocity = mY.velocity = 0.0f;
+            mX.stopFling();
+            mY.stopFling();
             mState = PanZoomState.NOTHING;
             // fall through
         case ANIMATED_ZOOM:
             // the zoom that's in progress likely makes no sense any more (such as if
             // the screen orientation changed) so abort it
             // fall through
         case NOTHING:
             // Don't do animations here; they're distracting and can cause flashes on page
@@ -387,28 +388,28 @@ public class PanZoomController
         mLastEventTime = time;
 
         if (mState == PanZoomState.PANNING_LOCKED) {
             // check to see if we should break the axis lock
             double angle = Math.atan2(mY.panDistance(y), mX.panDistance(x)); // range [-pi, pi]
             angle = Math.abs(angle); // range [0, pi]
             if (angle < AXIS_LOCK_ANGLE || angle > (Math.PI - AXIS_LOCK_ANGLE)) {
                 // lock to x-axis
-                mX.locked = false;
-                mY.locked = true;
+                mX.setLocked(false);
+                mY.setLocked(true);
             } else if (Math.abs(angle - (Math.PI / 2)) < AXIS_LOCK_ANGLE) {
                 // lock to y-axis
-                mX.locked = true;
-                mY.locked = false;
+                mX.setLocked(true);
+                mY.setLocked(false);
             } else {
                 // break axis lock but log the angle so we can fine-tune this when people complain
                 mState = PanZoomState.PANNING;
-                mX.locked = mY.locked = false;
+                mX.setLocked(false);
+                mY.setLocked(false);
                 angle = Math.abs(angle - (Math.PI / 2));  // range [0, pi/2]
-                Log.i(LOGTAG, "Breaking axis lock at " + (angle * 180.0 / Math.PI) + " degrees");
             }
         }
 
         mX.updateWithTouchAt(x, timeDelta);
         mY.updateWithTouchAt(y, timeDelta);
     }
 
     private void track(MotionEvent event) {
@@ -435,26 +436,24 @@ public class PanZoomController
         }
 
         mX.setFlingState(Axis.FlingStates.PANNING); mY.setFlingState(Axis.FlingStates.PANNING);
         mX.displace(mOverridePanning); mY.displace(mOverridePanning);
         updatePosition();
     }
 
     private void fling() {
-        mX.disableSnap = mY.disableSnap = mOverridePanning;
-
         mX.displace(mOverridePanning); mY.displace(mOverridePanning);
         updatePosition();
 
         stopAnimationTimer();
 
         boolean stopped = stopped();
-        mX.startFling(stopped);
-        mY.startFling(stopped);
+        mX.startFling(stopped, mOverridePanning);
+        mY.startFling(stopped, mOverridePanning);
 
         startAnimationTimer(new FlingRunnable());
     }
 
     /* Performs a bounce-back animation to the given viewport metrics. */
     private void bounce(ViewportMetrics metrics) {
         stopAnimationTimer();
 
@@ -701,19 +700,19 @@ public class PanZoomController
             MINUS,      // Overscrolled in the negative direction
             PLUS,       // Overscrolled in the positive direction
             BOTH,       // Overscrolled in both directions (page is zoomed to smaller than screen)
         }
 
         private float firstTouchPos;            /* Position of the first touch event on the current drag. */
         private float touchPos;                 /* Position of the most recent touch event on the current drag. */
         private float lastTouchPos;             /* Position of the touch event before touchPos. */
-        public float velocity;                  /* Velocity in this direction. */
-        public boolean locked;                  /* Whether movement on this axis is locked. */
-        public boolean disableSnap;             /* Whether overscroll snapping is disabled. */
+        private float velocity;                  /* Velocity in this direction. */
+        private boolean locked;                  /* Whether movement on this axis is locked. */
+        private boolean disableSnap;             /* Whether overscroll snapping is disabled. */
 
         private FlingStates mFlingState;        /* The fling state we're in on this axis. */
 
         public abstract float getOrigin();
         protected abstract float getViewportLength();
         protected abstract float getPageLength();
 
         public float displacement;
@@ -733,16 +732,20 @@ public class PanZoomController
             locked = false;
             firstTouchPos = touchPos = lastTouchPos = pos;
         }
 
         float panDistance(float currentPos) {
             return currentPos - firstTouchPos;
         }
 
+        void setLocked(boolean locked) {
+            this.locked = locked;
+        }
+
         void saveTouchPos() {
             lastTouchPos = touchPos;
         }
 
         void updateWithTouchAt(float pos, float timeDelta) {
             float newVelocity = (touchPos - pos) / timeDelta * MS_PER_FRAME;
 
             // If there's a direction change, or current velocity is very low,
@@ -805,17 +808,19 @@ public class PanZoomController
             return (excess > 0.0f) ? SNAP_LIMIT - excess / getViewportLength() : 1.0f;
         }
 
         /* Returns the velocity. If the axis is locked, returns 0. */
         public float getRealVelocity() {
             return locked ? 0.0f : velocity;
         }
 
-        public void startFling(boolean stopped) {
+        public void startFling(boolean stopped, boolean panningOverridden) {
+            disableSnap = panningOverridden;
+
             if (stopped) {
                 setFlingState(FlingStates.STOPPED);
             } else {
                 setFlingState(FlingStates.FLINGING);
             }
         }
 
         /* Advances a fling animation by one step. */