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