Menubar.File.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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( 'Are you sure?' ) ) {
  20. editor.config.setKey(
  21. 'camera/position', [ 500, 250, 500 ],
  22. 'camera/target', [ 0, 0, 0 ]
  23. );
  24. editor.storage.clear( function () {
  25. location.href = location.pathname;
  26. } );
  27. }
  28. } );
  29. options.add( option );
  30. //
  31. options.add( new UI.HorizontalRule() );
  32. // Import
  33. var fileInput = document.createElement( 'input' );
  34. fileInput.type = 'file';
  35. fileInput.addEventListener( 'change', function ( event ) {
  36. editor.loader.loadFile( fileInput.files[ 0 ] );
  37. } );
  38. var option = new UI.Panel();
  39. option.setClass( 'option' );
  40. option.setTextContent( 'Import' );
  41. option.onClick( function () {
  42. fileInput.click();
  43. } );
  44. options.add( option );
  45. //
  46. options.add( new UI.HorizontalRule() );
  47. // Export Geometry
  48. var option = new UI.Panel();
  49. option.setClass( 'option' );
  50. option.setTextContent( 'Export Geometry' );
  51. option.onClick( function () {
  52. var object = editor.selected;
  53. if ( object === null ) {
  54. alert( 'No object selected.' );
  55. return;
  56. }
  57. var geometry = object.geometry;
  58. if ( geometry === undefined ) {
  59. alert( 'The selected object doesn\'t have geometry.' );
  60. return;
  61. }
  62. var output = geometry.toJSON();
  63. output = JSON.stringify( output, null, '\t' );
  64. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  65. exportString( output );
  66. } );
  67. options.add( option );
  68. // Export Object
  69. var option = new UI.Panel();
  70. option.setClass( 'option' );
  71. option.setTextContent( 'Export Object' );
  72. option.onClick( function () {
  73. var object = editor.selected;
  74. if ( object === null ) {
  75. alert( 'No object selected' );
  76. return;
  77. }
  78. var output = object.toJSON();
  79. output = JSON.stringify( output, null, '\t' );
  80. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  81. exportString( output );
  82. } );
  83. options.add( option );
  84. // Export Scene
  85. var option = new UI.Panel();
  86. option.setClass( 'option' );
  87. option.setTextContent( 'Export Scene' );
  88. option.onClick( function () {
  89. var output = editor.scene.toJSON();
  90. output = JSON.stringify( output, null, '\t' );
  91. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  92. exportString( output );
  93. } );
  94. options.add( option );
  95. // Export OBJ
  96. var option = new UI.Panel();
  97. option.setClass( 'option' );
  98. option.setTextContent( 'Export OBJ' );
  99. option.onClick( function () {
  100. var object = editor.selected;
  101. if ( object === null ) {
  102. alert( 'No object selected.' );
  103. return;
  104. }
  105. var geometry = object.geometry;
  106. if ( geometry === undefined ) {
  107. alert( 'The selected object doesn\'t have geometry.' );
  108. return;
  109. }
  110. var exporter = new THREE.OBJExporter();
  111. exportString( exporter.parse( geometry ) );
  112. } );
  113. options.add( option );
  114. // Export STL
  115. var option = new UI.Panel();
  116. option.setClass( 'option' );
  117. option.setTextContent( 'Export STL' );
  118. option.onClick( function () {
  119. var exporter = new THREE.STLExporter();
  120. exportString( exporter.parse( editor.scene ) );
  121. } );
  122. options.add( option );
  123. //
  124. options.add( new UI.HorizontalRule() );
  125. /*
  126. // Test
  127. var option = new UI.Panel();
  128. option.setClass( 'option' );
  129. option.setTextContent( 'Test' );
  130. option.onClick( function () {
  131. var text = new UI.Text( 'blah' );
  132. editor.showDialog( text );
  133. } );
  134. options.add( option );
  135. */
  136. /*
  137. // Publish
  138. var option = new UI.Panel();
  139. option.setClass( 'option' );
  140. option.setTextContent( 'Publish' );
  141. option.onClick( function () {
  142. alert( 'Not yet...' );
  143. } );
  144. options.add( option );
  145. */
  146. //
  147. var exportString = function ( output ) {
  148. var blob = new Blob( [ output ], { type: 'text/plain' } );
  149. var objectURL = URL.createObjectURL( blob );
  150. window.open( objectURL, '_blank' );
  151. window.focus();
  152. };
  153. return container;
  154. };