Mercurial > MadButterfly
comparison nodejs/mbapp.js @ 1405:05e8d3ffa703
Change the changeScene to sleep less when the render speed is too slow.
author | wycc |
---|---|
date | Mon, 04 Apr 2011 11:59:19 +0800 |
parents | 0efa20061d70 |
children | c918b79892ab |
comparison
equal
deleted
inserted
replaced
1403:c297ceec37f2 | 1405:05e8d3ffa703 |
---|---|
121 | 121 |
122 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); | 122 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); |
123 this.keymap={}; | 123 this.keymap={}; |
124 this.onKeyPress = null; | 124 this.onKeyPress = null; |
125 this.svg = new svg.loadSVG(this.mb_rt,this.mb_rt.root,null); | 125 this.svg = new svg.loadSVG(this.mb_rt,this.mb_rt.root,null); |
126 this.frame_interval = 1000/24; // 12 frame per second | 126 this.frame_interval = 1000/30; // 12 frame per second |
127 this.timer = null; | 127 this.timer = null; |
128 this._time = Date.now(); | |
129 } | |
130 | |
131 app.prototype.ts=function(m) { | |
132 var now = Date.now(); | |
133 var t = now-this._time; | |
134 sys.puts("["+t+"] "+m); | |
128 } | 135 } |
129 app.prototype.loadSVG=function(fname) { | 136 app.prototype.loadSVG=function(fname) { |
130 this.svg.load(this.mb_rt,this.mb_rt.root,fname); | 137 this.svg.load(this.mb_rt,this.mb_rt.root,fname); |
131 this.changeScene(0); | 138 this.changeScene(0); |
132 sys.puts("xxxx"); | 139 sys.puts("xxxx"); |
153 app.prototype.addKeyboardListener=function(type,f) { | 160 app.prototype.addKeyboardListener=function(type,f) { |
154 return this.mb_rt.kbevents.add_event_observer(type,f); | 161 return this.mb_rt.kbevents.add_event_observer(type,f); |
155 } | 162 } |
156 app.prototype.refresh=function() { | 163 app.prototype.refresh=function() { |
157 this.mb_rt.redraw_changed(); | 164 this.mb_rt.redraw_changed(); |
165 //this.mb_rt.redraw_all(); | |
158 this.mb_rt.flush(); | 166 this.mb_rt.flush(); |
159 } | 167 } |
160 app.prototype.dump=function() { | 168 app.prototype.dump=function() { |
161 sys.puts(this.onKeyPress); | 169 sys.puts(this.onKeyPress); |
162 } | 170 } |
174 app.prototype.generateScaleTween=function(src,dest,p) { | 182 app.prototype.generateScaleTween=function(src,dest,p) { |
175 sys.puts("p="+ p); | 183 sys.puts("p="+ p); |
176 src.hide(); | 184 src.hide(); |
177 // Duplicate the group | 185 // Duplicate the group |
178 var nodes = src.children; | 186 var nodes = src.children; |
187 if (src.dup) { | |
188 //src.dup.remove(); | |
189 //src.dup = null; | |
190 } | |
179 if (src.dup == null) { | 191 if (src.dup == null) { |
180 var dup = this.mb_rt.coord_new(src.parent); | 192 var dup = this.mb_rt.coord_new(src.parent); |
181 for (i in nodes) { | 193 for (i in nodes) { |
182 var c = nodes[i]; | 194 var c = nodes[i]; |
183 var ng = this.mb_rt.coord_new(dup); | 195 var ng = this.mb_rt.coord_new(dup); |
275 nth = s; | 287 nth = s; |
276 } else { | 288 } else { |
277 nth = this.svg.getFrameNumber(s); | 289 nth = this.svg.getFrameNumber(s); |
278 if (nth == -1) return; | 290 if (nth == -1) return; |
279 } | 291 } |
280 sys.puts("goto to scene "+nth); | 292 this.ts("goto to scene "+nth); |
281 this.currentScene = nth; | 293 this.currentScene = nth; |
282 var scenes = this.svg.scenes; | 294 var scenes = this.svg.scenes; |
283 if (scenes == null) return; | 295 if (scenes == null) return; |
284 for(i=0;i<scenes.length-1;i++) { | 296 for(i=0;i<scenes.length-1;i++) { |
285 try { | 297 try { |
310 sys.puts(e); | 322 sys.puts(e); |
311 sys.puts(scenes[i].ref); | 323 sys.puts(scenes[i].ref); |
312 } | 324 } |
313 } | 325 } |
314 this.get(scenes[i].ref).hide(); | 326 this.get(scenes[i].ref).hide(); |
327 this.ts("refresh"); | |
315 this.refresh(); | 328 this.refresh(); |
329 this.ts("refresh done"); | |
316 } | 330 } |
317 | 331 |
318 app.prototype.runToScene=function(s) { | 332 app.prototype.runToScene=function(s) { |
319 if (typeof(s)=='number') { | 333 if (typeof(s)=='number') { |
320 var i; | 334 var i; |
332 } | 346 } |
333 this.targetScene = nth; | 347 this.targetScene = nth; |
334 this.startScene = this.currentScene; | 348 this.startScene = this.currentScene; |
335 this.starttime = Date.now(); | 349 this.starttime = Date.now(); |
336 if (this.timer == null) { | 350 if (this.timer == null) { |
337 this.timer = setTimeout(function() { | 351 this.skipFrame(); |
338 self.skipFrame() | |
339 }, this.frame_interval); | |
340 } | 352 } |
341 } | 353 } |
342 | 354 |
343 app.prototype.skipFrame=function() { | 355 app.prototype.skipFrame=function() { |
344 var self = this; | 356 var self = this; |
345 if (this.currentScene != this.targetScene) { | 357 if (this.currentScene != this.targetScene) { |
346 var step = Math.round((Date.now() - this.starttime)/this.frame_interval)*this.skipdir; | 358 var now = Date.now(); |
359 var step = Math.round((now - this.starttime)/this.frame_interval)*this.skipdir; | |
347 nextframe = this.startScene + step | 360 nextframe = this.startScene + step |
361 //nextframe = this.currentScene + this.skipdir; | |
362 this.ts("goto begin"); | |
363 | |
348 if (this.skipdir>0) { | 364 if (this.skipdir>0) { |
349 if (nextframe > this.targetScene) | 365 if (nextframe > this.targetScene) |
350 nextframe = this.targetScene; | 366 nextframe = this.targetScene; |
351 } else { | 367 } else { |
352 if (nextframe < this.targetScene) | 368 if (nextframe < this.targetScene) |
353 nextframe = this.targetScene; | 369 nextframe = this.targetScene; |
354 } | 370 } |
355 this.changeScene(nextframe); | |
356 if (nextframe != this.targetScene) { | 371 if (nextframe != this.targetScene) { |
372 var timegap = (nextframe-this.startScene)*this.skipdir*this.frame_interval+this.starttime - Date.now(); | |
373 sys.puts("goto "+timegap); | |
374 if (timegap <200) { | |
375 timegap = 0; | |
376 } else { | |
377 } | |
357 this.timer = setTimeout(function() { | 378 this.timer = setTimeout(function() { |
358 self.skipFrame() | 379 self.skipFrame() |
359 }, this.frame_interval); | 380 }, timegap); |
360 } else { | 381 } else { |
361 this.timer = null; | 382 this.timer = null; |
362 } | 383 } |
384 this.changeScene(nextframe); | |
385 now = Date.now(); | |
386 this.ts("goto end"); | |
363 } | 387 } |
364 } | 388 } |
365 | 389 |
366 app.prototype.addSceneListener=function(n, cb) { | 390 app.prototype.addSceneListener=function(n, cb) { |
367 sys.puts("This is not implemented yet") | 391 sys.puts("This is not implemented yet") |