SetSceneCommand.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  6. * @param scene containing children to import
  7. * @constructor
  8. */
  9. var SetSceneCommand = function ( scene ) {
  10. Command.call( this );
  11. this.type = 'SetSceneCommand';
  12. this.name = 'Set Scene';
  13. this.cmdArray = [];
  14. if ( scene !== undefined ) {
  15. this.cmdArray.push( new SetUuidCommand( this.editor.scene, scene.uuid ) );
  16. this.cmdArray.push( new SetValueCommand( this.editor.scene, 'name', scene.name ) );
  17. this.cmdArray.push( new SetValueCommand( this.editor.scene, 'userData', JSON.parse( JSON.stringify( scene.userData ) ) ) );
  18. while ( scene.children.length > 0 ) {
  19. var child = scene.children.pop();
  20. this.cmdArray.push( new AddObjectCommand( child ) );
  21. }
  22. }
  23. };
  24. SetSceneCommand.prototype = {
  25. execute: function () {
  26. this.editor.signals.sceneGraphChanged.active = false;
  27. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  28. this.cmdArray[ i ].execute();
  29. }
  30. this.editor.signals.sceneGraphChanged.active = true;
  31. this.editor.signals.sceneGraphChanged.dispatch();
  32. },
  33. undo: function () {
  34. this.editor.signals.sceneGraphChanged.active = false;
  35. for ( var i = this.cmdArray.length - 1; i >= 0; i -- ) {
  36. this.cmdArray[ i ].undo();
  37. }
  38. this.editor.signals.sceneGraphChanged.active = true;
  39. this.editor.signals.sceneGraphChanged.dispatch();
  40. },
  41. toJSON: function () {
  42. var output = Command.prototype.toJSON.call( this );
  43. var cmds = [];
  44. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  45. cmds.push( this.cmdArray[ i ].toJSON() );
  46. }
  47. output.cmds = cmds;
  48. return output;
  49. },
  50. fromJSON: function ( json ) {
  51. Command.prototype.fromJSON.call( this, json );
  52. var cmds = json.cmds;
  53. for ( var i = 0; i < cmds.length; i ++ ) {
  54. var cmd = new window[ cmds[ i ].type ](); // creates a new object of type "json.type"
  55. cmd.fromJSON( cmds[ i ] );
  56. this.cmdArray.push( cmd );
  57. }
  58. }
  59. };