CmdRemoveObject.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = [ object.geometry.toJSON( meta ) ];
  20. }
  21. if ( object.material !== undefined ) {
  22. this.objectJSON.materials = [ object.material.toJSON( meta ) ];
  23. }
  24. }
  25. };
  26. CmdRemoveObject.prototype = {
  27. execute: function () {
  28. this.index = this.parent.children.indexOf( this.object );
  29. var scope = this.editor;
  30. this.object.traverse( function ( child ) {
  31. scope.removeHelper( child );
  32. } );
  33. this.parent.remove( this.object );
  34. this.editor.signals.objectRemoved.dispatch( this.object );
  35. this.editor.signals.sceneGraphChanged.dispatch();
  36. },
  37. undo: function () {
  38. var scope = this.editor;
  39. this.object.traverse( function ( child ) {
  40. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  41. if ( child.material !== undefined ) scope.addMaterial( child.material );
  42. scope.addHelper( child );
  43. } );
  44. this.parent.children.splice( this.index, 0, this.object );
  45. this.object.parent = this.parent;
  46. this.editor.signals.objectAdded.dispatch( this.object );
  47. this.editor.signals.sceneGraphChanged.dispatch();
  48. },
  49. toJSON: function () {
  50. var output = Cmd.prototype.toJSON.call( this );
  51. output.object = this.objectJSON;
  52. output.index = this.index;
  53. output.parentUuid = this.parentUuid;
  54. return output;
  55. },
  56. fromJSON: function ( json ) {
  57. Cmd.prototype.fromJSON.call( this, json );
  58. this.parent = this.editor.objectByUuid( json.parentUuid );
  59. if ( this.parent === undefined ) {
  60. this.parent = this.editor.scene;
  61. }
  62. this.parentUuid = json.parentUuid;
  63. this.index = json.index;
  64. this.object = this.editor.objectByUuid( json.object.object.uuid );
  65. if ( this.object === undefined ) {
  66. var loader = new THREE.ObjectLoader();
  67. this.object = loader.parse( json.object );
  68. }
  69. this.objectJSON = json.object;
  70. }
  71. };