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