SetScriptValueCommand.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * @param cursorPosition javascript object with format {line: 2, ch: 3}
  11. * @param scrollInfo javascript object with values {left, top, width, height, clientWidth, clientHeight}
  12. * @constructor
  13. */
  14. var SetScriptValueCommand = function ( object, script, attributeName, newValue, cursorPosition, scrollInfo ) {
  15. Command.call( this );
  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. this.cursorPosition = cursorPosition;
  25. this.scrollInfo = scrollInfo;
  26. };
  27. SetScriptValueCommand.prototype = {
  28. execute: function () {
  29. this.script[ this.attributeName ] = this.newValue;
  30. this.editor.signals.scriptChanged.dispatch();
  31. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition, this.scrollInfo );
  32. },
  33. undo: function () {
  34. this.script[ this.attributeName ] = this.oldValue;
  35. this.editor.signals.scriptChanged.dispatch();
  36. this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition, this.scrollInfo );
  37. },
  38. update: function ( cmd ) {
  39. this.cursorPosition = cmd.cursorPosition;
  40. this.scrollInfo = cmd.scrollInfo;
  41. this.newValue = cmd.newValue;
  42. },
  43. toJSON: function () {
  44. var output = Command.prototype.toJSON.call( this );
  45. output.objectUuid = this.object.uuid;
  46. output.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  47. output.attributeName = this.attributeName;
  48. output.oldValue = this.oldValue;
  49. output.newValue = this.newValue;
  50. output.cursorPosition = this.cursorPosition;
  51. output.scrollInfo = this.scrollInfo;
  52. return output;
  53. },
  54. fromJSON: function ( json ) {
  55. Command.prototype.fromJSON.call( this, json );
  56. this.oldValue = json.oldValue;
  57. this.newValue = json.newValue;
  58. this.attributeName = json.attributeName;
  59. this.object = this.editor.objectByUuid( json.objectUuid );
  60. this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
  61. this.cursorPosition = json.cursorPosition;
  62. this.scrollInfo = json.scrollInfo;
  63. }
  64. };