AddScriptCommand.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Command } from '../Command.js';
  2. /**
  3. * @param editor Editor
  4. * @param object THREE.Object3D
  5. * @param script javascript object
  6. * @constructor
  7. */
  8. class AddScriptCommand extends Command {
  9. constructor( editor, object, script ) {
  10. super( editor );
  11. this.type = 'AddScriptCommand';
  12. this.name = 'Add Script';
  13. this.object = object;
  14. this.script = script;
  15. }
  16. execute() {
  17. if ( this.editor.scripts[ this.object.uuid ] === undefined ) {
  18. this.editor.scripts[ this.object.uuid ] = [];
  19. }
  20. this.editor.scripts[ this.object.uuid ].push( this.script );
  21. this.editor.signals.scriptAdded.dispatch( this.script );
  22. }
  23. undo() {
  24. if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
  25. const index = this.editor.scripts[ this.object.uuid ].indexOf( this.script );
  26. if ( index !== - 1 ) {
  27. this.editor.scripts[ this.object.uuid ].splice( index, 1 );
  28. }
  29. this.editor.signals.scriptRemoved.dispatch( this.script );
  30. }
  31. toJSON() {
  32. const output = super.toJSON( this );
  33. output.objectUuid = this.object.uuid;
  34. output.script = this.script;
  35. return output;
  36. }
  37. fromJSON( json ) {
  38. super.fromJSON( json );
  39. this.script = json.script;
  40. this.object = this.editor.objectByUuid( json.objectUuid );
  41. }
  42. }
  43. export { AddScriptCommand };