Menubar.File.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. 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 geometry
  46. var option = new UI.Panel();
  47. option.setClass( 'option' );
  48. option.setTextContent( 'Export Geometry' );
  49. option.onClick( function () {
  50. var geometry = editor.selected.geometry;
  51. if ( geometry instanceof THREE.BufferGeometry ) {
  52. exportGeometry( THREE.BufferGeometryExporter );
  53. } else if ( geometry instanceof THREE.Geometry ) {
  54. exportGeometry( THREE.GeometryExporter );
  55. }
  56. } );
  57. options.add( option );
  58. /*
  59. // export scene
  60. var option = new UI.Panel();
  61. option.setClass( 'option' );
  62. option.setTextContent( 'Export Scene' );
  63. option.onClick( function () {
  64. exportScene( THREE.SceneExporter );
  65. } );
  66. options.add( option );
  67. */
  68. // export object
  69. var option = new UI.Panel();
  70. option.setClass( 'option' );
  71. option.setTextContent( 'Export Object' );
  72. option.onClick( function () {
  73. exportObject( THREE.ObjectExporter );
  74. } );
  75. options.add( option );
  76. // export scene
  77. var option = new UI.Panel();
  78. option.setClass( 'option' );
  79. option.setTextContent( 'Export Scene' );
  80. option.onClick( function () {
  81. exportScene( THREE.ObjectExporter );
  82. } );
  83. options.add( option );
  84. // export OBJ
  85. var option = new UI.Panel();
  86. option.setClass( 'option' );
  87. option.setTextContent( 'Export OBJ' );
  88. option.onClick( function () {
  89. exportGeometry( THREE.OBJExporter );
  90. } );
  91. options.add( option );
  92. var exportGeometry = function ( exporterClass ) {
  93. var object = editor.selected;
  94. if ( object.geometry === undefined ) {
  95. alert( "Selected object doesn't have any geometry" );
  96. return;
  97. }
  98. var exporter = new exporterClass();
  99. var output;
  100. if ( exporter instanceof THREE.BufferGeometryExporter || exporter instanceof THREE.GeometryExporter ) {
  101. output = JSON.stringify( exporter.parse( object.geometry ), null, '\t' );
  102. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  103. } else {
  104. output = exporter.parse( object.geometry );
  105. }
  106. var blob = new Blob( [ output ], { type: 'text/plain' } );
  107. var objectURL = URL.createObjectURL( blob );
  108. window.open( objectURL, '_blank' );
  109. window.focus();
  110. };
  111. var exportObject = function ( exporterClass ) {
  112. var exporter = new exporterClass();
  113. var object = editor.selected;
  114. var output = JSON.stringify( exporter.parse( object ), null, '\t' );
  115. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  116. var blob = new Blob( [ output ], { type: 'text/plain' } );
  117. var objectURL = URL.createObjectURL( blob );
  118. window.open( objectURL, '_blank' );
  119. window.focus();
  120. };
  121. var exportScene = function ( exporterClass ) {
  122. var exporter = new exporterClass();
  123. var output = JSON.stringify( exporter.parse( editor.scene ), null, '\t' );
  124. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  125. var blob = new Blob( [ output ], { type: 'text/plain' } );
  126. var objectURL = URL.createObjectURL( blob );
  127. window.open( objectURL, '_blank' );
  128. window.focus();
  129. };
  130. options.add( new UI.HorizontalRule() );
  131. // share
  132. var option = new UI.Panel();
  133. option.setClass( 'option' );
  134. option.setTextContent( 'Share' );
  135. option.onClick( function () {
  136. var exporter = new THREE.ObjectExporter();
  137. var string = JSON.stringify( exporter.parse( editor.scene ) );
  138. window.location.hash = 'A/' + window.btoa( RawDeflate.deflate( string ) );
  139. } );
  140. options.add( option );
  141. return container;
  142. }