CmdRemoveObject.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Created by Daniel on 20.07.15.
  3. */
  4. CmdRemoveObject = function ( object ) {
  5. Cmd.call( this );
  6. this.type = 'CmdRemoveObject';
  7. this.object = object;
  8. this.parent = object !== undefined ? object.parent : undefined;
  9. this.parentUuid = object !== undefined ? object.parent.uuid : undefined;
  10. if ( object !== undefined ) {
  11. meta = {
  12. geometries: {},
  13. materials: {},
  14. textures: {},
  15. images: {}
  16. };
  17. this.objectJSON = object.toJSON( meta );
  18. if ( object.geometry !== undefined ) {
  19. this.objectJSON.geometries = [];
  20. this.objectJSON.geometries.push( object.geometry.toJSON( meta ) );
  21. }
  22. if ( object.material !== undefined ) {
  23. this.objectJSON.materials = [];
  24. this.objectJSON.materials.push( object.material.toJSON( meta ) );
  25. }
  26. }
  27. };
  28. CmdRemoveObject.prototype = {
  29. execute: function () {
  30. this.index = this.parent.children.indexOf( this.object );
  31. var scope = this.editor;
  32. this.object.traverse( function ( child ) {
  33. scope.removeHelper( child );
  34. } );
  35. this.parent.remove( this.object );
  36. this.editor.signals.objectRemoved.dispatch( this.object );
  37. this.editor.signals.sceneGraphChanged.dispatch();
  38. },
  39. undo: function () {
  40. var scope = this.editor;
  41. this.object.traverse( function ( child ) {
  42. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  43. if ( child.material !== undefined ) scope.addMaterial( child.material );
  44. scope.addHelper( child );
  45. } );
  46. this.parent.children.splice( this.index, 0, this.object );
  47. this.object.parent = this.parent;
  48. this.editor.signals.objectAdded.dispatch( this.object );
  49. this.editor.signals.sceneGraphChanged.dispatch();
  50. },
  51. toJSON: function () {
  52. var output = Cmd.prototype.toJSON.call( this );
  53. output.object = this.objectJSON;
  54. output.index = this.index;
  55. output.parentUuid = this.parentUuid;
  56. return output;
  57. },
  58. fromJSON: function ( json ) {
  59. Cmd.prototype.fromJSON.call( this, json );
  60. this.parent = this.editor.objectByUuid( json.parentUuid );
  61. if ( this.parent === undefined ) {
  62. this.parent = this.editor.scene;
  63. }
  64. this.parentUuid = json.parentUuid;
  65. this.index = json.index;
  66. this.object = this.editor.objectByUuid( json.object.object.uuid );
  67. if ( this.object === undefined ) {
  68. var loader = new THREE.ObjectLoader();
  69. this.object = loader.parse( json.object );
  70. }
  71. this.objectJSON = json.object;
  72. }
  73. };