SetScriptValueCommand.js 1.7 KB

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