Menubar.File.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. // Open Editor-JSON
  25. var editorJsonInput = document.createElement( 'input' );
  26. editorJsonInput.type = 'file';
  27. editorJsonInput.addEventListener( 'change', function ( event ) {
  28. var reader = new FileReader();
  29. reader.addEventListener( 'load', function ( event ) {
  30. var contents = event.target.result;
  31. var data;
  32. try {
  33. data = JSON.parse( contents );
  34. editor.clear();
  35. editor.fromJSON( data );
  36. } catch ( error ) {
  37. alert( error );
  38. return;
  39. }
  40. }, false );
  41. reader.readAsText( editorJsonInput.files[ 0 ] );
  42. } );
  43. var option = new UI.Panel();
  44. option.setClass( 'option' );
  45. option.setTextContent( 'Open Editor-JSON...' );
  46. option.onClick( function () {
  47. editorJsonInput.click();
  48. } );
  49. options.add( option );
  50. // Save Editor-JSON
  51. var option = new UI.Panel();
  52. option.setClass( 'option' );
  53. option.setTextContent( 'Save Editor-JSON...' );
  54. option.onClick( function () {
  55. var output = editor.toJSON();
  56. output = JSON.stringify( output, null, '\t' );
  57. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  58. exportString( output, 'editor.json' );
  59. } );
  60. options.add( option );
  61. //
  62. options.add( new UI.HorizontalRule() );
  63. // Import
  64. var fileInput = document.createElement( 'input' );
  65. fileInput.type = 'file';
  66. fileInput.addEventListener( 'change', function ( event ) {
  67. editor.loader.loadFile( fileInput.files[ 0 ] );
  68. } );
  69. var option = new UI.Panel();
  70. option.setClass( 'option' );
  71. option.setTextContent( 'Import' );
  72. option.onClick( function () {
  73. fileInput.click();
  74. } );
  75. options.add( option );
  76. //
  77. options.add( new UI.HorizontalRule() );
  78. // Export Geometry
  79. var option = new UI.Panel();
  80. option.setClass( 'option' );
  81. option.setTextContent( 'Export Geometry' );
  82. option.onClick( function () {
  83. var object = editor.selected;
  84. if ( object === null ) {
  85. alert( 'No object selected.' );
  86. return;
  87. }
  88. var geometry = object.geometry;
  89. if ( geometry === undefined ) {
  90. alert( 'The selected object doesn\'t have geometry.' );
  91. return;
  92. }
  93. var output = geometry.toJSON();
  94. output = JSON.stringify( output, null, '\t' );
  95. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  96. exportString( output, 'geometry.json' );
  97. } );
  98. options.add( option );
  99. // Export Object
  100. var option = new UI.Panel();
  101. option.setClass( 'option' );
  102. option.setTextContent( 'Export Object' );
  103. option.onClick( function () {
  104. var object = editor.selected;
  105. if ( object === null ) {
  106. alert( 'No object selected' );
  107. return;
  108. }
  109. var output = object.toJSON();
  110. output = JSON.stringify( output, null, '\t' );
  111. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  112. exportString( output, 'model.json' );
  113. } );
  114. options.add( option );
  115. // Export Scene
  116. var option = new UI.Panel();
  117. option.setClass( 'option' );
  118. option.setTextContent( 'Export Scene' );
  119. option.onClick( function () {
  120. var output = editor.scene.toJSON();
  121. output = JSON.stringify( output, null, '\t' );
  122. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  123. exportString( output, 'scene.json' );
  124. } );
  125. options.add( option );
  126. // Export OBJ
  127. var option = new UI.Panel();
  128. option.setClass( 'option' );
  129. option.setTextContent( 'Export OBJ' );
  130. option.onClick( function () {
  131. var object = editor.selected;
  132. if ( object === null ) {
  133. alert( 'No object selected.' );
  134. return;
  135. }
  136. var exporter = new THREE.OBJExporter();
  137. exportString( exporter.parse( object ), 'model.obj' );
  138. } );
  139. options.add( option );
  140. // Export STL
  141. var option = new UI.Panel();
  142. option.setClass( 'option' );
  143. option.setTextContent( 'Export STL' );
  144. option.onClick( function () {
  145. var exporter = new THREE.STLExporter();
  146. exportString( exporter.parse( editor.scene ), 'model.stl' );
  147. } );
  148. options.add( option );
  149. //
  150. options.add( new UI.HorizontalRule() );
  151. // Publish
  152. var option = new UI.Panel();
  153. option.setClass( 'option' );
  154. option.setTextContent( 'Publish' );
  155. option.onClick( function () {
  156. var camera = editor.camera;
  157. var zip = new JSZip();
  158. zip.file( 'index.html', [
  159. '<!DOCTYPE html>',
  160. '<html lang="en">',
  161. ' <head>',
  162. ' <title>three.js</title>',
  163. ' <meta charset="utf-8">',
  164. ' <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">',
  165. ' <style>',
  166. ' body {',
  167. ' margin: 0px;',
  168. ' overflow: hidden;',
  169. ' }',
  170. ' </style>',
  171. ' </head>',
  172. ' <body ontouchstart="">',
  173. ' <script src="js/three.min.js"></script>',
  174. ' <script src="js/app.js"></script>',
  175. ' <script>',
  176. '',
  177. ' var loader = new THREE.XHRLoader();',
  178. ' loader.load( \'app.json\', function ( text ) {',
  179. '',
  180. ' var player = new APP.Player();',
  181. ' player.load( JSON.parse( text ) );',
  182. ' player.setSize( window.innerWidth, window.innerHeight );',
  183. ' player.play();',
  184. '',
  185. ' document.body.appendChild( player.dom );',
  186. '',
  187. ' window.addEventListener( \'resize\', function () {',
  188. ' player.setSize( window.innerWidth, window.innerHeight );',
  189. ' } );',
  190. '',
  191. ' } );',
  192. '',
  193. ' </script>',
  194. ' </body>',
  195. '</html>'
  196. ].join( '\n' ) );
  197. //
  198. var output = editor.toJSON();
  199. output = JSON.stringify( output, null, '\t' );
  200. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  201. zip.file( 'app.json', output );
  202. //
  203. var manager = new THREE.LoadingManager( function () {
  204. location.href = 'data:application/zip;base64,' + zip.generate();
  205. } );
  206. var loader = new THREE.XHRLoader( manager );
  207. loader.load( 'js/libs/app.js', function ( content ) {
  208. zip.file( 'js/app.js', content );
  209. } );
  210. loader.load( '../build/three.min.js', function ( content ) {
  211. zip.file( 'js/three.min.js', content );
  212. } );
  213. } );
  214. options.add( option );
  215. /*
  216. // Test
  217. var option = new UI.Panel();
  218. option.setClass( 'option' );
  219. option.setTextContent( 'Test' );
  220. option.onClick( function () {
  221. var text = new UI.Text( 'blah' );
  222. editor.showDialog( text );
  223. } );
  224. options.add( option );
  225. */
  226. //
  227. var link = document.createElement( 'a' );
  228. link.style.display = 'none';
  229. document.body.appendChild( link ); // Firefox workaround, see #6594
  230. var exportString = function ( output, filename ) {
  231. var blob = new Blob( [ output ], { type: 'text/plain' } );
  232. var objectURL = URL.createObjectURL( blob );
  233. link.href = objectURL;
  234. link.download = filename || 'data.json';
  235. link.target = '_blank';
  236. var event = document.createEvent("MouseEvents");
  237. event.initMouseEvent(
  238. "click", true, false, window, 0, 0, 0, 0, 0
  239. , false, false, false, false, 0, null
  240. );
  241. link.dispatchEvent(event);
  242. };
  243. return container;
  244. };