CmdRemoveObject.js 3.0 KB

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