replayRecorder.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. private _getIndirectData(data: any) {
  13. if (!data.getClassName) {
  14. return data;
  15. }
  16. let indirectData = data.getClassName().toLowerCase();
  17. if (data.id) {
  18. if (indirectData === "Scene") {
  19. indirectData = `scene`;
  20. } else if (indirectData.indexOf("camera") > -1) {
  21. indirectData = `scene.getCameraByID("${data.id}")`;
  22. } else if (indirectData.indexOf("mesh") > -1) {
  23. indirectData = `scene.getMeshByID("${data.id}")`;
  24. } else if (indirectData.indexOf("light") > -1) {
  25. indirectData = `scene.getLightByID("${data.id}")`;
  26. } else if (indirectData === "transformnode") {
  27. indirectData = `scene.getTransformNodeByID("${data.id}")`;
  28. } else if (indirectData === "skeleton") {
  29. indirectData = `scene.getSkeletonById("${data.id}")`;
  30. } else if (indirectData.indexOf("material") > -1) {
  31. indirectData = `scene.getMaterialByID("${data.id}")`;
  32. }
  33. } else {
  34. indirectData = "new BABYLON." + data.getClassName() + "()";
  35. }
  36. return indirectData;
  37. }
  38. public record(event: PropertyChangedEvent) {
  39. if (!this._recordedCodeLines) {
  40. this._recordedCodeLines = [];
  41. }
  42. if (this._previousObject === event.object && this._previousProperty === event.property) {
  43. this._recordedCodeLines.pop();
  44. }
  45. let value = event.value;
  46. if (value.w !== undefined) { // Quaternion
  47. value = `new BABYLON.Quaternion(${value.x}, ${value.y}, ${value.z}, ${value.w})`;
  48. } else if (value.z !== undefined) { // Vector3
  49. value = `new BABYLON.Vector3(${value.x}, ${value.y}, ${value.z})`;
  50. } else if (value.y !== undefined) { // Vector2
  51. value = `new BABYLON.Vector2(${value.x}, ${value.y})`;
  52. } else if (value.a !== undefined) { // Color4
  53. value = `new BABYLON.Color4(${value.r}, ${value.g}, ${value.b}, ${value.a})`;
  54. } else if (value.b !== undefined) { // Color3
  55. value = `new BABYLON.Color3(${value.r}, ${value.g}, ${value.b})`;
  56. } else if (value.getClassName) {
  57. value = this._getIndirectData(value);
  58. }
  59. let target = this._getIndirectData(event.object);
  60. this._recordedCodeLines.push(`${target}.${event.property} = ${value};`);
  61. this._previousObject = event.object;
  62. this._previousProperty = event.property;
  63. }
  64. public export() {
  65. 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";
  66. if (this._recordedCodeLines) {
  67. content += this._recordedCodeLines.join("\r\n");
  68. }
  69. Tools.Download(new Blob([content]), "pseudo-code.txt");
  70. }
  71. }