SetScriptValueCommand.js 2.2 KB

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