SetPositionCommand.js 1.8 KB

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