diff options
Diffstat (limited to 'js/lib/drawing/world.js')
-rwxr-xr-x | js/lib/drawing/world.js | 170 |
1 files changed, 169 insertions, 1 deletions
diff --git a/js/lib/drawing/world.js b/js/lib/drawing/world.js index 049145ce..5a054224 100755 --- a/js/lib/drawing/world.js +++ b/js/lib/drawing/world.js | |||
@@ -117,6 +117,8 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
117 | 117 | ||
118 | this.getRenderer = function() { return this.renderer; }; | 118 | this.getRenderer = function() { return this.renderer; }; |
119 | 119 | ||
120 | // Flag to play/pause animation at authortime | ||
121 | this._previewAnimation = true; | ||
120 | //////////////////////////////////////////////////////////////////////////////////// | 122 | //////////////////////////////////////////////////////////////////////////////////// |
121 | // RDGE | 123 | // RDGE |
122 | // local variables | 124 | // local variables |
@@ -236,7 +238,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
236 | if (this._canvas.task) { | 238 | if (this._canvas.task) { |
237 | this._firstRender = false; | 239 | this._firstRender = false; |
238 | 240 | ||
239 | if (!this.hasAnimatedMaterials()) { | 241 | if (!this.hasAnimatedMaterials() || !this._previewAnimation) { |
240 | this._canvas.task.stop(); | 242 | this._canvas.task.stop(); |
241 | //this._renderCount = 10; | 243 | //this._renderCount = 10; |
242 | } | 244 | } |
@@ -363,6 +365,7 @@ var World = function GLWorld( canvas, use3D, preserveDrawingBuffer ) { | |||
363 | if (this._useWebGL) { | 365 | if (this._useWebGL) { |
364 | rdgeStarted = true; | 366 | rdgeStarted = true; |
365 | this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" ); | 367 | this._canvas.rdgeid = this._canvas.getAttribute( "data-RDGE-id" ); |
368 | g_Engine.unregisterCanvas( this._canvas ) | ||
366 | g_Engine.registerCanvas(this._canvas, this); | 369 | g_Engine.registerCanvas(this._canvas, this); |
367 | RDGEStart( this._canvas ); | 370 | RDGEStart( this._canvas ); |
368 | this._canvas.task.stop() | 371 | this._canvas.task.stop() |
@@ -729,6 +732,85 @@ World.prototype.getShapeFromPoint = function( offsetX, offsetY ) { | |||
729 | } | 732 | } |
730 | }; | 733 | }; |
731 | 734 | ||
735 | |||
736 | |||
737 | World.prototype.exportJSON = function() | ||
738 | { | ||
739 | // world properties | ||
740 | var worldObj = | ||
741 | { | ||
742 | 'version' : 1.1, | ||
743 | 'id' : this.getCanvas().getAttribute( "data-RDGE-id" ), | ||
744 | 'fov' : this._fov, | ||
745 | 'zNear' : this._zNear, | ||
746 | 'zFar' : this._zFar, | ||
747 | 'viewDist' : this._viewDist, | ||
748 | 'webGL' : this._useWebGL | ||
749 | }; | ||
750 | |||
751 | // RDGE scenegraph | ||
752 | if (this._useWebGL) | ||
753 | worldObj.scenedata = this.myScene.exportJSON(); | ||
754 | |||
755 | // object data | ||
756 | var strArray = []; | ||
757 | this.exportObjectsJSON( this._geomRoot, worldObj ); | ||
758 | |||
759 | // You would think that the RDGE export function | ||
760 | // would not be destructive of the data. You would be wrong... | ||
761 | // We need to rebuild everything | ||
762 | if (this._useWebGL) | ||
763 | { | ||
764 | if (worldObj.children && (worldObj.children.length === 1)) | ||
765 | { | ||
766 | this.rebuildTree(this._geomRoot); | ||
767 | this.restartRenderLoop(); | ||
768 | } | ||
769 | } | ||
770 | |||
771 | // convert the object to a string | ||
772 | var jStr = JSON.stringify( worldObj ); | ||
773 | |||
774 | // prepend some version information to the string. | ||
775 | // this string is also used to differentiate between JSON | ||
776 | // and pre-JSON versions of fileIO. | ||
777 | // the ending ';' in the version string is necessary | ||
778 | jStr = "v1.0;" + jStr; | ||
779 | |||
780 | return jStr; | ||
781 | } | ||
782 | |||
783 | World.prototype.rebuildTree = function( obj ) | ||
784 | { | ||
785 | if (!obj) return; | ||
786 | |||
787 | obj.buildBuffers(); | ||
788 | |||
789 | if (obj.getChild()) { | ||
790 | this.rebuildTree( obj.getChild () ); | ||
791 | } | ||
792 | |||
793 | if (obj.getNext()) | ||
794 | this.rebuildTree( obj.getNext() ); | ||
795 | } | ||
796 | |||
797 | World.prototype.exportObjectsJSON = function( obj, parentObj ) | ||
798 | { | ||
799 | if (!obj) return; | ||
800 | |||
801 | var jObj = obj.exportJSON(); | ||
802 | if (!parentObj.children) parentObj.children = []; | ||
803 | parentObj.children.push( jObj ); | ||
804 | |||
805 | if (obj.getChild()) { | ||
806 | this.exportObjectsJSON( obj.getChild (), jObj ); | ||
807 | } | ||
808 | |||
809 | if (obj.getNext()) | ||
810 | this.exportObjectsJSON( obj.getNext(), parentObj ); | ||
811 | } | ||
812 | |||
813 | /* | ||
732 | World.prototype.export = function() | 814 | World.prototype.export = function() |
733 | { | 815 | { |
734 | var exportStr = "GLWorld 1.0\n"; | 816 | var exportStr = "GLWorld 1.0\n"; |
@@ -791,6 +873,7 @@ World.prototype.exportObjects = function( obj ) { | |||
791 | 873 | ||
792 | return rtnStr; | 874 | return rtnStr; |
793 | }; | 875 | }; |
876 | */ | ||
794 | 877 | ||
795 | World.prototype.findTransformNodeByMaterial = function( materialNode, trNode ) { | 878 | World.prototype.findTransformNodeByMaterial = function( materialNode, trNode ) { |
796 | //if (trNode == null) trNode = this._ctrNode; | 879 | //if (trNode == null) trNode = this._ctrNode; |
@@ -810,6 +893,91 @@ World.prototype.findTransformNodeByMaterial = function( materialNode, trNode ) | |||
810 | return rtnNode; | 893 | return rtnNode; |
811 | }; | 894 | }; |
812 | 895 | ||
896 | World.prototype.importJSON = function( jObj ) | ||
897 | { | ||
898 | if (jObj.webGL) | ||
899 | { | ||
900 | // start RDGE | ||
901 | rdgeStarted = true; | ||
902 | var id = this._canvas.getAttribute( "data-RDGE-id" ); | ||
903 | this._canvas.rdgeid = id; | ||
904 | g_Engine.registerCanvas(this._canvas, this); | ||
905 | RDGEStart( this._canvas ); | ||
906 | this._canvas.task.stop() | ||
907 | } | ||
908 | |||
909 | // import the objects | ||
910 | // there should be exactly one child of the parent object | ||
911 | if (jObj.children && (jObj.children.length === 1)) | ||
912 | this.importObjectsJSON( jObj.children[0] ); | ||
913 | else | ||
914 | throw new Error ("unrecoverable canvas import error - inconsistent root object: " + jObj.children ); | ||
915 | |||
916 | if (!this._useWebGL) | ||
917 | { | ||
918 | // render using canvas 2D | ||
919 | this.render(); | ||
920 | } | ||
921 | else | ||
922 | this.restartRenderLoop(); | ||
923 | } | ||
924 | |||
925 | World.prototype.importObjectsJSON = function( jObj, parentGeomObj ) | ||
926 | { | ||
927 | // read the next object | ||
928 | var gObj = this.importObjectJSON( jObj, parentGeomObj ); | ||
929 | |||
930 | // determine if we have children | ||
931 | if (jObj.children) | ||
932 | { | ||
933 | var nKids = ojObjbj.chilodren.length; | ||
934 | for (var i=0; i<nKids; i++) | ||
935 | { | ||
936 | var child = jObj.children[i]; | ||
937 | this.importObjectsJSON( child, gObj ); | ||
938 | } | ||
939 | } | ||
940 | } | ||
941 | |||
942 | World.prototype.importObjectJSON = function( jObj, parentGeomObj ) | ||
943 | { | ||
944 | var type = jObj.type; | ||
945 | var BrushStroke = require("js/lib/geom/brush-stroke").BrushStroke; | ||
946 | |||
947 | var obj; | ||
948 | switch (type) | ||
949 | { | ||
950 | case 1: | ||
951 | obj = new Rectangle(); | ||
952 | obj.importJSON( jObj ); | ||
953 | break; | ||
954 | |||
955 | case 2: // circle | ||
956 | obj = new Circle(); | ||
957 | obj.importJSON( jObj ); | ||
958 | break; | ||
959 | |||
960 | case 3: // line | ||
961 | obj = new Line(); | ||
962 | obj.importJSON( jObj ); | ||
963 | break; | ||
964 | |||
965 | case 6: //brush stroke | ||
966 | obj = new BrushStroke(); | ||
967 | obj.importJSON(jObj); | ||
968 | break; | ||
969 | |||
970 | default: | ||
971 | throw new Error( "Unrecognized object type: " + type ); | ||
972 | break; | ||
973 | } | ||
974 | |||
975 | if (obj) | ||
976 | this.addObject( obj, parentGeomObj ); | ||
977 | |||
978 | return obj; | ||
979 | }; | ||
980 | |||
813 | World.prototype.import = function( importStr ) { | 981 | World.prototype.import = function( importStr ) { |
814 | // import the worldattributes - not currently used | 982 | // import the worldattributes - not currently used |
815 | 983 | ||