Menubar.File.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.clear();
  18. editor.storage.clear( function () {
  19. location.href = location.pathname;
  20. } );
  21. }
  22. } );
  23. options.add( option );
  24. //
  25. options.add( new UI.HorizontalRule() );
  26. // Import
  27. var fileInput = document.createElement( 'input' );
  28. fileInput.type = 'file';
  29. fileInput.addEventListener( 'change', function ( event ) {
  30. editor.loader.loadFile( fileInput.files[ 0 ] );
  31. } );
  32. var option = new UI.Panel();
  33. option.setClass( 'option' );
  34. option.setTextContent( 'Import' );
  35. option.onClick( function () {
  36. fileInput.click();
  37. } );
  38. options.add( option );
  39. //
  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 object = editor.selected;
  47. if ( object === null ) {
  48. alert( 'No object selected.' );
  49. return;
  50. }
  51. var geometry = object.geometry;
  52. if ( geometry === undefined ) {
  53. alert( 'The selected object doesn\'t have geometry.' );
  54. return;
  55. }
  56. var output = geometry.toJSON();
  57. output = JSON.stringify( output, null, '\t' );
  58. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  59. exportString( output );
  60. } );
  61. options.add( option );
  62. // Export Object
  63. var option = new UI.Panel();
  64. option.setClass( 'option' );
  65. option.setTextContent( 'Export Object' );
  66. option.onClick( function () {
  67. var object = editor.selected;
  68. if ( object === null ) {
  69. alert( 'No object selected' );
  70. return;
  71. }
  72. var output = object.toJSON();
  73. output = JSON.stringify( output, null, '\t' );
  74. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  75. exportString( output );
  76. } );
  77. options.add( option );
  78. // Export Scene
  79. var option = new UI.Panel();
  80. option.setClass( 'option' );
  81. option.setTextContent( 'Export Scene' );
  82. option.onClick( function () {
  83. var output = editor.scene.toJSON();
  84. output = JSON.stringify( output, null, '\t' );
  85. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  86. exportString( output );
  87. } );
  88. options.add( option );
  89. // Export OBJ
  90. var option = new UI.Panel();
  91. option.setClass( 'option' );
  92. option.setTextContent( 'Export OBJ' );
  93. option.onClick( function () {
  94. var object = editor.selected;
  95. if ( object === null ) {
  96. alert( 'No object selected.' );
  97. return;
  98. }
  99. var geometry = object.geometry;
  100. if ( geometry === undefined ) {
  101. alert( 'The selected object doesn\'t have geometry.' );
  102. return;
  103. }
  104. var exporter = new OBJExporter();
  105. exportString( exporter.parse( geometry ) );
  106. } );
  107. options.add( option );
  108. // Export STL
  109. var option = new UI.Panel();
  110. option.setClass( 'option' );
  111. option.setTextContent( 'Export STL' );
  112. option.onClick( function () {
  113. var exporter = new STLExporter();
  114. exportString( exporter.parse( editor.scene ) );
  115. } );
  116. options.add( option );
  117. //
  118. options.add( new UI.HorizontalRule() );
  119. /*
  120. // Test
  121. var option = new UI.Panel();
  122. option.setClass( 'option' );
  123. option.setTextContent( 'Test' );
  124. option.onClick( function () {
  125. var text = new UI.Text( 'blah' );
  126. editor.showDialog( text );
  127. } );
  128. options.add( option );
  129. */
  130. // Publish
  131. var option = new UI.Panel();
  132. option.setClass( 'option' );
  133. option.setTextContent( 'Publish' );
  134. option.onClick( function () {
  135. var preview = new Player( editor.scene.toJSON() );
  136. preview.setSize( 800, 600 );
  137. preview.update();
  138. var popup = window.open( '', 'preview', 'width=800,height=600' );
  139. popup.document.body.style.margin = 0;
  140. popup.document.body.appendChild( preview.dom );
  141. } );
  142. options.add( option );
  143. //
  144. var exportString = function ( output ) {
  145. var blob = new Blob( [ output ], { type: 'text/plain' } );
  146. var objectURL = URL.createObjectURL( blob );
  147. window.open( objectURL, '_blank' );
  148. window.focus();
  149. };
  150. return container;
  151. };