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 
17 	double start;
18 	double end;
19 	double timeStart;
20 	double duration;
21 	double function(double) func;
22 
23 	this(double value){
24 		this(value, value, 1);
25 	}
26 
27 	this(double value, double function(double) func){
28 		this(value, value, 1, func);
29 	}
30 
31 	this(double start, double end, double duration){
32 		this(start, end, duration, a => a);
33 	}
34 
35 	this(double start, double end, double duration, double function(double) func){
36 		this.start = start;
37 		this.end = end;
38 		this.timeStart = time;
39 		this.duration = duration;
40 		this.func = func;
41 	}
42 
43 	void change(double value){
44 		start = calculate;
45 		end = value;
46 		timeStart = time;
47 	}
48 
49 	void change(double value, double duration){
50 		change(value);
51 		this.duration = duration;
52 	}
53 
54 	void change(double value, double function(double) func){
55 		change(value);
56 		this.func = func;
57 	}
58 
59 	void replace(double start, double end){
60 		this.start = start;
61 		this.end = end;
62 		timeStart = time;
63 	}
64 
65 	void replace(double end){
66 		this.start = end;
67 		this.end = end;
68 		timeStart = 0;
69 	}
70 
71 	double completion(){
72 		return (time - timeStart).min(duration)/duration;
73 	}
74 
75 	double calculate(){
76 		return start + (end-start)*func(completion);
77 	}
78 
79 	bool done(){
80 		return timeStart+duration < time;
81 	}
82 
83 	double timeCurrent(){
84 		return time;
85 	}
86 
87 }
88 
89