SetPositionCommand.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /**
  6. * @param editor Editor
  7. * @param object THREE.Object3D
  8. * @param newPosition THREE.Vector3
  9. * @param optionalOldPosition THREE.Vector3
  10. * @constructor
  11. */
  12. var SetPositionCommand = function ( editor, object, newPosition, optionalOldPosition ) {
  13. Command.call( this, editor );
  14. this.type = 'SetPositionCommand';
  15. this.name = 'Set Position';
  16. this.updatable = true;
  17. this.object = object;
  18. if ( object !== undefined && newPosition !== undefined ) {
  19. this.oldPosition = object.position.clone();
  20. this.newPosition = newPosition.clone();
  21. }
  22. if ( optionalOldPosition !== undefined ) {
  23. this.oldPosition = optionalOldPosition.clone();
  24. }
  25. };
  26. SetPositionCommand.prototype = {
  27. execute: function () {
  28. this.object.position.copy( this.newPosition );
  29. this.object.updateMatrixWorld( true );
  30. this.editor.signals.objectChanged.dispatch( this.object );
  31. },
  32. undo: function () {
  33. this.object.position.copy( this.oldPosition );
  34. this.object.updateMatrixWorld( true );
  35. this.editor.signals.objectChanged.dispatch( this.object );
  36. },
  37. update: function ( command ) {
  38. this.newPosition.copy( command.newPosition );
  39. },
  40. toJSON: function () {
  41. var output = Command.prototype.toJSON.call( this );
  42. output.objectUuid = this.object.uuid;
  43. output.oldPosition = this.oldPosition.toArray();
  44. output.newPosition = this.newPosition.toArray();
  45. return output;
  46. },
  47. fromJSON: function ( json ) {
  48. Command.prototype.fromJSON.call( this, json );
  49. this.object = this.editor.objectByUuid( json.objectUuid );
  50. this.oldPosition = new THREE.Vector3().fromArray( json.oldPosition );
  51. this.newPosition = new THREE.Vector3().fromArray( json.newPosition );
  52. }
  53. };