2
0

AddObjectCommand.js 1007 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 AddObjectCommand extends Command {
  9. constructor( editor, object ) {
  10. super( editor );
  11. this.type = 'AddObjectCommand';
  12. this.object = object;
  13. if ( object !== undefined ) {
  14. this.name = `Add Object: ${object.name}`;
  15. }
  16. }
  17. execute() {
  18. this.editor.addObject( this.object );
  19. this.editor.select( this.object );
  20. }
  21. undo() {
  22. this.editor.removeObject( this.object );
  23. this.editor.deselect();
  24. }
  25. toJSON() {
  26. const output = super.toJSON( this );
  27. output.object = this.object.toJSON();
  28. return output;
  29. }
  30. fromJSON( json ) {
  31. super.fromJSON( json );
  32. this.object = this.editor.objectByUuid( json.object.object.uuid );
  33. if ( this.object === undefined ) {
  34. const loader = new ObjectLoader();
  35. this.object = loader.parse( json.object );
  36. }
  37. }
  38. }
  39. export { AddObjectCommand };