/editor/js/Menubar.File.js

https://gitlab.com/rs15010411/three.js · JavaScript · 335 lines · 170 code · 128 blank · 37 comment · 8 complexity · 249d3ca2de103e32d2f5445365c2c038 MD5 · raw file

  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Menubar.File = function ( editor ) {
  5. var container = new UI.Panel();
  6. container.setClass( 'menu' );
  7. var title = new UI.Panel();
  8. title.setClass( 'title' );
  9. title.setTextContent( 'File' );
  10. container.add( title );
  11. var options = new UI.Panel();
  12. options.setClass( 'options' );
  13. container.add( options );
  14. // New
  15. var option = new UI.Row();
  16. option.setClass( 'option' );
  17. option.setTextContent( 'New' );
  18. option.onClick( function () {
  19. if ( confirm( 'Any unsaved data will be lost. Are you sure?' ) ) {
  20. editor.clear();
  21. }
  22. } );
  23. options.add( option );
  24. //
  25. options.add( new UI.HorizontalRule() );
  26. // Import
  27. var fileInput = document.createElement( 'input' );
  28. fileInput.type = 'file';
  29. fileInput.addEventListener( 'change', function ( event ) {
  30. editor.loader.loadFile( fileInput.files[ 0 ] );
  31. } );
  32. var option = new UI.Row();
  33. option.setClass( 'option' );
  34. option.setTextContent( 'Import' );
  35. option.onClick( function () {
  36. fileInput.click();
  37. } );
  38. options.add( option );
  39. //
  40. options.add( new UI.HorizontalRule() );
  41. // Export Geometry
  42. var option = new UI.Row();
  43. option.setClass( 'option' );
  44. option.setTextContent( 'Export Geometry' );
  45. option.onClick( function () {
  46. var object = editor.selected;
  47. if ( object === null ) {
  48. alert( 'No object selected.' );
  49. return;
  50. }
  51. var geometry = object.geometry;
  52. if ( geometry === undefined ) {
  53. alert( 'The selected object doesn\'t have geometry.' );
  54. return;
  55. }
  56. var output = geometry.toJSON();
  57. try {
  58. output = JSON.stringify( output, null, '\t' );
  59. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  60. } catch ( e ) {
  61. output = JSON.stringify( output );
  62. }
  63. saveString( output, 'geometry.json' );
  64. } );
  65. options.add( option );
  66. // Export Object
  67. var option = new UI.Row();
  68. option.setClass( 'option' );
  69. option.setTextContent( 'Export Object' );
  70. option.onClick( function () {
  71. var object = editor.selected;
  72. if ( object === null ) {
  73. alert( 'No object selected' );
  74. return;
  75. }
  76. var output = object.toJSON();
  77. try {
  78. output = JSON.stringify( output, null, '\t' );
  79. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  80. } catch ( e ) {
  81. output = JSON.stringify( output );
  82. }
  83. saveString( output, 'model.json' );
  84. } );
  85. options.add( option );
  86. // Export Scene
  87. var option = new UI.Row();
  88. option.setClass( 'option' );
  89. option.setTextContent( 'Export Scene' );
  90. option.onClick( function () {
  91. var output = editor.scene.toJSON();
  92. try {
  93. output = JSON.stringify( output, null, '\t' );
  94. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  95. } catch ( e ) {
  96. output = JSON.stringify( output );
  97. }
  98. saveString( output, 'scene.json' );
  99. } );
  100. options.add( option );
  101. // Export OBJ
  102. var option = new UI.Row();
  103. option.setClass( 'option' );
  104. option.setTextContent( 'Export OBJ' );
  105. option.onClick( function () {
  106. var object = editor.selected;
  107. if ( object === null ) {
  108. alert( 'No object selected.' );
  109. return;
  110. }
  111. var exporter = new THREE.OBJExporter();
  112. saveString( exporter.parse( object ), 'model.obj' );
  113. } );
  114. options.add( option );
  115. // Export STL
  116. var option = new UI.Row();
  117. option.setClass( 'option' );
  118. option.setTextContent( 'Export STL' );
  119. option.onClick( function () {
  120. var exporter = new THREE.STLExporter();
  121. saveString( exporter.parse( editor.scene ), 'model.stl' );
  122. } );
  123. options.add( option );
  124. //
  125. options.add( new UI.HorizontalRule() );
  126. // Publish
  127. var option = new UI.Row();
  128. option.setClass( 'option' );
  129. option.setTextContent( 'Publish' );
  130. option.onClick( function () {
  131. var zip = new JSZip();
  132. //
  133. var output = editor.toJSON();
  134. output.metadata.type = 'App';
  135. delete output.history;
  136. var vr = output.project.vr;
  137. output = JSON.stringify( output, null, '\t' );
  138. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  139. zip.file( 'app.json', output );
  140. //
  141. var manager = new THREE.LoadingManager( function () {
  142. save( zip.generate( { type: 'blob' } ), 'download.zip' );
  143. } );
  144. var loader = new THREE.XHRLoader( manager );
  145. loader.load( 'js/libs/app/index.html', function ( content ) {
  146. var includes = [];
  147. if ( vr ) {
  148. includes.push( '<script src="js/VRControls.js"></script>' );
  149. includes.push( '<script src="js/VREffect.js"></script>' );
  150. includes.push( '<script src="js/WebVR.js"></script>' );
  151. }
  152. content = content.replace( '<!-- includes -->', includes.join( '\n\t\t' ) );
  153. zip.file( 'index.html', content );
  154. } );
  155. loader.load( 'js/libs/app.js', function ( content ) {
  156. zip.file( 'js/app.js', content );
  157. } );
  158. loader.load( '../build/three.min.js', function ( content ) {
  159. zip.file( 'js/three.min.js', content );
  160. } );
  161. if ( vr ) {
  162. loader.load( '../examples/js/controls/VRControls.js', function ( content ) {
  163. zip.file( 'js/VRControls.js', content );
  164. } );
  165. loader.load( '../examples/js/effects/VREffect.js', function ( content ) {
  166. zip.file( 'js/VREffect.js', content );
  167. } );
  168. loader.load( '../examples/js/WebVR.js', function ( content ) {
  169. zip.file( 'js/WebVR.js', content );
  170. } );
  171. }
  172. } );
  173. options.add( option );
  174. /*
  175. // Publish (Dropbox)
  176. var option = new UI.Row();
  177. option.setClass( 'option' );
  178. option.setTextContent( 'Publish (Dropbox)' );
  179. option.onClick( function () {
  180. var parameters = {
  181. files: [
  182. { 'url': 'data:text/plain;base64,' + window.btoa( "Hello, World" ), 'filename': 'app/test.txt' }
  183. ]
  184. };
  185. Dropbox.save( parameters );
  186. } );
  187. options.add( option );
  188. */
  189. //
  190. var link = document.createElement( 'a' );
  191. link.style.display = 'none';
  192. document.body.appendChild( link ); // Firefox workaround, see #6594
  193. function save( blob, filename ) {
  194. link.href = URL.createObjectURL( blob );
  195. link.download = filename || 'data.json';
  196. link.click();
  197. // URL.revokeObjectURL( url ); breaks Firefox...
  198. }
  199. function saveString( text, filename ) {
  200. save( new Blob( [ text ], { type: 'text/plain' } ), filename );
  201. }
  202. return container;
  203. };