2
0

AddScriptCommand.js 1.5 KB

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