RemoveObjectCommand.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 object THREE.Object3D
  7. * @constructor
  8. */
  9. var RemoveObjectCommand = function ( object ) {
  10. Command.call( this );
  11. this.type = 'RemoveObjectCommand';
  12. this.name = 'Remove Object';
  13. this.object = object;
  14. this.parent = ( object !== undefined ) ? object.parent : undefined;
  15. if ( this.parent !== undefined ) {
  16. this.index = this.parent.children.indexOf( this.object );
  17. }
  18. };
  19. RemoveObjectCommand.prototype = {
  20. execute: function () {
  21. var scope = this.editor;
  22. this.object.traverse( function ( child ) {
  23. scope.removeHelper( child );
  24. } );
  25. this.parent.remove( this.object );
  26. this.editor.select( this.parent );
  27. this.editor.signals.objectRemoved.dispatch( this.object );
  28. this.editor.signals.sceneGraphChanged.dispatch();
  29. },
  30. undo: function () {
  31. var scope = this.editor;
  32. this.object.traverse( function ( child ) {
  33. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  34. if ( child.material !== undefined ) scope.addMaterial( child.material );
  35. scope.addHelper( child );
  36. } );
  37. this.parent.children.splice( this.index, 0, this.object );
  38. this.object.parent = this.parent;
  39. this.editor.select( this.object );
  40. this.editor.signals.objectAdded.dispatch( this.object );
  41. this.editor.signals.sceneGraphChanged.dispatch();
  42. },
  43. toJSON: function () {
  44. var output = Command.prototype.toJSON.call( this );
  45. output.object = this.object.toJSON();
  46. output.index = this.index;
  47. output.parentUuid = this.parent.uuid;
  48. return output;
  49. },
  50. fromJSON: function ( json ) {
  51. Command.prototype.fromJSON.call( this, json );
  52. this.parent = this.editor.objectByUuid( json.parentUuid );
  53. if ( this.parent === undefined ) {
  54. this.parent = this.editor.scene;
  55. }
  56. this.index = json.index;
  57. this.object = this.editor.objectByUuid( json.object.object.uuid );
  58. if ( this.object === undefined ) {
  59. var loader = new THREE.ObjectLoader();
  60. this.object = loader.parse( json.object );
  61. }
  62. }
  63. };