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