CmdSetGeometry.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Created by Daniel on 21.07.15.
  3. */
  4. CmdSetGeometry = function ( object, newGeometry ) {
  5. Cmd.call( this );
  6. this.type = 'CmdSetGeometry';
  7. this.name = 'Set Geometry';
  8. this.updatable = true;
  9. this.object = object;
  10. this.objectUuid = ( object !== undefined ) ? object.uuid : undefined;
  11. this.oldGeometry = ( object !== undefined ) ? object.geometry : undefined;
  12. this.newGeometry = newGeometry;
  13. this.oldGeometryJSON = ( object !== undefined ) ? object.geometry.toJSON() : undefined;
  14. this.newGeometryJSON = ( newGeometry !== undefined ) ? newGeometry.toJSON() : undefined;
  15. };
  16. CmdSetGeometry.prototype = {
  17. init: function () {
  18. if ( this.object === undefined ) {
  19. this.object = this.editor.objectByUuid( this.objectUuid );
  20. }
  21. if ( this.oldGeometry === undefined ) {
  22. this.oldGeometry = parseGeometry( this.oldGeometryJSON );
  23. }
  24. if ( this.newGeometry === undefined ) {
  25. this.newGeometry = parseGeometry( this.newGeometryJSON );
  26. }
  27. function parseGeometry ( data ) {
  28. var loader = new THREE.ObjectLoader();
  29. return loader.parseGeometries( [ data ] )[ data.uuid ];
  30. }
  31. },
  32. execute: function () {
  33. this.init();
  34. this.object.geometry.dispose();
  35. this.object.geometry = this.newGeometry;
  36. this.object.geometry.computeBoundingSphere();
  37. this.editor.signals.geometryChanged.dispatch( this.object );
  38. this.editor.signals.sceneGraphChanged.dispatch();
  39. },
  40. undo: function () {
  41. this.init();
  42. this.object.geometry.dispose();
  43. this.object.geometry = this.oldGeometry;
  44. this.object.geometry.computeBoundingSphere();
  45. this.editor.signals.geometryChanged.dispatch( this.object );
  46. this.editor.signals.sceneGraphChanged.dispatch();
  47. },
  48. update: function ( cmd ) {
  49. this.newGeometry = cmd.newGeometry;
  50. this.newGeometryJSON = cmd.newGeometry.toJSON();
  51. },
  52. toJSON: function () {
  53. var output = Cmd.prototype.toJSON.call( this );
  54. output.objectUuid = this.objectUuid;
  55. output.oldGeometry = this.oldGeometryJSON;
  56. output.newGeometry = this.newGeometryJSON;
  57. return output;
  58. },
  59. fromJSON: function ( json ) {
  60. Cmd.prototype.fromJSON.call( this, json );
  61. this.objectUuid = json.objectUuid;
  62. this.oldGeometryJSON = json.oldGeometry;
  63. this.newGeometryJSON = json.newGeometry;
  64. }
  65. };