2
0

RemoveObjectCommand.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Command } from '../Command.js';
  2. import { ObjectLoader } from '../../../build/three.module.js';
  3. /**
  4. * @param editor Editor
  5. * @param object THREE.Object3D
  6. * @constructor
  7. */
  8. class RemoveObjectCommand extends Command {
  9. constructor( editor, object ) {
  10. super( editor );
  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. execute() {
  20. this.editor.removeObject( this.object );
  21. this.editor.deselect();
  22. }
  23. undo() {
  24. this.editor.addObject( this.object, this.parent, this.index );
  25. this.editor.select( this.object );
  26. }
  27. toJSON() {
  28. const output = super.toJSON( this );
  29. output.object = this.object.toJSON();
  30. output.index = this.index;
  31. output.parentUuid = this.parent.uuid;
  32. return output;
  33. }
  34. fromJSON( json ) {
  35. super.fromJSON( json );
  36. this.parent = this.editor.objectByUuid( json.parentUuid );
  37. if ( this.parent === undefined ) {
  38. this.parent = this.editor.scene;
  39. }
  40. this.index = json.index;
  41. this.object = this.editor.objectByUuid( json.object.object.uuid );
  42. if ( this.object === undefined ) {
  43. const loader = new ObjectLoader();
  44. this.object = loader.parse( json.object );
  45. }
  46. }
  47. }
  48. export { RemoveObjectCommand };