comparison nodejs/mbapp.js @ 730:e77ae2d23245

Add addKeyListener to handle key input.
author wycc
date Fri, 20 Aug 2010 07:21:24 +0800
parents 299ed9319dc6
children 1719484ed15d
comparison
equal deleted inserted replaced
729:299ed9319dc6 730:e77ae2d23245
1 var mbfly = require("mbfly"); 1 var mbfly = require("mbfly");
2 var svg = require("./svg"); 2 var svg = require("./svg");
3 var sys = require("sys");
3 var mb_rt = new mbfly.mb_rt(":0.0", 720,480); 4 var mb_rt = new mbfly.mb_rt(":0.0", 720,480);
4 var ldr = mbfly.img_ldr_new("."); 5 var ldr = mbfly.img_ldr_new(".");
5 var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); 6 var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0);
6 var paint = mb_rt.paint_color_new(1, 1, 1, 1); 7 var paint = mb_rt.paint_color_new(1, 1, 1, 1);
7 paint.fill(background); 8 paint.fill(background);
8 mb_rt.root.add_shape(background); 9 mb_rt.root.add_shape(background);
9 10
10 app=function() { 11 app=function() {
12 var self = this;
11 this.mb_rt = mb_rt; 13 this.mb_rt = mb_rt;
14 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);});
15 this.keymap={};
16 this.onKeyPress = null;
12 } 17 }
13 app.prototype.loadSVG=function(fname) { 18 app.prototype.loadSVG=function(fname) {
14 svg.loadSVG(this.mb_rt,this.mb_rt.root,fname); 19 svg.loadSVG(this.mb_rt,this.mb_rt.root,fname);
20 }
21
22 app.prototype.KeyPress = function(evt) {
23 if (this.onKeyPress) this.onKeyPress(evt.sym);
24 if (evt.sym in this.keymap) this.keymap[evt.sym]();
15 } 25 }
16 26
17 app.prototype.loop=function() { 27 app.prototype.loop=function() {
18 this.mb_rt.redraw_all(); 28 this.mb_rt.redraw_all();
19 this.mb_rt.flush(); 29 this.mb_rt.flush();
25 return this.mb_rt.kbevents.add_event_observer(type,f); 35 return this.mb_rt.kbevents.add_event_observer(type,f);
26 } 36 }
27 app.prototype.refresh=function() { 37 app.prototype.refresh=function() {
28 this.mb_rt.redraw_all(); 38 this.mb_rt.redraw_all();
29 this.mb_rt.flush(); 39 this.mb_rt.flush();
40 }
41 app.prototype.dump=function() {
42 sys.puts(this.onKeyPress);
43 }
44
45 app.prototype.addKeyListener=function(key,f) {
46 this.keymap[key] = f;
30 } 47 }
31 48
32 exports.app=app; 49 exports.app=app;
33 50
34 // Put all key definition here 51 // Put all key definition here