changeset 895:3136db0ac01b abs_n_rel_center

Merged from default branch
author Thinker K.F. Li <thinker@branda.to>
date Tue, 28 Sep 2010 17:27:01 +0800
parents 96bc29e948cc (current diff) 460b2629be30 (diff)
children ae86e03acb6d
files nodejs/animate.js nodejs/phone.js nodejs/testdesktop.js
diffstat 3 files changed, 124 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/nodejs/animate.js	Sun Sep 26 14:25:55 2010 +0800
+++ b/nodejs/animate.js	Tue Sep 28 17:27:01 2010 +0800
@@ -9,37 +9,17 @@
 var ffs = 12;
 var frame_interval = 1000 / ffs;
 
-function linear_draw() {
-    var percent;
+function shift_draw(percent) {
     var x, y;
     
-    percent = (Date.now() - this._start_tm) / this.duration;
-    if(percent >= 1) {
-	percent = 1;
-        if (this.obj.timer) {
-	    this.obj.timer.stop();
-	    delete this.obj.timer;
-	}
-    }
-    x = (this.targetx-this.startposx)*percent+this.startposx;
-    y = (this.targety-this.startposy)*percent+this.startposy;
+    x = (this.targetx - this.startposx) * percent + this.startposx;
+    y = (this.targety - this.startposy) * percent + this.startposy;
     this.obj.center.move(x, y);
     this.app.refresh();
 }
 
-function linear_draw_start() {
-    var obj = this.obj;
-    var self = this;
-    
-    if(obj.timer)
-	obj.timer.stop();
-
-    this._start_tm = Date.now();
-    obj.timer = setInterval(function() { self.draw(); }, frame_interval);
-}
-
-function linear(app,obj,shiftx,shifty,duration) {
-    obj.animated_linear = this;
+function shift(app,obj,shiftx,shifty) {
+    obj.animated_shift = this;
     this.app = app;
     this.obj = obj;
     this.end = 0;
@@ -47,51 +27,32 @@
     this.targety = shifty + obj.center.y;
     this.startposx = obj.center.x;
     this.startposy = obj.center.y;
-    this.duration = duration*1000;
 }
 
-exports.linear = linear;
-linear.prototype.start = linear_draw_start;
-linear.prototype.draw = linear_draw;
+exports.shift = shift;
+shift.prototype.draw = shift_draw;
 
 /* ------------------------------------------------------------ */
 function rotate(app, obj, ang, duration) {
     this._app = app;
     this._obj = obj;
     this._ang = ang;
-    this._duration = duration * 1000;
+    this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]];
 }
 
-function rotate_start() {
-    var obj = this._obj;
-    var self = this;
-    
-    if(obj.timer)
-	obj.timer.stop();
-
-    this._start_mtx = [obj[0], obj[1], obj[2], obj[3], obj[4], obj[5]];
-    this._start_tm = Date.now();
-    obj.timer = setInterval(function() { self.draw(); }, frame_interval);
-}
-
-function rotate_draw() {
+function rotate_draw(percent) {
     var percent;
     var ang;
     var sv, cv;
     var obj = this._obj;
     var mtx, shift;
 
-    percent = (Date.now() - this._start_tm) / this._duration;
-    if(percent >= 1) {
-	percent = 1;
-	obj.timer.stop();
-    }
 
     ang = percent * this._ang;
     sv = Math.sin(ang);
     cv = Math.cos(ang);
     mtx = [cv, -sv, 0, sv, cv, 0];
-
+    sys.puts('x='+obj.center.x+',y='+obj.center.y);
     shift = [1, 0, -obj.center.x, 0, 1, -obj.center.y];
     mtx = multiply(mtx, shift);
     shift = [1, 0, obj.center.x, 0, 1, obj.center.y];
@@ -108,7 +69,6 @@
     this._app.refresh();
 }
 
-rotate.prototype.start = rotate_start;
 rotate.prototype.draw = rotate_draw;
 exports.rotate = rotate;
 
@@ -124,15 +84,12 @@
 }
 
 
