replayRecorder.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { PropertyChangedEvent } from './propertyChangedEvent';
  2. import { Tools } from 'babylonjs/Misc/tools';
  3. import { CodeChangedEvent } from './codeChangedEvent';
  4. export class ReplayRecorder {
  5. private _recordedCodeLines: string[];
  6. private _previousObject: any;
  7. private _previousProperty: string;
  8. public reset() {
  9. this._recordedCodeLines = [];
  10. this._previousObject = null;
  11. this._previousProperty = "";
  12. }
  13. private _getIndirectData(data: any) {
  14. if (!data.getClassName) {
  15. return data;
  16. }
  17. let indirectData = data.getClassName().toLowerCase();
  18. if (data.id) {
  19. if (indirectData === "Scene") {
  20. indirectData = `scene`;
  21. } else if (indirectData.indexOf("camera") > -1) {
  22. indirectData = `scene.getCameraByID("${data.id}")`;
  23. } else if (indirectData.indexOf("mesh") > -1) {
  24. indirectData = `scene.getMeshByID("${data.id}")`;
  25. } else if (indirectData.indexOf("light") > -1) {
  26. indirectData = `scene.getLightByID("${data.id}")`;
  27. } else if (indirectData === "transformnode") {
  28. indirectData = `scene.getTransformNodeByID("${data.id}")`;
  29. } else if (indirectData === "skeleton") {
  30. indirectData = `scene.getSkeletonById("${data.id}")`;
  31. } else if (indirectData.indexOf("material") > -1) {
  32. indirectData = `scene.getMaterialByID("${data.id}")`;
  33. }else if (indirectData.indexOf("particle") > -1) {
  34. indirectData = `scene.getParticleSystemById("${data.id}")`;
  35. }
  36. } else {
  37. indirectData = "new BABYLON." + data.getClassName() + "()";
  38. }
  39. return indirectData;
  40. }
  41. public recordCode(event: CodeChangedEvent) {
  42. if (!this._recordedCodeLines) {
  43. this._recordedCodeLines = [];
  44. }
  45. let target = this._getIndirectData(event.object);
  46. this._recordedCodeLines.push(event.code.replace(/TARGET/g, target));
  47. }
  48. public record(event: PropertyChangedEvent) {
  49. if (!this._recordedCodeLines) {
  50. this._recordedCodeLines = [];
  51. }
  52. if (this._previousObject === event.object && this._previousProperty === event.property) {
  53. this._recordedCodeLines.pop();
  54. }
  55. let value = event.value;
  56. if (!event.allowNullValue || event.allowNullValue && value !== null) {
  57. if (value.w !== undefined) { // Quaternion
  58. value = `new BABYLON.Quaternion(${value.x}, ${value.y}, ${value.z}, ${value.w})`;
  59. } else if (value.z !== undefined) { // Vector3
  60. value = `new BABYLON.Vector3(${value.x}, ${value.y}, ${value.z})`;
  61. } else if (value.y !== undefined) { // Vector2
  62. value = `new BABYLON.Vector2(${value.x}, ${value.y})`;
  63. } else if (value.a !== undefined) { // Color4
  64. value = `new BABYLON.Color4(${value.r}, ${value.g}, ${value.b}, ${value.a})`;
  65. if (event.object._isLinearColor) {
  66. value += '.toLinearSpace()';
  67. }
  68. } else if (value.b !== undefined) { // Color3
  69. value = `new BABYLON.Color3(${value.r}, ${value.g}, ${value.b})`;
  70. if (event.object._isLinearColor) {
  71. value += '.toLinearSpace()';
  72. }
  73. } else if (value.getClassName) {
  74. value = this._getIndirectData(value);
  75. }
  76. }
  77. let target = this._getIndirectData(event.object);
  78. this._recordedCodeLines.push(`${target}.${event.property} = ${value};`);
  79. this._previousObject = event.object;
  80. this._previousProperty = event.property;
  81. }
  82. public export() {
  83. 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";
  84. if (this._recordedCodeLines) {
  85. content += this._recordedCodeLines.join("\r\n");
  86. }
  87. Tools.Download(new Blob([content]), "pseudo-code.txt");
  88. }
  89. }