diff options
Diffstat (limited to 'assets/canvas-runtime.js')
-rw-r--r-- | assets/canvas-runtime.js | 388 |
1 files changed, 152 insertions, 236 deletions
diff --git a/assets/canvas-runtime.js b/assets/canvas-runtime.js index fd823f35..dd4ad6f9 100644 --- a/assets/canvas-runtime.js +++ b/assets/canvas-runtime.js | |||
@@ -37,18 +37,15 @@ function CanvasDataManager() | |||
37 | for (var i=0; i<nWorlds; i++) | 37 | for (var i=0; i<nWorlds; i++) |
38 | { | 38 | { |
39 | var importStr = value[i]; | 39 | var importStr = value[i]; |
40 | var startIndex = importStr.indexOf( "id: " ); | 40 | var jObj = JSON.parse( importStr ); |
41 | if (startIndex >= 0) | 41 | |
42 | var id = jObj.id; | ||
43 | if (id) | ||
42 | { | 44 | { |
43 | var endIndex = importStr.indexOf( "\n", startIndex ); | 45 | var canvas = this.findCanvasWithID( id, root ); |
44 | if (endIndex > 0) | 46 | if (canvas) |
45 | { | 47 | { |
46 | var id = importStr.substring( startIndex+4, endIndex ); | 48 | new GLRuntime( canvas, jObj, assetPath ); |
47 | var canvas = this.findCanvasWithID( id, root ); | ||
48 | if (canvas) | ||
49 | { | ||
50 | var rt = new GLRuntime( canvas, importStr, assetPath ); | ||
51 | } | ||
52 | } | 49 | } |
53 | } | 50 | } |
54 | } | 51 | } |
@@ -95,14 +92,15 @@ function CanvasDataManager() | |||
95 | // Class GLRuntime | 92 | // Class GLRuntime |
96 | // Manages runtime fora WebGL canvas | 93 | // Manages runtime fora WebGL canvas |
97 | /////////////////////////////////////////////////////////////////////// | 94 | /////////////////////////////////////////////////////////////////////// |
98 | function GLRuntime( canvas, importStr, assetPath ) | 95 | function GLRuntime( canvas, jObj, assetPath ) |
99 | { | 96 | { |
100 | /////////////////////////////////////////////////////////////////////// | 97 | /////////////////////////////////////////////////////////////////////// |
101 | // Instance variables | 98 | // Instance variables |
102 | /////////////////////////////////////////////////////////////////////// | 99 | /////////////////////////////////////////////////////////////////////// |
103 | this._canvas = canvas; | 100 | this._canvas = canvas; |
104 | this._context = null; | 101 | this._context = null; |
105 | this._importStr = importStr; | 102 | //this._importStr = importStr; |
103 | this._jObj = jObj; | ||
106 | 104 | ||
107 | this.renderer = null; | 105 | this.renderer = null; |
108 | this.myScene = null; | 106 | this.myScene = null; |
@@ -157,21 +155,19 @@ function GLRuntime( canvas, importStr, assetPath ) | |||
157 | /////////////////////////////////////////////////////////////////////// | 155 | /////////////////////////////////////////////////////////////////////// |
158 | this.loadScene = function() | 156 | this.loadScene = function() |
159 | { | 157 | { |
158 | var jObj = this._jObj; | ||
159 | if (!jObj.children || (jObj.children.length != 1)) | ||
160 | throw new Error( "ill-formed JSON for runtime load: " + jObj ); | ||
161 | var root = jObj.children[0]; | ||
162 | |||
160 | // parse the data | 163 | // parse the data |
161 | // the GL runtime must start with a "sceneData: " | 164 | if (jObj.scenedata) |
162 | var index = importStr.indexOf( "scenedata: " ); | ||
163 | if (index >= 0) | ||
164 | { | 165 | { |
165 | this._useWebGL = true; | 166 | this._useWebGL = true; |
166 | 167 | ||
167 | var rdgeStr = importStr.substr( index+11 ); | 168 | var rdgeStr = jObj.scenedata; |
168 | var endIndex = rdgeStr.indexOf( "endscene\n" ); | ||
169 | if (endIndex < 0) throw new Error( "ill-formed WebGL data" ); | ||
170 | var len = endIndex - index + 11; | ||
171 | rdgeStr = rdgeStr.substr( 0, endIndex ); | ||
172 | |||
173 | this.myScene.importJSON( rdgeStr ); | 169 | this.myScene.importJSON( rdgeStr ); |
174 | this.importObjects( importStr ); | 170 | this.importObjects( root ); |
175 | this.linkMaterials( this._geomRoot ); | 171 | this.linkMaterials( this._geomRoot ); |
176 | this.initMaterials(); | 172 | this.initMaterials(); |
177 | this.linkLights(); | 173 | this.linkLights(); |
@@ -179,7 +175,7 @@ function GLRuntime( canvas, importStr, assetPath ) | |||
179 | else | 175 | else |
180 | { | 176 | { |
181 | this._context = this._canvas.getContext( "2d" ); | 177 | this._context = this._canvas.getContext( "2d" ); |
182 | this.importObjects( importStr ); | 178 | this.importObjects( root ); |
183 | this.render(); | 179 | this.render(); |
184 | } | 180 | } |
185 | } | 181 | } |
@@ -277,58 +273,42 @@ function GLRuntime( canvas, importStr, assetPath ) | |||
277 | } | 273 | } |
278 | } | 274 | } |
279 | 275 | ||
280 | this.importObjects = function( importStr, parent ) | 276 | this.importObjects = function( jObj, parent ) |
281 | { | 277 | { |
282 | var index = importStr.indexOf( "OBJECT\n", 0 ); | 278 | // read the next object |
283 | while (index >= 0) | 279 | var gObj = this.importObject( jObj, parent ); |
284 | { | ||
285 | // update the string to the current object | ||
286 | importStr = importStr.substr( index+7 ); | ||
287 | 280 | ||
288 | // read the next object | 281 | // load the children |
289 | var obj = this.importObject( importStr, parent ); | 282 | if (jObj.children) |
290 | 283 | { | |
291 | // determine if we have children | 284 | var nKids = jObj.children.length; |
292 | var endIndex = importStr.indexOf( "ENDOBJECT\n" ), | 285 | for (var i=0; i<nKids; i++) |
293 | childIndex = importStr.indexOf( "OBJECT\n" ); | ||
294 | if (endIndex < 0) throw new Error( "ill-formed object data" ); | ||
295 | if ((childIndex >= 0) && (childIndex < endIndex)) | ||
296 | { | 286 | { |
297 | importStr = importStr.substr( childIndex + 7 ); | 287 | var child = jObj.children[i]; |
298 | importStr = this.importObjects( importStr, obj ); | 288 | this.importObjects( child, gObj ); |
299 | endIndex = importStr.indexOf( "ENDOBJECT\n" ) | ||
300 | } | 289 | } |
301 | |||
302 | // remove the string for the object(s) just created | ||
303 | importStr = importStr.substr( endIndex ); | ||
304 | |||
305 | // get the location of the next object | ||
306 | index = importStr.indexOf( "OBJECT\n", endIndex ); | ||
307 | } | 290 | } |
308 | |||
309 | return importStr; | ||
310 | } | 291 | } |
311 | 292 | ||
312 | this.importObject = function( objStr, parent ) | 293 | this.importObject = function( jObj, parent ) |
313 | { | 294 | { |
314 | var type = Number( getPropertyFromString( "type: ", objStr ) ); | 295 | var type = jObj.type |
315 | |||
316 | var obj; | 296 | var obj; |
317 | switch (type) | 297 | switch (type) |
318 | { | 298 | { |
319 | case 1: | 299 | case 1: |
320 | obj = new RuntimeRectangle(); | 300 | obj = new RuntimeRectangle(); |
321 | obj.import( objStr, parent ); | 301 | obj.import( jObj, parent ); |
322 | break; | 302 | break; |
323 | 303 | ||
324 | case 2: // circle | 304 | case 2: // circle |
325 | obj = new RuntimeOval(); | 305 | obj = new RuntimeOval(); |
326 | obj.import( objStr, parent ); | 306 | obj.import( jObj, parent ); |
327 | break; | 307 | break; |
328 | 308 | ||
329 | case 3: // line | 309 | case 3: // line |
330 | obj = new RuntimeLine(); | 310 | obj = new RuntimeLine(); |
331 | obj.import( objStr, parent ); | 311 | obj.import( jObj, parent ); |
332 | break; | 312 | break; |
333 | 313 | ||
334 | default: | 314 | default: |
@@ -337,7 +317,7 @@ function GLRuntime( canvas, importStr, assetPath ) | |||
337 | } | 317 | } |
338 | 318 | ||
339 | if (obj) | 319 | if (obj) |
340 | this.addObject( obj ); | 320 | this.addObject( obj, parent ); |
341 | 321 | ||
342 | return obj; | 322 | return obj; |
343 | } | 323 | } |
@@ -396,17 +376,16 @@ function GLRuntime( canvas, importStr, assetPath ) | |||
396 | 376 | ||
397 | this.remapAssetFolder = function( url ) | 377 | this.remapAssetFolder = function( url ) |
398 | { | 378 | { |
399 | /* | 379 | // var searchStr = "assets/"; |
400 | var searchStr = "assets/"; | 380 | // var index = url.indexOf( searchStr ); |
401 | var index = url.indexOf( searchStr ); | 381 | // var rtnPath = url; |
402 | var rtnPath = url; | 382 | // if (index >= 0) |
403 | if (index >= 0) | 383 | // { |
404 | { | 384 | // rtnPath = url.substr( index + searchStr.length ); |
405 | rtnPath = url.substr( index + searchStr.length ); | 385 | // rtnPath = this._assetPath + rtnPath; |
406 | rtnPath = this._assetPath + rtnPath; | 386 | // } |
407 | } | 387 | // return rtnPath; |
408 | return rtnPath; | 388 | |
409 | */ | ||
410 | return url; | 389 | return url; |
411 | } | 390 | } |
412 | 391 | ||
@@ -450,8 +429,7 @@ function GLRuntime( canvas, importStr, assetPath ) | |||
450 | } | 429 | } |
451 | 430 | ||
452 | // start RDGE or load Canvas 2D objects | 431 | // start RDGE or load Canvas 2D objects |
453 | var index = importStr.indexOf( "scenedata: " ); | 432 | if (jObj.scenedata) this._useWebGL = true; |
454 | this._useWebGL = (index >= 0); | ||
455 | if (this._useWebGL) | 433 | if (this._useWebGL) |
456 | { | 434 | { |
457 | var id = canvas.getAttribute( "data-RDGE-id" ); | 435 | var id = canvas.getAttribute( "data-RDGE-id" ); |
@@ -515,16 +493,17 @@ function RuntimeGeomObj() | |||
515 | { | 493 | { |
516 | } | 494 | } |
517 | 495 | ||
518 | this.importMaterials = function(importStr) | 496 | this.importMaterials = function(jObj) |
519 | { | 497 | { |