SetScriptValueCommand.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /**
  7. * @param editor Editor
  8. * @param object THREE.Object3D
  9. * @param script javascript object
  10. * @param attributeName string
  11. * @param newValue string, object
  12. * @constructor
  13. */
  14. var SetScriptValueCommand = function ( editor, object, script, attributeName, newValue ) {
  15. Command.call( this, editor );
  16. this.type = 'SetScriptValueCommand';
  17. this.name = 'Set Script.' + attributeName;
  18. this.updatable = true;
  19. this.object = object;
  20. this.script = script;
  21. this.attributeName = attributeName;
  22. this.oldValue = ( script !== undefined ) ? script[ this.attributeName ] : undefined;
  23. this.newValue = newValue;
  24. };
  25. SetScriptValueCommand.prototype = {
  26. execute: function () {
  27. this.script[ this.attributeName ] = this.newValue;
  28. this.editor.signals.scriptChanged.dispatch();
  29. },
  30. undo: function () {
  31. this.script[ this.attributeName ] = this.oldValue;
  32. this.editor.signals.scriptChanged.dispatch();
  33. },
  34. update: function ( cmd ) {
  35. this.newValue = cmd.newValue;
  36. },
  37. toJSON: function () {
  38. var output = Command.prototype.toJSON.call( this );
  39. output.objectUuid = this.object.uuid;
  40. output.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  41. output.attributeName = this.attributeName;
  42. output.oldValue = this.oldValue;
  43. output.newValue = this.newValue;
  44. return output;
  45. },
  46. fromJSON: function ( json ) {
  47. Command.prototype.fromJSON.call( this, json );
  48. this.oldValue = json.oldValue;
  49. this.newValue = json.newValue;
  50. this.attributeName = json.attributeName;
  51. this.object = this.editor.objectByUuid( json.objectUuid );
  52. this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
  53. }
  54. };
  55. export { SetScriptValueCommand };