comparison inkscape/firefox/inkscape.js @ 274:96aae15527c8

Port all basic scene editor from pyGtk to the firefox
author wycc
date Thu, 29 Jan 2009 22:30:46 +0800
parents
children 40c0c907c6dd
comparison
equal deleted inserted replaced
273:0cadeb9fdfc3 274:96aae15527c8
1 var isInProgress=0;
2
3 var MAX_DUMP_DEPTH = 10;
4 var mbsvg;
5
6
7
8 function dumpObj(obj, name, indent, depth) {
9 if (depth > MAX_DUMP_DEPTH) {
10 return indent + name + ": <Maximum Depth Reached>\n";
11 }
12 if (typeof obj == "object") {
13 var child = null;
14 var output = indent + name + "\n";
15 indent += "\t";
16 for (var item in obj)
17 {
18 try {
19 child = obj[item];
20 } catch (e) {
21 child = "<Unable to Evaluate>";
22 }
23 if (typeof child == "object") {
24 output += dumpObj(child, item, indent, depth + 1);
25 } else {
26 output += indent + item + ": " + child + "\n";
27 }
28 }
29 return output;
30 } else {
31 return obj;
32 }
33 }
34 function dumpObjItem(obj, name, indent, depth) {
35 if (depth > MAX_DUMP_DEPTH) {
36 return indent + name + ": <Maximum Depth Reached>\n";
37 }
38 if (typeof obj == "object") {
39 var child = null;
40 var output = indent + name + "\n";
41 indent += "\t";
42 for (var item in obj)
43 {
44 try {
45 child = obj[item];
46 } catch (e) {
47 child = "<Unable to Evaluate>";
48 }
49 if (typeof child == "object") {
50 output += dumpObjItem(child, item, indent, depth + 1);
51 } else {
52 output += indent + item + ":\n";
53 }
54 }
55 return output;
56 } else {
57 return obj;
58 }
59 }
60
61 function inkscape_load(file)
62 {
63 var inkscape = document.getElementById('inkscape');
64 inkscape.innerHTML = "<embed src="+file+" width=1000 height=800 />";
65 }
66
67 function MBSVG(file)
68 {
69 var xmlDoc=document.implementation.createDocument("http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd","",null);
70 xmlDoc.async=false;
71 xmlDoc.load(file);
72 MBSVG_loadFromDoc(this,xmlDoc);
73
74 }
75 function MBSVGString(xml)
76 {
77 var xmlParser = new DOMParser();
78 var xmlDoc = xmlParser.parseFromString( xml, 'text/xml');
79 MBSVG_loadFromDoc(this,xmlDoc);
80 }
81
82
83
84 function MBSVG_loadFromDoc(self,xmlDoc)
85 {
86 var scenesNode = xmlDoc.getElementsByTagNameNS("http://madbutterfly.sourceforge.net/DTD/madbutterfly.dtd","scene");
87 if (scenesNode == null) {
88 alert('This is not a valid scene file');
89 }
90 var len = scenesNode.length;
91 var i,j;
92 var max = 0;
93 var scenes = new Array();
94
95 // Get the length of scenes
96 for(i=0;i<len;i++) {
97 var s = scenesNode[i];
98 var start = s.getAttribute("start");
99 var end = s.getAttribute("end");
100 var ref = s.getAttribute("ref");
101 var ss = new Object();
102
103 if (end == null) end = start
104 if (max <end) max = end;
105 ss.node = s;
106 ss.start = start;
107 ss.end = end;
108 ss.ref = ref;
109 ss.layer = null;
110 scenes.push(ss);
111 }
112 if (max < 20) max = 20;
113 // Collect all layers
114 var nodes = xmlDoc.getElementsByTagNameNS("http://www.w3.org/2000/svg","svg")[0].childNodes;
115 var layers = new Array();
116 len = nodes.length;
117 for(i=0;i<len;i++) {
118 if (nodes[i].localName == 'g') {
119 var subnodes = nodes[i].childNodes;
120 for(j=0;j<subnodes.length;j++) {
121 if (subnodes[j].localName == 'g') {
122 for(var k=0;k<scenes.length;k++) {
123 if (scenes[k].ref == subnodes[j].getAttribute('id')) {
124 scenes[k].layer = nodes[i].getAttribute('id');
125 break;
126 }
127 }
128 }
129 }
130 layers.push(nodes[i]);
131 }
132 }
133 self.layers = layers;
134 self.scenes = scenes;
135 self.maxframe = max;
136 }
137
138 function renderUI(mbsvg)
139 {
140 var layers = mbsvg.layers;
141 var scenes = mbsvg.scenes;
142 var max = mbsvg.maxframe;
143 var cmd = "<table border=1>\n";
144 cmd = cmd + "<tr><td></td>";
145 for(var j=1;j<=max;j++)
146 cmd = cmd + "<td>"+j+"</td>";
147
148 for(var i=layers.length-1;i>=0;i--) {
149 var l = layers[i];
150 var id = l.getAttribute('id');
151 var label = l.getAttribute('inkscape:label');
152 cmd = cmd + "<tr><td>"+label+"</td>";
153 for(j=0;j<max;j++) {
154 var empty = 1;
155 var n = j +1;
156 var id_str = id+"#"+n
157 for(var k=0;k<scenes.length;k++) {
158 if (id != scenes[k].layer) continue;
159 if (n == scenes[k].start) {
160 cmd = cmd + "<td><img class='normal' src=start.png id='"+id_str+"' onClick='selectCell(this)' /></td>";
161 empty = 0;
162 break;
163 } else if ((n>scenes[k].start)&&(n <= scenes[k].end)) {
164 cmd = cmd + "<td><img class='normal' src=fill.png id='"+id_str+"' onClick='selectCell(this)' /></td>";
165 empty = 0;
166 break;
167 }
168 }
169 if (empty) {
170 cmd = cmd + "<td><img class='normal' src=empty.png id='"+id_str+"'onClick='selectCell(this)' /></td>";
171 }
172
173 }
174 cmd = cmd + "</tr>\n";
175 }
176 cmd = cmd + "</table>\n";
177 var frame = document.getElementById('frame');
178 frame.innerHTML=cmd;
179 }
180
181 function selectCell(obj)
182 {
183 var id = obj.getAttribute('id');
184 var layer,n;
185 var f = id.split('#');
186 layer=f[0];
187 n = f[1];
188 var img = obj.getAttribute('src');
189 var f = img.split('-');
190
191 if (f[0] == 'active')
192 return;
193 else {
194 obj.setAttribute('src', 'active-'+img);
195 }
196
197 if (last_select != null) {
198 f = last_select.getAttribute('src').split('-');
199 last_select.setAttribute('src', f[1]);
200 }
201 last_select = obj;
202 currentScene = n;
203 currentLayer = layer;
204 }
205
206
207 function onButtonClick(obj)
208 {
209 if (isInProgress != 0) return;
210 var id = obj.getAttribute('id');
211 if (id == 'Jump') {
212 if (currentScene != 0)
213 gotoScene(currentScene);
214 } else if (id == 'InsertKey') {
215 InsertKey(currentScene);
216 } else if (id == 'ExtendScene') {
217 ExtendScene(currentScene);
218 } else if (id == 'DeleteScene') {
219 DeleteScene(currentScene);
220 }
221 }
222
223 function gotoScene_cb(resObj)
224 {
225
226 }
227 var nextScene;
228 var currentScene = 0;
229 var currentLayer = '';
230 function gotoScene(n)
231 {
232 nextScene = n;
233 var soapBody = new SOAPObject("START");
234 var sr = new SOAPRequest("START", soapBody);
235 SOAPClient.Proxy = "http://localhost:19192/";
236 SOAPClient.SendRequest(sr, gotoScene1);
237 isInProgress++;
238 }
239 function gotoScene1(resp,n)
240 {
241 var soapBody = new SOAPObject("SCENE");
242 var v1 = new SOAPObject("v1");
243 v1.val(nextScene);
244 soapBody.appendChild(v1);
245 var sr = new SOAPRequest("SCENE", soapBody);
246 SOAPClient.Proxy = "http://localhost:19192/";
247 SOAPClient.SendRequest(sr, gotoScene2);
248 }
249 function gotoScene2(resp)
250 {
251 var soapBody = new SOAPObject("PUBLISH");
252 var sr = new SOAPRequest("PUBLISH", soapBody);
253 SOAPClient.Proxy = "http://localhost:19192/";
254 SOAPClient.SendRequest(sr, gotoScene3);
255 }
256
257 function gotoScene3(resp)
258 {
259 isInProgress--;
260 }
261
262
263
264 function InsertKey(n)
265 {
266 nextScene = n;
267 var soapBody = new SOAPObject("START");
268 var sr = new SOAPRequest("START", soapBody);
269 SOAPClient.Proxy = "http://localhost:19192/";
270 SOAPClient.SendRequest(sr, InsertKey1);
271 isInProgress++;
272 }
273 function InsertKey1(resp)
274 {
275 var soapBody = new SOAPObject("INSERTKEY");
276 var v1 = new SOAPObject("v1");
277 v1.attr('type','string');
278 v1.val(currentLayer);
279 soapBody.appendChild(v1);
280 var v2 = new SOAPObject("v2");
281 v2.val(nextScene);
282 soapBody.appendChild(v2);
283 var sr = new SOAPRequest("INSERTKEY", soapBody);
284 SOAPClient.Proxy = "http://localhost:19192/";
285 SOAPClient.SendRequest(sr, RefreshDocument);
286 }
287 function PublishDocument(resp)
288 {
289 mbsvg = new MBSVGString(resp.Body[0].GETDOCResponse[0].Result[0].Text);
290 renderUI(mbsvg);
291
292 var soapBody = new SOAPObject("PUBLISH");
293 var sr = new SOAPRequest("PUBLISH", soapBody);
294 SOAPClient.Proxy = "http://localhost:19192/";
295 SOAPClient.SendRequest(sr, OperationDone);
296 }
297
298 function RefreshDocument(resp)
299 {
300 var soapBody = new SOAPObject("GETDOC");
301 var sr = new SOAPRequest("GETDOC", soapBody);
302 SOAPClient.Proxy = "http://localhost:19192/";
303 SOAPClient.SendRequest(sr, PublishDocument);
304 }
305
306 function OperationDone(res)
307 {
308 isInProgress--;
309 }
310
311
312 function ExtendScene()
313 {
314 var soapBody = new SOAPObject("START");
315 var sr = new SOAPRequest("START", soapBody);
316 SOAPClient.Proxy = "http://localhost:19192/";
317 SOAPClient.SendRequest(sr,ExtendScene1);
318 isInProgress++;
319 }
320
321
322 function ExtendScene1(resp)
323 {
324 var soapBody = new SOAPObject("EXTENDSCENE");
325 var v1 = new SOAPObject("v1");
326 v1.attr('type','string');
327 v1.val(currentLayer);
328 soapBody.appendChild(v1);
329 var v2 = new SOAPObject("v2");
330 v2.val(currentScene);
331 soapBody.appendChild(v2);
332 var sr = new SOAPRequest("EXTENDSCENE", soapBody);
333 SOAPClient.Proxy = "http://localhost:19192/";
334 SOAPClient.SendRequest(sr, RefreshDocument);
335 }
336
337
338 function DeleteScene()
339 {
340 var soapBody = new SOAPObject("START");
341 var sr = new SOAPRequest("START", soapBody);
342 SOAPClient.Proxy = "http://localhost:19192/";
343 SOAPClient.SendRequest(sr,DeleteScene1);
344 isInProgress++;
345 }
346
347 function DeleteScene1(resp)
348 {
349 var soapBody = new SOAPObject("DELETESCENE");
350 var v1 = new SOAPObject("v1");
351 v1.attr('type','string');
352 v1.val(currentLayer);
353 soapBody.appendChild(v1);
354 var v2 = new SOAPObject("v2");
355 v2.val(currentScene);
356 soapBody.appendChild(v2);
357 var sr = new SOAPRequest("EXTENDSCENE", soapBody);
358 SOAPClient.Proxy = "http://localhost:19192/";
359 SOAPClient.SendRequest(sr, RefreshDocument);
360
361 }
362
363 function FetchDocument()
364 {
365 var soapBody = new SOAPObject("START");
366 var sr = new SOAPRequest("START", soapBody);
367 SOAPClient.Proxy = "http://localhost:19192/";
368 SOAPClient.SendRequest(sr,RefreshDocument);
369 isInProgress++;
370 }
371
372 var last_select = null;
373 inkscape_load("scene.mbsvg");
374 setTimeout("FetchDocument()",4000);
375
376 $('a.button').mouseover(function () {
377 if (isInProgress==0)
378 this.style.MozOpacity = 0.1;
379 }).mouseout(function () {
380 this.style.MozOpacity= 1;
381 });
382