| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 | 6 |
| 7 #include "cc/page_scale_animation.h" | 7 #include "cc/page_scale_animation.h" |
| 8 | 8 |
| 9 #include "FloatPoint.h" | 9 #include "cc/geometry.h" |
| 10 #include "FloatSize.h" | |
| 11 #include "IntPoint.h" | |
| 12 #include "IntSize.h" | |
| 13 #include "ui/gfx/rect_f.h" | 10 #include "ui/gfx/rect_f.h" |
| 11 #include "ui/gfx/vector2d_conversions.h" |
| 12 #include "ui/gfx/vector2d_f.h" |
| 14 | 13 |
| 15 #include <math.h> | 14 #include <math.h> |
| 16 | 15 |
| 17 namespace cc { | 16 namespace cc { |
| 18 | 17 |
| 19 scoped_ptr<PageScaleAnimation> PageScaleAnimation::create(const IntSize& scrollS
tart, float pageScaleStart, const gfx::Size& windowSize, const gfx::Size& conten
tSize, double startTime) | 18 scoped_ptr<PageScaleAnimation> PageScaleAnimation::create(gfx::Vector2d scrollSt
art, float pageScaleStart, const gfx::Size& windowSize, const gfx::Size& content
Size, double startTime) |
| 20 { | 19 { |
| 21 return make_scoped_ptr(new PageScaleAnimation(scrollStart, pageScaleStart, w
indowSize, contentSize, startTime)); | 20 return make_scoped_ptr(new PageScaleAnimation(scrollStart, pageScaleStart, w
indowSize, contentSize, startTime)); |
| 22 } | 21 } |
| 23 | 22 |
| 24 PageScaleAnimation::PageScaleAnimation(const IntSize& scrollStart, float pageSca
leStart, const gfx::Size& windowSize, const gfx::Size& contentSize, double start
Time) | 23 PageScaleAnimation::PageScaleAnimation(gfx::Vector2d scrollStart, float pageScal
eStart, const gfx::Size& windowSize, const gfx::Size& contentSize, double startT
ime) |
| 25 : m_scrollStart(scrollStart) | 24 : m_scrollStart(scrollStart) |
| 26 , m_pageScaleStart(pageScaleStart) | 25 , m_pageScaleStart(pageScaleStart) |
| 27 , m_windowSize(windowSize) | 26 , m_windowSize(windowSize) |
| 28 , m_contentSize(contentSize) | 27 , m_contentSize(contentSize) |
| 29 , m_anchorMode(false) | 28 , m_anchorMode(false) |
| 30 , m_scrollEnd(scrollStart) | 29 , m_scrollEnd(scrollStart) |
| 31 , m_pageScaleEnd(pageScaleStart) | 30 , m_pageScaleEnd(pageScaleStart) |
| 32 , m_startTime(startTime) | 31 , m_startTime(startTime) |
| 33 , m_duration(0) | 32 , m_duration(0) |
| 34 { | 33 { |
| 35 } | 34 } |
| 36 | 35 |
| 37 PageScaleAnimation::~PageScaleAnimation() | 36 PageScaleAnimation::~PageScaleAnimation() |
| 38 { | 37 { |
| 39 } | 38 } |
| 40 | 39 |
| 41 void PageScaleAnimation::zoomTo(const IntSize& finalScroll, float finalPageScale
, double duration) | 40 void PageScaleAnimation::zoomTo(gfx::Vector2d finalScroll, float finalPageScale,
double duration) |
| 42 { | 41 { |
| 43 if (m_pageScaleStart != finalPageScale) { | 42 if (m_pageScaleStart != finalPageScale) { |
| 44 // For uniform-looking zooming, infer the anchor (point that remains in | 43 // For uniform-looking zooming, infer the anchor (point that remains in |
| 45 // place throughout the zoom) from the start and end rects. | 44 // place throughout the zoom) from the start and end rects. |
| 46 gfx::RectF startRect(cc::FloatPoint(cc::IntPoint(m_scrollStart)), m_wind
owSize); | 45 gfx::RectF startRect(gfx::PointAtOffsetFromOrigin(m_scrollStart), m_wind
owSize); |
| 47 gfx::RectF endRect(cc::FloatPoint(cc::IntPoint(finalScroll)), m_windowSi
ze); | 46 gfx::RectF endRect(gfx::PointAtOffsetFromOrigin(finalScroll), m_windowSi
ze); |
| 48 endRect.Scale(m_pageScaleStart / finalPageScale); | 47 endRect.Scale(m_pageScaleStart / finalPageScale); |
| 49 | 48 |
| 50 // The anchor is the point which is at the same ratio of the sides of | 49 // The anchor is the point which is at the same ratio of the sides of |
| 51 // both startRect and endRect. For example, a zoom-in double-tap to a | 50 // both startRect and endRect. For example, a zoom-in double-tap to a |
| 52 // perfectly centered rect will have anchor ratios (0.5, 0.5), while one | 51 // perfectly centered rect will have anchor ratios (0.5, 0.5), while one |
| 53 // to a rect touching the bottom-right of the screen will have anchor | 52 // to a rect touching the bottom-right of the screen will have anchor |
| 54 // ratios (1.0, 1.0). In other words, it obeys the equations: | 53 // ratios (1.0, 1.0). In other words, it obeys the equations: |
| 55 // anchorX = start_width * ratioX + start_x | 54 // anchorX = start_width * ratioX + start_x |
| 56 // anchorX = end_width * ratioX + end_x | 55 // anchorX = end_width * ratioX + end_x |
| 57 // anchorY = start_height * ratioY + start_y | 56 // anchorY = start_height * ratioY + start_y |
| 58 // anchorY = end_height * ratioY + end_y | 57 // anchorY = end_height * ratioY + end_y |
| 59 // where both anchor{x,y} and ratio{x,y} begin as unknowns. Solving | 58 // where both anchor{x,y} and ratio{x,y} begin as unknowns. Solving |
| 60 // for the ratios, we get the following formulas: | 59 // for the ratios, we get the following formulas: |
| 61 float ratioX = (startRect.x() - endRect.x()) / (endRect.width() - startR
ect.width()); | 60 float ratioX = (startRect.x() - endRect.x()) / (endRect.width() - startR
ect.width()); |
| 62 float ratioY = (startRect.y() - endRect.y()) / (endRect.height() - start
Rect.height()); | 61 float ratioY = (startRect.y() - endRect.y()) / (endRect.height() - start
Rect.height()); |
| 63 | 62 |
| 64 IntSize anchor(m_windowSize.width() * ratioX, m_windowSize.height() * ra
tioY); | 63 gfx::Vector2d anchor(m_windowSize.width() * ratioX, m_windowSize.height(
) * ratioY); |
| 65 zoomWithAnchor(anchor, finalPageScale, duration); | 64 zoomWithAnchor(anchor, finalPageScale, duration); |
| 66 } else { | 65 } else { |
| 67 // If this is a pure translation, then there exists no anchor. Linearly | 66 // If this is a pure translation, then there exists no anchor. Linearly |
| 68 // interpolate the scroll offset instead. | 67 // interpolate the scroll offset instead. |
| 69 m_scrollEnd = finalScroll; | 68 m_scrollEnd = finalScroll; |
| 70 m_pageScaleEnd = finalPageScale; | 69 m_pageScaleEnd = finalPageScale; |
| 71 m_duration = duration; | 70 m_duration = duration; |
| 72 m_anchorMode = false; | 71 m_anchorMode = false; |
| 73 } | 72 } |
| 74 } | 73 } |
| 75 | 74 |
| 76 void PageScaleAnimation::zoomWithAnchor(const IntSize& anchor, float finalPageSc
ale, double duration) | 75 void PageScaleAnimation::zoomWithAnchor(gfx::Vector2d anchor, float finalPageSca
le, double duration) |
| 77 { | 76 { |
| 78 m_scrollEnd = m_scrollStart + anchor; | 77 m_scrollEnd = m_scrollStart + anchor; |
| 79 m_scrollEnd.scale(finalPageScale / m_pageScaleStart); | 78 m_scrollEnd = gfx::ToFlooredVector2d(cc::ScaleVector2d(m_scrollEnd, finalPag
eScale / m_pageScaleStart)); |
| 80 m_scrollEnd -= anchor; | 79 m_scrollEnd -= anchor; |
| 81 | 80 |
| 82 m_scrollEnd.clampNegativeToZero(); | 81 m_scrollEnd = ClampFromBelow(m_scrollEnd, gfx::Vector2d()); |
| 83 gfx::SizeF scaledContentSize = m_contentSize.Scale(finalPageScale / m_pageSc
aleStart); | 82 gfx::SizeF scaledContentSize = m_contentSize.Scale(finalPageScale / m_pageSc
aleStart); |
| 84 IntSize maxScrollPosition = roundedIntSize(cc::FloatSize(scaledContentSize)
- cc::IntSize(m_windowSize)); | 83 gfx::Vector2d maxScrollOffset = gfx::ToRoundedVector2d(BottomRight(gfx::Rect
F(scaledContentSize)) - BottomRight(gfx::Rect(m_windowSize))); |
| 85 m_scrollEnd = m_scrollEnd.shrunkTo(maxScrollPosition); | 84 m_scrollEnd = m_scrollEnd; |
| 85 m_scrollEnd = ClampFromAbove(m_scrollEnd, maxScrollOffset); |
| 86 | 86 |
| 87 m_anchor = anchor; | 87 m_anchor = anchor; |
| 88 m_pageScaleEnd = finalPageScale; | 88 m_pageScaleEnd = finalPageScale; |
| 89 m_duration = duration; | 89 m_duration = duration; |
| 90 m_anchorMode = true; | 90 m_anchorMode = true; |
| 91 } | 91 } |
| 92 | 92 |
| 93 IntSize PageScaleAnimation::scrollOffsetAtTime(double time) const | 93 gfx::Vector2d PageScaleAnimation::scrollOffsetAtTime(double time) const |
| 94 { | 94 { |
| 95 return scrollOffsetAtRatio(progressRatioForTime(time)); | 95 return scrollOffsetAtRatio(progressRatioForTime(time)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 float PageScaleAnimation::pageScaleAtTime(double time) const | 98 float PageScaleAnimation::pageScaleAtTime(double time) const |
| 99 { | 99 { |
| 100 return pageScaleAtRatio(progressRatioForTime(time)); | 100 return pageScaleAtRatio(progressRatioForTime(time)); |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool PageScaleAnimation::isAnimationCompleteAtTime(double time) const | 103 bool PageScaleAnimation::isAnimationCompleteAtTime(double time) const |
| 104 { | 104 { |
| 105 return time >= endTime(); | 105 return time >= endTime(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 float PageScaleAnimation::progressRatioForTime(double time) const | 108 float PageScaleAnimation::progressRatioForTime(double time) const |
| 109 { | 109 { |
| 110 if (isAnimationCompleteAtTime(time)) | 110 if (isAnimationCompleteAtTime(time)) |
| 111 return 1; | 111 return 1; |
| 112 | 112 |
| 113 return (time - m_startTime) / m_duration; | 113 return (time - m_startTime) / m_duration; |
| 114 } | 114 } |
| 115 | 115 |
| 116 IntSize PageScaleAnimation::scrollOffsetAtRatio(float ratio) const | 116 gfx::Vector2d PageScaleAnimation::scrollOffsetAtRatio(float ratio) const |
| 117 { | 117 { |
| 118 if (ratio <= 0) | 118 if (ratio <= 0) |
| 119 return m_scrollStart; | 119 return m_scrollStart; |
| 120 if (ratio >= 1) | 120 if (ratio >= 1) |
| 121 return m_scrollEnd; | 121 return m_scrollEnd; |
| 122 | 122 |
| 123 float currentPageScale = pageScaleAtRatio(ratio); | 123 float currentPageScale = pageScaleAtRatio(ratio); |
| 124 IntSize currentScrollOffset; | 124 gfx::Vector2d currentScrollOffset; |
| 125 if (m_anchorMode) { | 125 if (m_anchorMode) { |
| 126 // Keep the anchor stable on the screen at the current scale. | 126 // Keep the anchor stable on the screen at the current scale. |
| 127 IntSize documentAnchor = m_scrollStart + m_anchor; | 127 gfx::Vector2dF documentAnchor = m_scrollStart + m_anchor; |
| 128 documentAnchor.scale(currentPageScale / m_pageScaleStart); | 128 documentAnchor.Scale(currentPageScale / m_pageScaleStart); |
| 129 currentScrollOffset = documentAnchor - m_anchor; | 129 currentScrollOffset = gfx::ToRoundedVector2d(documentAnchor - m_anchor); |
| 130 } else { | 130 } else { |
| 131 // First move both scroll offsets to the current coordinate space. | 131 // First move both scroll offsets to the current coordinate space. |
| 132 FloatSize scaledStartScroll(m_scrollStart); | 132 gfx::Vector2dF scaledStartScroll(m_scrollStart); |
| 133 scaledStartScroll.scale(currentPageScale / m_pageScaleStart); | 133 scaledStartScroll.Scale(currentPageScale / m_pageScaleStart); |
| 134 FloatSize scaledEndScroll(m_scrollEnd); | 134 gfx::Vector2dF scaledEndScroll(m_scrollEnd); |
| 135 scaledEndScroll.scale(currentPageScale / m_pageScaleEnd); | 135 scaledEndScroll.Scale(currentPageScale / m_pageScaleEnd); |
| 136 | 136 |
| 137 // Linearly interpolate between them. | 137 // Linearly interpolate between them. |
| 138 FloatSize delta = scaledEndScroll - scaledStartScroll; | 138 gfx::Vector2dF delta = scaledEndScroll - scaledStartScroll; |
| 139 delta.scale(ratio); | 139 delta.Scale(ratio); |
| 140 currentScrollOffset = roundedIntSize(scaledStartScroll + delta); | 140 currentScrollOffset = gfx::ToRoundedVector2d(scaledStartScroll + delta); |
| 141 } | 141 } |
| 142 | 142 |
| 143 return currentScrollOffset; | 143 return currentScrollOffset; |
| 144 } | 144 } |
| 145 | 145 |
| 146 float PageScaleAnimation::pageScaleAtRatio(float ratio) const | 146 float PageScaleAnimation::pageScaleAtRatio(float ratio) const |
| 147 { | 147 { |
| 148 if (ratio <= 0) | 148 if (ratio <= 0) |
| 149 return m_pageScaleStart; | 149 return m_pageScaleStart; |
| 150 if (ratio >= 1) | 150 if (ratio >= 1) |
| 151 return m_pageScaleEnd; | 151 return m_pageScaleEnd; |
| 152 | 152 |
| 153 // Linearly interpolate the magnitude in log scale. | 153 // Linearly interpolate the magnitude in log scale. |
| 154 // Log scale is needed to maintain the appearance of uniform zoom. For | 154 // Log scale is needed to maintain the appearance of uniform zoom. For |
| 155 // example, if we zoom from 0.5 to 4.0 in 3 seconds, then we should | 155 // example, if we zoom from 0.5 to 4.0 in 3 seconds, then we should |
| 156 // be zooming by 2x every second. | 156 // be zooming by 2x every second. |
| 157 float diff = m_pageScaleEnd / m_pageScaleStart; | 157 float diff = m_pageScaleEnd / m_pageScaleStart; |
| 158 float logDiff = log(diff); | 158 float logDiff = log(diff); |
| 159 logDiff *= ratio; | 159 logDiff *= ratio; |
| 160 diff = exp(logDiff); | 160 diff = exp(logDiff); |
| 161 return m_pageScaleStart * diff; | 161 return m_pageScaleStart * diff; |
| 162 } | 162 } |
| 163 | 163 |
| 164 } // namespace cc | 164 } // namespace cc |
| OLD | NEW |