SetRotationCommand.js 1.6 KB

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