Bug 831781 - Extract a getAdjustedZoomFactor method to encapsulate the zoom edge resistance. No functional change. r=Cwiiis

This commit is contained in:
Kartikaya Gupta
2013-03-01 22:50:48 -05:00
parent b9f7fe7d92
commit ca06281561

View File

@@ -979,63 +979,65 @@ class JavaPanZoomController
return true;
}
float spanRatio = detector.getCurrentSpan() / prevSpan;
/*
* Apply edge resistance if we're zoomed out smaller than the page size by scaling the zoom
* factor toward 1.0.
*/
float resistance = Math.min(mX.getEdgeResistance(true), mY.getEdgeResistance(true));
if (spanRatio > 1.0f)
spanRatio = 1.0f + (spanRatio - 1.0f) * resistance;
else
spanRatio = 1.0f - (1.0f - spanRatio) * resistance;
synchronized (mTarget.getLock()) {
float newZoomFactor = getMetrics().zoomFactor * spanRatio;
float minZoomFactor = 0.0f;
float maxZoomFactor = MAX_ZOOM;
ZoomConstraints constraints = mTarget.getZoomConstraints();
if (constraints.getMinZoom() > 0)
minZoomFactor = constraints.getMinZoom();
if (constraints.getMaxZoom() > 0)
maxZoomFactor = constraints.getMaxZoom();
if (newZoomFactor < minZoomFactor) {
// apply resistance when zooming past minZoomFactor,
// such that it asymptotically reaches minZoomFactor / 2.0
// but never exceeds that
final float rate = 0.5f; // controls how quickly we approach the limit
float excessZoom = minZoomFactor - newZoomFactor;
excessZoom = 1.0f - (float)Math.exp(-excessZoom * rate);
newZoomFactor = minZoomFactor * (1.0f - excessZoom / 2.0f);
}
if (newZoomFactor > maxZoomFactor) {
// apply resistance when zooming past maxZoomFactor,
// such that it asymptotically reaches maxZoomFactor + 1.0
// but never exceeds that
float excessZoom = newZoomFactor - maxZoomFactor;
excessZoom = 1.0f - (float)Math.exp(-excessZoom);
newZoomFactor = maxZoomFactor + excessZoom;
}
float zoomFactor = getAdjustedZoomFactor(detector.getCurrentSpan() / prevSpan);
scrollBy(mLastZoomFocus.x - detector.getFocusX(),
mLastZoomFocus.y - detector.getFocusY());
PointF focus = new PointF(detector.getFocusX(), detector.getFocusY());
scaleWithFocus(newZoomFactor, focus);
mLastZoomFocus.set(detector.getFocusX(), detector.getFocusY());
ImmutableViewportMetrics target = getMetrics().scaleTo(zoomFactor, mLastZoomFocus);
mTarget.setViewportMetrics(target);
}
mLastZoomFocus.set(detector.getFocusX(), detector.getFocusY());
GeckoEvent event = GeckoEvent.createNativeGestureEvent(GeckoEvent.ACTION_MAGNIFY, mLastZoomFocus, getMetrics().zoomFactor);
GeckoAppShell.sendEventToGecko(event);
return true;
}
private float getAdjustedZoomFactor(float zoomRatio) {
/*
* Apply edge resistance if we're zoomed out smaller than the page size by scaling the zoom
* factor toward 1.0.
*/
float resistance = Math.min(mX.getEdgeResistance(true), mY.getEdgeResistance(true));
if (zoomRatio > 1.0f)
zoomRatio = 1.0f + (zoomRatio - 1.0f) * resistance;
else
zoomRatio = 1.0f - (1.0f - zoomRatio) * resistance;
float newZoomFactor = getMetrics().zoomFactor * zoomRatio;
float minZoomFactor = 0.0f;
float maxZoomFactor = MAX_ZOOM;
ZoomConstraints constraints = mTarget.getZoomConstraints();
if (constraints.getMinZoom() > 0)
minZoomFactor = constraints.getMinZoom();
if (constraints.getMaxZoom() > 0)
maxZoomFactor = constraints.getMaxZoom();
if (newZoomFactor < minZoomFactor) {
// apply resistance when zooming past minZoomFactor,
// such that it asymptotically reaches minZoomFactor / 2.0
// but never exceeds that
final float rate = 0.5f; // controls how quickly we approach the limit
float excessZoom = minZoomFactor - newZoomFactor;
excessZoom = 1.0f - (float)Math.exp(-excessZoom * rate);
newZoomFactor = minZoomFactor * (1.0f - excessZoom / 2.0f);
}
if (newZoomFactor > maxZoomFactor) {
// apply resistance when zooming past maxZoomFactor,
// such that it asymptotically reaches maxZoomFactor + 1.0
// but never exceeds that
float excessZoom = newZoomFactor - maxZoomFactor;
excessZoom = 1.0f - (float)Math.exp(-excessZoom);
newZoomFactor = maxZoomFactor + excessZoom;
}
return newZoomFactor;
}
@Override
public void onScaleEnd(SimpleScaleGestureDetector detector) {
if (mState == PanZoomState.ANIMATED_ZOOM)
@@ -1057,16 +1059,6 @@ class JavaPanZoomController
GeckoAppShell.sendEventToGecko(event);
}
/**
* Scales the viewport, keeping the given focus point in the same place before and after the
* scale operation. You must hold the monitor while calling this.
*/
private void scaleWithFocus(float zoomFactor, PointF focus) {
ImmutableViewportMetrics viewportMetrics = getMetrics();
viewportMetrics = viewportMetrics.scaleTo(zoomFactor, focus);
mTarget.setViewportMetrics(viewportMetrics);
}
@Override
public boolean getRedrawHint() {
switch (mState) {