Menubar.Edit.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. Menubar.Edit = function ( editor ) {
  5. var strings = editor.strings;
  6. var container = new UI.Panel();
  7. container.setClass( 'menu' );
  8. var title = new UI.Panel();
  9. title.setClass( 'title' );
  10. title.setTextContent( strings.getKey( 'menubar/edit' ) );
  11. container.add( title );
  12. var options = new UI.Panel();
  13. options.setClass( 'options' );
  14. container.add( options );
  15. // Undo
  16. var undo = new UI.Row();
  17. undo.setClass( 'option' );
  18. undo.setTextContent( strings.getKey( 'menubar/edit/undo' ) );
  19. undo.onClick( function () {
  20. editor.undo();
  21. } );
  22. options.add( undo );
  23. // Redo
  24. var redo = new UI.Row();
  25. redo.setClass( 'option' );
  26. redo.setTextContent( strings.getKey( 'menubar/edit/redo' ) );
  27. redo.onClick( function () {
  28. editor.redo();
  29. } );
  30. options.add( redo );
  31. // Clear History
  32. var option = new UI.Row();
  33. option.setClass( 'option' );
  34. option.setTextContent( strings.getKey( 'menubar/edit/clear_history' ) );
  35. option.onClick( function () {
  36. if ( confirm( 'The Undo/Redo History will be cleared. Are you sure?' ) ) {
  37. editor.history.clear();
  38. }
  39. } );
  40. options.add( option );
  41. editor.signals.historyChanged.add( function () {
  42. var history = editor.history;
  43. undo.setClass( 'option' );
  44. redo.setClass( 'option' );
  45. if ( history.undos.length == 0 ) {
  46. undo.setClass( 'inactive' );
  47. }
  48. if ( history.redos.length == 0 ) {
  49. redo.setClass( 'inactive' );
  50. }
  51. } );
  52. // ---
  53. options.add( new UI.HorizontalRule() );
  54. // Clone
  55. var option = new UI.Row();
  56. option.setClass( 'option' );
  57. option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
  58. option.onClick( function () {
  59. var object = editor.selected;
  60. if ( object.parent === null ) return; // avoid cloning the camera or scene
  61. object = object.clone();
  62. editor.execute( new AddObjectCommand( object ) );
  63. } );
  64. options.add( option );
  65. // Delete
  66. var option = new UI.Row();
  67. option.setClass( 'option' );
  68. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  69. option.onClick( function () {
  70. var object = editor.selected;
  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( strings.getKey( 'menubar/edit/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. };