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