MoveObjectCommand.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { Command } from '../Command.js';
  2. /**
  3. * @param editor Editor
  4. * @param object THREE.Object3D
  5. * @param newParent THREE.Object3D
  6. * @param newBefore THREE.Object3D
  7. * @constructor
  8. */
  9. class MoveObjectCommand extends Command {
  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.sceneGraphChanged.dispatch();
  35. }
  36. undo() {
  37. this.newParent.remove( this.object );
  38. const children = this.oldParent.children;
  39. children.splice( this.oldIndex, 0, this.object );
  40. this.object.parent = this.oldParent;
  41. this.object.dispatchEvent( { type: 'added' } );
  42. this.editor.signals.sceneGraphChanged.dispatch();
  43. }
  44. toJSON() {
  45. const output = super.toJSON( this );
  46. output.objectUuid = this.object.uuid;
  47. output.newParentUuid = this.newParent.uuid;
  48. output.oldParentUuid = this.oldParent.uuid;
  49. output.newIndex = this.newIndex;
  50. output.oldIndex = this.oldIndex;
  51. return output;
  52. }
  53. fromJSON( json ) {
  54. super.fromJSON( json );
  55. this.object = this.editor.objectByUuid( json.objectUuid );
  56. this.oldParent = this.editor.objectByUuid( json.oldParentUuid );
  57. if ( this.oldParent === undefined ) {
  58. this.oldParent = this.editor.scene;
  59. }
  60. this.newParent = this.editor.objectByUuid( json.newParentUuid );
  61. if ( this.newParent === undefined ) {
  62. this.newParent = this.editor.scene;
  63. }
  64. this.newIndex = json.newIndex;
  65. this.oldIndex = json.oldIndex;
  66. }
  67. }
  68. export { MoveObjectCommand };