CmdSetScene.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Created by Daniel on 20.07.15.
  3. */
  4. CmdSetScene = function ( oldScene, 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( oldScene, newScene.uuid ) );
  11. this.cmdArray.push( new CmdSetValue( oldScene, 'name', newScene.name ) );
  12. this.cmdArray.push( new CmdSetValue( oldScene, '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 ].editor = this.editor;
  24. this.cmdArray[ i ].execute();
  25. }
  26. this.editor.signals.sceneGraphChanged.active = true;
  27. this.editor.signals.sceneGraphChanged.dispatch();
  28. },
  29. undo: function () {
  30. this.editor.signals.sceneGraphChanged.active = false;
  31. for ( var i = this.cmdArray.length - 1; i >= 0; i -- ) {
  32. this.cmdArray[ i ].undo();
  33. }
  34. this.editor.signals.sceneGraphChanged.active = true;
  35. this.editor.signals.sceneGraphChanged.dispatch();
  36. },
  37. toJSON: function () {
  38. var output = Cmd.prototype.toJSON.call( this );
  39. var cmds = [];
  40. for ( var i = 0; i < this.cmdArray.length; i ++ ) {
  41. cmds.push( this.cmdArray[ i ].toJSON() );
  42. }
  43. output.cmds = cmds;
  44. return output;
  45. },
  46. fromJSON: function ( json ) {
  47. Cmd.prototype.fromJSON.call( this, json );
  48. var cmds = json.cmds;
  49. for ( var i = 0; i < cmds.length; i ++ ) {
  50. var cmd = new window[ cmds[ i ].type ](); // creates a new object of type "json.type"
  51. cmd.editor = this.editor;
  52. cmd.fromJSON( cmds[ i ] );
  53. this.cmdArray.push( cmd );
  54. }
  55. }
  56. };