CmdRemoveObject.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. init: function () {
  46. if ( this.parent === undefined ) {
  47. this.parent = this.editor.objectByUuid( this.parentUuid );
  48. }
  49. if ( this.parent === undefined ) {
  50. this.parent = this.editor.scene;
  51. }
  52. if ( this.object === undefined ) {
  53. this.object = this.editor.objectByUuid( this.objectJSON.object.uuid );
  54. }
  55. if ( this.object === undefined ) {
  56. var loader = new THREE.ObjectLoader();
  57. this.object = loader.parse( this.objectJSON );
  58. }
  59. },
  60. execute: function () {
  61. this.init();
  62. this.index = this.parent.children.indexOf( this.object );
  63. var scope = this.editor;
  64. this.object.traverse( function ( child ) {
  65. scope.removeHelper( child );
  66. } );
  67. this.parent.remove( this.object );
  68. this.editor.signals.objectRemoved.dispatch( this.object );
  69. this.editor.signals.sceneGraphChanged.dispatch();
  70. },
  71. undo: function () {
  72. this.init();
  73. var scope = this.editor;
  74. this.object.traverse( function ( child ) {
  75. if ( child.geometry !== undefined ) scope.addGeometry( child.geometry );
  76. if ( child.material !== undefined ) scope.addMaterial( child.material );
  77. scope.addHelper( child );
  78. } );
  79. this.parent.children.splice( this.index, 0, this.object );
  80. this.object.parent = this.parent;
  81. this.editor.signals.objectAdded.dispatch( this.object );
  82. this.editor.signals.sceneGraphChanged.dispatch();
  83. },
  84. toJSON: function () {
  85. var output = Cmd.prototype.toJSON.call( this );
  86. output.object = this.objectJSON;
  87. output.index = this.index;
  88. output.parentUuid = this.parentUuid;
  89. return output;
  90. },
  91. fromJSON: function ( json ) {
  92. Cmd.prototype.fromJSON.call( this, json );
  93. this.objectJSON = json.object;
  94. this.index = json.index;
  95. this.parentUuid = json.parentUuid;
  96. }
  97. };
粤ICP备19079148号