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. function AddScriptCommand( editor, object, script ) {
  9. Command.call( this, editor );
  10. this.type = 'AddScriptCommand';
  11. this.name = 'Add Script';
  12. this.object = object;
  13. this.script = script;
  14. }
  15. AddScriptCommand.prototype = {
  16. execute: function () {
  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: function () {
  24. if ( this.editor.scripts[ this.object.uuid ] === undefined ) return;
  25. var 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: function () {
  32. var output = Command.prototype.toJSON.call( this );
  33. output.objectUuid = this.object.uuid;
  34. output.script = this.script;
  35. return output;
  36. },
  37. fromJSON: function ( json ) {
  38. Command.prototype.fromJSON.call( this, json );
  39. this.script = json.script;
  40. this.object = this.editor.objectByUuid( json.objectUuid );
  41. }
  42. };
  43. export { AddScriptCommand };