Menubar.Edit.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. editor.signals.historyChanged.add( function () {
  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. // ---
  46. options.add( new UIHorizontalRule() );
  47. // Center
  48. let option = new UIRow();
  49. option.setClass( 'option' );
  50. option.setTextContent( strings.getKey( 'menubar/edit/center' ) );
  51. option.onClick( function () {
  52. const object = editor.selected;
  53. if ( object === null || object.parent === null ) return; // avoid centering the camera or scene
  54. const aabb = new Box3().setFromObject( object );
  55. const center = aabb.getCenter( new Vector3() );
  56. const newPosition = new Vector3();
  57. newPosition.x = object.position.x - center.x;
  58. newPosition.y = object.position.y - center.y;
  59. newPosition.z = object.position.z - center.z;
  60. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  61. } );
  62. options.add( option );
  63. // Clone
  64. option = new UIRow();
  65. option.setClass( 'option' );
  66. option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
  67. option.onClick( function () {
  68. let object = editor.selected;
  69. if ( object === null || object.parent === null ) return; // avoid cloning the camera or scene
  70. object = clone( object );
  71. editor.execute( new AddObjectCommand( editor, object ) );
  72. } );
  73. options.add( option );
  74. // Delete
  75. option = new UIRow();
  76. option.setClass( 'option' );
  77. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  78. option.onClick( function () {
  79. const object = editor.selected;
  80. if ( object !== null && object.parent !== null ) {
  81. editor.execute( new RemoveObjectCommand( editor, object ) );
  82. }
  83. } );
  84. options.add( option );
  85. return container;
  86. }
  87. export { MenubarEdit };