AddScriptCommand.js 1.4 KB

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