Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Go to most recent revision | 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
 
11
dojo.provide("dojo.lfx.Animation");
12
dojo.require("dojo.lang.func");
13
dojo.lfx.Line = function (start, end) {
14
	this.start = start;
15
	this.end = end;
16
	if (dojo.lang.isArray(start)) {
17
		var diff = [];
18
		dojo.lang.forEach(this.start, function (s, i) {
19
			diff[i] = this.end[i] - s;
20
		}, this);
21
		this.getValue = function (n) {
22
			var res = [];
23
			dojo.lang.forEach(this.start, function (s, i) {
24
				res[i] = (diff[i] * n) + s;
25
			}, this);
26
			return res;
27
		};
28
	} else {
29
		var diff = end - start;
30
		this.getValue = function (n) {
31
			return (diff * n) + this.start;
32
		};
33
	}
34
};
35
if ((dojo.render.html.khtml) && (!dojo.render.html.safari)) {
36
	dojo.lfx.easeDefault = function (n) {
37
		return (parseFloat("0.5") + ((Math.sin((n + parseFloat("1.5")) * Math.PI)) / 2));
38
	};
39
} else {
40
	dojo.lfx.easeDefault = function (n) {
41
		return (0.5 + ((Math.sin((n + 1.5) * Math.PI)) / 2));
42
	};
43
}
44
dojo.lfx.easeIn = function (n) {
45
	return Math.pow(n, 3);
46
};
47
dojo.lfx.easeOut = function (n) {
48
	return (1 - Math.pow(1 - n, 3));
49
};
50
dojo.lfx.easeInOut = function (n) {
51
	return ((3 * Math.pow(n, 2)) - (2 * Math.pow(n, 3)));
52
};
53
dojo.lfx.IAnimation = function () {
54
};
55
dojo.lang.extend(dojo.lfx.IAnimation, {curve:null, duration:1000, easing:null, repeatCount:0, rate:10, handler:null, beforeBegin:null, onBegin:null, onAnimate:null, onEnd:null, onPlay:null, onPause:null, onStop:null, play:null, pause:null, stop:null, connect:function (evt, scope, newFunc) {
56
	if (!newFunc) {
57
		newFunc = scope;
58
		scope = this;
59
	}
60
	newFunc = dojo.lang.hitch(scope, newFunc);
61
	var oldFunc = this[evt] || function () {
62
	};
63
	this[evt] = function () {
64
		var ret = oldFunc.apply(this, arguments);
65
		newFunc.apply(this, arguments);
66
		return ret;
67
	};
68
	return this;
69
}, fire:function (evt, args) {
70
	if (this[evt]) {
71
		this[evt].apply(this, (args || []));
72
	}
73
	return this;
74
}, repeat:function (count) {
75
	this.repeatCount = count;
76
	return this;
77
}, _active:false, _paused:false});
78
dojo.lfx.Animation = function (handlers, duration, curve, easing, repeatCount, rate) {
79
	dojo.lfx.IAnimation.call(this);
80
	if (dojo.lang.isNumber(handlers) || (!handlers && duration.getValue)) {
81
		rate = repeatCount;
82
		repeatCount = easing;
83
		easing = curve;
84
		curve = duration;
85
		duration = handlers;
86
		handlers = null;
87
	} else {
88
		if (handlers.getValue || dojo.lang.isArray(handlers)) {
89
			rate = easing;
90
			repeatCount = curve;
91
			easing = duration;
92
			curve = handlers;
93
			duration = null;
94
			handlers = null;
95
		}
96
	}
97
	if (dojo.lang.isArray(curve)) {
98
		this.curve = new dojo.lfx.Line(curve[0], curve[1]);
99
	} else {
100
		this.curve = curve;
101
	}
102
	if (duration != null && duration > 0) {
103
		this.duration = duration;
104
	}
105
	if (repeatCount) {
106
		this.repeatCount = repeatCount;
107
	}
108
	if (rate) {
109
		this.rate = rate;
110
	}
111
	if (handlers) {
112
		dojo.lang.forEach(["handler", "beforeBegin", "onBegin", "onEnd", "onPlay", "onStop", "onAnimate"], function (item) {
113
			if (handlers[item]) {
114
				this.connect(item, handlers[item]);
115
			}
116
		}, this);
117
	}
118
	if (easing && dojo.lang.isFunction(easing)) {
119
		this.easing = easing;
120
	}
121
};
122
dojo.inherits(dojo.lfx.Animation, dojo.lfx.IAnimation);
123
dojo.lang.extend(dojo.lfx.Animation, {_startTime:null, _endTime:null, _timer:null, _percent:0, _startRepeatCount:0, play:function (delay, gotoStart) {
124
	if (gotoStart) {
125
		clearTimeout(this._timer);
126
		this._active = false;
127
		this._paused = false;
128
		this._percent = 0;
129
	} else {
130
		if (this._active && !this._paused) {
131
			return this;
132
		}
133
	}
134
	this.fire("handler", ["beforeBegin"]);
135
	this.fire("beforeBegin");
136
	if (delay > 0) {
137
		setTimeout(dojo.lang.hitch(this, function () {
138
			this.play(null, gotoStart);
139
		}), delay);
140
		return this;
141
	}
142
	this._startTime = new Date().valueOf();
143
	if (this._paused) {
144
		this._startTime -= (this.duration * this._percent / 100);
145
	}
146
	this._endTime = this._startTime + this.duration;
147
	this._active = true;
148
	this._paused = false;
149
	var step = this._percent / 100;
150
	var value = this.curve.getValue(step);
151
	if (this._percent == 0) {
152
		if (!this._startRepeatCount) {
153
			this._startRepeatCount = this.repeatCount;
154
		}
155
		this.fire("handler", ["begin", value]);
156
		this.fire("onBegin", [value]);
157
	}
158
	this.fire("handler", ["play", value]);
159
	this.fire("onPlay", [value]);
160
	this._cycle();
161
	return this;
162
}, pause:function () {
163
	clearTimeout(this._timer);
164
	if (!this._active) {
165
		return this;
166
	}
167
	this._paused = true;
168
	var value = this.curve.getValue(this._percent / 100);
169
	this.fire("handler", ["pause", value]);
170
	this.fire("onPause", [value]);
171
	return this;
172
}, gotoPercent:function (pct, andPlay) {
173
	clearTimeout(this._timer);
174
	this._active = true;
175
	this._paused = true;
176
	this._percent = pct;
177
	if (andPlay) {
178
		this.play();
179
	}
180
	return this;
181
}, stop:function (gotoEnd) {
182
	clearTimeout(this._timer);
183
	var step = this._percent / 100;
184
	if (gotoEnd) {
185
		step = 1;
186
	}
187
	var value = this.curve.getValue(step);
188
	this.fire("handler", ["stop", value]);
189
	this.fire("onStop", [value]);
190
	this._active = false;
191
	this._paused = false;
192
	return this;
193
}, status:function () {
194
	if (this._active) {
195
		return this._paused ? "paused" : "playing";
196
	} else {
197
		return "stopped";
198
	}
199
	return this;
200
}, _cycle:function () {
201
	clearTimeout(this._timer);
202
	if (this._active) {
203
		var curr = new Date().valueOf();
204
		var step = (curr - this._startTime) / (this._endTime - this._startTime);
205
		if (step >= 1) {
206
			step = 1;
207
			this._percent = 100;
208
		} else {
209
			this._percent = step * 100;
210
		}
211
		if ((this.easing) && (dojo.lang.isFunction(this.easing))) {
212
			step = this.easing(step);
213
		}
214
		var value = this.curve.getValue(step);
215
		this.fire("handler", ["animate", value]);
216
		this.fire("onAnimate", [value]);
217
		if (step < 1) {
218
			this._timer = setTimeout(dojo.lang.hitch(this, "_cycle"), this.rate);
219
		} else {
220
			this._active = false;
221
			this.fire("handler", ["end"]);
222
			this.fire("onEnd");
223
			if (this.repeatCount > 0) {
224
				this.repeatCount--;
225
				this.play(null, true);
226
			} else {
227
				if (this.repeatCount == -1) {
228
					this.play(null, true);
229
				} else {
230
					if (this._startRepeatCount) {
231
						this.repeatCount = this._startRepeatCount;
232
						this._startRepeatCount = 0;
233
					}
234
				}
235
			}
236
		}
237
	}
238
	return this;
239
}});
240
dojo.lfx.Combine = function (animations) {
241
	dojo.lfx.IAnimation.call(this);
242
	this._anims = [];
243
	this._animsEnded = 0;
244
	var anims = arguments;
245
	if (anims.length == 1 && (dojo.lang.isArray(anims[0]) || dojo.lang.isArrayLike(anims[0]))) {
246
		anims = anims[0];
247
	}
248
	dojo.lang.forEach(anims, function (anim) {
249
		this._anims.push(anim);
250
		anim.connect("onEnd", dojo.lang.hitch(this, "_onAnimsEnded"));
251
	}, this);
252
};
253
dojo.inherits(dojo.lfx.Combine, dojo.lfx.IAnimation);
254
dojo.lang.extend(dojo.lfx.Combine, {_animsEnded:0, play:function (delay, gotoStart) {
255
	if (!this._anims.length) {
256
		return this;
257
	}
258
	this.fire("beforeBegin");
259
	if (delay > 0) {
260
		setTimeout(dojo.lang.hitch(this, function () {
261
			this.play(null, gotoStart);
262
		}), delay);
263
		return this;
264
	}
265
	if (gotoStart || this._anims[0].percent == 0) {
266
		this.fire("onBegin");
267
	}
268
	this.fire("onPlay");
269
	this._animsCall("play", null, gotoStart);
270
	return this;
271
}, pause:function () {
272
	this.fire("onPause");
273
	this._animsCall("pause");
274
	return this;
275
}, stop:function (gotoEnd) {
276
	this.fire("onStop");
277
	this._animsCall("stop", gotoEnd);
278
	return this;
279
}, _onAnimsEnded:function () {
280
	this._animsEnded++;
281
	if (this._animsEnded >= this._anims.length) {
282
		this.fire("onEnd");
283
	}
284
	return this;
285
}, _animsCall:function (funcName) {
286
	var args = [];
287
	if (arguments.length > 1) {
288
		for (var i = 1; i < arguments.length; i++) {
289
			args.push(arguments[i]);
290
		}
291
	}
292
	var _this = this;
293
	dojo.lang.forEach(this._anims, function (anim) {
294
		anim[funcName](args);
295
	}, _this);
296
	return this;
297
}});
298
dojo.lfx.Chain = function (animations) {
299
	dojo.lfx.IAnimation.call(this);
300
	this._anims = [];
301
	this._currAnim = -1;
302
	var anims = arguments;
303
	if (anims.length == 1 && (dojo.lang.isArray(anims[0]) || dojo.lang.isArrayLike(anims[0]))) {
304
		anims = anims[0];
305
	}
306
	var _this = this;
307
	dojo.lang.forEach(anims, function (anim, i, anims_arr) {
308
		this._anims.push(anim);
309
		if (i < anims_arr.length - 1) {
310
			anim.connect("onEnd", dojo.lang.hitch(this, "_playNext"));
311
		} else {
312
			anim.connect("onEnd", dojo.lang.hitch(this, function () {
313
				this.fire("onEnd");
314
			}));
315
		}
316
	}, this);
317
};
318
dojo.inherits(dojo.lfx.Chain, dojo.lfx.IAnimation);
319
dojo.lang.extend(dojo.lfx.Chain, {_currAnim:-1, play:function (delay, gotoStart) {
320
	if (!this._anims.length) {
321
		return this;
322
	}
323
	if (gotoStart || !this._anims[this._currAnim]) {
324
		this._currAnim = 0;
325
	}
326
	var currentAnimation = this._anims[this._currAnim];
327
	this.fire("beforeBegin");
328
	if (delay > 0) {
329
		setTimeout(dojo.lang.hitch(this, function () {
330
			this.play(null, gotoStart);
331
		}), delay);
332
		return this;
333
	}
334
	if (currentAnimation) {
335
		if (this._currAnim == 0) {
336
			this.fire("handler", ["begin", this._currAnim]);
337
			this.fire("onBegin", [this._currAnim]);
338
		}
339
		this.fire("onPlay", [this._currAnim]);
340
		currentAnimation.play(null, gotoStart);
341
	}
342
	return this;
343
}, pause:function () {
344
	if (this._anims[this._currAnim]) {
345
		this._anims[this._currAnim].pause();
346
		this.fire("onPause", [this._currAnim]);
347
	}
348
	return this;
349
}, playPause:function () {
350
	if (this._anims.length == 0) {
351
		return this;
352
	}
353
	if (this._currAnim == -1) {
354
		this._currAnim = 0;
355
	}
356
	var currAnim = this._anims[this._currAnim];
357
	if (currAnim) {
358
		if (!currAnim._active || currAnim._paused) {
359
			this.play();
360
		} else {
361
			this.pause();
362
		}
363
	}
364
	return this;
365
}, stop:function () {
366
	var currAnim = this._anims[this._currAnim];
367
	if (currAnim) {
368
		currAnim.stop();
369
		this.fire("onStop", [this._currAnim]);
370
	}
371
	return currAnim;
372
}, _playNext:function () {
373
	if (this._currAnim == -1 || this._anims.length == 0) {
374
		return this;
375
	}
376
	this._currAnim++;
377
	if (this._anims[this._currAnim]) {
378
		this._anims[this._currAnim].play(null, true);
379
	}
380
	return this;
381
}});
382
dojo.lfx.combine = function (animations) {
383
	var anims = arguments;
384
	if (dojo.lang.isArray(arguments[0])) {
385
		anims = arguments[0];
386
	}
387
	if (anims.length == 1) {
388
		return anims[0];
389
	}
390
	return new dojo.lfx.Combine(anims);
391
};
392
dojo.lfx.chain = function (animations) {
393
	var anims = arguments;
394
	if (dojo.lang.isArray(arguments[0])) {
395
		anims = arguments[0];
396
	}
397
	if (anims.length == 1) {
398
		return anims[0];
399
	}
400
	return new dojo.lfx.Chain(anims);
401
};
402