Mercurial > MadButterfly
annotate nodejs/mbapp.js @ 804:55875133fa60
Compare performance of setInterval() and setTimeout()
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 01 Sep 2010 16:23:43 +0800 |
parents | 37a1bd3e3ce1 |
children | 7875e8026e86 |
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) { |
33 if (this.onKeyPress) this.onKeyPress(evt.sym); | |
34 if (evt.sym in this.keymap) this.keymap[evt.sym](); | |
35 } | |
36 | |
716 | 37 app.prototype.loop=function() { |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
38 this.mb_rt.redraw_all(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
39 this.mb_rt.flush(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
40 } |
762 | 41 app.prototype.update=function() { |
42 this.mb_rt.redraw_all(); | |
43 this.mb_rt.flush(); | |
44 } | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
45 app.prototype.get=function(name) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
46 return this.mb_rt.mbnames[name]; |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
47 } |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
48 app.prototype.addKeyboardListener=function(type,f) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
49 return this.mb_rt.kbevents.add_event_observer(type,f); |
716 | 50 } |
724 | 51 app.prototype.refresh=function() { |
52 this.mb_rt.redraw_all(); | |
53 this.mb_rt.flush(); | |
54 } | |
730 | 55 app.prototype.dump=function() { |
56 sys.puts(this.onKeyPress); | |
57 } | |
58 | |
59 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
|
60 if (typeof(key) == 'number') |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
61 this.keymap[key] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
62 else { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
63 for(k in key) { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
64 this.keymap[k] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
65 } |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
66 } |
730 | 67 } |
716 | 68 |
69 exports.app=app; | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
70 |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
71 // Put all key definition here |
729
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
72 exports.KEY_UP = 0xff52; |
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
73 exports.KEY_DOWN = 0xff54; |
724 | 74 exports.EVT_ANY=0; |
75 exports.EVT_MOUSE_OVER=1; | |
76 exports.EVT_MOUSE_OUT=2; | |
77 exports.EVT_MOUSE_MOVE=3; | |
78 exports.EVT_MOUSE_BUT_PRESS4; | |
79 exports.EVT_MOUSE_BUT_RELEASE=5 | |
80 exports.EVT_KB_PRESS=6; | |
81 exports.EVT_KB_RELEASE=7; | |
82 exports.EVT_PROGM_COMPLETE=8; | |
83 exports.EVT_RDMAN_REDRAW=9; | |
84 exports.EVT_MONITOR_ADD=10; | |
85 exports.EVT_MONITOR_REMOVE=11; | |
86 exports.EVT_MONITOR_FREE=12; | |
87 exports.EVT_MOUSE_MOVE_RAW=13; |