MoveObjectCommand.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { Command } from '../Command.js';
  2. class MoveObjectCommand extends Command {
  3. /**
  4. * @param {Editor} editor
  5. * @param {THREE.Object3D|null} [object=null]
  6. * @param {THREE.Object3D|null} [newParent=null]
  7. * @param {THREE.Object3D|null} [newBefore=null]
  8. * @constructor
  9. */
  10. constructor( editor, object = null, newParent = null, newBefore = null ) {
  11. super( editor );
  12. this.type = 'MoveObjectCommand';
  13. this.name = editor.strings.getKey( 'command/MoveObject' );
  14. this.object = object;
  15. this.oldParent = ( object !== null ) ? object.parent : null;
  16. this.oldIndex = ( this.oldParent !== null ) ? this.oldParent.children.indexOf( this.object ) : null;
  17. this.newParent = newParent;
  18. if ( newBefore !== null ) {
  19. this.newIndex = ( newParent !== null ) ? newParent.children.indexOf( newBefore ) : null;
  20. } else {
  21. this.newIndex = ( newParent !== null ) ? newParent.children.length : null;
  22. }
  23. if ( this.oldParent === this.newParent && this.newIndex > this.oldIndex ) {
  24. this.newIndex --;
  25. }
  26. this.newBefore = newBefore;
  27. }
  28. execute() {
  29. this.oldParent.remove( this.object );
  30. const children = this.newParent.children;
  31. children.splice( this.newIndex, 0, this.object );
  32. this.object.parent = this.newParent;
  33. this.object.dispatchEvent( { type: 'added' } );
  34. this.editor.signals.objectChanged.dispatch( this.object );
  35. this.editor.signals.objectChanged.dispatch( this.newParent );
  36. this.editor.signals.objectChanged.dispatch( this.oldParent );
  37. this.editor.signals.sceneGraphChanged.dispatch();
  38. }
  39. undo() {
  40. this.newParent.remove( this.object );
  41. const children = this.oldParent.children;
  42. children.splice( this.oldIndex, 0, this.object );
  43. this.object.parent = this.oldParent;
  44. this.object.dispatchEvent( { type: 'added' } );
  45. this.editor.signals.objectChanged.dispatch( this.object );
  46. this.editor.signals.objectChanged.dispatch( this.newParent );
  47. this.editor.signals.objectChanged.dispatch( this.oldParent );
  48. this.editor.signals.sceneGraphChanged.dispatch();
  49. }
  50. toJSON() {
  51. const output = super.toJSON( this );
  52. output.objectUuid = this.object.uuid;
  53. output.newParentUuid = this.newParent.uuid;
  54. output.oldParentUuid = this.oldParent.uuid;
  55. output.newIndex = this.newIndex;
  56. output.oldIndex = this.oldIndex;
  57. return output;
  58. }
  59. fromJSON( json ) {
  60. super.fromJSON( json );
  61. this.object = this.editor.objectByUuid( json.objectUuid );
  62. this.oldParent = this.editor.objectByUuid( json.oldParentUuid );
  63. if ( this.oldParent === undefined ) {
  64. this.oldParent = this.editor.scene;
  65. }
  66. this.newParent = this.editor.objectByUuid( json.newParentUuid );
  67. if ( this.newParent === undefined ) {
  68. this.newParent = this.editor.scene;
  69. }
  70. this.newIndex = json.newIndex;
  71. this.oldIndex = json.oldIndex;
  72. }
  73. }
  74. export { MoveObjectCommand };
粤ICP备19079148号