CmdRemoveObject.js 2.9 KB

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