Bug 701594 - Part 3: Add viewport interpolation functions. r=kats

This commit is contained in:
Patrick Walton 2011-12-07 10:44:36 -08:00
parent b7fdca35f8
commit 28eb691095
4 changed files with 67 additions and 0 deletions

View File

@ -69,5 +69,14 @@ public class FloatSize {
public FloatSize scale(float factor) {
return new FloatSize(width * factor, height * factor);
}
/*
* Returns the size that represents a linear transition between this size and `to` at time `t`,
* which is on the scale [0, 1).
*/
public FloatSize interpolate(FloatSize to, float t) {
return new FloatSize(FloatUtils.interpolate(width, to.width, t),
FloatUtils.interpolate(height, to.height, t));
}
}

View File

@ -37,6 +37,7 @@
package org.mozilla.gecko.gfx;
import org.mozilla.gecko.FloatUtils;
import android.graphics.Rect;
import android.graphics.RectF;
import org.json.JSONException;
@ -118,4 +119,15 @@ public final class RectUtils {
float y = Math.max(dest.top, Math.min(dest.bottom-height, rect.top));
return new RectF(x, y, x+width, y+height);
}
/*
* Returns the rect that represents a linear transition between `from` and `to` at time `t`,
* which is on the scale [0, 1).
*/
public static RectF interpolate(RectF from, RectF to, float t) {
return new RectF(FloatUtils.interpolate(from.left, to.left, t),
FloatUtils.interpolate(from.top, to.top, t),
FloatUtils.interpolate(from.right, to.right, t),
FloatUtils.interpolate(from.bottom, to.bottom, t));
}
}

View File

@ -42,6 +42,7 @@ import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import org.mozilla.gecko.FloatUtils;
import org.mozilla.gecko.gfx.FloatSize;
import org.mozilla.gecko.gfx.LayerController;
import org.mozilla.gecko.gfx.RectUtils;
@ -216,6 +217,20 @@ public class ViewportMetrics {
mZoomFactor = newZoomFactor;
}
/*
* Returns the viewport metrics that represent a linear transition between `from` and `to` at
* time `t`, which is on the scale [0, 1). This function interpolates the viewport rect, the
* page size, the offset, and the zoom factor.
*/
public ViewportMetrics interpolate(ViewportMetrics to, float t) {
ViewportMetrics result = new ViewportMetrics();
result.mPageSize = mPageSize.interpolate(to.mPageSize, t);
result.mZoomFactor = FloatUtils.interpolate(mZoomFactor, to.mZoomFactor, t);
result.mViewportRect = RectUtils.interpolate(mViewportRect, to.mViewportRect, t);
result.mViewportOffset = PointUtils.interpolate(mViewportOffset, to.mViewportOffset, t);
return result;
}
public String toJSON() {
return "{ \"x\" : " + mViewportRect.left +
", \"y\" : " + mViewportRect.top +

View File

@ -43,6 +43,7 @@ import org.mozilla.gecko.gfx.FloatSize;
import org.mozilla.gecko.gfx.LayerController;
import org.mozilla.gecko.gfx.PointUtils;
import org.mozilla.gecko.gfx.RectUtils;
import org.mozilla.gecko.gfx.ViewportMetrics;
import org.mozilla.gecko.FloatUtils;
import org.mozilla.gecko.GeckoApp;
import org.mozilla.gecko.GeckoAppShell;
@ -752,6 +753,36 @@ public class PanZoomController
}
}
/* Returns the nearest viewport metrics with no overscroll visible. */
private ViewportMetrics getValidViewportMetrics() {
ViewportMetrics viewportMetrics = new ViewportMetrics(mController.getViewportMetrics());
/* First, we adjust the zoom factor so that we can make no overscrolled area visible. */
float zoomFactor = viewportMetrics.getZoomFactor();
FloatSize pageSize = viewportMetrics.getPageSize();
RectF viewport = viewportMetrics.getViewport();
float minZoomFactor = 0.0f;
if (viewport.width() > pageSize.width) {
float scaleFactor = viewport.width() / pageSize.width;
minZoomFactor = (float)Math.max(minZoomFactor, zoomFactor * scaleFactor);
}
if (viewport.height() > pageSize.height) {
float scaleFactor = viewport.height() / pageSize.height;
minZoomFactor = (float)Math.max(minZoomFactor, zoomFactor * scaleFactor);
}
if (!FloatUtils.fuzzyEquals(minZoomFactor, 0.0f)) {
PointF center = new PointF(viewport.width() / 2.0f, viewport.height() / 2.0f);
viewportMetrics.scaleTo(minZoomFactor, center);
}
/* Now we pan to the right origin. */
viewportMetrics.setViewport(viewportMetrics.getClampedViewport());
return viewportMetrics;
}
private class AxisX extends Axis {
@Override
public float getOrigin() { return mController.getOrigin().x; }