CmdSetScriptValue.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Created by Daniel on 21.07.15.
  3. */
  4. CmdSetScriptValue = function ( object, script, attributeName, newValue, cursorPosition ) {
  5. Cmd.call( this );
  6. this.type = 'CmdSetScriptValue';
  7. this.name = 'Set Script.' + attributeName;
  8. this.updatable = true;
  9. this.object = object;
  10. this.script = script;
  11. this.attributeName = attributeName;
  12. this.oldValue = script !== undefined ? script[ this.attributeName ] : undefined;
  13. this.newValue = newValue;
  14. this.objectUuid = object !== undefined ? object.uuid : undefined;
  15. this.cursorPosition = cursorPosition; // Format {line: 2, ch: 3}
  16. };
  17. CmdSetScriptValue.prototype = {
  18. execute: function () {
  19. this.index = this.editor.scripts[ this.objectUuid ].indexOf( this.script );
  20. this.script[ this.attributeName ] = this.newValue;
  21. this.editor.signals.scriptChanged.dispatch();
  22. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  23. },
  24. undo: function () {
  25. this.script[ this.attributeName ] = this.oldValue;
  26. this.editor.signals.scriptChanged.dispatch();
  27. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  28. },
  29. update: function ( cmd ) {
  30. this.cursorPosition = cmd.cursorPosition;
  31. this.newValue = cmd.newValue;
  32. },
  33. toJSON: function () {
  34. var output = Cmd.prototype.toJSON.call( this );
  35. output.objectUuid = this.objectUuid;
  36. output.index = this.index;
  37. output.attributeName = this.attributeName;
  38. output.oldValue = this.oldValue;
  39. output.newValue = this.newValue;
  40. output.cursorPosition = this.cursorPosition;
  41. return output;
  42. },
  43. fromJSON: function ( json ) {
  44. Cmd.prototype.fromJSON.call( this, json );
  45. this.objectUuid = json.objectUuid;
  46. this.index = json.index;
  47. this.oldValue = json.oldValue;
  48. this.newValue = json.newValue;
  49. this.attributeName = json.attributeName;
  50. this.object = this.editor.objectByUuid( json.objectUuid );
  51. this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
  52. this.cursorPosition = json.cursorPosition;
  53. }
  54. };