diff options
author | aelias@chromium.org <aelias@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 06:51:44 +0000 |
---|---|---|
committer | aelias@chromium.org <aelias@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 06:51:44 +0000 |
commit | 69a2a5be71022a23aeef22ee6661cf3e01314326 (patch) | |
tree | 3ad8e48399c0a076a44cfaa5d20f9316b2190f04 /cc | |
parent | 6c2abf95fb4194f567fd35803acc4529f1577003 (diff) | |
download | chromium_src-69a2a5be71022a23aeef22ee6661cf3e01314326.zip chromium_src-69a2a5be71022a23aeef22ee6661cf3e01314326.tar.gz chromium_src-69a2a5be71022a23aeef22ee6661cf3e01314326.tar.bz2 |
cc: Clean up PageScaleAnimation for readability.
No-op change.
BUG=152505
Review URL: https://chromiumcodereview.appspot.com/11364189
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167615 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cc')
-rw-r--r-- | cc/layer_tree_host_impl.cc | 10 | ||||
-rw-r--r-- | cc/layer_tree_host_impl.h | 2 | ||||
-rw-r--r-- | cc/page_scale_animation.cc | 86 | ||||
-rw-r--r-- | cc/page_scale_animation.h | 4 |
4 files changed, 49 insertions, 53 deletions
diff --git a/cc/layer_tree_host_impl.cc b/cc/layer_tree_host_impl.cc index 01afc00..56b16e57 100644 --- a/cc/layer_tree_host_impl.cc +++ b/cc/layer_tree_host_impl.cc @@ -288,7 +288,7 @@ void LayerTreeHostImpl::animate(base::TimeTicks monotonicTime, base::Time wallCl animateScrollbars(monotonicTime); } -void LayerTreeHostImpl::startPageScaleAnimation(gfx::Vector2d targetPosition, bool anchorPoint, float pageScale, base::TimeTicks startTime, base::TimeDelta duration) +void LayerTreeHostImpl::startPageScaleAnimation(gfx::Vector2d targetOffset, bool anchorPoint, float pageScale, base::TimeTicks startTime, base::TimeDelta duration) { if (!m_rootScrollLayerImpl) return; @@ -305,15 +305,15 @@ void LayerTreeHostImpl::startPageScaleAnimation(gfx::Vector2d targetPosition, bo m_pageScaleAnimation = PageScaleAnimation::create(scrollTotal, m_pinchZoomViewport.totalPageScaleFactor(), viewportSize, scaledContentSize, startTimeSeconds); if (anchorPoint) { - gfx::Vector2dF anchor(targetPosition); + gfx::Vector2dF anchor(targetOffset); if (!Settings::pageScalePinchZoomEnabled()) anchor.Scale(1 / pageScale); m_pageScaleAnimation->zoomWithAnchor(anchor, pageScale, duration.InSecondsF()); } else { - gfx::Vector2dF scaledTargetPosition = targetPosition; + gfx::Vector2dF scaledTargetOffset = targetOffset; if (!Settings::pageScalePinchZoomEnabled()) - scaledTargetPosition.Scale(1 / pageScale); - m_pageScaleAnimation->zoomTo(scaledTargetPosition, pageScale, duration.InSecondsF()); + scaledTargetOffset.Scale(1 / pageScale); + m_pageScaleAnimation->zoomTo(scaledTargetOffset, pageScale, duration.InSecondsF()); } m_client->setNeedsRedrawOnImplThread(); diff --git a/cc/layer_tree_host_impl.h b/cc/layer_tree_host_impl.h index 3c9dba7..10020c8 100644 --- a/cc/layer_tree_host_impl.h +++ b/cc/layer_tree_host_impl.h @@ -116,7 +116,7 @@ public: virtual void pinchGestureBegin() OVERRIDE; virtual void pinchGestureUpdate(float, gfx::Point) OVERRIDE; virtual void pinchGestureEnd() OVERRIDE; - virtual void startPageScaleAnimation(gfx::Vector2d targetPosition, bool anchorPoint, float pageScale, base::TimeTicks startTime, base::TimeDelta duration) OVERRIDE; + virtual void startPageScaleAnimation(gfx::Vector2d targetOffset, bool anchorPoint, float pageScale, base::TimeTicks startTime, base::TimeDelta duration) OVERRIDE; virtual void scheduleAnimation() OVERRIDE; struct CC_EXPORT FrameData : public RenderPassSink { diff --git a/cc/page_scale_animation.cc b/cc/page_scale_animation.cc index e5b1fec..e1d5f45 100644 --- a/cc/page_scale_animation.cc +++ b/cc/page_scale_animation.cc @@ -4,19 +4,16 @@ #include "cc/page_scale_animation.h" +#include "base/logging.h" #include "cc/geometry.h" #include "ui/gfx/point_f.h" #include "ui/gfx/rect_f.h" +#include "ui/gfx/vector2d_conversions.h" #include <math.h> namespace { -gfx::PointF toPointF(const gfx::Vector2dF& vector) -{ - return gfx::PointF(vector.x(), vector.y()); -} - // This takes a viewport-relative vector and returns a vector whose values are // between 0 and 1, representing the percentage position within the viewport. gfx::Vector2dF normalizeFromViewport(const gfx::Vector2dF& denormalized, const gfx::SizeF& viewportSize) @@ -31,10 +28,7 @@ gfx::Vector2dF denormalizeToViewport(const gfx::Vector2dF& normalized, const gfx gfx::Vector2dF interpolateBetween(const gfx::Vector2dF& start, const gfx::Vector2dF end, float interp) { - gfx::Vector2dF result; - result.set_x(start.x() + interp * (end.x() - start.x())); - result.set_y(start.y() + interp * (end.y() - start.y())); - return result; + return start + gfx::ScaleVector2d(end - start, interp); } } @@ -97,47 +91,52 @@ void PageScaleAnimation::zoomWithAnchor(const gfx::Vector2dF& anchor, float targ inferTargetAnchorFromScrollOffsets(); } -gfx::SizeF PageScaleAnimation::viewportSizeAtScale(float pageScaleFactor) const -{ - return gfx::ScaleSize(m_viewportSize, 1 / pageScaleFactor); -} - void PageScaleAnimation::inferTargetScrollOffsetFromStartAnchor() { - gfx::Vector2dF anchorRelativeToStartViewport = m_startAnchor - m_startScrollOffset; - gfx::Vector2dF normalized = normalizeFromViewport(anchorRelativeToStartViewport, viewportSizeAtScale(m_startPageScaleFactor)); - m_targetScrollOffset = m_startAnchor - denormalizeToViewport(normalized, viewportSizeAtScale(m_targetPageScaleFactor)); + gfx::Vector2dF normalized = normalizeFromViewport(m_startAnchor - m_startScrollOffset, startViewportSize()); + m_targetScrollOffset = m_startAnchor - denormalizeToViewport(normalized, targetViewportSize()); } void PageScaleAnimation::inferTargetAnchorFromScrollOffsets() { - gfx::RectF startRect(toPointF(m_startScrollOffset), viewportSizeAtScale(m_startPageScaleFactor)); - gfx::RectF targetRect(toPointF(m_targetScrollOffset), viewportSizeAtScale(m_targetPageScaleFactor)); - // The anchor is the point which is at the same normalized relative position - // within both startRect and endRect. For example, a zoom-in double-tap to a - // perfectly centered rect will have normalized anchor (0.5, 0.5), while one - // to a rect touching the bottom-right of the screen will have normalized - // anchor (1.0, 1.0). In other words, it obeys the equations: - // anchorX = start_width * normalizedX + start_x - // anchorX = target_width * normalizedX + target_x - // anchorY = start_height * normalizedY + start_y - // anchorY = target_height * normalizedY + target_y - // where both anchor{x,y} and normalized{x,y} begin as unknowns. Solving - // for the normalized, we get the following formulas: - gfx::Vector2dF normalized; - normalized.set_x((startRect.x() - targetRect.x()) / (targetRect.width() - startRect.width())); - normalized.set_y((startRect.y() - targetRect.y()) / (targetRect.height() - startRect.height())); - m_targetAnchor = m_targetScrollOffset + denormalizeToViewport(normalized, viewportSizeAtScale(m_targetPageScaleFactor)); + // within both start viewport rect and target viewport rect. For example, a + // zoom-in double-tap to a perfectly centered rect will have normalized + // anchor (0.5, 0.5), while one to a rect touching the bottom-right of the + // screen will have normalized anchor (1.0, 1.0). In other words, it obeys + // the equations: + // anchor = start_size * normalized + start_offset + // anchor = target_size * normalized + target_offset + // where both anchor and normalized begin as unknowns. Solving + // for the normalized, we get the following: + float widthScale = 1 / (targetViewportSize().width() - startViewportSize().width()); + float heightScale = 1 / (targetViewportSize().height() - startViewportSize().height()); + gfx::Vector2dF normalized = gfx::ScaleVector2d(m_startScrollOffset - m_targetScrollOffset, widthScale, heightScale); + m_targetAnchor = m_targetScrollOffset + denormalizeToViewport(normalized, targetViewportSize()); } void PageScaleAnimation::clampTargetScrollOffset() { - gfx::Vector2dF maxScrollOffset = BottomRight(gfx::RectF(m_rootLayerSize)) - BottomRight(gfx::RectF(viewportSizeAtScale(m_targetPageScaleFactor))); + gfx::Vector2dF maxScrollOffset = BottomRight(gfx::RectF(m_rootLayerSize)) - BottomRight(gfx::RectF(targetViewportSize())); m_targetScrollOffset.ClampToMin(gfx::Vector2dF()); m_targetScrollOffset.ClampToMax(maxScrollOffset); } +gfx::SizeF PageScaleAnimation::startViewportSize() const +{ + return gfx::ScaleSize(m_viewportSize, 1 / m_startPageScaleFactor); +} + +gfx::SizeF PageScaleAnimation::targetViewportSize() const +{ + return gfx::ScaleSize(m_viewportSize, 1 / m_targetPageScaleFactor); +} + +gfx::SizeF PageScaleAnimation::viewportSizeAt(float interp) const +{ + return gfx::ScaleSize(m_viewportSize, 1 / pageScaleFactorAt(interp)); +} + gfx::Vector2dF PageScaleAnimation::scrollOffsetAtTime(double time) const { return scrollOffsetAt(interpAtTime(time)); @@ -155,6 +154,7 @@ bool PageScaleAnimation::isAnimationCompleteAtTime(double time) const float PageScaleAnimation::interpAtTime(double time) const { + DCHECK_GE(time, m_startTime); if (isAnimationCompleteAtTime(time)) return 1; @@ -174,23 +174,17 @@ gfx::Vector2dF PageScaleAnimation::scrollOffsetAt(float interp) const gfx::Vector2dF PageScaleAnimation::anchorAt(float interp) const { // Interpolate from start to target anchor in absolute space. - gfx::Vector2dF delta = gfx::ScaleVector2d(m_targetAnchor - m_startAnchor, interp); - return m_startAnchor + delta; + return interpolateBetween(m_startAnchor, m_targetAnchor, interp); } gfx::Vector2dF PageScaleAnimation::viewportRelativeAnchorAt(float interp) const { - // Interpolate from start to target anchor in the space relative to the - // viewport at its current scale level. - gfx::Vector2dF anchorRelativeToStartViewport = m_startAnchor - m_startScrollOffset; - gfx::Vector2dF anchorRelativeToTargetViewport = m_targetAnchor - m_targetScrollOffset; - - gfx::Vector2dF startNormalized = normalizeFromViewport(anchorRelativeToStartViewport, viewportSizeAtScale(m_startPageScaleFactor)); - gfx::Vector2dF targetNormalized = normalizeFromViewport(anchorRelativeToTargetViewport, viewportSizeAtScale(m_targetPageScaleFactor)); + // Interpolate from start to target anchor in normalized space. + gfx::Vector2dF startNormalized = normalizeFromViewport(m_startAnchor - m_startScrollOffset, startViewportSize()); + gfx::Vector2dF targetNormalized = normalizeFromViewport(m_targetAnchor - m_targetScrollOffset, targetViewportSize()); gfx::Vector2dF interpNormalized = interpolateBetween(startNormalized, targetNormalized, interp); - gfx::SizeF currentViewportSize = viewportSizeAtScale(pageScaleFactorAt(interp)); - return denormalizeToViewport(interpNormalized, currentViewportSize); + return denormalizeToViewport(interpNormalized, viewportSizeAt(interp)); } float PageScaleAnimation::pageScaleFactorAt(float interp) const diff --git a/cc/page_scale_animation.h b/cc/page_scale_animation.h index 2d8f6d5..fe09d5b 100644 --- a/cc/page_scale_animation.h +++ b/cc/page_scale_animation.h @@ -58,9 +58,11 @@ private: void clampTargetScrollOffset(); void inferTargetScrollOffsetFromStartAnchor(); void inferTargetAnchorFromScrollOffsets(); - gfx::SizeF viewportSizeAtScale(float pageScaleFactor) const; + gfx::SizeF startViewportSize() const; + gfx::SizeF targetViewportSize() const; float interpAtTime(double time) const; + gfx::SizeF viewportSizeAt(float interp) const; gfx::Vector2dF scrollOffsetAt(float interp) const; gfx::Vector2dF anchorAt(float interp) const; gfx::Vector2dF viewportRelativeAnchorAt(float interp) const; |