Menubar.File.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 geometry = editor.selected.geometry;
  47. if ( geometry instanceof THREE.BufferGeometry ) {
  48. exportGeometry( THREE.BufferGeometryExporter );
  49. } else if ( geometry instanceof THREE.Geometry ) {
  50. exportGeometry( THREE.GeometryExporter );
  51. }
  52. } );
  53. options.add( option );
  54. /*
  55. // export scene
  56. var option = new UI.Panel();
  57. option.setClass( 'option' );
  58. option.setTextContent( 'Export Scene' );
  59. option.onClick( function () {
  60. exportScene( THREE.SceneExporter );
  61. } );
  62. options.add( option );
  63. */
  64. // export object
  65. var option = new UI.Panel();
  66. option.setClass( 'option' );
  67. option.setTextContent( 'Export Object' );
  68. option.onClick( function () {
  69. exportObject( THREE.ObjectExporter );
  70. } );
  71. options.add( option );
  72. // export scene
  73. var option = new UI.Panel();
  74. option.setClass( 'option' );
  75. option.setTextContent( 'Export Scene' );
  76. option.onClick( function () {
  77. exportScene( THREE.ObjectExporter );
  78. } );
  79. options.add( option );
  80. // export OBJ
  81. var option = new UI.Panel();
  82. option.setClass( 'option' );
  83. option.setTextContent( 'Export OBJ' );
  84. option.onClick( function () {
  85. exportGeometry( THREE.OBJExporter );
  86. } );
  87. options.add( option );
  88. var exportGeometry = function ( exporterClass ) {
  89. var object = editor.selected;
  90. if ( object.geometry === undefined ) {
  91. alert( "Selected object doesn't have any geometry" );
  92. return;
  93. }
  94. var exporter = new exporterClass();
  95. var output;
  96. if ( exporter instanceof THREE.BufferGeometryExporter || 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 exporter = new exporterClass();
  109. var object = editor.selected;
  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. }