AddScriptCommand.js 1.5 KB

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