SetSceneCommand.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Command } from '../Command.js';
  2. import { SetUuidCommand } from './SetUuidCommand.js';
  3. import { SetValueCommand } from './SetValueCommand.js';
  4. import { AddObjectCommand } from './AddObjectCommand.js';
  5. /**
  6. * @param editor Editor
  7. * @param scene containing children to import
  8. * @constructor
  9. */
  10. class SetSceneCommand extends Command {
  11. constructor( editor, scene = null ) {
  12. super( editor );
  13. this.type = 'SetSceneCommand';
  14. this.name = editor.strings.getKey( 'command/SetScene' );
  15. this.cmdArray = [];
  16. if ( scene !== null ) {
  17. this.cmdArray.push( new SetUuidCommand( this.editor, this.editor.scene, scene.uuid ) );
  18. this.cmdArray.push( new SetValueCommand( this.editor, this.editor.scene, 'name', scene.name ) );
  19. this.cmdArray.push( new SetValueCommand( this.editor, this.editor.scene, 'userData', JSON.parse( JSON.stringify( scene.userData ) ) ) );
  20. while ( scene.children.length > 0 ) {
  21. const child = scene.children.pop();
  22. this.cmdArray.push( new AddObjectCommand( this.editor, child ) );
  23. }
  24. }
  25. }
  26. execute() {
  27. this.editor.signals.sceneGraphChanged.active = false;
  28. for ( let i = 0; i < this.cmdArray.length; i ++ ) {
  29. this.cmdArray[ i ].execute();
  30. }
  31. this.editor.signals.sceneGraphChanged.active = true;
  32. this.editor.signals.sceneGraphChanged.dispatch();
  33. }
  34. undo() {
  35. this.editor.signals.sceneGraphChanged.active = false;
  36. for ( let i = this.cmdArray.length - 1; i >= 0; i -- ) {
  37. this.cmdArray[ i ].undo();
  38. }
  39. this.editor.signals.sceneGraphChanged.active = true;
  40. this.editor.signals.sceneGraphChanged.dispatch();
  41. }
  42. toJSON() {
  43. const output = super.toJSON( this );
  44. const cmds = [];
  45. for ( let i = 0; i < this.cmdArray.length; i ++ ) {
  46. cmds.push( this.cmdArray[ i ].toJSON() );
  47. }
  48. output.cmds = cmds;
  49. return output;
  50. }
  51. fromJSON( json ) {
  52. super.fromJSON( json );
  53. const cmds = json.cmds;
  54. for ( let i = 0; i < cmds.length; i ++ ) {
  55. const cmd = new window[ cmds[ i ].type ](); // creates a new object of type "json.type"
  56. cmd.fromJSON( cmds[ i ] );
  57. this.cmdArray.push( cmd );
  58. }
  59. }
  60. }
  61. export { SetSceneCommand };