aboutsummaryrefslogtreecommitdiff
path: root/js/lib/drawing
diff options
context:
space:
mode:
authorhwc4872012-03-16 12:26:30 -0700
committerhwc4872012-03-16 12:26:30 -0700
commita0d23354802ebc6b437698acb4b18d3395d47cd1 (patch)
treea0081c079c9fc557e10a828db9adeed5a91d5a72 /js/lib/drawing
parent57d4a82977a1f0e809511fe894886f88581d9615 (diff)
downloadninja-a0d23354802ebc6b437698acb4b18d3395d47cd1.tar.gz
Conversion to JSON based file IO for canvas2D and WebGL rendering
Diffstat (limited to 'js/lib/drawing')
-rwxr-xr-xjs/lib/drawing/world.js128
1 files changed, 128 insertions, 0 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js
index 44c9e37d..9e502c3e 100755
--- a/js/lib/drawing/world.js
+++ b/js/lib/drawing/world.js
@@ -727,6 +727,57 @@ World.prototype.getShapeFromPoint = function( offsetX, offsetY ) {
727 } 727 }
728}; 728};
729 729
730World.prototype.exportJSON = function()
731{
732 // world properties
733 var worldObj =
734 {
735 'version' : 1.1,
736 'id' : this.getCanvas().getAttribute( "data-RDGE-id" ),
737 'fov' : this._fov,
738 'zNear' : this._zNear,
739 'zFar' : this._zFar,
740 'viewDist' : this._viewDist,
741 'webGL' : this._useWebGL
742 };
743
744 // RDGE scenegraph
745 if (this._useWebGL)
746 worldObj.scenedata = this.myScene.exportJSON();
747
748 // object data
749 var strArray = [];
750 this.exportObjectsJSON( this._geomRoot, worldObj );
751
752 // convert the object to a string
753 var jStr = JSON.stringify( worldObj );
754
755 // the RDGE export function corrupts the data.
756 // rebuild the tree
757 var root = this._rootNode;
758 root.children = new Array();
759 if (worldObj.children && (worldObj.children.length === 1))
760 this.importObjectsJSON( worldObj.children[0] );
761
762 return jStr;
763}
764
765World.prototype.exportObjectsJSON = function( obj, parentObj )
766{
767 if (!obj) return;
768
769 var jObj = obj.exportJSON();
770 if (!parentObj.children) parentObj.children = [];
771 parentObj.children.push( jObj );
772
773 if (obj.getChild()) {
774 this.exportObjects( obj.getChild (), jObj );
775 }
776
777 if (obj.getNext())
778 this.exportObjects( obj.getNext(), parentObj );
779}
780
730World.prototype.export = function() 781World.prototype.export = function()
731{ 782{
732 var exportStr = "GLWorld 1.0\n"; 783 var exportStr = "GLWorld 1.0\n";
@@ -808,6 +859,83 @@ World.prototype.findTransformNodeByMaterial = function( materialNode, trNode )
808 return rtnNode; 859 return rtnNode;
809}; 860};
810 861
862World.prototype.importJSON = function( jObj )
863{
864 if (jObj.webGL)
865 {
866 // start RDGE
867 rdgeStarted = true;
868 var id = this._canvas.getAttribute( "data-RDGE-id" );
869 this._canvas.rdgeid = id;
870 g_Engine.registerCanvas(this._canvas, this);
871 RDGEStart( this._canvas );
872 this._canvas.task.stop()
873 }
874
875 // import the objects
876 // there should be exactly one child of the parent object
877 if (jObj.children && (jObj.children.length === 1))
878 this.importObjectsJSON( jObj.children[0] );
879 else
880 throw new Error ("unrecoverable canvas import error - inconsistent root object: " + jObj.children );
881
882 if (!this._useWebGL)
883 {
884 // render using canvas 2D
885 this.render();
886 }
887}
888
889World.prototype.importObjectsJSON = function( jObj, parentGeomObj )
890{
891 // read the next object
892 var gObj = this.importObjectJSON( jObj, parentGeomObj );
893
894 // determine if we have children
895 if (jObj.children)
896 {
897 var nKids = ojObjbj.chilodren.length;
898 for (var i=0; i<nKids; i++)
899 {
900 var child = jObj.children[i];
901 this.importObjectsJSON( child, gObj );
902 }
903 }
904}
905
906World.prototype.importObjectJSON = function( jObj, parentGeomObj )
907{
908 var type = jObj.type;
909
910 var obj;
911 switch (type)
912 {
913 case 1:
914 obj = new Rectangle();
915 obj.importJSON( jObj );
916 break;
917
918 case 2: // circle
919 obj = new Circle();
920 obj.importJSON( jObj );
921 break;
922
923 case 3: // line
924 obj = new Line();
925 obj.importJSON( jObj );
926 break;
927
928 default:
929 throw new Error( "Unrecognized object type: " + type );
930 break;
931 }
932
933 if (obj)
934 this.addObject( obj, parentGeomObj );
935
936 return obj;
937};
938
811World.prototype.import = function( importStr ) { 939World.prototype.import = function( importStr ) {
812 // import the worldattributes - not currently used 940 // import the worldattributes - not currently used
813 941