CmdMoveObject.js 2.3 KB

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