CmdMoveObject.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Created by Daniel on 20.07.15.
  3. */
  4. CmdMoveObject = function ( object, newParent, newBefore ) {
  5. Cmd.call( this );
  6. this.type = 'CmdMoveObject';
  7. this.object = object;
  8. this.objectUuid = object !== undefined ? object.uuid : undefined;
  9. this.oldParent = object !== undefined ? object.parent : undefined;
  10. this.oldParentUuid = this.oldParent !== undefined ? this.oldParent.uuid : undefined;
  11. this.oldIndex = this.oldParent !== undefined ? this.oldParent.children.indexOf( this.object ) : undefined;
  12. this.newParent = newParent;
  13. this.newParentUuid = newParent !== undefined ? newParent.uuid : undefined;
  14. if ( newBefore !== undefined ) {
  15. this.newIndex = newParent !== undefined ? newParent.children.indexOf( newBefore ) : undefined;
  16. } else {
  17. this.newIndex = newParent !== undefined ? newParent.children.length : undefined;
  18. }
  19. if ( this.oldParent === this.newParent && this.newIndex > this.oldIndex ) {
  20. this.newIndex --;
  21. }
  22. this.newBefore = newBefore;
  23. };
  24. CmdMoveObject.prototype = {
  25. execute: function () {
  26. this.oldParent.remove( this.object );
  27. var children = this.newParent.children;
  28. children.splice( this.newIndex, 0, this.object );
  29. this.object.parent = this.newParent;
  30. this.editor.signals.sceneGraphChanged.dispatch();
  31. },
  32. undo: function () {
  33. this.newParent.remove( this.object );
  34. var children = this.oldParent.children;
  35. children.splice( this.oldIndex, 0, this.object );
  36. this.object.parent = this.oldParent;
  37. this.editor.signals.sceneGraphChanged.dispatch();
  38. },
  39. toJSON: function () {
  40. var output = Cmd.prototype.toJSON.call( this );
  41. output.objectUuid = this.objectUuid;
  42. output.newParentUuid = this.newParentUuid;
  43. output.oldParentUuid = this.oldParentUuid;
  44. output.newIndex = this.newIndex;
  45. output.oldIndex = this.oldIndex;
  46. return output;
  47. },
  48. fromJSON: function ( json ) {
  49. Cmd.prototype.fromJSON.call( this, json );
  50. this.object = this.editor.objectByUuid( json.objectUuid );
  51. this.objectUuid = json.objectUuid;
  52. this.oldParent = this.editor.objectByUuid( json.oldParentUuid );
  53. this.oldParentUuid = json.oldParentUuid;
  54. this.newParent = this.editor.objectByUuid( json.newParentUuid );
  55. this.newParentUuid = json.newParentUuid;
  56. this.newIndex = json.newIndex;
  57. this.oldIndex = json.oldIndex;
  58. }
  59. };