CmdSetScene.js 1.8 KB

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