Selaa lähdekoodia

Removing the static manager

rickfromwork 4 vuotta sitten
vanhempi
commit
9a2542fe39

+ 2 - 70
gui/src/3D/controls/touchButton3D.ts

@@ -1,6 +1,6 @@
 // Assumptions: absolute position of button mesh is inside the mesh
 
-import { DeepImmutableObject, Nullable } from "babylonjs/types";
+import { DeepImmutableObject } from "babylonjs/types";
 import { Vector3, Quaternion } from "babylonjs/Maths/math.vector";
 import { Mesh } from "babylonjs/Meshes/mesh";
 import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
@@ -22,57 +22,6 @@ enum ButtonState {
     Press = 2
 }
 
-class TouchButton3DManager {
-    private _touchButtonList = new Map<number, TouchButton3D>();
-    private _buttonIndex = 1;
-
-    private _sceneRegisteredOn: Nullable<Scene>;
-
-    private _handleCollisions = () => {
-        if (this._sceneRegisteredOn != null) {
-            const touchMeshes = this._sceneRegisteredOn.getMeshesByTags("touchEnabled");
-
-            this._touchButtonList.forEach(function (button: TouchButton3D) {
-                touchMeshes.forEach(function (mesh: Mesh) {
-                    button._collisionCheckForStateChange(mesh);
-                });
-            });
-        }
-    }
-
-    /**
-     * Creates a new touchButton3DManager
-     */
-    constructor() {
-    }
-
-    public addButton(button: TouchButton3D): number {
-        const index = this._buttonIndex++;
-        this._touchButtonList.set(index, button);
-        return index;
-    }
-
-    public removeButton(index: number): boolean {
-        return this._touchButtonList.delete(index);
-    }
-
-    public enableCollisionHandling(scene: Scene) {
-        if (this._sceneRegisteredOn != scene) {
-            if (this._sceneRegisteredOn != null) {
-                this.disableCollisionHandling();
-            }
-
-            scene.registerBeforeRender(this._handleCollisions);
-            this._sceneRegisteredOn = scene;
-        }
-    }
-
-    public disableCollisionHandling() {
-        this._sceneRegisteredOn?.unregisterBeforeRender(this._handleCollisions);
-        this._sceneRegisteredOn = null;
-    }
-}
-
 /**
  * Class used to create a touchable button in 3D
  */
