replayRecorder.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 (!event.allowNullValue || event.allowNullValue && value !== null) {
  47. if (value.w !== undefined) { // Quaternion
  48. value = `new BABYLON.Quaternion(${value.x}, ${value.y}, ${value.z}, ${value.w})`;
  49. } else if (value.z !== undefined) { // Vector3
  50. value = `new BABYLON.Vector3(${value.x}, ${value.y}, ${value.z})`;
  51. } else if (value.y !== undefined) { // Vector2
  52. value = `new BABYLON.Vector2(${value.x}, ${value.y})`;
  53. } else if (value.a !== undefined) { // Color4
  54. value = `new BABYLON.Color4(${value.r}, ${value.g}, ${value.b}, ${value.a})`;
  55. if (event.object._isLinearColor) {
  56. value += '.toLinearSpace()';
  57. }
  58. } else if (value.b !== undefined) { // Color3
  59. value = `new BABYLON.Color3(${value.r}, ${value.g}, ${value.b})`;
  60. if (event.object._isLinearColor) {
  61. value += '.toLinearSpace()';
  62. }
  63. } else if (value.getClassName) {
  64. value = this._getIndirectData(value);
  65. }
  66. }
  67. let target = this._getIndirectData(event.object);
  68. this._recordedCodeLines.push(`${target}.${event.property} = ${value};`);
  69. this._previousObject = event.object;
  70. this._previousProperty = event.property;
  71. }
  72. public export() {
  73. 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";
  74. if (this._recordedCodeLines) {
  75. content += this._recordedCodeLines.join("\r\n");
  76. }
  77. Tools.Download(new Blob([content]), "pseudo-code.txt");
  78. }
  79. }