SetScaleCommand.js 1.6 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 newScale THREE.Vector3
  7. * @param optionalOldScale THREE.Vector3
  8. * @constructor
  9. */
  10. class SetScaleCommand extends Command {
  11. constructor( editor, object = null, newScale = null, optionalOldScale = null ) {
  12. super( editor );
  13. this.type = 'SetScaleCommand';
  14. this.name = editor.strings.getKey( 'command/SetScale' );
  15. this.updatable = true;
  16. this.object = object;
  17. if ( object !== null && newScale !== null ) {
  18. this.oldScale = object.scale.clone();
  19. this.newScale = newScale.clone();
  20. }
  21. if ( optionalOldScale !== null ) {
  22. this.oldScale = optionalOldScale.clone();
  23. }
  24. }
  25. execute() {
  26. this.object.scale.copy( this.newScale );
  27. this.object.updateMatrixWorld( true );
  28. this.editor.signals.objectChanged.dispatch( this.object );
  29. }
  30. undo() {
  31. this.object.scale.copy( this.oldScale );
  32. this.object.updateMatrixWorld( true );
  33. this.editor.signals.objectChanged.dispatch( this.object );
  34. }
  35. update( command ) {
  36. this.newScale.copy( command.newScale );
  37. }
  38. toJSON() {
  39. const output = super.toJSON( this );
  40. output.objectUuid = this.object.uuid;
  41. output.oldScale = this.oldScale.toArray();
  42. output.newScale = this.newScale.toArray();
  43. return output;
  44. }
  45. fromJSON( json ) {
  46. super.fromJSON( json );
  47. this.object = this.editor.objectByUuid( json.objectUuid );
  48. this.oldScale = new Vector3().fromArray( json.oldScale );
  49. this.newScale = new Vector3().fromArray( json.newScale );
  50. }
  51. }
  52. export { SetScaleCommand };