Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.animation.Animation");
14
dojo.require("dojo.animation.AnimationEvent");
15
dojo.require("dojo.lang.func");
16
dojo.require("dojo.math");
17
dojo.require("dojo.math.curves");
18
dojo.deprecated("dojo.animation.Animation is slated for removal in 0.5; use dojo.lfx.* instead.", "0.5");
19
dojo.animation.Animation = function (curve, duration, accel, repeatCount, rate) {
20
	if (dojo.lang.isArray(curve)) {
21
		curve = new dojo.math.curves.Line(curve[0], curve[1]);
22
	}
23
	this.curve = curve;
24
	this.duration = duration;
25
	this.repeatCount = repeatCount || 0;
26
	this.rate = rate || 25;
27
	if (accel) {
28
		if (dojo.lang.isFunction(accel.getValue)) {
29
			this.accel = accel;
30
		} else {
31
			var i = 0.35 * accel + 0.5;
32
			this.accel = new dojo.math.curves.CatmullRom([[0], [i], [1]], 0.45);
33
		}
34
	}
35
};
36
dojo.lang.extend(dojo.animation.Animation, {curve:null, duration:0, repeatCount:0, accel:null, onBegin:null, onAnimate:null, onEnd:null, onPlay:null, onPause:null, onStop:null, handler:null, _animSequence:null, _startTime:null, _endTime:null, _lastFrame:null, _timer:null, _percent:0, _active:false, _paused:false, _startRepeatCount:0, play:function (gotoStart) {
37
	if (gotoStart) {
38
		clearTimeout(this._timer);
39
		this._active = false;
40
		this._paused = false;
41
		this._percent = 0;
42
	} else {
43
		if (this._active && !this._paused) {
44
			return;
45
		}
46
	}
47
	this._startTime = new Date().valueOf();
48
	if (this._paused) {
49
		this._startTime -= (this.duration * this._percent / 100);
50
	}
51
	this._endTime = this._startTime + this.duration;
52
	this._lastFrame = this._startTime;
53
	var e = new dojo.animation.AnimationEvent(this, null, this.curve.getValue(this._percent), this._startTime, this._startTime, this._endTime, this.duration, this._percent, 0);
54
	this._active = true;
55
	this._paused = false;
56
	if (this._percent == 0) {
57
		if (!this._startRepeatCount) {
58
			this._startRepeatCount = this.repeatCount;
59
		}
60
		e.type = "begin";
61
		if (typeof this.handler == "function") {
62
			this.handler(e);
63
		}
64
		if (typeof this.onBegin == "function") {
65
			this.onBegin(e);
66
		}
67
	}
68
	e.type = "play";
69
	if (typeof this.handler == "function") {
70
		this.handler(e);
71
	}
72
	if (typeof this.onPlay == "function") {
73
		this.onPlay(e);
74
	}
75
	if (this._animSequence) {
76
		this._animSequence._setCurrent(this);
77
	}
78
	this._cycle();
79
}, pause:function () {
80
	clearTimeout(this._timer);
81
	if (!this._active) {
82
		return;
83
	}
84
	this._paused = true;
85
	var e = new dojo.animation.AnimationEvent(this, "pause", this.curve.getValue(this._percent), this._startTime, new Date().valueOf(), this._endTime, this.duration, this._percent, 0);
86
	if (typeof this.handler == "function") {
87
		this.handler(e);
88
	}
89
	if (typeof this.onPause == "function") {
90
		this.onPause(e);
91
	}
92
}, playPause:function () {
93
	if (!this._active || this._paused) {
94
		this.play();
95
	} else {
96
		this.pause();
97
	}
98
}, gotoPercent:function (pct, andPlay) {
99
	clearTimeout(this._timer);
100
	this._active = true;
101
	this._paused = true;
102
	this._percent = pct;
103
	if (andPlay) {
104
		this.play();
105
	}
106
}, stop:function (gotoEnd) {
107
	clearTimeout(this._timer);
108
	var step = this._percent / 100;
109
	if (gotoEnd) {
110
		step = 1;
111
	}
112
	var e = new dojo.animation.AnimationEvent(this, "stop", this.curve.getValue(step), this._startTime, new Date().valueOf(), this._endTime, this.duration, this._percent);
113
	if (typeof this.handler == "function") {
114
		this.handler(e);
115
	}
116
	if (typeof this.onStop == "function") {
117
		this.onStop(e);
118
	}
119
	this._active = false;
120
	this._paused = false;
121
}, status:function () {
122
	if (this._active) {
123
		return this._paused ? "paused" : "playing";
124
	} else {
125
		return "stopped";
126
	}
127
}, _cycle:function () {
128
	clearTimeout(this._timer);
129
	if (this._active) {
130
		var curr = new Date().valueOf();
131
		var step = (curr - this._startTime) / (this._endTime - this._startTime);
132
		var fps = 1000 / (curr - this._lastFrame);
133
		this._lastFrame = curr;
134
		if (step >= 1) {
135
			step = 1;
136
			this._percent = 100;
137
		} else {
138
			this._percent = step * 100;
139
		}
140
		if (this.accel && this.accel.getValue) {
141
			step = this.accel.getValue(step);
142
		}
143
		var e = new dojo.animation.AnimationEvent(this, "animate", this.curve.getValue(step), this._startTime, curr, this._endTime, this.duration, this._percent, Math.round(fps));
144
		if (typeof this.handler == "function") {
145
			this.handler(e);
146
		}
147
		if (typeof this.onAnimate == "function") {
148
			this.onAnimate(e);
149
		}
150
		if (step < 1) {
151
			this._timer = setTimeout(dojo.lang.hitch(this, "_cycle"), this.rate);
152
		} else {
153
			e.type = "end";
154
			this._active = false;
155
			if (typeof this.handler == "function") {
156
				this.handler(e);
157
			}
158
			if (typeof this.onEnd == "function") {
159
				this.onEnd(e);
160
			}
161
			if (this.repeatCount > 0) {
162
				this.repeatCount--;
163
				this.play(true);
164
			} else {
165
				if (this.repeatCount == -1) {
166
					this.play(true);
167
				} else {
168
					if (this._startRepeatCount) {
169
						this.repeatCount = this._startRepeatCount;
170
						this._startRepeatCount = 0;
171
					}
172
					if (this._animSequence) {
173
						this._animSequence._playNext();
174
					}
175
				}
176
			}
177
		}
178
	}
179
}});
180