Mercurial > MadButterfly
annotate nodejs/mbapp.js @ 731:1719484ed15d
Make the argument key can be an array to register multiple keys.
author | wycc |
---|---|
date | Fri, 20 Aug 2010 07:25:59 +0800 |
parents | e77ae2d23245 |
children | 6133319d3fb4 |
rev | line source |
---|---|
716 | 1 var mbfly = require("mbfly"); |
2 var svg = require("./svg"); | |
730 | 3 var sys = require("sys"); |
716 | 4 var mb_rt = new mbfly.mb_rt(":0.0", 720,480); |
5 var ldr = mbfly.img_ldr_new("."); | |
6 var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); | |
7 var paint = mb_rt.paint_color_new(1, 1, 1, 1); | |
8 paint.fill(background); | |
9 mb_rt.root.add_shape(background); | |
10 | |
11 app=function() { | |
730 | 12 var self = this; |
716 | 13 this.mb_rt = mb_rt; |
730 | 14 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); |
15 this.keymap={}; | |
16 this.onKeyPress = null; | |
716 | 17 } |
18 app.prototype.loadSVG=function(fname) { | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
19 svg.loadSVG(this.mb_rt,this.mb_rt.root,fname); |
716 | 20 } |
21 | |
730 | 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](); | |
25 } | |
26 | |
716 | 27 app.prototype.loop=function() { |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
28 this.mb_rt.redraw_all(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
29 this.mb_rt.flush(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
30 } |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
31 app.prototype.get=function(name) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
32 return this.mb_rt.mbnames[name]; |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
33 } |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
34 app.prototype.addKeyboardListener=function(type,f) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
35 return this.mb_rt.kbevents.add_event_observer(type,f); |
716 | 36 } |
724 | 37 app.prototype.refresh=function() { |
38 this.mb_rt.redraw_all(); | |
39 this.mb_rt.flush(); | |
40 } | |
730 | 41 app.prototype.dump=function() { |
42 sys.puts(this.onKeyPress); | |
43 } | |
44 | |
45 app.prototype.addKeyListener=function(key,f) { | |
731
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
46 if (typeof(key) == 'number') |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
47 this.keymap[key] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
48 else { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
49 for(k in key) { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
50 this.keymap[k] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
51 } |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
52 } |
730 | 53 } |
716 | 54 |
55 exports.app=app; | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
56 |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
57 // Put all key definition here |
729
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
58 exports.KEY_UP = 0xff52; |
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
59 exports.KEY_DOWN = 0xff54; |
724 | 60 exports.EVT_ANY=0; |
61 exports.EVT_MOUSE_OVER=1; | |
62 exports.EVT_MOUSE_OUT=2; | |
63 exports.EVT_MOUSE_MOVE=3; | |
64 exports.EVT_MOUSE_BUT_PRESS4; | |
65 exports.EVT_MOUSE_BUT_RELEASE=5 | |
66 exports.EVT_KB_PRESS=6; | |
67 exports.EVT_KB_RELEASE=7; | |
68 exports.EVT_PROGM_COMPLETE=8; | |
69 exports.EVT_RDMAN_REDRAW=9; | |
70 exports.EVT_MONITOR_ADD=10; | |
71 exports.EVT_MONITOR_REMOVE=11; | |
72 exports.EVT_MONITOR_FREE=12; | |
73 exports.EVT_MOUSE_MOVE_RAW=13; |