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