[DevTools] Emulation domain implementation (browser side).
[chromium-blink-merge.git] / cc / input / page_scale_animation.h
blob0e430c212fe805854be09c049170934999e8ab1c
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/geometry/size.h"
13 #include "ui/gfx/geometry/vector2d.h"
14 #include "ui/gfx/geometry/vector2d_f.h"
16 namespace cc {
18 class TimingFunction;
20 // Used in the CC to pass around a scale animation that hasn't yet been
21 // initialized.
22 struct PendingPageScaleAnimation {
23 PendingPageScaleAnimation(
24 const gfx::Vector2d _target_offset,
25 bool _use_anchor,
26 float _scale,
27 const base::TimeDelta& _duration)
28 : target_offset(_target_offset),
29 use_anchor(_use_anchor),
30 scale(_scale),
31 duration(_duration) {}
32 gfx::Vector2d target_offset;
33 bool use_anchor;
34 float scale;
35 base::TimeDelta duration;
38 // A small helper class that does the math for zoom animations, primarily for
39 // double-tap zoom. Initialize it with starting and ending scroll/page scale
40 // positions and an animation length time, then call ...AtTime() at every frame
41 // to obtain the current interpolated position. The supplied timing function
42 // is used to ease the animation.
44 // All sizes and vectors in this class's public methods are in the root scroll
45 // layer's coordinate space.
46 class CC_EXPORT PageScaleAnimation {
47 public:
48 // Construct with the state at the beginning of the animation.
49 static scoped_ptr<PageScaleAnimation> Create(
50 const gfx::Vector2dF& start_scroll_offset,
51 float start_page_scale_factor,
52 const gfx::SizeF& viewport_size,
53 const gfx::SizeF& root_layer_size,
54 scoped_ptr<TimingFunction> timing_function);
56 ~PageScaleAnimation();
58 // The following methods initialize the animation. Call one of them
59 // immediately after construction to set the final scroll and page scale.
61 // Zoom while explicitly specifying the top-left scroll position.
62 void ZoomTo(const gfx::Vector2dF& target_scroll_offset,
63 float target_page_scale_factor,
64 double duration);
66 // Zoom based on a specified anchor. The animator will attempt to keep it
67 // at the same position on the physical display throughout the animation,
68 // unless the edges of the root layer are hit. The anchor is specified
69 // as an offset from the content layer.
70 void ZoomWithAnchor(const gfx::Vector2dF& anchor,
71 float target_page_scale_factor,
72 double duration);
74 // These should be called before the first frame of animation to initialize
75 // the start time. StartAnimation should only be called once after creation.
76 bool IsAnimationStarted() const;
77 void StartAnimation(base::TimeTicks time);
79 // Call these functions while the animation is in progress to output the
80 // current state.
81 gfx::Vector2dF ScrollOffsetAtTime(base::TimeTicks time) const;
82 float PageScaleFactorAtTime(base::TimeTicks time) const;
83 bool IsAnimationCompleteAtTime(base::TimeTicks time) const;
85 // The following methods return state which is invariant throughout the
86 // course of the animation.
87 base::TimeTicks start_time() const { return start_time_; }
88 base::TimeDelta duration() const { return duration_; }
89 base::TimeTicks end_time() const { return start_time_ + duration_; }
90 gfx::Vector2dF target_scroll_offset() const { return target_scroll_offset_; }
91 float target_page_scale_factor() const { return target_page_scale_factor_; }
93 protected:
94 PageScaleAnimation(const gfx::Vector2dF& start_scroll_offset,
95 float start_page_scale_factor,
96 const gfx::SizeF& viewport_size,
97 const gfx::SizeF& root_layer_size,
98 scoped_ptr<TimingFunction> timing_function);
100 private:
101 void ClampTargetScrollOffset();
102 void InferTargetScrollOffsetFromStartAnchor();
103 void InferTargetAnchorFromScrollOffsets();
105 gfx::SizeF StartViewportSize() const;
106 gfx::SizeF TargetViewportSize() const;
107 float InterpAtTime(base::TimeTicks time) const;
108 gfx::SizeF ViewportSizeAt(float interp) const;
109 gfx::Vector2dF ScrollOffsetAt(float interp) const;
110 gfx::Vector2dF AnchorAt(float interp) const;
111 gfx::Vector2dF ViewportRelativeAnchorAt(float interp) const;
112 float PageScaleFactorAt(float interp) const;
114 float start_page_scale_factor_;
115 float target_page_scale_factor_;
116 gfx::Vector2dF start_scroll_offset_;
117 gfx::Vector2dF target_scroll_offset_;
119 gfx::Vector2dF start_anchor_;
120 gfx::Vector2dF target_anchor_;
122 gfx::SizeF viewport_size_;
123 gfx::SizeF root_layer_size_;
125 base::TimeTicks start_time_;
126 base::TimeDelta duration_;
128 scoped_ptr<TimingFunction> timing_function_;
130 DISALLOW_COPY_AND_ASSIGN(PageScaleAnimation);
133 } // namespace cc
135 #endif // CC_INPUT_PAGE_SCALE_ANIMATION_H_