Menubar.File.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Menubar.File = function ( editor ) {
  2. var container = new UI.Panel();
  3. container.setClass( 'menu' );
  4. var title = new UI.Panel();
  5. title.setTextContent( 'File' );
  6. title.setMargin( '0px' );
  7. title.setPadding( '8px' );
  8. container.add( title );
  9. //
  10. var options = new UI.Panel();
  11. options.setClass( 'options' );
  12. container.add( options );
  13. // new
  14. var option = new UI.Panel();
  15. option.setClass( 'option' );
  16. option.setTextContent( 'New' );
  17. option.onClick( function () {
  18. if ( confirm( 'Are you sure?' ) ) {
  19. editor.config.clear();
  20. editor.storage.clear( function () {
  21. location.href = location.pathname;
  22. } );
  23. }
  24. } );
  25. options.add( option );
  26. options.add( new UI.HorizontalRule() );
  27. // import
  28. var input = document.createElement( 'input' );
  29. input.type = 'file';
  30. input.addEventListener( 'change', function ( event ) {
  31. editor.loader.loadFile( input.files[ 0 ] );
  32. } );
  33. var option = new UI.Panel();
  34. option.setClass( 'option' );
  35. option.setTextContent( 'Import' );
  36. option.onClick( function () {
  37. input.click();
  38. } );
  39. options.add( option );
  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. var exportGeometry = function ( exporterClass ) {
  92. var object = editor.selected;
  93. var exporter = new exporterClass();
  94. var output;
  95. if ( exporter instanceof THREE.BufferGeometryExporter ||
  96. exporter instanceof THREE.GeometryExporter ) {
  97. output = JSON.stringify( exporter.parse( object.geometry ), null, '\t' );
  98. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  99. } else {
  100. output = exporter.parse( object.geometry );
  101. }
  102. var blob = new Blob( [ output ], { type: 'text/plain' } );
  103. var objectURL = URL.createObjectURL( blob );
  104. window.open( objectURL, '_blank' );
  105. window.focus();
  106. };
  107. var exportObject = function ( exporterClass ) {
  108. var object = editor.selected;
  109. var exporter = new exporterClass();
  110. var output = JSON.stringify( exporter.parse( object ), null, '\t' );
  111. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  112. var blob = new Blob( [ output ], { type: 'text/plain' } );
  113. var objectURL = URL.createObjectURL( blob );
  114. window.open( objectURL, '_blank' );
  115. window.focus();
  116. };
  117. var exportScene = function ( exporterClass ) {
  118. var exporter = new exporterClass();
  119. var output = JSON.stringify( exporter.parse( editor.scene ), null, '\t' );
  120. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  121. var blob = new Blob( [ output ], { type: 'text/plain' } );
  122. var objectURL = URL.createObjectURL( blob );
  123. window.open( objectURL, '_blank' );
  124. window.focus();
  125. };
  126. return container;
  127. }