MoveObjectCommand.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. /**
  6. * @param object THREE.Object3D
  7. * @param newParent THREE.Object3D
  8. * @param newBefore THREE.Object3D
  9. * @constructor
  10. */
  11. var MoveObjectCommand = function ( object, newParent, newBefore ) {
  12. Command.call( this );
  13. this.type = 'MoveObjectCommand';
  14. this.name = 'Move Object';
  15. this.object = object;
  16. this.oldParent = ( object !== undefined ) ? object.parent : undefined;
  17. this.oldIndex = ( this.oldParent !== undefined ) ? this.oldParent.children.indexOf( this.object ) : undefined;
  18. this.newParent = newParent;
  19. if ( newBefore !== undefined ) {
  20. this.newIndex = ( newParent !== undefined ) ? newParent.children.indexOf( newBefore ) : undefined;
  21. } else {
  22. this.newIndex = ( newParent !== undefined ) ? newParent.children.length : undefined;
  23. }
  24. if ( this.oldParent === this.newParent && this.newIndex > this.oldIndex ) {
  25. this.newIndex --;
  26. }
  27. this.newBefore = newBefore;
  28. };
  29. MoveObjectCommand.prototype = {
  30. execute: function () {
  31. this.oldParent.remove( this.object );
  32. var children = this.newParent.children;
  33. children.splice( this.newIndex, 0, this.object );
  34. this.object.parent = this.newParent;
  35. this.editor.signals.sceneGraphChanged.dispatch();
  36. },
  37. undo: function () {
  38. this.newParent.remove( this.object );
  39. var children = this.oldParent.children;
  40. children.splice( this.oldIndex, 0, this.object );
  41. this.object.parent = this.oldParent;
  42. this.editor.signals.sceneGraphChanged.dispatch();
  43. },
  44. toJSON: function () {
  45. var output = Command.prototype.toJSON.call( 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: function ( json ) {
  54. Command.prototype.fromJSON.call( this, 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. };