Menubar.Edit.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { Box3, Vector3 } from 'three';
  2. import { clone } from 'three/addons/utils/SkeletonUtils.js';
  3. import { UIPanel, UIRow, UIHorizontalRule, UIText } from './libs/ui.js';
  4. import { AddObjectCommand } from './commands/AddObjectCommand.js';
  5. import { MultiCmdsCommand } from './commands/MultiCmdsCommand.js';
  6. import { RemoveObjectCommand } from './commands/RemoveObjectCommand.js';
  7. import { SetPositionCommand } from './commands/SetPositionCommand.js';
  8. function MenubarEdit( editor ) {
  9. const strings = editor.strings;
  10. const container = new UIPanel();
  11. container.setClass( 'menu' );
  12. const title = new UIPanel();
  13. title.setClass( 'title' );
  14. title.setTextContent( strings.getKey( 'menubar/edit' ) );
  15. container.add( title );
  16. const options = new UIPanel();
  17. options.setClass( 'options' );
  18. container.add( options );
  19. // Undo
  20. const undo = new UIRow();
  21. undo.setClass( 'option' );
  22. undo.setTextContent( strings.getKey( 'menubar/edit/undo' ) );
  23. undo.add( new UIText( 'CTRL+Z' ).setClass( 'key' ) );
  24. undo.onClick( function () {
  25. editor.undo();
  26. } );
  27. options.add( undo );
  28. // Redo
  29. const redo = new UIRow();
  30. redo.setClass( 'option' );
  31. redo.setTextContent( strings.getKey( 'menubar/edit/redo' ) );
  32. redo.add( new UIText( 'CTRL+SHIFT+Z' ).setClass( 'key' ) );
  33. redo.onClick( function () {
  34. editor.redo();
  35. } );
  36. options.add( redo );
  37. function onHistoryChanged() {
  38. const history = editor.history;
  39. undo.setClass( 'option' );
  40. redo.setClass( 'option' );
  41. if ( history.undos.length == 0 ) {
  42. undo.setClass( 'inactive' );
  43. }
  44. if ( history.redos.length == 0 ) {
  45. redo.setClass( 'inactive' );
  46. }
  47. }
  48. editor.signals.historyChanged.add( onHistoryChanged );
  49. onHistoryChanged();
  50. // ---
  51. options.add( new UIHorizontalRule() );
  52. // Center
  53. let option = new UIRow();
  54. option.setClass( 'option' );
  55. option.setTextContent( strings.getKey( 'menubar/edit/center' ) );
  56. option.onClick( function () {
  57. const object = editor.selected;
  58. if ( object === null || object.parent === null ) return; // avoid centering the camera or scene
  59. const aabb = new Box3().setFromObject( object );
  60. const center = aabb.getCenter( new Vector3() );
  61. const newPosition = new Vector3();
  62. newPosition.x = object.position.x - center.x;
  63. newPosition.y = object.position.y - center.y;
  64. newPosition.z = object.position.z - center.z;
  65. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  66. } );
  67. options.add( option );
  68. // Clone
  69. option = new UIRow();
  70. option.setClass( 'option' );
  71. option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
  72. option.onClick( function () {
  73. let object = editor.selected;
  74. if ( object === null || object.parent === null ) return; // avoid cloning the camera or scene
  75. object = clone( object );
  76. editor.execute( new AddObjectCommand( editor, object ) );
  77. } );
  78. options.add( option );
  79. // Delete
  80. option = new UIRow();
  81. option.setClass( 'option' );
  82. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  83. option.add( new UIText( 'DEL' ).setClass( 'key' ) );
  84. option.onClick( function () {
  85. const object = editor.selected;
  86. if ( object === null || object.parent === null ) return;
  87. if ( object.isSpotLight || object.isDirectionalLight ) {
  88. editor.execute( new MultiCmdsCommand( editor, [
  89. new RemoveObjectCommand( editor, object ),
  90. new RemoveObjectCommand( editor, object.target )
  91. ] ) );
  92. } else {
  93. editor.execute( new RemoveObjectCommand( editor, object ) );
  94. }
  95. } );
  96. options.add( option );
  97. return container;
  98. }
  99. export { MenubarEdit };
粤ICP备19079148号