RemoveScriptCommand.js 1.6 KB

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