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 "cc/base/cc_export.h"
12#include "ui/gfx/size.h"
13#include "ui/gfx/vector2d_f.h"
14
15namespace cc {
16
17class TimingFunction;
18
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.
24//
25// All sizes and vectors in this class's public methods are in the root scroll
26// layer's coordinate space.
27class CC_EXPORT PageScaleAnimation {
28 public:
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);
36
37  ~PageScaleAnimation();
38
39  // The following methods initialize the animation. Call one of them
40  // immediately after construction to set the final scroll and page scale.
41
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,
45              double duration);
46
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,
53                      double duration);
54
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);
59
60  // Call these functions while the animation is in progress to output the
61  // current state.
62  gfx::Vector2dF ScrollOffsetAtTime(base::TimeTicks time) const;
63  float PageScaleFactorAtTime(base::TimeTicks time) const;
64  bool IsAnimationCompleteAtTime(base::TimeTicks time) const;
65
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_; }
73
74 protected:
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);
80
81 private:
82  void ClampTargetScrollOffset();
83  void InferTargetScrollOffsetFromStartAnchor();
84  void InferTargetAnchorFromScrollOffsets();
85
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;
94
95  float start_page_scale_factor_;
96  float target_page_scale_factor_;
97  gfx::Vector2dF start_scroll_offset_;
98  gfx::Vector2dF target_scroll_offset_;
99
100  gfx::Vector2dF start_anchor_;
101  gfx::Vector2dF target_anchor_;
102
103  gfx::SizeF viewport_size_;
104  gfx::SizeF root_layer_size_;
105
106  base::TimeTicks start_time_;
107  base::TimeDelta duration_;
108
109  scoped_ptr<TimingFunction> timing_function_;
110
111  DISALLOW_COPY_AND_ASSIGN(PageScaleAnimation);
112};
113
114}  // namespace cc
115
116#endif  // CC_INPUT_PAGE_SCALE_ANIMATION_H_
117