Mercurial > MadButterfly
annotate nodejs/mbapp.js @ 820:7875e8026e86
Change the key from up/down to left/right
author | wycc |
---|---|
date | Fri, 10 Sep 2010 08:33:07 +0800 |
parents | 37a1bd3e3ce1 |
children | 586e50f82c1f |
rev | line source |
---|---|
716 | 1 var mbfly = require("mbfly"); |
2 var svg = require("./svg"); | |
730 | 3 var sys = require("sys"); |
716 | 4 var ldr = mbfly.img_ldr_new("."); |
784
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
5 |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
6 |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
7 app=function(display, w, h) { |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
8 var self = this; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
9 var mb_rt; |
716 | 10 |
784
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
11 if(typeof display == "undefined") |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
12 display = ":0.0"; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
13 if(typeof w == "undefined") |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
14 w = 720; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
15 if(typeof h == "undefined") |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
16 h = 480; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
17 |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
18 mb_rt = this.mb_rt = new mbfly.mb_rt(display, w, h); |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
19 var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
20 var paint = mb_rt.paint_color_new(1, 1, 1, 1); |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
21 paint.fill(background); |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
22 mb_rt.root.add_shape(background); |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
23 |
730 | 24 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); |
25 this.keymap={}; | |
26 this.onKeyPress = null; | |
716 | 27 } |
28 app.prototype.loadSVG=function(fname) { | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
29 svg.loadSVG(this.mb_rt,this.mb_rt.root,fname); |
716 | 30 } |
31 | |
730 | 32 app.prototype.KeyPress = function(evt) { |
820 | 33 sys.puts(evt.sym); |
730 | 34 if (this.onKeyPress) this.onKeyPress(evt.sym); |
35 if (evt.sym in this.keymap) this.keymap[evt.sym](); | |
36 } | |
37 | |
716 | 38 app.prototype.loop=function() { |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
39 this.mb_rt.redraw_all(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
40 this.mb_rt.flush(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
41 } |
762 | 42 app.prototype.update=function() { |
43 this.mb_rt.redraw_all(); | |
44 this.mb_rt.flush(); | |
45 } | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
46 app.prototype.get=function(name) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
47 return this.mb_rt.mbnames[name]; |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
48 } |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
49 app.prototype.addKeyboardListener=function(type,f) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
50 return this.mb_rt.kbevents.add_event_observer(type,f); |
716 | 51 } |
724 | 52 app.prototype.refresh=function() { |
53 this.mb_rt.redraw_all(); | |
54 this.mb_rt.flush(); | |
55 } | |
730 | 56 app.prototype.dump=function() { |
57 sys.puts(this.onKeyPress); | |
58 } | |
59 | |
60 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
|
61 if (typeof(key) == 'number') |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
62 this.keymap[key] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
63 else { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
64 for(k in key) { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
65 this.keymap[k] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
66 } |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
67 } |
730 | 68 } |
716 | 69 |
70 exports.app=app; | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
71 |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
72 // Put all key definition here |
820 | 73 exports.KEY_LEFT = 0xff51; |
729
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
74 exports.KEY_UP = 0xff52; |
820 | 75 exports.KEY_RIGHT = 0xff53; |
729
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
76 exports.KEY_DOWN = 0xff54; |
820 | 77 exports.KEY_ENTER = 0xff0d; |
724 | 78 exports.EVT_ANY=0; |
79 exports.EVT_MOUSE_OVER=1; | |
80 exports.EVT_MOUSE_OUT=2; | |
81 exports.EVT_MOUSE_MOVE=3; | |
82 exports.EVT_MOUSE_BUT_PRESS4; | |
83 exports.EVT_MOUSE_BUT_RELEASE=5 | |
84 exports.EVT_KB_PRESS=6; | |
85 exports.EVT_KB_RELEASE=7; | |
86 exports.EVT_PROGM_COMPLETE=8; | |
87 exports.EVT_RDMAN_REDRAW=9; | |
88 exports.EVT_MONITOR_ADD=10; | |
89 exports.EVT_MONITOR_REMOVE=11; | |
90 exports.EVT_MONITOR_FREE=12; | |
91 exports.EVT_MOUSE_MOVE_RAW=13; |