Menubar.File.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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' ).setColor( '#666' );
  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. if ( localStorage.threejsEditor !== undefined ) {
  24. delete localStorage.threejsEditor;
  25. }
  26. location.href = location.pathname;
  27. }
  28. } );
  29. options.add( option );
  30. options.add( new UI.HorizontalRule() );
  31. // import
  32. var input = document.createElement( 'input' );
  33. input.type = 'file';
  34. input.addEventListener( 'change', function ( event ) {
  35. editor.loader.loadFile( input.files[ 0 ] );
  36. } );
  37. var option = new UI.Panel();
  38. option.setClass( 'option' );
  39. option.setTextContent( 'Import' );
  40. option.onClick( function () {
  41. input.click();
  42. } );
  43. options.add( option );
  44. options.add( new UI.HorizontalRule() );
  45. // export buffergeometry
  46. var option = new UI.Panel();
  47. option.setClass( 'option' );
  48. option.setTextContent( 'Export BufferGeometry' );
  49. option.onClick( function () {
  50. exportGeometry( THREE.BufferGeometryExporter );
  51. } );
  52. options.add( option );
  53. // export geometry
  54. var option = new UI.Panel();
  55. option.setClass( 'option' );
  56. option.setTextContent( 'Export Geometry' );
  57. option.onClick( function () {
  58. exportGeometry( THREE.GeometryExporter );
  59. } );
  60. options.add( option );
  61. /*
  62. // export scene
  63. var option = new UI.Panel();
  64. option.setClass( 'option' );
  65. option.setTextContent( 'Export Scene' );
  66. option.onClick( function () {
  67. exportScene( THREE.SceneExporter );
  68. } );
  69. options.add( option );
  70. */
  71. // export object
  72. var option = new UI.Panel();
  73. option.setClass( 'option' );
  74. option.setTextContent( 'Export Object' );
  75. option.onClick( function () {
  76. exportObject( THREE.ObjectExporter );
  77. } );
  78. options.add( option );
  79. // export scene
  80. var option = new UI.Panel();
  81. option.setClass( 'option' );
  82. option.setTextContent( 'Export Scene' );
  83. option.onClick( function () {
  84. exportScene( THREE.ObjectExporter );
  85. } );
  86. options.add( option );
  87. // export OBJ
  88. var option = new UI.Panel();
  89. option.setClass( 'option' );
  90. option.setTextContent( 'Export OBJ' );
  91. option.onClick( function () {
  92. exportGeometry( THREE.OBJExporter );
  93. } );
  94. options.add( option );
  95. var exportGeometry = function ( exporterClass ) {
  96. var object = editor.selected;
  97. if ( object.geometry === undefined ) {
  98. alert( "Selected object doesn't have any geometry" );
  99. return;
  100. }
  101. var exporter = new exporterClass();
  102. var output;
  103. if ( exporter instanceof THREE.BufferGeometryExporter || exporter instanceof THREE.GeometryExporter ) {
  104. output = JSON.stringify( exporter.parse( object.geometry ), null, '\t' );
  105. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  106. } else {
  107. output = exporter.parse( object.geometry );
  108. }
  109. var blob = new Blob( [ output ], { type: 'text/plain' } );
  110. var objectURL = URL.createObjectURL( blob );
  111. window.open( objectURL, '_blank' );
  112. window.focus();
  113. };
  114. var exportObject = function ( exporterClass ) {
  115. var exporter = new exporterClass();
  116. var object = editor.selected;
  117. var output = JSON.stringify( exporter.parse( object ), null, '\t' );
  118. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  119. var blob = new Blob( [ output ], { type: 'text/plain' } );
  120. var objectURL = URL.createObjectURL( blob );
  121. window.open( objectURL, '_blank' );
  122. window.focus();
  123. };
  124. var exportScene = function ( exporterClass ) {
  125. var exporter = new exporterClass();
  126. var output = JSON.stringify( exporter.parse( editor.scene ), null, '\t' );
  127. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  128. var blob = new Blob( [ output ], { type: 'text/plain' } );
  129. var objectURL = URL.createObjectURL( blob );
  130. window.open( objectURL, '_blank' );
  131. window.focus();
  132. };
  133. options.add( new UI.HorizontalRule() );
  134. // share
  135. var option = new UI.Panel();
  136. option.setClass( 'option' );
  137. option.setTextContent( 'Share' );
  138. option.onClick( function () {
  139. var exporter = new THREE.ObjectExporter();
  140. var string = JSON.stringify( exporter.parse( editor.scene ) );
  141. window.location.hash = 'A/' + window.btoa( RawDeflate.deflate( string ) );
  142. } );
  143. options.add( option );
  144. return container;
  145. }