MoveObjectCommand.js 2.3 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. function MoveObjectCommand( editor, object, newParent, newBefore ) {
  10. Command.call( this, editor );
  11. this.type = 'MoveObjectCommand';
  12. this.name = 'Move Object';
  13. this.object = object;
  14. this.oldParent = ( object !== undefined ) ? object.parent : undefined;
  15. this.oldIndex = ( this.oldParent !== undefined ) ? this.oldParent.children.indexOf( this.object ) : undefined;
  16. this.newParent = newParent;
  17. if ( newBefore !== undefined ) {
  18. this.newIndex = ( newParent !== undefined ) ? newParent.children.indexOf( newBefore ) : undefined;
  19. } else {
  20. this.newIndex = ( newParent !== undefined ) ? newParent.children.length : undefined;
  21. }
  22. if ( this.oldParent === this.newParent && this.newIndex > this.oldIndex ) {
  23. this.newIndex --;
  24. }
  25. this.newBefore = newBefore;
  26. }
  27. MoveObjectCommand.prototype = {
  28. execute: function () {
  29. this.oldParent.remove( this.object );
  30. var 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: function () {
  36. this.newParent.remove( this.object );
  37. var 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: function () {
  43. var output = Command.prototype.toJSON.call( 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: function ( json ) {
  52. Command.prototype.fromJSON.call( this, 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 };