Menubar.File.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.Panel();
  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.Panel();
  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.Panel();
  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. output = JSON.stringify( output, null, '\t' );
  58. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  59. exportString( output );
  60. } );
  61. options.add( option );
  62. // Export Object
  63. var option = new UI.Panel();
  64. option.setClass( 'option' );
  65. option.setTextContent( 'Export Object' );
  66. option.onClick( function () {
  67. var object = editor.selected;
  68. if ( object === null ) {
  69. alert( 'No object selected' );
  70. return;
  71. }
  72. var output = object.toJSON();
  73. output = JSON.stringify( output, null, '\t' );
  74. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  75. exportString( output );
  76. } );
  77. options.add( option );
  78. // Export Scene
  79. var option = new UI.Panel();
  80. option.setClass( 'option' );
  81. option.setTextContent( 'Export Scene' );
  82. option.onClick( function () {
  83. var output = editor.scene.toJSON();
  84. output = JSON.stringify( output, null, '\t' );
  85. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  86. exportString( output );
  87. } );
  88. options.add( option );
  89. // Export OBJ
  90. var option = new UI.Panel();
  91. option.setClass( 'option' );
  92. option.setTextContent( 'Export OBJ' );
  93. option.onClick( function () {
  94. var object = editor.selected;
  95. if ( object === null ) {
  96. alert( 'No object selected.' );
  97. return;
  98. }
  99. var exporter = new THREE.OBJExporter();
  100. exportString( exporter.parse( object ) );
  101. } );
  102. options.add( option );
  103. // Export STL
  104. var option = new UI.Panel();
  105. option.setClass( 'option' );
  106. option.setTextContent( 'Export STL' );
  107. option.onClick( function () {
  108. var exporter = new THREE.STLExporter();
  109. exportString( exporter.parse( editor.scene ) );
  110. } );
  111. options.add( option );
  112. //
  113. options.add( new UI.HorizontalRule() );
  114. // Publish
  115. var option = new UI.Panel();
  116. option.setClass( 'option' );
  117. option.setTextContent( 'Publish' );
  118. option.onClick( function () {
  119. var camera = editor.camera;
  120. var zip = new JSZip();
  121. zip.file( 'index.html', [
  122. '<!DOCTYPE html>',
  123. '<html lang="en">',
  124. ' <head>',
  125. ' <title>three.js</title>',
  126. ' <meta charset="utf-8">',
  127. ' <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">',
  128. ' <style>',
  129. ' body {',
  130. ' margin: 0px;',
  131. ' overflow: hidden;',
  132. ' }',
  133. ' </style>',
  134. ' </head>',
  135. ' <body ontouchstart="">',
  136. ' <script src="js/three.min.js"></script>',
  137. ' <script src="js/app.js"></script>',
  138. ' <script>',
  139. '',
  140. ' var loader = new THREE.XHRLoader();',
  141. ' loader.load( \'app.json\', function ( text ) {',
  142. '',
  143. ' var player = new APP.Player();',
  144. ' player.load( JSON.parse( text ) );',
  145. ' player.setSize( window.innerWidth, window.innerHeight );',
  146. ' player.play();',
  147. '',
  148. ' document.body.appendChild( player.dom );',
  149. '',
  150. ' } );',
  151. '',
  152. ' </script>',
  153. ' </body>',
  154. '</html>'
  155. ].join( '\n' ) );
  156. //
  157. var output = editor.toJSON();
  158. output = JSON.stringify( output, null, '\t' );
  159. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  160. zip.file( 'app.json', output );
  161. //
  162. var manager = new THREE.LoadingManager( function () {
  163. location.href = 'data:application/zip;base64,' + zip.generate();
  164. } );
  165. var loader = new THREE.XHRLoader( manager );
  166. loader.load( 'js/libs/app.js', function ( content ) {
  167. zip.file( 'js/app.js', content );
  168. } );
  169. loader.load( '../build/three.min.js', function ( content ) {
  170. zip.file( 'js/three.min.js', content );
  171. } );
  172. } );
  173. options.add( option );
  174. /*
  175. // Test
  176. var option = new UI.Panel();
  177. option.setClass( 'option' );
  178. option.setTextContent( 'Test' );
  179. option.onClick( function () {
  180. var text = new UI.Text( 'blah' );
  181. editor.showDialog( text );
  182. } );
  183. options.add( option );
  184. */
  185. //
  186. var exportString = function ( output ) {
  187. var blob = new Blob( [ output ], { type: 'text/plain' } );
  188. var objectURL = URL.createObjectURL( blob );
  189. window.open( objectURL, '_blank' );
  190. window.focus();
  191. };
  192. return container;
  193. };