Mercurial > projects > dynamin
annotate dynamin/core/animated_value.d @ 103:73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 22:06:02 -0500 |
parents | aa4efef0f0b1 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
5 * Distributed under the Boost Software License, Version 1.0. |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
6 * (See accompanying file BOOST_LICENSE.txt or copy at |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.animated_value; | |
12 | |
13 /// AnimatedValue!(int) | |
14 alias AnimatedValue!(int) AnimatedInt; | |
15 /// AnimatedValue!(float) | |
16 alias AnimatedValue!(float) AnimatedFloat; | |
17 /// AnimatedValue!(double) | |
18 alias AnimatedValue!(double) AnimatedDouble; | |
19 /// AnimatedValue!(real) | |
20 alias AnimatedValue!(real) AnimatedReal; | |
21 | |
22 // TODO: change to a struct? | |
23 /** | |
24 * Holds a value that changes over time. The value can be changed by calling | |
25 * animate(), then calling advance() with an amount of time. | |
26 * Example: | |
27 * ----- | |
28 * AnimatedInt x = new AnimatedInt; | |
29 * x.set(100); /+ start the animation at 100 +/ | |
30 * x.animate(900, 1000); /+ animate to 900, over a period of 1000 ms +/ | |
31 * x.advance(250); /+ advance 250 ms, one-fourth of the way to 1000 +/ | |
32 * x.get(); /+ returns 300, one-fourth of the way between 100 and 900 +/ | |
33 * x.advance(2000); /+ advance past the end of the animation +/ | |
34 * x.get(); /+ returns 900 +/ | |
35 * ----- | |
36 */ | |
37 class AnimatedValue(T) { | |
38 T _value = 0; | |
39 T _startValue, _endValue; | |
40 int _elapsed, _duration; | |
41 bool _animating; | |
42 T get() { | |
43 return _value; | |
44 } | |
45 T end() { | |
46 return _endValue; | |
47 } | |
48 AnimatedValue set(T newVal) { | |
49 _value = newVal; | |
50 _endValue = _value; | |
51 _animating = false; | |
52 return this; | |
53 } | |
54 void animate(T endVal, int dur) { | |
55 _animating = true; | |
56 _elapsed = 0; | |
57 _duration = dur; | |
58 _startValue = _value; | |
59 _endValue = endVal; | |
60 } | |
61 bool animating() { return _animating; } | |
62 void advance(int time) { | |
63 _elapsed += time; | |
64 if(!_animating) | |
65 return; | |
66 if(_elapsed > _duration) | |
67 set(_endValue); | |
68 else | |
69 _value = _startValue+(_endValue-_startValue)*_elapsed/_duration; | |
70 } | |
71 int elapsed() { | |
72 return _elapsed; | |
73 } | |
74 int duration() { | |
75 return _duration; | |
76 } | |
77 } |