-function scale_draw() {
-    if (this.end == 1) return;
-    var percent = (Date.now() - this.starttime)/this.duration;
-    if (percent > 1) percent = 1;
+function scale_draw(percent) {
     var sx = 1 + (this.totalsx - 1) * percent;
     var sy = 1 + (this.totalsy - 1) * percent;
     var sh1 = [1, 0, -this.center_x, 0, 1, -this.center_y];
     var sh2 = [1, 0, this.center_x, 0, 1, this.center_y];
-    var scale=[sx, 0, 0, 0, sy, 0];
+    var scale = [sx, 0, 0, 0, sy, 0];
     var obj = this.obj;
     var mtx;
 
@@ -147,13 +104,6 @@
     obj[5] = mtx[5];
 
     this.app.refresh();
-    var self = this;
-    if (percent < 1) {
-	obj.timer=setTimeout(function() { self.draw();}, frame_interval);
-	return;
-    }
-    this.app.refresh();
-    obj.animated_scale = null;
 }
 
 function scale(app, obj, fact_x, fact_y, duration) {
@@ -184,7 +134,6 @@
 
 
 exports.scale = scale;
-scale.prototype.start = scale_draw;
 scale.prototype.draw = scale_draw;
 
 function holder(app, coord) {
@@ -225,20 +174,11 @@
 
 
 
-function alpha_draw() {
+function alpha_draw(percent) {
 
     if (this.end == 1) return;
-    var percent = (Date.now() - this.starttime)/this.duration;
-    if (percent > 1) percent = 1;
     var sx = (this.targetalpha-this.startalpha)*percent+this.startalpha;
     this.obj.opacity=sx;
-
-    this.app.refresh();
-    var self = this;
-    if (percent < 1) {
-	this.obj.timer=setTimeout(function() { self.draw();}, frame_interval);
-	return;
-    }
     this.app.refresh();
     this.obj.animated_alpha = null;
 }
@@ -261,6 +201,90 @@
     this.duration = duration*1000;
 }
 
-alpha.prototype.start = alpha_draw;
 alpha.prototype.draw = alpha_draw;
 exports.alpha = alpha;
+
+function linear_update()
+{
+    var now = Date.now();
+    var i;
+    
+    sys.puts("real time is "+now);
+    sys.puts("end is "+this.end);
+    if (now >= this.end) {
+        this.timer.stop();
+	now = this.end;
+    }
+    if (now < this.startmove) return;
+    sys.puts("now is "+now+" offset is "+(now-this.startmove));
+    var per = (now-this.startmove)/this.duration/1000;
+    if (per > 1) per = 1;
+    this.action.draw(per);
+}
+
+function linear_start()
+{
+    var self = this;
+    if (this.timer)
+        this.timer.stop();
+    this.timer = setInterval(function() { self.update();}, frame_interval);
+    this.startmove = Date.now()+this.starttime*1000;
+    this.end = this.startmove+this.duration*1000;
+}
+function linear_stop() 
+{
+    if (this.timer) {
+        this.timer.stop();
+	this.timer = null;
+    }
+}
+
+function linear_finish()
+{
+    this.action.draw(1);
+}
+function linear(action,start, duration) 
+{
+    this.action = action;
+    this.duration = duration;
+    this.starttime = start;
+    this.timer=null;
+}
+linear.prototype.update = linear_update;
+linear.prototype.start = linear_start;
+linear.prototype.stop = linear_stop;
+linear.prototype.finish = linear_finish;
+exports.linear = linear;
+
+
+function program(words)
+{
+    this.words = wrods;
+}
+
+program.prototype.start=function() {
+    for(w in this.words) {
+        w.start();
+    }
+}
+
+program.prototype.stop=function(s) {
+    for(w in this.words) {
+        w.stop();
+    }
+}
+program.prototype.finish=function() {
+    for(w in this.words) {
+        w.finish();
+    }
+}
+
+exports.run = function(actions,start,duration) {
+    for(a in actions) {
+        var li = new linear(actions[a],start,duration);
+	sys.puts(li);
+	li.start();
+    }
+}
+exports.runexp=function(actions,start,exp) {
+}
--- a/nodejs/phone.js	Sun Sep 26 14:25:55 2010 +0800
+++ b/nodejs/phone.js	Tue Sep 28 17:27:01 2010 +0800
@@ -41,19 +41,19 @@
 	});
     icon.mouse_event.add_event_observer(4, function(evt) {
 	    pressholder.go_center(evt.cur_tgt);
-	    var rotate = new animate.rotate(app, evt.cur_tgt, 2 * 3.1415, 0.7);
-	    rotate.start();
+	    var rotate = new animate.rotate(app, evt.cur_tgt, 2 * 3.1415);
+	    animate.run([rotate], 0, 0.7);
 	});
 }
 
 var sw = 0;
