Menubar.Edit.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { Box3, Vector3 } from '../../build/three.module.js';
  2. import { UIPanel, UIRow, UIHorizontalRule } from './libs/ui.js';
  3. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  4. import { RemoveObjectCommand } from './commands/RemoveObjectCommand.js';
  5. import { SetPositionCommand } from './commands/SetPositionCommand.js';
  6. function MenubarEdit( editor ) {
  7. var strings = editor.strings;
  8. var container = new UIPanel();
  9. container.setClass( 'menu' );
  10. var title = new UIPanel();
  11. title.setClass( 'title' );
  12. title.setTextContent( strings.getKey( 'menubar/edit' ) );
  13. container.add( title );
  14. var options = new UIPanel();
  15. options.setClass( 'options' );
  16. container.add( options );
  17. // Undo
  18. var undo = new UIRow();
  19. undo.setClass( 'option' );
  20. undo.setTextContent( strings.getKey( 'menubar/edit/undo' ) );
  21. undo.onClick( function () {
  22. editor.undo();
  23. } );
  24. options.add( undo );
  25. // Redo
  26. var redo = new UIRow();
  27. redo.setClass( 'option' );
  28. redo.setTextContent( strings.getKey( 'menubar/edit/redo' ) );
  29. redo.onClick( function () {
  30. editor.redo();
  31. } );
  32. options.add( redo );
  33. // Clear History
  34. var option = new UIRow();
  35. option.setClass( 'option' );
  36. option.setTextContent( strings.getKey( 'menubar/edit/clear_history' ) );
  37. option.onClick( function () {
  38. if ( confirm( 'The Undo/Redo History will be cleared. Are you sure?' ) ) {
  39. editor.history.clear();
  40. }
  41. } );
  42. options.add( option );
  43. editor.signals.historyChanged.add( function () {
  44. var history = editor.history;
  45. undo.setClass( 'option' );
  46. redo.setClass( 'option' );
  47. if ( history.undos.length == 0 ) {
  48. undo.setClass( 'inactive' );
  49. }
  50. if ( history.redos.length == 0 ) {
  51. redo.setClass( 'inactive' );
  52. }
  53. } );
  54. // ---
  55. options.add( new UIHorizontalRule() );
  56. // Center
  57. var option = new UIRow();
  58. option.setClass( 'option' );
  59. option.setTextContent( strings.getKey( 'menubar/edit/center' ) );
  60. option.onClick( function () {
  61. var object = editor.selected;
  62. if ( object === null || object.parent === null ) return; // avoid centering the camera or scene
  63. const aabb = new Box3().setFromObject( object );
  64. const center = aabb.getCenter( new Vector3() );
  65. const newPosition = new Vector3();
  66. newPosition.x = object.position.x + ( object.position.x - center.x );
  67. newPosition.y = object.position.y + ( object.position.y - center.y );
  68. newPosition.z = object.position.z + ( object.position.z - center.z );
  69. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  70. } );
  71. options.add( option );
  72. // Clone
  73. var option = new UIRow();
  74. option.setClass( 'option' );
  75. option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
  76. option.onClick( function () {
  77. var object = editor.selected;
  78. if ( object === null || object.parent === null ) return; // avoid cloning the camera or scene
  79. object = object.clone();
  80. editor.execute( new AddObjectCommand( editor, object ) );
  81. } );
  82. options.add( option );
  83. // Delete
  84. var option = new UIRow();
  85. option.setClass( 'option' );
  86. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  87. option.onClick( function () {
  88. var object = editor.selected;
  89. if ( object !== null && object.parent !== null ) {
  90. editor.execute( new RemoveObjectCommand( editor, object ) );
  91. }
  92. } );
  93. options.add( option );
  94. //
  95. options.add( new UIHorizontalRule() );
  96. // Set textures to sRGB. See #15903
  97. var option = new UIRow();
  98. option.setClass( 'option' );
  99. option.setTextContent( strings.getKey( 'menubar/edit/fixcolormaps' ) );
  100. option.onClick( function () {
  101. editor.scene.traverse( fixColorMap );
  102. } );
  103. options.add( option );
  104. var colorMaps = [ 'map', 'envMap', 'emissiveMap' ];
  105. function fixColorMap( obj ) {
  106. var material = obj.material;
  107. if ( material !== undefined ) {
  108. if ( Array.isArray( material ) === true ) {
  109. for ( var i = 0; i < material.length; i ++ ) {
  110. fixMaterial( material[ i ] );
  111. }
  112. } else {
  113. fixMaterial( material );
  114. }
  115. editor.signals.sceneGraphChanged.dispatch();
  116. }
  117. }
  118. function fixMaterial( material ) {
  119. var needsUpdate = material.needsUpdate;
  120. for ( var i = 0; i < colorMaps.length; i ++ ) {
  121. var map = material[ colorMaps[ i ] ];
  122. if ( map ) {
  123. map.encoding = THREE.sRGBEncoding;
  124. needsUpdate = true;
  125. }
  126. }
  127. material.needsUpdate = needsUpdate;
  128. }
  129. return container;
  130. }
  131. export { MenubarEdit };