RemoveObjectCommand.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 * as THREE 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. this.editor.removeObject( this.object );
  25. this.editor.deselect();
  26. },
  27. undo: function () {
  28. this.editor.addObject( this.object, this.parent, this.index );
  29. this.editor.select( this.object );
  30. },
  31. toJSON: function () {
  32. var output = Command.prototype.toJSON.call( this );
  33. output.object = this.object.toJSON();
  34. output.index = this.index;
  35. output.parentUuid = this.parent.uuid;
  36. return output;
  37. },
  38. fromJSON: function ( json ) {
  39. Command.prototype.fromJSON.call( this, json );
  40. this.parent = this.editor.objectByUuid( json.parentUuid );
  41. if ( this.parent === undefined ) {
  42. this.parent = this.editor.scene;
  43. }
  44. this.index = json.index;
  45. this.object = this.editor.objectByUuid( json.object.object.uuid );
  46. if ( this.object === undefined ) {
  47. var loader = new THREE.ObjectLoader();
  48. this.object = loader.parse( json.object );
  49. }
  50. }
  51. };
  52. export { RemoveObjectCommand };