CmdRemoveObject.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. object.updateMatrixWorld( true );
  12. meta = {
  13. geometries: {},
  14. materials: {},
  15. textures: {},
  16. images: {}
  17. };
  18. var json = object.toJSON( meta );
  19. var geometries = extractFromCache( meta.geometries );
  20. var materials = extractFromCache( meta.materials );
  21. var textures = extractFromCache( meta.textures );
  22. var images = extractFromCache( meta.images );
  23. if ( geometries.length > 0 ) json.geometries = geometries;
  24. if ( materials.length > 0 ) json.materials = materials;
  25. if ( textures.length > 0 ) json.textures = textures;
  26. if ( images.length > 0 ) json.images = images;
  27. this.objectJSON = json;
  28. }
  29. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  30. // extract data from the cache hash
  31. // remove metadata on each item
  32. // and return as array
  33. function extractFromCache ( cache ) {
  34. var values = [];
  35. for ( var key in cache ) {
  36. var data = cache[ key ];
  37. delete data.metadata;
  38. values.push( data );
  39. }
  40. return values;
  41. }
  42. };
  43. CmdRemoveObject.prototype = {
  44. execute: function () {
  45. this.index = this.parent.children.indexOf( this.object );
  46. var scope = this.editor;
  47. this.object.traverse( function ( child ) {
  48. scope.removeHelper( child );
  49. } );
  50. this.parent.remove( this.object );
  51. this.editor.signals.objectRemoved.dispatch( this.object );
  52. this.editor.signals.sceneGraphChanged.dispatch();
  53. },
  54. undo: function () {
  55. var scope = this.editor;
  56. this.object.traverse( function ( child ) {
  57. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  58. if ( child.material !== undefined ) scope.addMaterial( child.material );
  59. scope.addHelper( child );
  60. } );
  61. this.parent.children.splice( this.index, 0, this.object );
  62. this.object.parent = this.parent;
  63. this.editor.signals.objectAdded.dispatch( this.object );
  64. this.editor.signals.sceneGraphChanged.dispatch();
  65. },
  66. toJSON: function () {
  67. var output = Cmd.prototype.toJSON.call( this );
  68. output.object = this.objectJSON;
  69. output.index = this.index;
  70. output.parentUuid = this.parentUuid;
  71. return output;
  72. },
  73. fromJSON: function ( json ) {
  74. Cmd.prototype.fromJSON.call( this, json );
  75. this.parent = this.editor.objectByUuid( json.parentUuid );
  76. if ( this.parent === undefined ) {
  77. this.parent = this.editor.scene;
  78. }
  79. this.parentUuid = json.parentUuid;
  80. this.index = json.index;
  81. this.object = this.editor.objectByUuid( json.object.object.uuid );
  82. if ( this.object === undefined ) {
  83. var loader = new THREE.ObjectLoader();
  84. this.object = loader.parse( json.object );
  85. }
  86. this.objectJSON = json.object;
  87. }
  88. };