MoveObjectCommand.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, newParent, newBefore ) {
  11. super( editor );
  12. this.type = 'MoveObjectCommand';
  13. this.name = 'Move Object';
  14. this.object = object;
  15. this.oldParent = ( object !== undefined ) ? object.parent : undefined;
  16. this.oldIndex = ( this.oldParent !== undefined ) ? this.oldParent.children.indexOf( this.object ) : undefined;
  17. this.newParent = newParent;
  18. if ( newBefore !== undefined ) {
  19. this.newIndex = ( newParent !== undefined ) ? newParent.children.indexOf( newBefore ) : undefined;
  20. } else {
  21. this.newIndex = ( newParent !== undefined ) ? newParent.children.length : undefined;
  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.editor.signals.sceneGraphChanged.dispatch();
  34. }
  35. undo() {
  36. this.newParent.remove( this.object );
  37. const children = this.oldParent.children;
  38. children.splice( this.oldIndex, 0, this.object );
  39. this.object.parent = this.oldParent;
  40. this.editor.signals.sceneGraphChanged.dispatch();
  41. }
  42. toJSON() {
  43. const output = super.toJSON( this );
  44. output.objectUuid = this.object.uuid;
  45. output.newParentUuid = this.newParent.uuid;
  46. output.oldParentUuid = this.oldParent.uuid;
  47. output.newIndex = this.newIndex;
  48. output.oldIndex = this.oldIndex;
  49. return output;
  50. }
  51. fromJSON( json ) {
  52. super.fromJSON( json );
  53. this.object = this.editor.objectByUuid( json.objectUuid );
  54. this.oldParent = this.editor.objectByUuid( json.oldParentUuid );
  55. if ( this.oldParent === undefined ) {
  56. this.oldParent = this.editor.scene;
  57. }
  58. this.newParent = this.editor.objectByUuid( json.newParentUuid );
  59. if ( this.newParent === undefined ) {
  60. this.newParent = this.editor.scene;
  61. }
  62. this.newIndex = json.newIndex;
  63. this.oldIndex = json.oldIndex;
  64. }
  65. }
  66. export { MoveObjectCommand };