RemoveObjectCommand.js 2.1 KB

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