Mercurial > MadButterfly
comparison nodejs/mbapp.js @ 1067:7b4e80ab671a openvg
merge from default branch
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Wed, 01 Dec 2010 12:25:56 +0800 |
parents | 78312b44f48c |
children | 593a418ed8bf |
comparison
equal
deleted
inserted
replaced
630:bd18951b51d5 | 1067:7b4e80ab671a |
---|---|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- | |
2 // vim: sw=4:ts=8:sts=4 | |
3 var mbfly = require("mbfly"); | |
4 var svg = require("./svg"); | |
5 var sys = require("sys"); | |
6 var ldr = mbfly.img_ldr_new("."); | |
7 | |
8 function _reverse(m1) { | |
9 var rev = new Array(1, 0, 0, 0, 1, 0); | |
10 var m = new Array(m1[0], m1[1], m1[2], m1[3], m1[4], m1[5]); | |
11 | |
12 rev[3] = -m[3] / m[0]; | |
13 m[3] = 0; | |
14 m[4] += rev[3] * m[1]; | |
15 m[5] += rev[3] * m[2]; | |
16 | |
17 rev[1] = -m[1] / m[4]; | |
18 rev[0] += rev[1] * rev[3]; | |
19 m[1] = 0; | |
20 m[2] += rev[1] * m[5]; | |
21 | |
22 rev[2] = -m[2]; | |
23 rev[5] = -m[5]; | |
24 | |
25 rev[0] = rev[0] / m[0]; | |
26 rev[1] = rev[1] / m[0]; | |
27 rev[2] = rev[2] / m[0]; | |
28 | |
29 rev[3] = rev[3] / m[4]; | |
30 rev[4] = rev[4] / m[4]; | |
31 rev[5] = rev[5] / m[4]; | |
32 | |
33 return rev; | |
34 } | |
35 | |
36 function _decorate_mb_rt(mb_rt) { | |
37 var coord; | |
38 | |
39 mb_rt._mbapp_saved_coord_new = mb_rt.coord_new; | |
40 mb_rt.coord_new = function(parent) { | |
41 var coord; | |
42 | |
43 coord = this._mbapp_saved_coord_new(parent); | |
44 coord.type = "coord"; | |
45 coord.children = []; | |
46 coord._mbapp_saved_mtx = [coord[0], coord[1], coord[2], | |
47 coord[3], coord[4], coord[5]]; | |
48 coord._mbapp_saved_rev_mtx = _reverse(coord._mbapp_saved_mtx); | |
49 coord.parent = parent; | |
50 coord._mbapp_saved_add_shape = coord.add_shape; | |
51 coord.add_shape = function(shape) { | |
52 var coord; | |
53 | |
54 this._mbapp_saved_add_shape(shape); | |
55 shape.parent = this; | |
56 this.children.push(shape); | |
57 } | |
58 | |
59 parent.children.push(coord); | |
60 | |
61 return coord; | |
62 }; | |
63 | |
64 /* | |
65 * Decorate root coord | |
66 */ | |
67 coord = mb_rt.root; | |
68 coord.type = "coord"; | |
69 coord.children = []; | |
70 coord._mbapp_saved_mtx = [coord[0], coord[1], coord[2], | |
71 coord[3], coord[4], coord[5]]; | |
72 coord._mbapp_saved_rev_mtx = _reverse(coord._mbapp_saved_mtx); | |
73 coord._mbapp_saved_add_shape = coord.add_shape; | |
74 coord.add_shape = function(shape) { | |
75 var coord; | |
76 | |
77 this._mbapp_saved_add_shape(shape); | |
78 shape.parent = this; | |
79 } | |
80 } | |
81 | |
82 app=function(display, w, h) { | |
83 var self = this; | |
84 var mb_rt; | |
85 | |
86 if(typeof display == "undefined") | |
87 display = ":0.0"; | |
88 if(typeof w == "undefined") | |
89 w = 720; | |
90 if(typeof h == "undefined") | |
91 h = 480; | |
92 | |
93 mb_rt = this.mb_rt = new mbfly.mb_rt(display, w, h); | |
94 _decorate_mb_rt(mb_rt); | |
95 var background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); | |
96 var paint = mb_rt.paint_color_new(1, 1, 1, 1); | |
97 paint.fill(background); | |
98 mb_rt.root.add_shape(background); | |
99 | |
100 this.mb_rt.kbevents.add_event_observer(exports.EVT_KB_PRESS, function(evt) { self.KeyPress(evt);}); | |
101 this.keymap={}; | |
102 this.onKeyPress = null; | |
103 this.svg = new svg.loadSVG(this.mb_rt,this.mb_rt.root,null); | |
104 } | |
105 app.prototype.loadSVG=function(fname) { | |
106 this.svg.load(this.mb_rt,this.mb_rt.root,fname) | |
107 } | |
108 | |
109 app.prototype.KeyPress = function(evt) { | |
110 if (this.onKeyPress) this.onKeyPress(evt.sym); | |
111 if (evt.sym in this.keymap) { | |
112 this.keymap[evt.sym](); | |
113 } | |
114 } | |
115 | |
116 app.prototype.loop=function() { | |
117 this.mb_rt.redraw_all(); | |
118 this.mb_rt.flush(); | |
119 } | |
120 app.prototype.update=function() { | |
121 this.mb_rt.redraw_all(); | |
122 this.mb_rt.flush(); | |
123 } | |
124 app.prototype.get=function(name) { | |
125 return this.mb_rt.mbnames[name]; | |
126 } | |
127 app.prototype.addKeyboardListener=function(type,f) { | |
128 return this.mb_rt.kbevents.add_event_observer(type,f); | |
129 } | |
130 app.prototype.refresh=function() { | |
131 this.mb_rt.redraw_changed(); | |
132 this.mb_rt.flush(); | |
133 } | |
134 app.prototype.dump=function() { | |
135 sys.puts(this.onKeyPress); | |
136 } | |
137 | |
138 app.prototype.addKeyListener=function(key,f) { | |
139 if (typeof(key) == 'number') | |
140 this.keymap[key] = f; | |
141 else { | |
142 for(k in key) { | |
143 this.keymap[k] = f; | |
144 } | |
145 } | |
146 } | |
147 | |
148 | |
149 app.prototype.changeScene=function(s) { | |
150 var nth; | |
151 if (typeof(s)=='number') { | |
152 var i; | |
153 nth = s; | |
154 } else { | |
155 nth = this.svg.getFrameNumber(s); | |
156 if (nth == -1) return; | |
157 } | |
158 var scenes = this.svg.scenes; | |
159 for(i=0;i<scenes.length;i++) { | |
160 try { | |
161 if (nth >=scenes[i].start && nth <=scenes[i].end) { | |
162 this.get(scenes[i].ref).show(); | |
163 } else { | |
164 this.get(scenes[i].ref).hide(); | |
165 } | |
166 } catch(e) { | |
167 sys.puts(e); | |
168 sys.puts(scenes[i].ref); | |
169 } | |
170 } | |
171 } | |
172 app.prototype.addSceneListener=function(n, cb) { | |
173 sys.puts("This is not implemented yet") | |
174 } | |
175 | |
176 var app_with_win = function(display, win) { | |
177 var self = this; | |
178 var mb_rt; | |
179 var background; | |
180 var paint; | |
181 | |
182 if(typeof display == "undefined" || typeof win == "undefined") | |
183 throw "Invalid argument"; | |
184 | |
185 mb_rt = this.mb_rt = new mbfly.mb_rt_with_win(display, win); | |
186 _decorate_mb_rt(mb_rt); | |
187 background = mb_rt.rect_new(0, 0, 720, 480, 0, 0); | |
188 paint = mb_rt.paint_color_new(1, 1, 1, 1); | |
189 paint.fill(background); | |
190 mb_rt.root.add_shape(background); | |
191 | |
192 this.mb_rt.kbevents. | |
193 add_event_observer(exports.EVT_KB_PRESS, | |
194 function(evt) { self.KeyPress(evt); }); | |
195 this.keymap = {}; | |
196 this.onKeyPress = null; | |
197 } | |
198 | |
199 app_with_win.prototype = app.prototype; | |
200 | |
201 exports.app=app; | |
202 exports.app_with_win = app_with_win; | |
203 | |
204 // Put all key definition here | |
205 exports.KEY_LEFT = 0xff51; | |
206 exports.KEY_UP = 0xff52; | |
207 exports.KEY_RIGHT = 0xff53; | |
208 exports.KEY_DOWN = 0xff54; | |
209 exports.KEY_ENTER = 0xff0d; | |
210 exports.EVT_ANY=0; | |
211 exports.EVT_MOUSE_OVER=1; | |
212 exports.EVT_MOUSE_OUT=2; | |
213 exports.EVT_MOUSE_MOVE=3; | |
214 exports.EVT_MOUSE_BUT_PRESS4; | |
215 exports.EVT_MOUSE_BUT_RELEASE=5 | |
216 exports.EVT_KB_PRESS=6; | |
217 exports.EVT_KB_RELEASE=7; | |
218 exports.EVT_PROGM_COMPLETE=8; | |
219 exports.EVT_RDMAN_REDRAW=9; | |
220 exports.EVT_MONITOR_ADD=10; | |
221 exports.EVT_MONITOR_REMOVE=11; | |
222 exports.EVT_MONITOR_FREE=12; | |
223 exports.EVT_MOUSE_MOVE_RAW=13; | |
224 exports.ldr = ldr; |