CmdSetGeometry.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.updatable = true;
  8. this.object = object;
  9. this.objectUuid = object !== undefined ? object.uuid : undefined;
  10. this.newGeometry = newGeometry; // only needed for update(cmd)
  11. this.oldGeometryJSON = object !== undefined ? object.geometry.toJSON() : undefined;
  12. this.newGeometryJSON = newGeometry !== undefined ? newGeometry.toJSON() : undefined;
  13. };
  14. CmdSetGeometry.prototype = {
  15. execute: function () {
  16. this.object.geometry.dispose();
  17. this.object.geometry = this.parseGeometry( this.newGeometryJSON );
  18. this.object.geometry.computeBoundingSphere();
  19. this.editor.signals.geometryChanged.dispatch( this.object );
  20. this.editor.signals.sceneGraphChanged.dispatch();
  21. },
  22. undo: function () {
  23. this.object.geometry.dispose();
  24. this.object.geometry = this.parseGeometry( this.oldGeometryJSON );
  25. this.object.geometry.computeBoundingSphere();
  26. this.editor.signals.geometryChanged.dispatch( this.object );
  27. this.editor.signals.sceneGraphChanged.dispatch();
  28. },
  29. update: function ( cmd ) {
  30. this.newGeometryJSON = cmd.newGeometry.toJSON();
  31. },
  32. toJSON: function () {
  33. var output = Cmd.prototype.toJSON.call( this );
  34. output.objectUuid = this.objectUuid;
  35. output.oldGeometryJSON = this.oldGeometryJSON;
  36. output.newGeometryJSON = this.newGeometryJSON;
  37. return output;
  38. },
  39. fromJSON: function ( json ) {
  40. Cmd.prototype.fromJSON.call( this, json );
  41. this.object = this.editor.objectByUuid( json.objectUuid );
  42. this.objectUuid = json.objectUuid;
  43. this.oldGeometryJSON = json.oldGeometryJSON;
  44. this.newGeometryJSON = json.newGeometryJSON;
  45. },
  46. parseGeometry: function ( data ) {
  47. var loader = new THREE.ObjectLoader();
  48. return loader.parseGeometries( [ data ] )[ data.uuid ];
  49. }
  50. };