Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / animation / multi_animation.h
blob9e60b66edefc0e95b3114437a7155a5c1d513e39
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.
5 #ifndef UI_GFX_ANIMATION_MULTI_ANIMATION_H_
6 #define UI_GFX_ANIMATION_MULTI_ANIMATION_H_
8 #include <vector>
10 #include "ui/gfx/animation/animation.h"
11 #include "ui/gfx/animation/tween.h"
13 namespace gfx {
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.
19 class GFX_EXPORT MultiAnimation : public Animation {
20 public:
21 // Defines part of the animation. Each part consists of the following:
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.
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) {}
40 int time_ms;
41 int start_time_ms;
42 int end_time_ms;
43 Tween::Type type;
46 typedef std::vector<Part> Parts;
48 MultiAnimation(const Parts& parts, base::TimeDelta timer_interval);
49 ~MultiAnimation() override;
51 // Default interval.
52 static base::TimeDelta GetDefaultTimerInterval();
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; }
58 // Returns the current value. The current value for a MultiAnimation is
59 // determined from the tween type of the current part.
60 double GetCurrentValue() const override;
62 // Returns the index of the current part.
63 size_t current_part_index() const { return current_part_index_; }
65 protected:
66 // Animation overrides.
67 void Step(base::TimeTicks time_now) override;
68 void SetStartTime(base::TimeTicks start_time) override;
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);
76 // The parts that make up the animation.
77 const Parts parts_;
79 // Total time of all the parts.
80 const int cycle_time_ms_;
82 // Current value for the animation.
83 double current_value_;
85 // Index of the current part.
86 size_t current_part_index_;
88 // See description above setter.
89 bool continuous_;
91 DISALLOW_COPY_AND_ASSIGN(MultiAnimation);
94 } // namespace gfx
96 #endif // UI_GFX_ANIMATION_MULTI_ANIMATION_H_