CmdSetScriptValue.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. */
  4. /**
  5. * @param object THREE.Object3D
  6. * @param script javascript object
  7. * @param attributeName string
  8. * @param newValue string, object
  9. * @param cursorPosition javascript object with format {line: 2, ch: 3}
  10. * @constructor
  11. */
  12. CmdSetScriptValue = function ( object, script, attributeName, newValue, cursorPosition ) {
  13. Cmd.call( this );
  14. this.type = 'CmdSetScriptValue';
  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. this.cursorPosition = cursorPosition;
  23. };
  24. CmdSetScriptValue.prototype = {
  25. execute: function () {
  26. this.script[ this.attributeName ] = this.newValue;
  27. this.editor.signals.scriptChanged.dispatch();
  28. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  29. },
  30. undo: function () {
  31. this.script[ this.attributeName ] = this.oldValue;
  32. this.editor.signals.scriptChanged.dispatch();
  33. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
  34. },
  35. update: function ( cmd ) {
  36. this.cursorPosition = cmd.cursorPosition;
  37. this.newValue = cmd.newValue;
  38. },
  39. toJSON: function () {
  40. var output = Cmd.prototype.toJSON.call( this );
  41. output.objectUuid = this.object.uuid;
  42. output.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  43. output.attributeName = this.attributeName;
  44. output.oldValue = this.oldValue;
  45. output.newValue = this.newValue;
  46. output.cursorPosition = this.cursorPosition;
  47. return output;
  48. },
  49. fromJSON: function ( json ) {
  50. Cmd.prototype.fromJSON.call( this, json );
  51. this.oldValue = json.oldValue;
  52. this.newValue = json.newValue;
  53. this.attributeName = json.attributeName;
  54. this.object = this.editor.objectByUuid( json.objectUuid );
  55. this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
  56. this.cursorPosition = json.cursorPosition;
  57. }
  58. };