2
0

CmdMoveObject.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. init: function () {
  27. if ( this.object === undefined ) {
  28. this.object = this.editor.objectByUuid( this.objectUuid );
  29. }
  30. if ( this.oldParent === undefined ) {
  31. this.oldParent = this.editor.objectByUuid( this.oldParentUuid );
  32. }
  33. if ( this.newParent === undefined ) {
  34. this.newParent = this.editor.objectByUuid( this.newParentUuid );
  35. }
  36. },
  37. execute: function () {
  38. this.init();
  39. this.oldParent.remove( this.object );
  40. var children = this.newParent.children;
  41. children.splice( this.newIndex, 0, this.object );
  42. this.object.parent = this.newParent;
  43. this.editor.signals.sceneGraphChanged.dispatch();
  44. },
  45. undo: function () {
  46. this.init();
  47. this.newParent.remove( this.object );
  48. var children = this.oldParent.children;
  49. children.splice( this.oldIndex, 0, this.object );
  50. this.object.parent = this.oldParent;
  51. this.editor.signals.sceneGraphChanged.dispatch();
  52. },
  53. toJSON: function () {
  54. var output = Cmd.prototype.toJSON.call( this );
  55. output.objectUuid = this.objectUuid;
  56. output.newParentUuid = this.newParentUuid;
  57. output.oldParentUuid = this.oldParentUuid;
  58. output.newIndex = this.newIndex;
  59. output.oldIndex = this.oldIndex;
  60. return output;
  61. },
  62. fromJSON: function ( json ) {
  63. Cmd.prototype.fromJSON.call( this, json );
  64. this.objectUuid = json.objectUuid;
  65. this.oldParentUuid = json.oldParentUuid;
  66. this.newParentUuid = json.newParentUuid;
  67. this.newIndex = json.newIndex;
  68. this.oldIndex = json.oldIndex;
  69. }
  70. };