Menubar.Edit.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Menubar.Edit = function ( editor ) {
  5. var container = new UI.Panel();
  6. container.setClass( 'menu' );
  7. var title = new UI.Panel();
  8. title.setClass( 'title' );
  9. title.setTextContent( 'Edit' );
  10. container.add( title );
  11. var options = new UI.Panel();
  12. options.setClass( 'options' );
  13. container.add( options );
  14. // Undo
  15. var undo = new UI.Row();
  16. undo.setClass( 'option' );
  17. undo.setTextContent( 'Undo (Ctrl+Z)' );
  18. undo.onClick( function () {
  19. editor.undo();
  20. } );
  21. options.add( undo );
  22. // Redo
  23. var redo = new UI.Row();
  24. redo.setClass( 'option' );
  25. redo.setTextContent( 'Redo (Ctrl+Shift+Z)' );
  26. redo.onClick( function () {
  27. editor.redo();
  28. } );
  29. options.add( redo );
  30. // Clear History
  31. var option = new UI.Row();
  32. option.setClass( 'option' );
  33. option.setTextContent( 'Clear History' );
  34. option.onClick( function () {
  35. if ( confirm( 'The Undo/Redo History will be cleared. Are you sure?' ) ) {
  36. editor.history.clear();
  37. }
  38. } );
  39. options.add( option );
  40. editor.signals.historyChanged.add( function () {
  41. var history = editor.history;
  42. undo.setClass( 'option' );
  43. redo.setClass( 'option' );
  44. if ( history.undos.length == 0 ) {
  45. undo.setClass( 'inactive' );
  46. }
  47. if ( history.redos.length == 0 ) {
  48. redo.setClass( 'inactive' );
  49. }
  50. } );
  51. // ---
  52. options.add( new UI.HorizontalRule() );
  53. // Clone
  54. var option = new UI.Row();
  55. option.setClass( 'option' );
  56. option.setTextContent( 'Clone' );
  57. option.onClick( function () {
  58. var object = editor.selected;
  59. if ( object.parent === null ) return; // avoid cloning the camera or scene
  60. object = object.clone();
  61. editor.execute( new AddObjectCommand( object ) );
  62. } );
  63. options.add( option );
  64. // Delete
  65. var option = new UI.Row();
  66. option.setClass( 'option' );
  67. option.setTextContent( 'Delete (Del)' );
  68. option.onClick( function () {
  69. var object = editor.selected;
  70. if ( confirm( 'Delete ' + object.name + '?' ) === false ) return;
  71. var parent = object.parent;
  72. if ( parent === undefined ) return; // avoid deleting the camera or scene
  73. editor.execute( new RemoveObjectCommand( object ) );
  74. } );
  75. options.add( option );
  76. // Minify shaders
  77. var option = new UI.Row();
  78. option.setClass( 'option' );
  79. option.setTextContent( 'Minify Shaders' );
  80. option.onClick( function() {
  81. var root = editor.selected || editor.scene;
  82. var errors = [];
  83. var nMaterialsChanged = 0;
  84. var path = [];
  85. function getPath ( object ) {
  86. path.length = 0;
  87. var parent = object.parent;
  88. if ( parent !== undefined ) getPath( parent );
  89. path.push( object.name || object.uuid );
  90. return path;
  91. }
  92. var cmds = [];
  93. root.traverse( function ( object ) {
  94. var material = object.material;
  95. if ( material instanceof THREE.ShaderMaterial ) {
  96. try {
  97. var shader = glslprep.minifyGlsl( [
  98. material.vertexShader, material.fragmentShader ] );
  99. cmds.push( new SetMaterialValueCommand( object, 'vertexShader', shader[ 0 ] ) );
  100. cmds.push( new SetMaterialValueCommand( object, 'fragmentShader', shader[ 1 ] ) );
  101. ++nMaterialsChanged;
  102. } catch ( e ) {
  103. var path = getPath( object ).join( "/" );
  104. if ( e instanceof glslprep.SyntaxError )
  105. errors.push( path + ":" +
  106. e.line + ":" + e.column + ": " + e.message );
  107. else {
  108. errors.push( path +
  109. ": Unexpected error (see console for details)." );
  110. console.error( e.stack || e );
  111. }
  112. }
  113. }
  114. } );
  115. if ( nMaterialsChanged > 0 ) {
  116. editor.execute( new MultiCmdsCommand( cmds ), 'Minify Shaders' );
  117. }
  118. window.alert( nMaterialsChanged +
  119. " material(s) were changed.\n" + errors.join( "\n" ) );
  120. } );
  121. options.add( option );
  122. return container;
  123. };