SetScaleCommand.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * @param newScale THREE.Vector3
  7. * @param optionalOldScale THREE.Vector3
  8. * @constructor
  9. */
  10. function SetScaleCommand( editor, object, newScale, optionalOldScale ) {
  11. Command.call( this, editor );
  12. this.type = 'SetScaleCommand';
  13. this.name = 'Set Scale';
  14. this.updatable = true;
  15. this.object = object;
  16. if ( object !== undefined && newScale !== undefined ) {
  17. this.oldScale = object.scale.clone();
  18. this.newScale = newScale.clone();
  19. }
  20. if ( optionalOldScale !== undefined ) {
  21. this.oldScale = optionalOldScale.clone();
  22. }
  23. }
  24. SetScaleCommand.prototype = {
  25. execute: function () {
  26. this.object.scale.copy( this.newScale );
  27. this.object.updateMatrixWorld( true );
  28. this.editor.signals.objectChanged.dispatch( this.object );
  29. },
  30. undo: function () {
  31. this.object.scale.copy( this.oldScale );
  32. this.object.updateMatrixWorld( true );
  33. this.editor.signals.objectChanged.dispatch( this.object );
  34. },
  35. update: function ( command ) {
  36. this.newScale.copy( command.newScale );
  37. },
  38. toJSON: function () {
  39. var output = Command.prototype.toJSON.call( 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: function ( json ) {
  46. Command.prototype.fromJSON.call( this, json );
  47. this.object = this.editor.objectByUuid( json.objectUuid );
  48. this.oldScale = new THREE.Vector3().fromArray( json.oldScale );
  49. this.newScale = new THREE.Vector3().fromArray( json.newScale );
  50. }
  51. };
  52. export { SetScaleCommand };