CmdRemoveObject.js 2.8 KB

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