-var dock_up = new animate.linear(app, dock, 0, -300, 0.5);
-var dock_down = new animate.linear(app, dock, 0, 0, 0.2);
+var dock_up = new animate.shift(app, dock, 0, -300);
+var dock_down = new animate.shift(app, dock, 0, 0);
 dock.mouse_event.add_event_observer(4, function(evt) {
 	if(sw == 0) {
-	    dock_up.start();
+	    animate.run([dock_up], 0, 0.5);
 	} else {
-	    dock_down.start();
+	    animate.run([dock_down], 0, 0.2);
 	}
 	sw = sw ^ 1;
     });
--- a/nodejs/testdesktop.js	Sun Sep 26 14:25:55 2010 +0800
+++ b/nodejs/testdesktop.js	Tue Sep 28 17:27:01 2010 +0800
@@ -27,9 +27,10 @@
 items=[video, audio, picture, setting];
 
 item = 0;
-an = new animate.scale(app,items[item],1,1.5,0.1);
-an.start();
+
+animate.run([new animate.scale(app,items[item], 1, 1.5)], 0, 0.1);
 app.refresh();
+
 app.addKeyListener(mbapp.KEY_LEFT, function() {
     var old = items[item];
     item = item - 1;
@@ -43,10 +44,10 @@
     old.bbox.update();
     target.bbox.update();
     
-    var an = new animate.scale(app, old, 1, 1, 0.1);
-    an.start();
-    an = new animate.scale(app, target, 1, 1.5, 0.3);
-    an.start();
+    var an = new animate.scale(app, old, 1, 1);
+    animate.run([an], 0, 0.1);
+    an = new animate.scale(app, target, 1, 1.5);
+    animate.run([an], 0, 0.3);
 });
 
 app.addKeyListener(mbapp.KEY_RIGHT, function() {
@@ -62,10 +63,10 @@
     old.bbox.update();
     target.bbox.update();
     
-    var an = new animate.scale(app, old, 1, 1, 0.1);
-    an.start();
-    an = new animate.scale(app, target, 1, 1.5, 0.3);
-    an.start();
+    var an = new animate.scale(app, old, 1, 1);
+    animate.run([an], 0, 0.1);
+    an = new animate.scale(app, target, 1, 1.5);
+    animate.run([an], 0, 0.3);
 });
 
 app.addKeyListener(mbapp.KEY_UP, function() {
@@ -78,8 +79,8 @@
     var target = lines[line];
     var sy = target.center.y - lightbar.center.y;
     sys.puts(sy);
-    var an = new animate.linear(app,lightbar,0,sy,0.3);
-    an.start();
+    var an = new animate.shift(app, lightbar, 0, sy);
+    animate.run([an], 0, 0.3);
 });
 app.addKeyListener(mbapp.KEY_DOWN, function() {
     var old = lines[line];
@@ -94,8 +95,8 @@
     sys.puts("sy="+sy);
     sys.puts("target.y="+target.center.y);
     sys.puts("lightbar.y="+lightbar.center.y);
-    var an = new animate.linear(app,lightbar,0,sy,0.3);
-    an.start();
+    var an = new animate.shift(app, lightbar, 0, sy);
+    animate.run([an], 0, 0.3);
 });
 
 app.addKeyListener(mbapp.KEY_ENTER, function() {
@@ -103,7 +104,7 @@
     var sx = 500 - target.center.x;
     var sy = 220 - target.center.y;
     sys.puts("target "+sx+','+sy);
-    var an = new animate.linear(app,target,sx,sy,1);
+    var an = new animate.shift(app,target,sx,sy,1);
     an.start();
     for(i=0;i<items.length;i++) {
 	if (i == item) continue;
@@ -115,10 +116,10 @@
 	else y = -500;
 	sx = x - items[i].center.x;
 	sy = y - items[i].center.y;
-	an = new animate.linear(app,items[i], sx,sy,2);
-	an.start();
-	alpha = new animate.alpha(app,items[i],0, 1);
-	alpha.start();
+	an = new animate.shift(app,items[i], sx, sy);
+	animate.run([an], 0, 2);
+	alpha = new animate.alpha(app,items[i], 0);
+	animate.run([an], 0, 1);
     }
 });