SetPositionCommand.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. import { Command } from '../Command.js';
  6. import { Vector3 } from '../../../build/three.module.js';
  7. /**
  8. * @param editor Editor
  9. * @param object THREE.Object3D
  10. * @param newPosition THREE.Vector3
  11. * @param optionalOldPosition THREE.Vector3
  12. * @constructor
  13. */
  14. var SetPositionCommand = function ( editor, object, newPosition, optionalOldPosition ) {
  15. Command.call( this, editor );
  16. this.type = 'SetPositionCommand';
  17. this.name = 'Set Position';
  18. this.updatable = true;
  19. this.object = object;
  20. if ( object !== undefined && newPosition !== undefined ) {
  21. this.oldPosition = object.position.clone();
  22. this.newPosition = newPosition.clone();
  23. }
  24. if ( optionalOldPosition !== undefined ) {
  25. this.oldPosition = optionalOldPosition.clone();
  26. }
  27. };
  28. SetPositionCommand.prototype = {
  29. execute: function () {
  30. this.object.position.copy( this.newPosition );
  31. this.object.updateMatrixWorld( true );
  32. this.editor.signals.objectChanged.dispatch( this.object );
  33. },
  34. undo: function () {
  35. this.object.position.copy( this.oldPosition );
  36. this.object.updateMatrixWorld( true );
  37. this.editor.signals.objectChanged.dispatch( this.object );
  38. },
  39. update: function ( command ) {
  40. this.newPosition.copy( command.newPosition );
  41. },
  42. toJSON: function () {
  43. var output = Command.prototype.toJSON.call( this );
  44. output.objectUuid = this.object.uuid;
  45. output.oldPosition = this.oldPosition.toArray();
  46. output.newPosition = this.newPosition.toArray();
  47. return output;
  48. },
  49. fromJSON: function ( json ) {
  50. Command.prototype.fromJSON.call( this, json );
  51. this.object = this.editor.objectByUuid( json.objectUuid );
  52. this.oldPosition = new Vector3().fromArray( json.oldPosition );
  53. this.newPosition = new Vector3().fromArray( json.newPosition );
  54. }
  55. };
  56. export { SetPositionCommand };