Menubar.File.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. Menubar.File = function ( signals ) {
  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( 'none' ) } );
  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. /*
  18. // open
  19. var option = new UI.Panel();
  20. option.setClass( 'option' );
  21. option.setTextContent( 'Open' );
  22. option.onClick( function () { alert( 'Open' ) } );
  23. options.add( option );
  24. */
  25. // reset
  26. var option = new UI.Panel();
  27. option.setClass( 'option' );
  28. option.setTextContent( 'Reset' );
  29. option.onClick( function () {
  30. if ( confirm( 'Are you sure?' ) ) {
  31. if ( localStorage.threejsEditor !== undefined ) {
  32. delete localStorage.threejsEditor;
  33. }
  34. location.replace( location.origin + location.pathname );
  35. }
  36. } );
  37. options.add( option );
  38. // export geometry
  39. var option = new UI.Panel();
  40. option.setClass( 'option' );
  41. option.setTextContent( 'Export Geometry' );
  42. option.onClick( function () {
  43. exportGeometry( THREE.GeometryExporter );
  44. } );
  45. options.add( option );
  46. /*
  47. // export scene
  48. var option = new UI.Panel();
  49. option.setClass( 'option' );
  50. option.setTextContent( 'Export Scene' );
  51. option.onClick( function () {
  52. exportScene( THREE.SceneExporter );
  53. } );
  54. options.add( option );
  55. */
  56. // export object
  57. var option = new UI.Panel();
  58. option.setClass( 'option' );
  59. option.setTextContent( 'Export Object' );
  60. option.onClick( function () {
  61. exportObject( THREE.ObjectExporter );
  62. } );
  63. options.add( option );
  64. // export scene
  65. var option = new UI.Panel();
  66. option.setClass( 'option' );
  67. option.setTextContent( 'Export Scene' );
  68. option.onClick( function () {
  69. exportScene( THREE.ObjectExporter );
  70. } );
  71. options.add( option );
  72. // export OBJ
  73. var option = new UI.Panel();
  74. option.setClass( 'option' );
  75. option.setTextContent( 'Export OBJ' );
  76. option.onClick( function () {
  77. exportGeometry( THREE.OBJExporter );
  78. } );
  79. options.add( option );
  80. var exportGeometry = function ( exporterClass ) {
  81. var selected;
  82. // TODO: handle multiple selection
  83. for ( var i in editor.selected ) {
  84. if ( editor.objects[ editor.selected[ i ].uuid ] ) selected = editor.selected[ i ];
  85. }
  86. if ( !selected ) return;
  87. if ( selected.geometry === undefined ) {
  88. alert( "Selected object doesn't have any geometry" );
  89. return;
  90. }
  91. var exporter = new exporterClass();
  92. var output;
  93. if ( exporter instanceof THREE.GeometryExporter ) {
  94. output = JSON.stringify( exporter.parse( selected.geometry ), null, '\t' );
  95. output = output.replace( /[\n\t]+([\d\.e\-\[\]]+)/g, '$1' );
  96. } else {
  97. output = exporter.parse( selected.geometry );
  98. }
  99. var blob = new Blob( [ output ], { type: 'text/plain' } );
  100. var objectURL = URL.createObjectURL( blob );
  101. window.open( objectURL, '_blank' );
  102. window.focus();
  103. };
  104. var exportObject = function ( exporterClass ) {
  105. var selected;
  106. // TODO: handle multiple selection
  107. for ( var i in editor.selected ) {
  108. if ( editor.objects[ editor.selected[ i ].uuid ] ) selected = editor.selected[ i ];
  109. }
  110. if ( !selected ) return;
  111. var exporter = new exporterClass();
  112. var output = JSON.stringify( exporter.parse( selected ), 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. return container;
  129. }