SetPositionCommand.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Command } from '../Command.js';
  2. import { Vector3 } from 'three';
  3. /**
  4. * @param editor Editor
  5. * @param object THREE.Object3D
  6. * @param newPosition THREE.Vector3
  7. * @param optionalOldPosition THREE.Vector3
  8. * @constructor
  9. */
  10. class SetPositionCommand extends Command {
  11. constructor( editor, object = null, newPosition = null, optionalOldPosition = null ) {
  12. super( editor );
  13. this.type = 'SetPositionCommand';
  14. this.name = editor.strings.getKey( 'command/SetPosition' );
  15. this.updatable = true;
  16. this.object = object;
  17. if ( object !== null && newPosition !== null ) {
  18. this.oldPosition = object.position.clone();
  19. this.newPosition = newPosition.clone();
  20. }
  21. if ( optionalOldPosition !== null ) {
  22. this.oldPosition = optionalOldPosition.clone();
  23. }
  24. }
  25. execute() {
  26. this.object.position.copy( this.newPosition );
  27. this.object.updateMatrixWorld( true );
  28. this.editor.signals.objectChanged.dispatch( this.object );
  29. }
  30. undo() {
  31. this.object.position.copy( this.oldPosition );
  32. this.object.updateMatrixWorld( true );
  33. this.editor.signals.objectChanged.dispatch( this.object );
  34. }
  35. update( command ) {
  36. this.newPosition.copy( command.newPosition );
  37. }
  38. toJSON() {
  39. const output = super.toJSON( this );
  40. output.objectUuid = this.object.uuid;
  41. output.oldPosition = this.oldPosition.toArray();
  42. output.newPosition = this.newPosition.toArray();
  43. return output;
  44. }
  45. fromJSON( json ) {
  46. super.fromJSON( json );
  47. this.object = this.editor.objectByUuid( json.objectUuid );
  48. this.oldPosition = new Vector3().fromArray( json.oldPosition );
  49. this.newPosition = new Vector3().fromArray( json.newPosition );
  50. }
  51. }
  52. export { SetPositionCommand };