Subversion Repositories Applications.papyrus

Rev

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