MoveObjectCommand.js 2.5 KB

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