SetSceneCommand.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. function SetSceneCommand( editor, scene ) {
  11. Command.call( this, editor );
  12. this.type = 'SetSceneCommand';
  13. this.name = 'Set Scene';
  14. this.cmdArray = [];
  15. if ( scene !== undefined ) {
  16. this.cmdArray.push( new SetUuidCommand( this.editor, this.editor.scene, scene.uuid ) );
  17. this.cmdArray.push( new SetValueCommand( this.editor, this.editor.scene, 'name', scene.name ) );
  18. this.cmdArray.push( new SetValueCommand( this.editor, this.editor.scene, 'userData', JSON.parse( JSON.stringify( scene.userData ) ) ) );
  19. while ( scene.children.length > 0 ) {
  20. var child = scene.children.pop();
  21. this.cmdArray.push( new AddObjectCommand( this.editor, child ) );
  22. }
  23. }
  24. }
  25. SetSceneCommand.prototype = {
  26. execute: function () {
  27. this.editor.signals.sceneGraphChanged.active = false;
  28. for ( var 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: function () {
  35. this.editor.signals.sceneGraphChanged.active = false;
  36. for ( var 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: function () {
  43. var output = Command.prototype.toJSON.call( this );
  44. var cmds = [];
  45. for ( var 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: function ( json ) {
  52. Command.prototype.fromJSON.call( this, json );
  53. var cmds = json.cmds;
  54. for ( var i = 0; i < cmds.length; i ++ ) {
  55. var 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 };