Menubar.File.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. Menubar.File = function ( editor ) {
  2. var container = new UI.Panel();
  3. container.setClass( 'menu' );
  4. var title = new UI.Panel();
  5. title.setClass( 'title' );
  6. title.setTextContent( 'File' );
  7. container.add( title );
  8. var options = new UI.Panel();
  9. options.setClass( 'options' );
  10. container.add( options );
  11. // New
  12. var option = new UI.Panel();
  13. option.setClass( 'option' );
  14. option.setTextContent( 'New' );
  15. option.onClick( function () {
  16. if ( confirm( 'Are you sure?' ) ) {
  17. editor.config.setKey(
  18. 'camera/position', [ 500, 250, 500 ],
  19. 'camera/target', [ 0, 0, 0 ]
  20. );
  21. editor.storage.clear( function () {
  22. location.href = location.pathname;
  23. } );
  24. }
  25. } );
  26. options.add( option );
  27. //
  28. options.add( new UI.HorizontalRule() );
  29. // Import
  30. var fileInput = document.createElement( 'input' );
  31. fileInput.type = 'file';
  32. fileInput.addEventListener( 'change', function ( event ) {
  33. editor.loader.loadFile( fileInput.files[ 0 ] );
  34. } );
  35. var option = new UI.Panel();
  36. option.setClass( 'option' );
  37. option.setTextContent( 'Import' );
  38. option.onClick( function () {
  39. fileInput.click();
  40. } );
  41. options.add( option );
  42. //
  43. options.add( new UI.HorizontalRule() );
  44. // Export Geometry
  45. var option = new UI.Panel();
  46. option.setClass( 'option' );
  47. option.setTextContent( 'Export Geometry' );
  48. option.onClick( function () {
  49. var object = editor.selected;
  50. if ( object === null ) {
  51. alert( 'No object selected.' );
  52. return;
  53. }
  54. var geometry = object.geometry;
  55. if ( geometry === undefined ) {
  56. alert( 'The selected object doesn\'t have geometry.' );
  57. return;
  58. }
  59. var output = geometry.toJSON();
  60. output = JSON.stringify( output, null, '\t' );
  61. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  62. exportString( output );
  63. } );
  64. options.add( option );
  65. // Export Object
  66. var option = new UI.Panel();
  67. option.setClass( 'option' );
  68. option.setTextContent( 'Export Object' );
  69. option.onClick( function () {
  70. var object = editor.selected;
  71. if ( object === null ) {
  72. alert( 'No object selected' );
  73. return;
  74. }
  75. var output = object.toJSON();
  76. output = JSON.stringify( output, null, '\t' );
  77. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  78. exportString( output );
  79. } );
  80. options.add( option );
  81. // Export Scene
  82. var option = new UI.Panel();
  83. option.setClass( 'option' );
  84. option.setTextContent( 'Export Scene' );
  85. option.onClick( function () {
  86. var output = editor.scene.toJSON();
  87. output = JSON.stringify( output, null, '\t' );
  88. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  89. exportString( output );
  90. } );
  91. options.add( option );
  92. // Export OBJ
  93. var option = new UI.Panel();
  94. option.setClass( 'option' );
  95. option.setTextContent( 'Export OBJ' );
  96. option.onClick( function () {
  97. var object = editor.selected;
  98. if ( object === null ) {
  99. alert( 'No object selected.' );
  100. return;
  101. }
  102. var geometry = object.geometry;
  103. if ( geometry === undefined ) {
  104. alert( 'The selected object doesn\'t have geometry.' );
  105. return;
  106. }
  107. var exporter = new THREE.OBJExporter();
  108. exportString( exporter.parse( geometry ) );
  109. } );
  110. options.add( option );
  111. // Export STL
  112. var option = new UI.Panel();
  113. option.setClass( 'option' );
  114. option.setTextContent( 'Export STL' );
  115. option.onClick( function () {
  116. var exporter = new THREE.STLExporter();
  117. exportString( exporter.parse( editor.scene ) );
  118. } );
  119. options.add( option );
  120. //
  121. options.add( new UI.HorizontalRule() );
  122. /*
  123. // Test
  124. var option = new UI.Panel();
  125. option.setClass( 'option' );
  126. option.setTextContent( 'Test' );
  127. option.onClick( function () {
  128. var text = new UI.Text( 'blah' );
  129. editor.showDialog( text );
  130. } );
  131. options.add( option );
  132. */
  133. // Publish
  134. var option = new UI.Panel();
  135. option.setClass( 'option' );
  136. option.setTextContent( 'Publish' );
  137. option.onClick( function () {
  138. alert( 'Not yet...' );
  139. } );
  140. options.add( option );
  141. //
  142. var exportString = function ( output ) {
  143. var blob = new Blob( [ output ], { type: 'text/plain' } );
  144. var objectURL = URL.createObjectURL( blob );
  145. window.open( objectURL, '_blank' );
  146. window.focus();
  147. };
  148. return container;
  149. };