Menubar.File.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. Menubar.File = function ( editor ) {
  2. var container = new UI.Panel();
  3. container.setClass( 'menu' );
  4. var title = new UI.Panel();
  5. title.setClass( 'title' );
  6. title.setTextContent( 'File' );
  7. container.add( title );
  8. var options = new UI.Panel();
  9. options.setClass( 'options' );
  10. container.add( options );
  11. // New
  12. var option = new UI.Panel();
  13. option.setClass( 'option' );
  14. option.setTextContent( 'New' );
  15. option.onClick( function () {
  16. if ( confirm( 'Are you sure?' ) ) {
  17. editor.config.clear();
  18. editor.storage.clear( function () {
  19. location.href = location.pathname;
  20. } );
  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. if ( geometry instanceof THREE.BufferGeometry ) {
  57. exportGeometry( THREE.BufferGeometryExporter );
  58. } else if ( geometry instanceof THREE.Geometry ) {
  59. exportGeometry( THREE.GeometryExporter );
  60. }
  61. } );
  62. options.add( option );
  63. // Export Object
  64. var option = new UI.Panel();
  65. option.setClass( 'option' );
  66. option.setTextContent( 'Export Object' );
  67. option.onClick( function () {
  68. if ( editor.selected === null ) {
  69. alert( 'No object selected' );
  70. return;
  71. }
  72. exportObject( THREE.ObjectExporter );
  73. } );
  74. options.add( option );
  75. // Export Scene
  76. var option = new UI.Panel();
  77. option.setClass( 'option' );
  78. option.setTextContent( 'Export Scene' );
  79. option.onClick( function () {
  80. exportScene( THREE.ObjectExporter );
  81. } );
  82. options.add( option );
  83. // Export OBJ
  84. var option = new UI.Panel();
  85. option.setClass( 'option' );
  86. option.setTextContent( 'Export OBJ' );
  87. option.onClick( function () {
  88. exportGeometry( THREE.OBJExporter );
  89. } );
  90. options.add( option );
  91. // Export STL
  92. var option = new UI.Panel();
  93. option.setClass( 'option' );
  94. option.setTextContent( 'Export STL' );
  95. option.onClick( function () {
  96. exportScene( THREE.STLExporter );
  97. } );
  98. options.add( option );
  99. //
  100. options.add( new UI.HorizontalRule() );
  101. // Test
  102. var option = new UI.Panel();
  103. option.setClass( 'option' );
  104. option.setTextContent( 'Test' );
  105. option.onClick( function () {
  106. var text = new UI.Text( 'blah' );
  107. editor.showDialog( text );
  108. } );
  109. options.add( option );
  110. //
  111. var exportGeometry = function ( exporterClass ) {
  112. var object = editor.selected;
  113. var exporter = new exporterClass();
  114. var output = exporter.parse( object.geometry );
  115. if ( exporter instanceof THREE.BufferGeometryExporter ||
  116. exporter instanceof THREE.GeometryExporter ) {
  117. output = JSON.stringify( output, null, '\t' );
  118. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  119. }
  120. var blob = new Blob( [ output ], { type: 'text/plain' } );
  121. var objectURL = URL.createObjectURL( blob );
  122. window.open( objectURL, '_blank' );
  123. window.focus();
  124. };
  125. var exportObject = function ( exporterClass ) {
  126. var object = editor.selected;
  127. var exporter = new exporterClass();
  128. var output = JSON.stringify( exporter.parse( object ), null, '\t' );
  129. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  130. var blob = new Blob( [ output ], { type: 'text/plain' } );
  131. var objectURL = URL.createObjectURL( blob );
  132. window.open( objectURL, '_blank' );
  133. window.focus();
  134. };
  135. var exportScene = function ( exporterClass ) {
  136. var exporter = new exporterClass();
  137. var output = exporter.parse( editor.scene );
  138. if ( exporter instanceof THREE.ObjectExporter ) {
  139. output = JSON.stringify( output, null, '\t' );
  140. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  141. }
  142. var blob = new Blob( [ output ], { type: 'text/plain' } );
  143. var objectURL = URL.createObjectURL( blob );
  144. window.open( objectURL, '_blank' );
  145. window.focus();
  146. };
  147. return container;
  148. };