CmdSetScene.js 1.8 KB

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