CmdAddObject.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Created by Daniel on 20.07.15.
  3. */
  4. CmdAddObject = function ( object ) {
  5. Cmd.call( this );
  6. this.type = 'CmdAddObject';
  7. this.object = object;
  8. if ( object !== undefined ) {
  9. this.name = 'Add Object: ' + object.name;
  10. object.updateMatrixWorld( true );
  11. meta = {
  12. geometries: {},
  13. materials: {},
  14. textures: {},
  15. images: {}
  16. };
  17. var json = object.toJSON( meta );
  18. var geometries = extractFromCache( meta.geometries );
  19. var materials = extractFromCache( meta.materials );
  20. var textures = extractFromCache( meta.textures );
  21. var images = extractFromCache( meta.images );
  22. if ( geometries.length > 0 ) json.geometries = geometries;
  23. if ( materials.length > 0 ) json.materials = materials;
  24. if ( textures.length > 0 ) json.textures = textures;
  25. if ( images.length > 0 ) json.images = images;
  26. this.objectJSON = json;
  27. }
  28. // Note: The function 'extractFromCache' is copied from Object3D.toJSON()
  29. // extract data from the cache hash
  30. // remove metadata on each item
  31. // and return as array
  32. function extractFromCache ( cache ) {
  33. var values = [];
  34. for ( var key in cache ) {
  35. var data = cache[ key ];
  36. delete data.metadata;
  37. values.push( data );
  38. }
  39. return values;
  40. }
  41. };
  42. CmdAddObject.prototype = {
  43. init: function () {
  44. if ( this.object === undefined ) {
  45. this.object = this.editor.objectByUuid( this.objectJSON.object.uuid );
  46. }
  47. if ( this.object === undefined ) {
  48. var loader = new THREE.ObjectLoader();
  49. this.object = loader.parse( this.objectJSON );
  50. }
  51. },
  52. execute: function () {
  53. this.init();
  54. this.editor.addObject( this.object );
  55. },
  56. undo: function () {
  57. this.init();
  58. this.editor.removeObject( this.object );
  59. this.editor.deselect();
  60. },
  61. toJSON: function () {
  62. var output = Cmd.prototype.toJSON.call( this );
  63. output.object = this.objectJSON;
  64. return output;
  65. },
  66. fromJSON: function ( json ) {
  67. Cmd.prototype.fromJSON.call( this, json );
  68. this.objectJSON = json.object;
  69. }
  70. };