overscroll_glow.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1// Copyright (c) 2012 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 CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_
6#define CONTENT_BROWSER_ANDROID_OVERSCROLL_GLOW_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/time/time.h"
11#include "content/browser/android/edge_effect.h"
12#include "ui/gfx/size_f.h"
13#include "ui/gfx/vector2d_f.h"
14
15class SkBitmap;
16
17namespace cc {
18class Layer;
19}
20
21namespace content {
22
23/* |OverscrollGlow| mirrors its Android counterpart, EdgeEffect.java.
24 * Conscious tradeoffs were made to align this as closely as possible with the
25 * original Android java version.
26 */
27class OverscrollGlow {
28 public:
29  // Create and initialize a new effect with the necessary resources.
30  // If |enabled| is false, the effect will be be deactivated until
31  // SetEnabled(true) is called.
32  // The caller should attach |root_layer| to the desired layer tree.
33  static scoped_ptr<OverscrollGlow> Create(bool enabled);
34
35  // Force loading of any necessary resources.  This function is thread-safe.
36  static void EnsureResources();
37
38  ~OverscrollGlow();
39
40  // If false, the glow will be deactivated, and subsequent calls to
41  // OnOverscrolled or Animate will have no effect.
42  void SetEnabled(bool enabled);
43
44  // |overscroll| is the accumulated overscroll for the current gesture.
45  // |velocity| is the instantaneous velocity for the overscroll.
46  void OnOverscrolled(base::TimeTicks current_time,
47                      gfx::Vector2dF overscroll,
48                      gfx::Vector2dF velocity);
49
50  // Returns true if the effect still needs animation ticks.
51  bool Animate(base::TimeTicks current_time);
52
53  // Returns true if the effect needs animation ticks.
54  bool NeedsAnimate() const;
55
56  // The root layer of the effect (not necessarily of the tree).
57  scoped_refptr<cc::Layer> root_layer() const {
58    return root_layer_;
59  }
60
61  // Horizontal overscroll will be ignored when false.
62  void set_horizontal_overscroll_enabled(bool enabled) {
63    horizontal_overscroll_enabled_ = enabled;
64  }
65  // Vertical overscroll will be ignored when false.
66  void set_vertical_overscroll_enabled(bool enabled) {
67    vertical_overscroll_enabled_ = enabled;
68  }
69  // The size of the layer for which edges will be animated.
70  void set_size(gfx::SizeF size) {
71    size_ = size;
72  }
73
74 private:
75  enum Axis { AXIS_X, AXIS_Y };
76
77  OverscrollGlow(bool enabled, const SkBitmap& edge, const SkBitmap& glow);
78
79  void Pull(base::TimeTicks current_time,
80            gfx::Vector2dF added_overscroll);
81  void Absorb(base::TimeTicks current_time,
82              gfx::Vector2dF velocity,
83              gfx::Vector2dF overscroll,
84              gfx::Vector2dF old_overscroll);
85
86  void Release(base::TimeTicks current_time);
87  void Release(Axis axis, base::TimeTicks current_time);
88
89  EdgeEffect* GetOppositeEdge(int edge_index);
90
91  scoped_ptr<EdgeEffect> edge_effects_[EdgeEffect::EDGE_COUNT];
92
93  bool enabled_;
94  gfx::SizeF size_;
95  gfx::Vector2dF old_overscroll_;
96  gfx::Vector2dF old_velocity_;
97  bool horizontal_overscroll_enabled_;
98  bool vertical_overscroll_enabled_;
99
100  scoped_refptr<cc::Layer> root_layer_;
101
102  DISALLOW_COPY_AND_ASSIGN(OverscrollGlow);
103};
104
105}  // namespace content
106
107#endif  // CONTENT_BROWSER_ANDROID_SCROLL_GLOW_H_
108