globalState.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { GLTFFileLoader, IGLTFLoaderExtension } from "babylonjs-loaders/glTF/index";
  2. import { IGLTFValidationResults } from "babylonjs-gltf2interface";
  3. import { Nullable } from "babylonjs/types";
  4. import { Observable, Observer } from "babylonjs/Misc/observable";
  5. import { ISceneLoaderPlugin, ISceneLoaderPluginAsync } from "babylonjs/Loading/sceneLoader";
  6. import { Scene } from "babylonjs/scene";
  7. import { Light } from "babylonjs/Lights/light";
  8. import { LightGizmo } from "babylonjs/Gizmos/lightGizmo";
  9. import { PropertyChangedEvent } from "./propertyChangedEvent";
  10. import { ReplayRecorder } from './replayRecorder';
  11. import { DataStorage } from 'babylonjs/Misc/dataStorage';
  12. import { CodeChangedEvent } from './codeChangedEvent';
  13. export class GlobalState {
  14. public onSelectionChangedObservable: Observable<any>;
  15. public onPropertyChangedObservable: Observable<PropertyChangedEvent>;
  16. public onCodeChangedObservable = new Observable<CodeChangedEvent>();
  17. public onInspectorClosedObservable = new Observable<Scene>();
  18. public onTabChangedObservable = new Observable<number>();
  19. public onSelectionRenamedObservable = new Observable<void>();
  20. public onPluginActivatedObserver: Nullable<Observer<ISceneLoaderPlugin | ISceneLoaderPluginAsync>>;
  21. public sceneImportDefaults: { [key: string]: any } = {};
  22. public validationResults: Nullable<IGLTFValidationResults> = null;
  23. public onValidationResultsUpdatedObservable = new Observable<Nullable<IGLTFValidationResults>>();
  24. public onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  25. public glTFLoaderExtensionDefaults: { [name: string]: { [key: string]: any } } = {};
  26. public glTFLoaderDefaults: { [key: string]: any } = { "validate": true };
  27. public blockMutationUpdates = false;
  28. public selectedLineContainerTitle = "";
  29. public recorder = new ReplayRecorder();
  30. private _onlyUseEulers: Nullable<boolean> = null;
  31. public get onlyUseEulers(): boolean {
  32. if (this._onlyUseEulers === null) {
  33. this._onlyUseEulers = DataStorage.ReadBoolean("settings_onlyUseEulers", true);
  34. }
  35. return this._onlyUseEulers!;
  36. }
  37. public set onlyUseEulers(value: boolean) {
  38. this._onlyUseEulers = value;
  39. DataStorage.WriteBoolean("settings_onlyUseEulers", value);
  40. }
  41. private _ignoreBackfacesForPicking: Nullable<boolean> = null;
  42. public get ignoreBackfacesForPicking(): boolean {
  43. if (this._ignoreBackfacesForPicking === null) {
  44. this._ignoreBackfacesForPicking = DataStorage.ReadBoolean("settings_ignoreBackfacesForPicking", false);
  45. }
  46. return this._ignoreBackfacesForPicking!;
  47. }
  48. public set ignoreBackfacesForPicking(value: boolean) {
  49. this._ignoreBackfacesForPicking = value;
  50. DataStorage.WriteBoolean("settings_ignoreBackfacesForPicking", value);
  51. }
  52. public init(propertyChangedObservable: Observable<PropertyChangedEvent>) {
  53. this.onPropertyChangedObservable = propertyChangedObservable;
  54. propertyChangedObservable.add(event => {
  55. this.recorder.record(event);
  56. if (event.property === "name") {
  57. this.onSelectionRenamedObservable.notifyObservers();
  58. }
  59. });
  60. this.onCodeChangedObservable.add(code => {
  61. this.recorder.recordCode(code);
  62. });
  63. }
  64. public prepareGLTFPlugin(loader: GLTFFileLoader) {
  65. var loaderState = this.glTFLoaderDefaults;
  66. if (loaderState !== undefined) {
  67. for (const key in loaderState) {
  68. (loader as any)[key] = loaderState[key];
  69. }
  70. }
  71. loader.onExtensionLoadedObservable.add((extension: IGLTFLoaderExtension) => {
  72. var extensionState = this.glTFLoaderExtensionDefaults[extension.name];
  73. if (extensionState !== undefined) {
  74. for (const key in extensionState) {
  75. (extension as any)[key] = extensionState[key];
  76. }
  77. }
  78. });
  79. if (this.validationResults) {
  80. this.validationResults = null;
  81. this.onValidationResultsUpdatedObservable.notifyObservers(null);
  82. }
  83. loader.onValidatedObservable.add((results: IGLTFValidationResults) => {
  84. this.validationResults = results;
  85. this.onValidationResultsUpdatedObservable.notifyObservers(results);
  86. if (results.issues.numErrors || results.issues.numWarnings) {
  87. this.onTabChangedObservable.notifyObservers(3);
  88. }
  89. });
  90. }
  91. // Light gizmos
  92. public lightGizmos: Array<LightGizmo> = [];
  93. public enableLightGizmo(light: Light, enable = true) {
  94. if (enable) {
  95. if (!light.reservedDataStore) {
  96. light.reservedDataStore = {}
  97. }
  98. if (!light.reservedDataStore.lightGizmo) {
  99. light.reservedDataStore.lightGizmo = new LightGizmo();
  100. this.lightGizmos.push(light.reservedDataStore.lightGizmo)
  101. light.reservedDataStore.lightGizmo.light = light;
  102. light.reservedDataStore.lightGizmo.material.reservedDataStore = {hidden: true};
  103. }
  104. } else if (light.reservedDataStore && light.reservedDataStore.lightGizmo) {
  105. this.lightGizmos.splice(this.lightGizmos.indexOf(light.reservedDataStore.lightGizmo), 1);
  106. light.reservedDataStore.lightGizmo.dispose();
  107. light.reservedDataStore.lightGizmo = null;
  108. }
  109. }
  110. }