CmdRemoveObject.js 2.8 KB

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