CmdRemoveObject.js 3.0 KB

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