Menubar.File.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. Menubar.File = function ( editor ) {
  2. var container = new UI.Panel();
  3. container.setClass( 'menu' );
  4. container.onMouseOver( function () { options.setDisplay( 'block' ) } );
  5. container.onMouseOut( function () { options.setDisplay( 'none' ) } );
  6. container.onClick( function () { options.setDisplay( 'block' ) } );
  7. var title = new UI.Panel();
  8. title.setTextContent( 'File' );
  9. title.setMargin( '0px' );
  10. title.setPadding( '8px' );
  11. container.add( title );
  12. //
  13. var options = new UI.Panel();
  14. options.setClass( 'options' );
  15. options.setDisplay( 'none' );
  16. container.add( options );
  17. // new
  18. var option = new UI.Panel();
  19. option.setClass( 'option' );
  20. option.setTextContent( 'New' );
  21. option.onClick( function () {
  22. if ( confirm( 'Are you sure?' ) ) {
  23. editor.localStorage.clear();
  24. location.href = location.pathname;
  25. }
  26. } );
  27. options.add( option );
  28. options.add( new UI.HorizontalRule() );
  29. // import
  30. var input = document.createElement( 'input' );
  31. input.type = 'file';
  32. input.addEventListener( 'change', function ( event ) {
  33. editor.loader.loadFile( input.files[ 0 ] );
  34. } );
  35. var option = new UI.Panel();
  36. option.setClass( 'option' );
  37. option.setTextContent( 'Import' );
  38. option.onClick( function () {
  39. input.click();
  40. } );
  41. options.add( option );
  42. options.add( new UI.HorizontalRule() );
  43. // export geometry
  44. var option = new UI.Panel();
  45. option.setClass( 'option' );
  46. option.setTextContent( 'Export Geometry' );
  47. option.onClick( function () {
  48. var geometry = editor.selected.geometry;
  49. if ( geometry instanceof THREE.BufferGeometry ) {
  50. exportGeometry( THREE.BufferGeometryExporter );
  51. } else if ( geometry instanceof THREE.Geometry ) {
  52. exportGeometry( THREE.GeometryExporter );
  53. }
  54. } );
  55. options.add( option );
  56. /*
  57. // export scene
  58. var option = new UI.Panel();
  59. option.setClass( 'option' );
  60. option.setTextContent( 'Export Scene' );
  61. option.onClick( function () {
  62. exportScene( THREE.SceneExporter );
  63. } );
  64. options.add( option );
  65. */
  66. // export object
  67. var option = new UI.Panel();
  68. option.setClass( 'option' );
  69. option.setTextContent( 'Export Object' );
  70. option.onClick( function () {
  71. exportObject( THREE.ObjectExporter );
  72. } );
  73. options.add( option );
  74. // export scene
  75. var option = new UI.Panel();
  76. option.setClass( 'option' );
  77. option.setTextContent( 'Export Scene' );
  78. option.onClick( function () {
  79. exportScene( THREE.ObjectExporter );
  80. } );
  81. options.add( option );
  82. // export OBJ
  83. var option = new UI.Panel();
  84. option.setClass( 'option' );
  85. option.setTextContent( 'Export OBJ' );
  86. option.onClick( function () {
  87. exportGeometry( THREE.OBJExporter );
  88. } );
  89. options.add( option );
  90. var exportGeometry = function ( exporterClass ) {
  91. var object = editor.selected;
  92. if ( object.geometry === undefined ) {
  93. alert( "Selected object doesn't have any geometry" );
  94. return;
  95. }
  96. var exporter = new exporterClass();
  97. var output;
  98. if ( exporter instanceof THREE.BufferGeometryExporter || exporter instanceof THREE.GeometryExporter ) {
  99. output = JSON.stringify( exporter.parse( object.geometry ), null, '\t' );
  100. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  101. } else {
  102. output = exporter.parse( object.geometry );
  103. }
  104. var blob = new Blob( [ output ], { type: 'text/plain' } );
  105. var objectURL = URL.createObjectURL( blob );
  106. window.open( objectURL, '_blank' );
  107. window.focus();
  108. };
  109. var exportObject = function ( exporterClass ) {
  110. var exporter = new exporterClass();
  111. var object = editor.selected;
  112. var output = JSON.stringify( exporter.parse( object ), null, '\t' );
  113. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  114. var blob = new Blob( [ output ], { type: 'text/plain' } );
  115. var objectURL = URL.createObjectURL( blob );
  116. window.open( objectURL, '_blank' );
  117. window.focus();
  118. };
  119. var exportScene = function ( exporterClass ) {
  120. var exporter = new exporterClass();
  121. var output = JSON.stringify( exporter.parse( editor.scene ), null, '\t' );
  122. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  123. var blob = new Blob( [ output ], { type: 'text/plain' } );
  124. var objectURL = URL.createObjectURL( blob );
  125. window.open( objectURL, '_blank' );
  126. window.focus();
  127. };
  128. options.add( new UI.HorizontalRule() );
  129. // share
  130. var option = new UI.Panel();
  131. option.setClass( 'option' );
  132. option.setTextContent( 'Share' );
  133. option.onClick( function () {
  134. var exporter = new THREE.ObjectExporter();
  135. var string = JSON.stringify( exporter.parse( editor.scene ) );
  136. window.location.hash = 'A/' + window.btoa( RawDeflate.deflate( string ) );
  137. } );
  138. options.add( option );
  139. return container;
  140. }