1 module ws.animation;
2 
3 
4 import
5 	std.math,
6 	std.algorithm,
7 	ws.time;
8 
9 
10 class Animation {
11 
12 	static double time;
13 	static void update(){
14 		time = now;
15 	}
16 	static this(){
17 		update;
18 	}
19 
20 	double start;
21 	double end;
22 	double timeStart;
23 	double duration;
24 	double function(double) func;
25 
26 	this(double value){
27 		this(value, value, 1);
28 	}
29 
30 	this(double value, double function(double) func){
31 		this(value, value, 1, func);
32 	}
33 
34 	this(double start, double end, double duration){
35 		this(start, end, duration, a => a);
36 	}
37 
38 	this(double start, double end, double duration, double function(double) func){
39 		this.start = start;
40 		this.end = end;
41 		this.timeStart = time;
42 		this.duration = duration;
43 		this.func = func;
44 	}
45 
46 	void change(double value){
47 		start = calculate;
48 		end = value;
49 		timeStart = time;
50 	}
51 
52 	void change(double value, double duration){
53 		change(value);
54 		this.duration = duration;
55 	}
56 
57 	void change(double value, double function(double) func){
58 		change(value);
59 		this.func = func;
60 	}
61 
62 	void replace(double start, double end){
63 		this.start = start;
64 		this.end = end;
65 		timeStart = time;
66 	}
67 
68 	void replace(double end){
69 		this.start = end;
70 		this.end = end;
71 		timeStart = 0;
72 	}
73 
74 	double completion(){
75 		return (time - timeStart).min(duration)/duration;
76 	}
77 
78 	double calculate(){
79 		return start + (end-start)*func(completion);
80 	}
81 
82 	bool done(){
83 		return timeStart+duration < time;
84 	}
85 
86 	double timeCurrent(){
87 		return time;
88 	}
89 
90 }
91 
92