diff nodejs/examples/mce/mainmenu.js @ 912:a934ad0c8968

Add the sample app framework. The scene framework will be added latter for multi scene applications.
author wycc
date Tue, 19 Oct 2010 04:07:47 +0800
parents
children 80000948fcde
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nodejs/examples/mce/mainmenu.js	Tue Oct 19 04:07:47 2010 +0800
@@ -0,0 +1,134 @@
+// -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*-
+// vim: sw=4:ts=8:sts=4
+var svg = require("svg");
+var mbapp = require("mbapp");
+var sys=require("sys");
+var animate=require("animate");
+var fs = require("fs");
+
+function MainMenu(app) 
+{
+    var self = this;
+    app.loadSVG("desktop.svg");
+
+    this.video = app.get("video");
+    this.audio = app.get("audio");
+    this.picture = app.get("picture");
+    this.setting = app.get("setting");
+    this.app = app;
+
+    this.lightbar = app.get("lightbar");
+    this.lines = [];
+    for(i = 0; i < 5; i++) {
+        var line = app.get("line" + (i + 1));
+        this.lines.push(line);
+    }
+    this.line=0;
+
+    this.items=[this.video, this.audio, this.picture, this.setting];
+    this.item = 0;
+
+    animate.run([new animate.scale(app,this.items[this.item], 1, 1.5)], 0, 0.1);
+    app.refresh();
+
+    app.addKeyListener(mbapp.KEY_LEFT, function() { self.key_left();});
+    app.addKeyListener(mbapp.KEY_RIGHT, function() { self.key_right();});
+    app.addKeyListener(mbapp.KEY_UP, function() {self.key_up();});
+    app.addKeyListener(mbapp.KEY_DOWN, function() {self.key_down();});
+    app.addKeyListener(mbapp.KEY_ENTER, function() {self.key_enter();});
+}
+
+MainMenu.prototype.key_left=function () 
+{
+    var old = this.items[this.item];
+    this.item = this.item - 1;
+    if (this.item == -1) {
+	this.item = 0;
+	return;
+    }
+    
+    var target = this.items[this.item];
+
+    old.bbox.update();
+    target.bbox.update();
+    
+    var an = new animate.scale(this.app, old, 1, 1);
+    animate.run([an], 0, 0.1);
+    an = new animate.scale(this.app, target, 1, 1.5);
+    animate.run([an], 0, 0.3);
+}
+
+MainMenu.prototype.key_right=function() 
+{
+    var old = this.items[this.item];
+    this.item = this.item + 1;
+    if (this.item == this.items.length) {
+	this.item = this.item - 1;
+	return;
+    }
+    
+    var target = this.items[this.item];
+
+    old.bbox.update();
+    target.bbox.update();
+    
+    var an = new animate.scale(this.app, old, 1, 1);
+    animate.run([an], 0, 0.1);
+    an = new animate.scale(this.app, target, 1, 1.5);
+    animate.run([an], 0, 0.3);
+}
+
+MainMenu.prototype.key_up=function() 
+{
+    var old = this.lines[this.line];
+    this.line = this.line - 1;
+    if (this.line == -1) {
+	this.line = 0;
+	return;
+    }
+    var target = this.lines[this.line];
+    var sy = target.center.y - this.lightbar.center.y;
+    var an = new animate.shift(this.app, this.lightbar, 0, sy);
+    animate.run([an], 0, 0.3);
+}
+
+
+MainMenu.prototype.key_down=function () 
+{
+    var old = this.lines[this.line];
+    this.line = this.line + 1;
+    if (this.line == this.lines.length) {
+	this.line = this.line - 1; 
+	return;
+    }
+    var target = this.lines[this.line];
+    var sy = target.center.y - this.lightbar.center.y;
+    var an = new animate.shift(this.app, this.lightbar, 0, sy);
+    animate.run([an], 0, 0.3);
+}
+
+MainMenu.prototype.key_enter=function() 
+{
+    var target = this.items[this.item];
+    var sx = 500 - target.center.x;
+    var sy = 220 - target.center.y;
+    var an = new animate.shift(this.app,target,sx,sy,1);
+    an.start();
+    for(i=0;i<this.items.length;i++) {
+	if (i == this.item) continue;
+	var x = Math.random();
+	var y = Math.random();
+	if (x > 0.5) x = 900;
+	else x = -500;
+	if (y > 0.5) y = 900;
+	else y = -500;
+	sx = x - this.items[i].center.x;
+	sy = y - this.items[i].center.y;
+	an = new animate.shift(this.app,this.items[i], sx, sy);
+	animate.run([an], 0, 2);
+	alpha = new animate.alpha(this.app,this.items[i], 0);
+	animate.run([an], 0, 1);
+    }
+}
+
+exports.MainMenu=MainMenu;