replayRecorder.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { PropertyChangedEvent } from './propertyChangedEvent';
  2. import { Tools } from 'babylonjs/Misc/tools';
  3. export class ReplayRecorder {
  4. private _recordedCodeLines: string[];
  5. private _previousObject: any;
  6. private _previousProperty: string;
  7. public reset() {
  8. this._recordedCodeLines = [];
  9. this._previousObject = null;
  10. this._previousProperty = "";
  11. }
  12. public record(event: PropertyChangedEvent) {
  13. if (!this._recordedCodeLines) {
  14. this._recordedCodeLines = [];
  15. }
  16. if (this._previousObject === event.object && this._previousProperty === event.property) {
  17. this._recordedCodeLines.pop();
  18. }
  19. let value = event.value;
  20. if (value.w !== undefined) { // Quaternion
  21. value = `new BABYLON.Quaternion(${value.x}, ${value.y}, ${value.z}, ${value.w})`;
  22. } else if (value.z !== undefined) { // Vector3
  23. value = `new BABYLON.Vector3(${value.x}, ${value.y}, ${value.z})`;
  24. } else if (value.y !== undefined) { // Vector2
  25. value = `new BABYLON.Vector2(${value.x}, ${value.y})`;
  26. } else if (value.a !== undefined) { // Color4
  27. value = `new BABYLON.Color4(${value.r}, ${value.g}, ${value.b}, ${value.a})`;
  28. } else if (value.b !== undefined) { // Color3
  29. value = `new BABYLON.Color3(${value.r}, ${value.g}, ${value.b})`;
  30. }
  31. let target = event.object.getClassName().toLowerCase();
  32. if (event.object.id) {
  33. if (target === "Scene") {
  34. target = `scene`;
  35. } else if (target.indexOf("camera") > -1) {
  36. target = `scene.getCameraByID("${event.object.id}")`;
  37. } else if (target.indexOf("mesh") > -1) {
  38. target = `scene.getMeshByID("${event.object.id}")`;
  39. } else if (target.indexOf("light") > -1) {
  40. target = `scene.getLightByID("${event.object.id}")`;
  41. } else if (target === "transformnode") {
  42. target = `scene.getTransformNodeByID("${event.object.id}")`;
  43. } else if (target === "skeleton") {
  44. target = `scene.getSkeletonById("${event.object.id}")`;
  45. } else if (target.indexOf("material") > -1) {
  46. target = `scene.getMaterialByID("${event.object.id}")`;
  47. }
  48. }
  49. this._recordedCodeLines.push(`${target}.${event.property} = ${value};`);
  50. this._previousObject = event.object;
  51. this._previousProperty = event.property;
  52. }
  53. public export() {
  54. let content = "// Code generated by babylon.js Inspector\r\n// Please keep in mind to define the 'scene' variable before using that code\r\n\r\n";
  55. if (this._recordedCodeLines) {
  56. content += this._recordedCodeLines.join("\r\n");
  57. }
  58. Tools.Download(new Blob([content]), "pseudo-code.txt");
  59. }
  60. }