AddObjectCommand.js 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 AddObjectCommand extends Command {
  9. constructor( editor, object = null ) {
  10. super( editor );
  11. this.type = 'AddObjectCommand';
  12. this.object = object;
  13. if ( object !== null ) {
  14. this.name = editor.strings.getKey( 'command/AddObject' ) + ': ' + 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 };