RemoveObjectCommand.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Command } from '../Command.js';
  2. import * as THREE from '../../../build/three.module.js';
  3. /**
  4. * @param editor Editor
  5. * @param object THREE.Object3D
  6. * @constructor
  7. */
  8. function RemoveObjectCommand( editor, object ) {
  9. Command.call( this, editor );
  10. this.type = 'RemoveObjectCommand';
  11. this.name = 'Remove Object';
  12. this.object = object;
  13. this.parent = ( object !== undefined ) ? object.parent : undefined;
  14. if ( this.parent !== undefined ) {
  15. this.index = this.parent.children.indexOf( this.object );
  16. }
  17. }
  18. RemoveObjectCommand.prototype = {
  19. execute: function () {
  20. this.editor.removeObject( this.object );
  21. this.editor.deselect();
  22. },
  23. undo: function () {
  24. this.editor.addObject( this.object, this.parent, this.index );
  25. this.editor.select( this.object );
  26. },
  27. toJSON: function () {
  28. var output = Command.prototype.toJSON.call( this );
  29. output.object = this.object.toJSON();
  30. output.index = this.index;
  31. output.parentUuid = this.parent.uuid;
  32. return output;
  33. },
  34. fromJSON: function ( json ) {
  35. Command.prototype.fromJSON.call( this, 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. var loader = new THREE.ObjectLoader();
  44. this.object = loader.parse( json.object );
  45. }
  46. }
  47. };
  48. export { RemoveObjectCommand };