1// Copyright (c) 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 UI_GFX_ANIMATION_MULTI_ANIMATION_H_
6#define UI_GFX_ANIMATION_MULTI_ANIMATION_H_
7
8#include <vector>
9
10#include "ui/gfx/animation/animation.h"
11#include "ui/gfx/animation/tween.h"
12
13namespace gfx {
14
15// MultiAnimation is an animation that consists of a number of sub animations.
16// To create a MultiAnimation pass in the parts, invoke Start() and the delegate
17// is notified as the animation progresses. By default MultiAnimation runs until
18// Stop is invoked, see |set_continuous()| for details.
19class GFX_EXPORT MultiAnimation : public Animation {
20 public:
21  // Defines part of the animation. Each part consists of the following:
22  //
23  // time_ms: the time of the part.
24  // start_time_ms: the amount of time to offset this part by when calculating
25  // the percented completed.
26  // end_time_ms: the end time used to calculate the percentange completed.
27  //
28  // In most cases |start_time_ms| = 0 and |end_time_ms| = |time_ms|. But you
29  // can adjust the start/end for different effects. For example, to run a part
30  // for 200ms with a % between .25 and .75 use the following three values: 200,
31  // 100, 400.
32  struct Part {
33    Part() : time_ms(0), start_time_ms(0), end_time_ms(0), type(Tween::ZERO) {}
34    Part(int time_ms, Tween::Type type)
35        : time_ms(time_ms),
36          start_time_ms(0),
37          end_time_ms(time_ms),
38          type(type) {}
39
40    int time_ms;
41    int start_time_ms;
42    int end_time_ms;
43    Tween::Type type;
44  };
45
46  typedef std::vector<Part> Parts;
47
48  MultiAnimation(const Parts& parts, base::TimeDelta timer_interval);
49  virtual ~MultiAnimation();
50
51  // Default interval.
52  static base::TimeDelta GetDefaultTimerInterval();
53
54  // Sets whether the animation continues after it reaches the end. If true, the
55  // animation runs until explicitly stopped. The default is true.
56  void set_continuous(bool continuous) { continuous_ = continuous; }
57
58  // Returns the current value. The current value for a MultiAnimation is
59  // determined from the tween type of the current part.
60  virtual double GetCurrentValue() const OVERRIDE;
61
62  // Returns the index of the current part.
63  size_t current_part_index() const { return current_part_index_; }
64
65 protected:
66  // Animation overrides.
67  virtual void Step(base::TimeTicks time_now) OVERRIDE;
68  virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE;
69
70 private:
71  // Returns the part containing the specified time. |time_ms| is reset to be
72  // relative to the part containing the time and |part_index| the index of the
73  // part.
74  const Part& GetPart(int* time_ms, size_t* part_index);
75
76  // The parts that make up the animation.
77  const Parts parts_;
78
79  // Total time of all the parts.
80  const int cycle_time_ms_;
81
82  // Current value for the animation.
83  double current_value_;
84
85  // Index of the current part.
86  size_t current_part_index_;
87
88  // See description above setter.
89  bool continuous_;
90
91  DISALLOW_COPY_AND_ASSIGN(MultiAnimation);
92};
93
94}  // namespace gfx
95
96#endif  // UI_GFX_ANIMATION_MULTI_ANIMATION_H_
97