@@ -93,9 +42,6 @@ export class TouchButton3D extends Button3D {
     private _activeInteractions = new Map<number, ButtonState>();
     private _previousHeight = new Map<number, number>();
 
-    private static _buttonManager = new TouchButton3DManager();
-    private _buttonManagerIndex = 0;
-
     /**
      * Creates a new touchable button
      * @param name defines the control name
@@ -109,8 +55,6 @@ export class TouchButton3D extends Button3D {
         if (collisionMesh) {
             this.collisionMesh = collisionMesh;
         }
-
-        this._buttonManagerIndex = TouchButton3D._buttonManager.addButton(this);
     }
 
     /**
@@ -158,14 +102,6 @@ export class TouchButton3D extends Button3D {
         this._collidableInitialized = true;
     }
 
-    /**
-     * Sets the scene used on the manager to register the collision callback on
-     * @param scene the scene to use
-     */
-    public set sceneForCollisions(scene: Scene) {
-        TouchButton3D._buttonManager.enableCollisionHandling(scene);
-    }
-
     /*
      * Given a point, and two points on a line, this returns the distance between
      * the point and the closest point on the line. The closest point on the line
@@ -346,7 +282,7 @@ export class TouchButton3D extends Button3D {
 
     // Decides whether to change button state based on the planar depth of the input source
     /** @hidden */
-    public _collisionCheckForStateChange(mesh: Mesh) {
+    public _collisionCheckForStateChange(mesh: AbstractMesh) {
         if (this._collidableInitialized) {
             this._updateDistanceOffsets();
 
@@ -416,8 +352,6 @@ export class TouchButton3D extends Button3D {
     // Mesh association
     protected _createNode(scene: Scene): TransformNode {
         return super._createNode(scene);
-
-        this.sceneForCollisions = scene;
     }
 
     /**
@@ -429,7 +363,5 @@ export class TouchButton3D extends Button3D {
         if (this._collisionMesh) {
             this._collisionMesh.dispose();
         }
-
-        TouchButton3D._buttonManager.removeButton(this._buttonManagerIndex);
     }
 }

+ 0 - 1
gui/src/3D/controls/touchHolographicButton.ts

@@ -277,7 +277,6 @@ export class TouchHolographicButton extends TouchButton3D {
 
         this.collisionMesh = this._frontPlate;
         this.collidableFrontDirection = this._frontPlate.forward.negate(); // Mesh is facing the wrong way
-        this.sceneForCollisions = scene;
 
         return this._backPlate;
     }

+ 0 - 2
gui/src/3D/controls/touchMeshButton3D.ts

@@ -73,8 +73,6 @@ export class TouchMeshButton3D extends TouchButton3D {
             mesh.metadata = this;
         });
 
-        this.sceneForCollisions = scene;
-
         return this._currentMesh;
     }
 

+ 39 - 0
gui/src/3D/gui3DManager.ts

@@ -11,6 +11,7 @@ import { IDisposable, Scene } from "babylonjs/scene";
 
 import { Container3D } from "./controls/container3D";
 import { Control3D } from "./controls/control3D";
+import { TouchButton3D } from "./controls/touchButton3D";
 
 /**
  * Class used to manage 3D user interface
@@ -23,6 +24,7 @@ export class GUI3DManager implements IDisposable {
     private _rootContainer: Container3D;
     private _pointerObserver: Nullable<Observer<PointerInfo>>;
     private _pointerOutObserver: Nullable<Observer<number>>;
+    private _touchableButtons = new Set<TouchButton3D>();
     /** @hidden */
     public _lastPickedControl: Control3D;
     /** @hidden */
@@ -154,6 +156,19 @@ export class GUI3DManager implements IDisposable {
         return true;
     }
 
+    private _processTouchControls = () => {
+        let utilityLayerScene = this._utilityLayer ? this._utilityLayer.utilityLayerScene : null;
+        if (utilityLayerScene) {
+            const touchMeshes = utilityLayerScene.getMeshesByTags("touchEnabled");
+
+            this._touchableButtons.forEach(function (button: TouchButton3D) {
+                touchMeshes.forEach(function (mesh: AbstractMesh) {
+                    button._collisionCheckForStateChange(mesh);
+                });
+            });
+        }
+    }
+
     /**
      * Gets the root container
      */
@@ -177,6 +192,16 @@ export class GUI3DManager implements IDisposable {
      */
     public addControl(control: Control3D): GUI3DManager {
         this._rootContainer.addControl(control);
+
+        let utilityLayerScene = this._utilityLayer ? this._utilityLayer.utilityLayerScene : null;
+        if (utilityLayerScene && (control instanceof TouchButton3D)) {
+            if (this._touchableButtons.size == 0) {
+                utilityLayerScene.registerBeforeRender(this._processTouchControls);
+            }
+
+            this._touchableButtons.add(control as TouchButton3D);
+        }
+
         return this;
     }
 
@@ -187,6 +212,16 @@ export class GUI3DManager implements IDisposable {
      */
     public removeControl(control: Control3D): GUI3DManager {
         this._rootContainer.removeControl(control);
+
+        let utilityLayerScene = this._utilityLayer ? this._utilityLayer.utilityLayerScene : null;
+        if (utilityLayerScene && (control instanceof TouchButton3D)) {
+            this._touchableButtons.delete(control);
+
+            if (this._touchableButtons.size == 0) {
+                utilityLayerScene.unregisterBeforeRender(this._processTouchControls);
+            }
+        }
+
         return this;
     }
 
@@ -226,6 +261,10 @@ export class GUI3DManager implements IDisposable {
         let utilityLayerScene = this._utilityLayer ? this._utilityLayer.utilityLayerScene : null;
 
         if (utilityLayerScene) {
+            if (this._touchableButtons.size != 0) {
+                utilityLayerScene.unregisterBeforeRender(this._processTouchControls);
+            }
+
             if (this._pointerObserver) {
                 utilityLayerScene.onPointerObservable.remove(this._pointerObserver);
                 this._pointerObserver = null;