Menubar.Edit.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { Box3, Vector3 } from 'three';
  2. import { UIPanel, UIRow, UIHorizontalRule, UIText } 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.add( new UIText( 'CTRL+Z' ).setClass( 'key' ) );
  23. undo.onClick( function () {
  24. editor.undo();
  25. } );
  26. options.add( undo );
  27. // Redo
  28. const redo = new UIRow();
  29. redo.setClass( 'option' );
  30. redo.setTextContent( strings.getKey( 'menubar/edit/redo' ) );
  31. redo.add( new UIText( 'CTRL+SHIFT+Z' ).setClass( 'key' ) );
  32. redo.onClick( function () {
  33. editor.redo();
  34. } );
  35. options.add( redo );
  36. function onHistoryChanged() {
  37. const history = editor.history;
  38. undo.setClass( 'option' );
  39. redo.setClass( 'option' );
  40. if ( history.undos.length == 0 ) {
  41. undo.setClass( 'inactive' );
  42. }
  43. if ( history.redos.length == 0 ) {
  44. redo.setClass( 'inactive' );
  45. }
  46. }
  47. editor.signals.historyChanged.add( onHistoryChanged );
  48. onHistoryChanged();
  49. // ---
  50. options.add( new UIHorizontalRule() );
  51. // Center
  52. let option = new UIRow();
  53. option.setClass( 'option' );
  54. option.setTextContent( strings.getKey( 'menubar/edit/center' ) );
  55. option.onClick( function () {
  56. const object = editor.selected;
  57. if ( object === null || object.parent === null ) return; // avoid centering the camera or scene
  58. const aabb = new Box3().setFromObject( object );
  59. const center = aabb.getCenter( new Vector3() );
  60. const newPosition = new Vector3();
  61. newPosition.x = object.position.x - center.x;
  62. newPosition.y = object.position.y - center.y;
  63. newPosition.z = object.position.z - center.z;
  64. editor.execute( new SetPositionCommand( editor, object, newPosition ) );
  65. } );
  66. options.add( option );
  67. // Clone
  68. option = new UIRow();
  69. option.setClass( 'option' );
  70. option.setTextContent( strings.getKey( 'menubar/edit/clone' ) );
  71. option.onClick( function () {
  72. let object = editor.selected;
  73. if ( object === null || object.parent === null ) return; // avoid cloning the camera or scene
  74. object = clone( object );
  75. editor.execute( new AddObjectCommand( editor, object ) );
  76. } );
  77. options.add( option );
  78. // Delete
  79. option = new UIRow();
  80. option.setClass( 'option' );
  81. option.setTextContent( strings.getKey( 'menubar/edit/delete' ) );
  82. option.add( new UIText( 'DEL' ).setClass( 'key' ) );
  83. option.onClick( function () {
  84. const object = editor.selected;
  85. if ( object !== null && object.parent !== null ) {
  86. editor.execute( new RemoveObjectCommand( editor, object ) );
  87. }
  88. } );
  89. options.add( option );
  90. return container;
  91. }
  92. export { MenubarEdit };