Mercurial > MadButterfly
comparison nodejs/mbapp.js @ 1380:76d2aad5da35
Implement runToScene method for the Flash-like animation.
author | wycc |
---|---|
date | Wed, 23 Mar 2011 00:33:51 +0800 |
parents | f0a156705977 |
children | 7ee0658025f3 |
comparison
equal
deleted
inserted
replaced
1379:f0a156705977 | 1380:76d2aad5da35 |
---|---|
99 | 99 |
100 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); | 100 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); |
101 this.keymap={}; | 101 this.keymap={}; |
102 this.onKeyPress = null; | 102 this.onKeyPress = null; |
103 this.svg = new svg.loadSVG(this.mb_rt,this.mb_rt.root,null); | 103 this.svg = new svg.loadSVG(this.mb_rt,this.mb_rt.root,null); |
104 this.frame_interval = 1000/12; // 12 frame per second | |
104 } | 105 } |
105 app.prototype.loadSVG=function(fname) { | 106 app.prototype.loadSVG=function(fname) { |
106 this.svg.load(this.mb_rt,this.mb_rt.root,fname) | 107 this.svg.load(this.mb_rt,this.mb_rt.root,fname); |
108 this.changeScene(1); | |
107 } | 109 } |
108 | 110 |
109 app.prototype.KeyPress = function(evt) { | 111 app.prototype.KeyPress = function(evt) { |
110 if (this.onKeyPress) this.onKeyPress(evt.sym); | 112 if (this.onKeyPress) this.onKeyPress(evt.sym); |
111 if (evt.sym in this.keymap) { | 113 if (evt.sym in this.keymap) { |
185 return [a[0]*b[0]+a[1]*b[3], a[0]*b[1]+a[1]*b[4], a[0]*b[2]+a[1]*b[5]+a[2], | 187 return [a[0]*b[0]+a[1]*b[3], a[0]*b[1]+a[1]*b[4], a[0]*b[2]+a[1]*b[5]+a[2], |
186 a[3]*b[0]+a[4]*b[3], a[3]*b[1]+a[4]*b[4], a[3]*b[2]+a[4]*b[5]+a[5]]; | 188 a[3]*b[0]+a[4]*b[3], a[3]*b[1]+a[4]*b[4], a[3]*b[2]+a[4]*b[5]+a[5]]; |
187 } | 189 } |
188 | 190 |
189 app.prototype.generateScaleTweenObject=function(coord,src,dest,p) { | 191 app.prototype.generateScaleTweenObject=function(coord,src,dest,p) { |
190 sys.puts("src=["+src.sx+","+src.sy+","+src.r+","+src.tx+","+src.ty); | 192 //sys.puts("src=["+src.sx+","+src.sy+","+src.r+","+src.tx+","+src.ty); |
191 sys.puts("dest=["+dest.sx+","+dest.sy+","+dest.r+","+dest.tx+","+dest.ty); | 193 //sys.puts("dest=["+dest.sx+","+dest.sy+","+dest.r+","+dest.tx+","+dest.ty); |
192 var p1 = 1-p; | 194 var p1 = 1-p; |
193 var sx = src.sx*p+dest.sx*p1; | 195 var sx = src.sx*p+dest.sx*p1; |
194 var sy = src.sy*p+dest.sy*p1; | 196 var sy = src.sy*p+dest.sy*p1; |
195 var r = src.r*p+dest.r*p1; | 197 var r = src.r*p+dest.r*p1; |
196 var tx = src.tx*p+dest.tx*p1; | 198 var tx = src.tx*p+dest.tx*p1; |
198 var mt = [1, 0, -src.tx, 0, 1, -src.ty]; | 200 var mt = [1, 0, -src.tx, 0, 1, -src.ty]; |
199 var ms; | 201 var ms; |
200 if (r == 0) { | 202 if (r == 0) { |
201 m = mt; | 203 m = mt; |
202 } else { | 204 } else { |
203 ms= [Math.cos(r), -Math.sin(r),0,Math.sin(r), Math.cos(r),0]; | 205 ms= [Math.cos(r), Math.sin(r),0,-Math.sin(r), Math.cos(r),0]; |
204 m = mul(ms,mt); | 206 m = mul(ms,mt); |
205 } | 207 } |
206 m = mul([sx,0,0,0,sy,0],m); | 208 m = mul([sx,0,0,0,sy,0],m); |
207 m = mul([1,0,tx,0,1,ty],m); | 209 m = mul([1,0,tx,0,1,ty],m); |
208 //m[2] = -m[2]; | 210 m[2] = -m[2]; |
209 coord[0] = m[0]; | 211 coord[0] = m[0]; |
210 coord[1] = m[1]; | 212 coord[1] = m[1]; |
211 coord[2] = m[2]; | 213 coord[2] = m[2]; |
212 coord[3] = m[3]; | 214 coord[3] = m[3]; |
213 coord[4] = m[4]; | 215 coord[4] = m[4]; |
214 coord[5] = m[5]; | 216 coord[5] = m[5]; |
215 //sys.puts(coord); | 217 //sys.puts(coord); |
216 sys.puts(m[0]+","+m[1]+","+m[2]+","+m[3]+","+m[4]+","+m[5]); | 218 sys.puts("p="+p+" "+m[0]+","+m[1]+","+m[2]+","+m[3]+","+m[4]+","+m[5]); |
217 } | 219 } |
218 | 220 |
219 app.prototype.changeScene=function(s) { | 221 app.prototype.changeScene=function(s) { |
220 var nth; | 222 var nth; |
221 if (typeof(s)=='number') { | 223 if (typeof(s)=='number') { |
223 nth = s; | 225 nth = s; |
224 } else { | 226 } else { |
225 nth = this.svg.getFrameNumber(s); | 227 nth = this.svg.getFrameNumber(s); |
226 if (nth == -1) return; | 228 if (nth == -1) return; |
227 } | 229 } |
230 this.currentScene = nth; | |
228 var scenes = this.svg.scenes; | 231 var scenes = this.svg.scenes; |
229 for(i=0;i<scenes.length-1;i++) { | 232 for(i=0;i<scenes.length-1;i++) { |
230 try { | 233 try { |
231 if (nth >=scenes[i].start && nth <=scenes[i].end) { | 234 if (nth >=scenes[i].start && nth <=scenes[i].end) { |
232 if (scenes[i].type == 'normal' || i == scenes.length-1) { | 235 if (scenes[i].type == 'normal' || i == scenes.length-1) { |
251 } | 254 } |
252 } | 255 } |
253 this.refresh(); | 256 this.refresh(); |
254 } | 257 } |
255 | 258 |
256 app.prototype.runToScene=function(n) { | 259 app.prototype.runToScene=function(s) { |
257 | 260 if (typeof(s)=='number') { |
261 var i; | |
262 nth = s; | |
263 } else { | |
264 nth = this.svg.getFrameNumber(s); | |
265 if (nth == -1) return; | |
266 } | |
267 var self = this; | |
268 if (nth > this.currentScene) { | |
269 this.targetScene = nth; | |
270 this.startScene = this.currentScene; | |
271 this.starttime = Date.now(); | |
272 setTimeout(function() { | |
273 self.skipFrame() | |
274 }, this.frame_interval); | |
275 } else { | |
276 this.changeScene(nth); | |
277 } | |
278 } | |
279 | |
280 app.prototype.skipFrame=function() { | |
281 var self = this; | |
282 sys.puts("n="+this.currentScene); | |
283 if (this.currentScene != this.targetScene) { | |
284 nextframe = this.startScene + Math.round((Date.now() - this.starttime)/this.frame_interval); | |
285 if (nextframe > this.targetScene) | |
286 nextframe = this.targetScene; | |
287 this.changeScene(nextframe); | |
288 setTimeout(function() { | |
289 self.skipFrame() | |
290 }, this.frame_interval); | |
291 } | |
258 } | 292 } |
259 | 293 |
260 app.prototype.addSceneListener=function(n, cb) { | 294 app.prototype.addSceneListener=function(n, cb) { |
261 sys.puts("This is not implemented yet") | 295 sys.puts("This is not implemented yet") |
262 } | 296 } |