CmdSetScriptValue.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.updatable = true;
  8. this.object = object;
  9. this.script = script;
  10. this.attributeName = attributeName;
  11. this.oldValue = script !== undefined ? script[ this.attributeName ] : undefined;
  12. this.newValue = newValue;
  13. this.objectUuid = object !== undefined ? object.uuid : undefined;
  14. this.cursorPosition = cursorPosition; // Format {line: 2, ch: 3}
  15. };
  16. CmdSetScriptValue.prototype = {
  17. execute: function () {
  18. this.index = this.editor.scripts[ this.objectUuid ].indexOf( this.script );
  19. this.script[ this.attributeName ] = this.newValue;
  20. this.editor.signals.scriptChanged.dispatch();
  21. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  22. },
  23. undo: function () {
  24. this.script[ this.attributeName ] = this.oldValue;
  25. this.editor.signals.scriptChanged.dispatch();
  26. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  27. },
  28. update: function ( cmd ) {
  29. this.cursorPosition = cmd.cursorPosition;
  30. this.newValue = cmd.newValue;
  31. },
  32. toJSON: function () {
  33. var output = Cmd.prototype.toJSON.call( this );
  34. output.objectUuid = this.objectUuid;
  35. output.index = this.index;
  36. output.attributeName = this.attributeName;
  37. output.oldValue = this.oldValue;
  38. output.newValue = this.newValue;
  39. output.cursorPosition = this.cursorPosition;
  40. return output;
  41. },
  42. fromJSON: function ( json ) {
  43. Cmd.prototype.fromJSON.call( this, json );
  44. this.objectUuid = json.objectUuid;
  45. this.index = json.index;
  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. this.cursorPosition = json.cursorPosition;
  52. }
  53. };