comparison dynamin/core/animated_value.d @ 0:aa4efef0f0b1

Initial commit of code.
author Jordan Miner <jminer7@gmail.com>
date Mon, 15 Jun 2009 22:10:48 -0500
parents
children 73060bc3f004
comparison
equal deleted inserted replaced
-1:000000000000 0:aa4efef0f0b1
1 // Written in the D programming language
2 // www.digitalmars.com/d/
3
4 /*
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Dynamin library.
16 *
17 * The Initial Developer of the Original Code is Jordan Miner.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Jordan Miner <jminer7@gmail.com>
23 *
24 */
25
26 module dynamin.core.animated_value;
27
28 /// AnimatedValue!(int)
29 alias AnimatedValue!(int) AnimatedInt;
30 /// AnimatedValue!(float)
31 alias AnimatedValue!(float) AnimatedFloat;
32 /// AnimatedValue!(double)
33 alias AnimatedValue!(double) AnimatedDouble;
34 /// AnimatedValue!(real)
35 alias AnimatedValue!(real) AnimatedReal;
36
37 // TODO: change to a struct?
38 /**
39 * Holds a value that changes over time. The value can be changed by calling
40 * animate(), then calling advance() with an amount of time.
41 * Example:
42 * -----
43 * AnimatedInt x = new AnimatedInt;
44 * x.set(100); /+ start the animation at 100 +/
45 * x.animate(900, 1000); /+ animate to 900, over a period of 1000 ms +/
46 * x.advance(250); /+ advance 250 ms, one-fourth of the way to 1000 +/
47 * x.get(); /+ returns 300, one-fourth of the way between 100 and 900 +/
48 * x.advance(2000); /+ advance past the end of the animation +/
49 * x.get(); /+ returns 900 +/
50 * -----
51 */
52 class AnimatedValue(T) {
53 T _value = 0;
54 T _startValue, _endValue;
55 int _elapsed, _duration;
56 bool _animating;
57 T get() {
58 return _value;
59 }
60 T end() {
61 return _endValue;
62 }
63 AnimatedValue set(T newVal) {
64 _value = newVal;
65 _endValue = _value;
66 _animating = false;
67 return this;
68 }
69 void animate(T endVal, int dur) {
70 _animating = true;
71 _elapsed = 0;
72 _duration = dur;
73 _startValue = _value;
74 _endValue = endVal;
75 }
76 bool animating() { return _animating; }
77 void advance(int time) {
78 _elapsed += time;
79 if(!_animating)
80 return;
81 if(_elapsed > _duration)
82 set(_endValue);
83 else
84 _value = _startValue+(_endValue-_startValue)*_elapsed/_duration;
85 }
86 int elapsed() {
87 return _elapsed;
88 }
89 int duration() {
90 return _duration;
91 }
92 }