Menubar.Edit.js 3.0 KB

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