1 // Copyright 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CC_INPUT_PAGE_SCALE_ANIMATION_H_ 6 #define CC_INPUT_PAGE_SCALE_ANIMATION_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/time/time.h" 11 #include "ui/gfx/size.h" 12 #include "ui/gfx/vector2d_f.h" 13 14 namespace cc { 15 16 class TimingFunction; 17 18 // A small helper class that does the math for zoom animations, primarily for 19 // double-tap zoom. Initialize it with starting and ending scroll/page scale 20 // positions and an animation length time, then call ...AtTime() at every frame 21 // to obtain the current interpolated position. The supplied timing function 22 // is used to ease the animation. 23 // 24 // All sizes and vectors in this class's public methods are in the root scroll 25 // layer's coordinate space. 26 class PageScaleAnimation { 27 public: 28 // Construct with the state at the beginning of the animation. 29 static scoped_ptr<PageScaleAnimation> Create( 30 const gfx::Vector2dF& start_scroll_offset, 31 float start_page_scale_factor, 32 const gfx::SizeF& viewport_size, 33 const gfx::SizeF& root_layer_size, 34 scoped_ptr<TimingFunction> timing_function); 35 36 ~PageScaleAnimation(); 37 38 // The following methods initialize the animation. Call one of them 39 // immediately after construction to set the final scroll and page scale. 40 41 // Zoom while explicitly specifying the top-left scroll position. 42 void ZoomTo(const gfx::Vector2dF& target_scroll_offset, 43 float target_page_scale_factor, 44 double duration); 45 46 // Zoom based on a specified anchor. The animator will attempt to keep it 47 // at the same position on the physical display throughout the animation, 48 // unless the edges of the root layer are hit. The anchor is specified 49 // as an offset from the content layer. 50 void ZoomWithAnchor(const gfx::Vector2dF& anchor, 51 float target_page_scale_factor, 52 double duration); 53 54 // These should be called before the first frame of animation to initialize 55 // the start time. StartAnimation should only be called once after creation. 56 bool IsAnimationStarted() const; 57 void StartAnimation(base::TimeTicks time); 58 59 // Call these functions while the animation is in progress to output the 60 // current state. 61 gfx::Vector2dF ScrollOffsetAtTime(base::TimeTicks time) const; 62 float PageScaleFactorAtTime(base::TimeTicks time) const; 63 bool IsAnimationCompleteAtTime(base::TimeTicks time) const; 64 65 // The following methods return state which is invariant throughout the 66 // course of the animation. 67 base::TimeTicks start_time() const { return start_time_; } 68 base::TimeDelta duration() const { return duration_; } 69 base::TimeTicks end_time() const { return start_time_ + duration_; } 70 gfx::Vector2dF target_scroll_offset() const { return target_scroll_offset_; } 71 float target_page_scale_factor() const { return target_page_scale_factor_; } 72 73 protected: 74 PageScaleAnimation(const gfx::Vector2dF& start_scroll_offset, 75 float start_page_scale_factor, 76 const gfx::SizeF& viewport_size, 77 const gfx::SizeF& root_layer_size, 78 scoped_ptr<TimingFunction> timing_function); 79 80 private: 81 void ClampTargetScrollOffset(); 82 void InferTargetScrollOffsetFromStartAnchor(); 83 void InferTargetAnchorFromScrollOffsets(); 84 85 gfx::SizeF StartViewportSize() const; 86 gfx::SizeF TargetViewportSize() const; 87 float InterpAtTime(base::TimeTicks time) const; 88 gfx::SizeF ViewportSizeAt(float interp) const; 89 gfx::Vector2dF ScrollOffsetAt(float interp) const; 90 gfx::Vector2dF AnchorAt(float interp) const; 91 gfx::Vector2dF ViewportRelativeAnchorAt(float interp) const; 92 float PageScaleFactorAt(float interp) const; 93 94 float start_page_scale_factor_; 95 float target_page_scale_factor_; 96 gfx::Vector2dF start_scroll_offset_; 97 gfx::Vector2dF target_scroll_offset_; 98 99 gfx::Vector2dF start_anchor_; 100 gfx::Vector2dF target_anchor_; 101 102 gfx::SizeF viewport_size_; 103 gfx::SizeF root_layer_size_; 104 105 base::TimeTicks start_time_; 106 base::TimeDelta duration_; 107 108 scoped_ptr<TimingFunction> timing_function_; 109 110 DISALLOW_COPY_AND_ASSIGN(PageScaleAnimation); 111 }; 112 113 } // namespace cc 114 115 #endif // CC_INPUT_PAGE_SCALE_ANIMATION_H_ 116