CmdMoveObject.js 2.2 KB

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