summaryrefslogtreecommitdiffstats
path: root/ui/gfx/compositor/layer_animator.h
blob: 52b8870b66c4ddb43faa696f24652619ac704617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_
#define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_
#pragma once

#include <map>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/skia/include/core/SkScalar.h"
#include "third_party/skia/include/utils/SkMatrix44.h"
#include "ui/base/animation/animation_delegate.h"
#include "ui/gfx/compositor/compositor_export.h"

namespace gfx {
class Point;
}

namespace ui {

class Animation;
class Layer;
class LayerAnimatorDelegate;
class Transform;

// LayerAnimator manages animating various properties of a Layer.
class COMPOSITOR_EXPORT LayerAnimator : public ui::AnimationDelegate {
 public:
  // Types of properties that can be animated.
  enum AnimationProperty {
    LOCATION,
    OPACITY,
    TRANSFORM,
  };

  explicit LayerAnimator(Layer* layer);
  virtual ~LayerAnimator();

  // Sets the animation to use. LayerAnimator takes ownership of the animation.
  void SetAnimation(Animation* animation);

  ui::Layer* layer() { return layer_; }

  // Animates the layer to the specified point. The point is relative to the
  // parent layer.
  void AnimateToPoint(const gfx::Point& target);

  // Animates the transform from the current transform to |transform|.
  void AnimateTransform(const Transform& transform);

  // Animates the opacity from the current opacity to |target_opacity|.
  void AnimateOpacity(float target_opacity);

  // Returns the target value for the specified type. If the specified property
  // is not animating, the current value is returned.
  gfx::Point GetTargetPoint();
  float GetTargetOpacity();
  ui::Transform GetTargetTransform();

  // Returns true if animating |property|.
  bool IsAnimating(AnimationProperty property) const;

  // Returns true if the animation is running.
  bool IsRunning() const;

  // Returns true if the animation has progressed at least once since
  // SetAnimation() was invoked.
  bool got_initial_tick() const { return got_initial_tick_; }

  // AnimationDelegate:
  virtual void AnimationProgressed(const Animation* animation) OVERRIDE;
  virtual void AnimationEnded(const Animation* animation) OVERRIDE;

 private:
  // Parameters used when animating the location.
  struct LocationParams {
    int start_x;
    int start_y;
    int target_x;
    int target_y;
  };

  // Parameters used when animating the transform.
  struct TransformParams {
    SkMScalar start[16];
    SkMScalar target[16];
  };

  // Parameters used when animating the opacity.
  struct OpacityParams {
    float start;
    float target;
  };

  union Params {
    LocationParams location;
    OpacityParams opacity;
    TransformParams transform;
  };

  typedef std::map<AnimationProperty, Params> Elements;

  // Stops animating the specified property. This does not set the property
  // being animated to its final value.
  void StopAnimating(AnimationProperty property);

  LayerAnimatorDelegate* delegate();

  // The layer.
  Layer* layer_;

  // Properties being animated.
  Elements elements_;

  scoped_ptr<ui::Animation> animation_;

  bool got_initial_tick_;

  DISALLOW_COPY_AND_ASSIGN(LayerAnimator);
};

}  // namespace ui

#endif  // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_