globalState.ts 5.7 KB

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