2
0

SetSceneCommand.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. import { Command } from '../Command.js';
  6. import { SetUuidCommand } from './SetUuidCommand.js';
  7. import { SetValueCommand } from './SetValueCommand.js';
  8. import { AddObjectCommand } from './AddObjectCommand.js';
  9. /**
  10. * @param editor Editor
  11. * @param scene containing children to import
  12. * @constructor
  13. */
  14. var SetSceneCommand = function ( editor, scene ) {
  15. Command.call( this, editor );
  16. this.type = 'SetSceneCommand';
  17. this.name = 'Set Scene';
  18. this.cmdArray = [];
  19. if ( scene !== undefined ) {
  20. this.cmdArray.push( new SetUuidCommand( this.editor, this.editor.scene, scene.uuid ) );
  21. this.cmdArray.push( new SetValueCommand( this.editor, this.editor.scene, 'name', scene.name ) );
  22. this.cmdArray.push( new SetValueCommand( this.editor, this.editor.scene, 'userData', JSON.parse( JSON.stringify( scene.userData ) ) ) );
  23. while ( scene.children.length > 0 ) {
  24. var child = scene.children.pop();
  25. this.cmdArray.push( new AddObjectCommand( this.editor, child ) );
  26. }
  27. }
  28. };
  29. SetSceneCommand.prototype = {
  30. execute: function () {
  31. this.editor.signals.sceneGraphChanged.active = false;
  32. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  33. this.cmdArray[ i ].execute();
  34. }
  35. this.editor.signals.sceneGraphChanged.active = true;
  36. this.editor.signals.sceneGraphChanged.dispatch();
  37. },
  38. undo: function () {
  39. this.editor.signals.sceneGraphChanged.active = false;
  40. for ( var i = this.cmdArray.length - 1; i >= 0; i -- ) {
  41. this.cmdArray[ i ].undo();
  42. }
  43. this.editor.signals.sceneGraphChanged.active = true;
  44. this.editor.signals.sceneGraphChanged.dispatch();
  45. },
  46. toJSON: function () {
  47. var output = Command.prototype.toJSON.call( this );
  48. var cmds = [];
  49. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  50. cmds.push( this.cmdArray[ i ].toJSON() );
  51. }
  52. output.cmds = cmds;
  53. return output;
  54. },
  55. fromJSON: function ( json ) {
  56. Command.prototype.fromJSON.call( this, json );
  57. var cmds = json.cmds;
  58. for ( var i = 0; i < cmds.length; i ++ ) {
  59. var cmd = new window[ cmds[ i ].type ](); // creates a new object of type "json.type"
  60. cmd.fromJSON( cmds[ i ] );
  61. this.cmdArray.push( cmd );
  62. }
  63. }
  64. };
  65. export { SetSceneCommand };