Mercurial > MadButterfly
annotate nodejs/mbapp.js @ 872:bcc63b20d5c6
mbapp.js support to create an application with existed window
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Fri, 24 Sep 2010 16:00:04 +0800 |
parents | 7eb5421a9864 |
children | 881efcd8a18f |
rev | line source |
---|---|
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
820
diff
changeset
|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
820
diff
changeset
|
2 // vim: sw=4:ts=8:sts=4 |
716 | 3 var mbfly = require("mbfly"); |
4 var svg = require("./svg"); | |
730 | 5 var sys = require("sys"); |
716 | 6 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
|
7 |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
8 |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
9 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
|
10 var self = this; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
11 var mb_rt; |
716 | 12 |
784
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
13 if(typeof display == "undefined") |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
14 display = ":0.0"; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
15 if(typeof w == "undefined") |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
16 w = 720; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
17 if(typeof h == "undefined") |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
18 h = 480; |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
19 |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 paint.fill(background); |
37a1bd3e3ce1
mbapp accept arguments for display, width and height
Thinker K.F. Li <thinker@codemud.net>
parents:
762
diff
changeset
|
24 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
|
25 |
730 | 26 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); |
27 this.keymap={}; | |
28 this.onKeyPress = null; | |
716 | 29 } |
30 app.prototype.loadSVG=function(fname) { | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
31 svg.loadSVG(this.mb_rt,this.mb_rt.root,fname); |
716 | 32 } |
33 | |
730 | 34 app.prototype.KeyPress = function(evt) { |
820 | 35 sys.puts(evt.sym); |
730 | 36 if (this.onKeyPress) this.onKeyPress(evt.sym); |
37 if (evt.sym in this.keymap) this.keymap[evt.sym](); | |
38 } | |
39 | |
716 | 40 app.prototype.loop=function() { |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
41 this.mb_rt.redraw_all(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
42 this.mb_rt.flush(); |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
43 } |
762 | 44 app.prototype.update=function() { |
45 this.mb_rt.redraw_all(); | |
46 this.mb_rt.flush(); | |
47 } | |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
48 app.prototype.get=function(name) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
49 return this.mb_rt.mbnames[name]; |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
50 } |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
51 app.prototype.addKeyboardListener=function(type,f) { |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
52 return this.mb_rt.kbevents.add_event_observer(type,f); |
716 | 53 } |
724 | 54 app.prototype.refresh=function() { |
55 this.mb_rt.redraw_all(); | |
56 this.mb_rt.flush(); | |
57 } | |
730 | 58 app.prototype.dump=function() { |
59 sys.puts(this.onKeyPress); | |
60 } | |
61 | |
62 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
|
63 if (typeof(key) == 'number') |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
64 this.keymap[key] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
65 else { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
66 for(k in key) { |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
67 this.keymap[k] = f; |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
68 } |
1719484ed15d
Make the argument key can be an array to register multiple keys.
wycc
parents:
730
diff
changeset
|
69 } |
730 | 70 } |
716 | 71 |
872
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
72 var app_with_win = function(display, win) { |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
73 var self = this; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
74 var mb_rt; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
75 var background; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
76 var paint; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
77 |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
78 if(typeof display == "undefined" || typeof win == "undefined") |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
79 throw "Invalid argument"; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
80 |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
81 mb_rt = this.mb_rt = new mbfly.mb_rt_with_win(display, win); |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
82 background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
83 paint = mb_rt.paint_color_new(1, 1, 1, 1); |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
84 paint.fill(background); |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
85 mb_rt.root.add_shape(background); |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
86 |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
87 this.mb_rt.kbevents. |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
88 add_event_observer(exports.EVT_KB_PRESS, |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
89 function(evt) { self.KeyPress(evt); }); |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
90 this.keymap = {}; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
91 this.onKeyPress = null; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
92 } |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
93 |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
94 app_with_win.prototype = app.prototype; |
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
95 |
716 | 96 exports.app=app; |
872
bcc63b20d5c6
mbapp.js support to create an application with existed window
Thinker K.F. Li <thinker@codemud.net>
parents:
827
diff
changeset
|
97 exports.app_with_win = app_with_win; |
721
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
98 |
01d02382dea7
Refactory the mbapp and testsvg to use keyboard events
wycc
parents:
716
diff
changeset
|
99 // Put all key definition here |
820 | 100 exports.KEY_LEFT = 0xff51; |
729
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
101 exports.KEY_UP = 0xff52; |
820 | 102 exports.KEY_RIGHT = 0xff53; |
729
299ed9319dc6
Use key symbols instead of keycodes.
Thinker K.F. Li <thinker@branda.to>
parents:
724
diff
changeset
|
103 exports.KEY_DOWN = 0xff54; |
820 | 104 exports.KEY_ENTER = 0xff0d; |
724 | 105 exports.EVT_ANY=0; |
106 exports.EVT_MOUSE_OVER=1; | |
107 exports.EVT_MOUSE_OUT=2; | |
108 exports.EVT_MOUSE_MOVE=3; | |
109 exports.EVT_MOUSE_BUT_PRESS4; | |
110 exports.EVT_MOUSE_BUT_RELEASE=5 | |
111 exports.EVT_KB_PRESS=6; | |
112 exports.EVT_KB_RELEASE=7; | |
113 exports.EVT_PROGM_COMPLETE=8; | |
114 exports.EVT_RDMAN_REDRAW=9; | |
115 exports.EVT_MONITOR_ADD=10; | |
116 exports.EVT_MONITOR_REMOVE=11; | |
117 exports.EVT_MONITOR_FREE=12; | |
118 exports.EVT_MOUSE_MOVE_RAW=13; | |
827 | 119 exports.ldr = ldr; |