RemoveScriptCommand.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 editor Editor
  7. * @param object THREE.Object3D
  8. * @param script javascript object
  9. * @constructor
  10. */
  11. var RemoveScriptCommand = function ( editor, object, script ) {
  12. Command.call( this, editor );
  13. this.type = 'RemoveScriptCommand';
  14. this.name = 'Remove Script';
  15. this.object = object;
  16. this.script = script;
  17. if ( this.object && this.script ) {
  18. this.index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  19. }
  20. };
  21. RemoveScriptCommand.prototype = {
  22. execute: function () {
  23. if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
  24. if ( this.index !== - 1 ) {
  25. this.editor.scripts[ this.object.uuid ].splice( this.index, 1 );
  26. }
  27. this.editor.signals.scriptRemoved.dispatch( this.script );
  28. },
  29. undo: function () {
  30. if ( this.editor.scripts[ this.object.uuid ] === undefined ) {
  31. this.editor.scripts[ this.object.uuid ] = [];
  32. }
  33. this.editor.scripts[ this.object.uuid ].splice( this.index, 0, this.script );
  34. this.editor.signals.scriptAdded.dispatch( this.script );
  35. },
  36. toJSON: function () {
  37. var output = Command.prototype.toJSON.call( this );
  38. output.objectUuid = this.object.uuid;
  39. output.script = this.script;
  40. output.index = this.index;
  41. return output;
  42. },
  43. fromJSON: function ( json ) {
  44. Command.prototype.fromJSON.call( this, json );
  45. this.script = json.script;
  46. this.index = json.index;
  47. this.object = this.editor.objectByUuid( json.objectUuid );
  48. }
  49. };