RemoveObjectCommand.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { Command } from '../Command.js';
  2. import { ObjectLoader } from 'three';
  3. /**
  4. * @param editor Editor
  5. * @param object THREE.Object3D
  6. * @constructor
  7. */
  8. class RemoveObjectCommand extends Command {
  9. constructor( editor, object = null ) {
  10. super( editor );
  11. this.type = 'RemoveObjectCommand';
  12. this.object = object;
  13. this.parent = ( object !== null ) ? object.parent : null;
  14. if ( this.parent !== null ) {
  15. this.index = this.parent.children.indexOf( this.object );
  16. }
  17. if ( object !== null ) {
  18. this.name = editor.strings.getKey( 'command/RemoveObject' ) + ': ' + object.name;
  19. }
  20. }
  21. execute() {
  22. this.editor.removeObject( this.object );
  23. this.editor.deselect();
  24. }
  25. undo() {
  26. this.editor.addObject( this.object, this.parent, this.index );
  27. this.editor.select( this.object );
  28. }
  29. toJSON() {
  30. const output = super.toJSON( this );
  31. output.object = this.object.toJSON();
  32. output.index = this.index;
  33. output.parentUuid = this.parent.uuid;
  34. return output;
  35. }
  36. fromJSON( json ) {
  37. super.fromJSON( json );
  38. this.parent = this.editor.objectByUuid( json.parentUuid );
  39. if ( this.parent === undefined ) {
  40. this.parent = this.editor.scene;
  41. }
  42. this.index = json.index;
  43. this.object = this.editor.objectByUuid( json.object.object.uuid );
  44. if ( this.object === undefined ) {
  45. const loader = new ObjectLoader();
  46. this.object = loader.parse( json.object );
  47. }
  48. }
  49. }
  50. export { RemoveObjectCommand };