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