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