2
0

RemoveScriptCommand.js 1.4 KB

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