CmdRemoveObject.js 2.0 KB

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