MoveObjectCommand.js 2.4 KB

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