Menubar.Edit.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  2. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  3. import { RemoveObjectCommand } from './commands/RemoveObjectCommand.js';
  4. function MenubarEdit( editor ) {
  5. var strings = editor.strings;
  6. var container = new UIPanel();
  7. container.setClass( 'menu' );
  8. var title = new UIPanel();
  9. title.setClass( 'title' );
  10. title.setTextContent( strings.getKey( 'menubar/edit' ) );
  11. container.add( title );
  12. var options = new UIPanel();
  13. options.setClass( 'options' );
  14. container.add( options );
  15. // Undo
  16. var undo = new UIRow();
  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 UIRow();
  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 UIRow();
  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 UIHorizontalRule() );
  54. // Clone
  55. var option = new UIRow();
  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( editor, object ) );
  63. } );
  64. options.add( option );
  65. // Delete
  66. var option = new UIRow();
  67. option.setClass( 'option' );
  68. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  69. option.onClick( function () {
  70. var object = editor.selected;
  71. if ( object !== null && object.parent !== null ) {
  72. editor.execute( new RemoveObjectCommand( editor, object ) );
  73. }
  74. } );
  75. options.add( option );
  76. //
  77. options.add( new UIHorizontalRule() );
  78. // Set textures to sRGB. See #15903
  79. var option = new UIRow();
  80. option.setClass( 'option' );
  81. option.setTextContent( strings.getKey( 'menubar/edit/fixcolormaps' ) );
  82. option.onClick( function () {
  83. editor.scene.traverse( fixColorMap );
  84. } );
  85. options.add( option );
  86. var colorMaps = [ 'map', 'envMap', 'emissiveMap' ];
  87. function fixColorMap( obj ) {
  88. var material = obj.material;
  89. if ( material !== undefined ) {
  90. if ( Array.isArray( material ) === true ) {
  91. for ( var i = 0; i < material.length; i ++ ) {
  92. fixMaterial( material[ i ] );
  93. }
  94. } else {
  95. fixMaterial( material );
  96. }
  97. editor.signals.sceneGraphChanged.dispatch();
  98. }
  99. }
  100. function fixMaterial( material ) {
  101. var needsUpdate = material.needsUpdate;
  102. for ( var i = 0; i < colorMaps.length; i ++ ) {
  103. var map = material[ colorMaps[ i ] ];
  104. if ( map ) {
  105. map.encoding = THREE.sRGBEncoding;
  106. needsUpdate = true;
  107. }
  108. }
  109. material.needsUpdate = needsUpdate;
  110. }
  111. return container;
  112. }
  113. export { MenubarEdit };