Browse Source

Fixed window object reference

David Catuhe 6 năm trước cách đây
mục cha
commit
bb9e727ec6

+ 2 - 2
src/Cameras/Inputs/BaseCameraPointersInput.ts

@@ -228,7 +228,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
         element.addEventListener("contextmenu",
         element.addEventListener("contextmenu",
             <EventListener>this.onContextMenu.bind(this), false);
             <EventListener>this.onContextMenu.bind(this), false);
 
 
-        Tools.RegisterTopRootEvents([
+        Tools.RegisterTopRootEvents(this.camera.getScene().getEngine().getHostWindow(), [
             { name: "blur", handler: this._onLostFocus }
             { name: "blur", handler: this._onLostFocus }
         ]);
         ]);
     }
     }
@@ -239,7 +239,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
      */
      */
     public detachControl(element: Nullable<HTMLElement>): void {
     public detachControl(element: Nullable<HTMLElement>): void {
         if (this._onLostFocus) {
         if (this._onLostFocus) {
-            Tools.UnregisterTopRootEvents([
+            Tools.UnregisterTopRootEvents(this.camera.getScene().getEngine().getHostWindow(),[
                 { name: "blur", handler: this._onLostFocus }
                 { name: "blur", handler: this._onLostFocus }
             ]);
             ]);
         }
         }

+ 4 - 1
src/Cameras/Inputs/arcRotateCameraVRDeviceOrientationInput.ts

@@ -63,7 +63,10 @@ export class ArcRotateCameraVRDeviceOrientationInput implements ICameraInput<Arc
      */
      */
     public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
     public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
         this.camera.attachControl(element, noPreventDefault);
         this.camera.attachControl(element, noPreventDefault);
-        window.addEventListener("deviceorientation", this._deviceOrientationHandler);
+
+        let hostWindow = this.camera.getScene().getEngine().getHostWindow();
+
+        hostWindow.addEventListener("deviceorientation", this._deviceOrientationHandler);
     }
     }
 
 
     /** @hidden */
     /** @hidden */

+ 6 - 2
src/Cameras/Inputs/freeCameraDeviceOrientationInput.ts

@@ -117,8 +117,12 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
      * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
      * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
      */
      */
     public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
     public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
-        window.addEventListener("orientationchange", this._orientationChanged);
-        window.addEventListener("deviceorientation", this._deviceOrientation);
+        
+        let hostWindow = this.camera.getScene().getEngine().getHostWindow();
+
+        hostWindow.addEventListener("orientationchange", this._orientationChanged);
+        hostWindow.addEventListener("deviceorientation", this._deviceOrientation);
+        
         //In certain cases, the attach control is called AFTER orientation was changed,
         //In certain cases, the attach control is called AFTER orientation was changed,
         //So this is needed.
         //So this is needed.
         this._orientationChanged();
         this._orientationChanged();

+ 5 - 2
src/Cameras/VR/vrExperienceHelper.ts

@@ -771,7 +771,10 @@ export class VRExperienceHelper {
         }
         }
 
 
         // Window events
         // Window events
-        window.addEventListener("resize", this._onResize);
+
+        let hostWindow = this._scene.getEngine().getHostWindow();
+
+        hostWindow.addEventListener("resize", this._onResize);
         document.addEventListener("fullscreenchange", this._onFullscreenChange, false);
         document.addEventListener("fullscreenchange", this._onFullscreenChange, false);
         document.addEventListener("mozfullscreenchange", this._onFullscreenChange, false);
         document.addEventListener("mozfullscreenchange", this._onFullscreenChange, false);
         document.addEventListener("webkitfullscreenchange", this._onFullscreenChange, false);
         document.addEventListener("webkitfullscreenchange", this._onFullscreenChange, false);
@@ -821,7 +824,7 @@ export class VRExperienceHelper {
         scene.getEngine().onVRDisplayChangedObservable.add(this._onVRDisplayChanged);
         scene.getEngine().onVRDisplayChangedObservable.add(this._onVRDisplayChanged);
         scene.getEngine().onVRRequestPresentStart.add(this._onVRRequestPresentStart);
         scene.getEngine().onVRRequestPresentStart.add(this._onVRRequestPresentStart);
         scene.getEngine().onVRRequestPresentComplete.add(this._onVRRequestPresentComplete);
         scene.getEngine().onVRRequestPresentComplete.add(this._onVRRequestPresentComplete);
-        window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
+        hostWindow.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
 
 
         scene.onDisposeObservable.add(() => {
         scene.onDisposeObservable.add(() => {
             this.dispose();
             this.dispose();

+ 3 - 1
src/Cameras/VR/webVRCamera.ts

@@ -508,7 +508,9 @@ export class WebVRFreeCamera extends FreeCamera implements PoseControlled {
         if (this._vrDevice) {
         if (this._vrDevice) {
             this.getEngine().enableVR();
             this.getEngine().enableVR();
         }
         }
-        window.addEventListener('vrdisplaypresentchange', this._detachIfAttached);
+        
+        let hostWindow = this._scene.getEngine().getHostWindow();
+        hostWindow.addEventListener('vrdisplaypresentchange', this._detachIfAttached);
     }
     }
 
 
     /**
     /**

+ 9 - 6
src/Engines/Extensions/engine.webVR.ts

@@ -139,9 +139,10 @@ Engine.prototype.initWebVRAsync = function(): Promise<IDisplayChangedEventArgs>
         this._onVrDisplayPresentChange = () => {
         this._onVrDisplayPresentChange = () => {
             this._vrExclusivePointerMode = this._vrDisplay && this._vrDisplay.isPresenting;
             this._vrExclusivePointerMode = this._vrDisplay && this._vrDisplay.isPresenting;
         };
         };
-        window.addEventListener('vrdisplayconnect', this._onVrDisplayConnect);
-        window.addEventListener('vrdisplaydisconnect', this._onVrDisplayDisconnect);
-        window.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
+        let hostWindow = this.getHostWindow();
+        hostWindow.addEventListener('vrdisplayconnect', this._onVrDisplayConnect);
+        hostWindow.addEventListener('vrdisplaydisconnect', this._onVrDisplayDisconnect);
+        hostWindow.addEventListener('vrdisplaypresentchange', this._onVrDisplayPresentChange);
     }
     }
     this._webVRInitPromise = this._webVRInitPromise || this._getVRDisplaysAsync();
     this._webVRInitPromise = this._webVRInitPromise || this._getVRDisplaysAsync();
     this._webVRInitPromise.then(notifyObservers);
     this._webVRInitPromise.then(notifyObservers);
@@ -243,9 +244,11 @@ Engine.prototype._connectVREvents = function(canvas: HTMLCanvasElement, document
         document.exitPointerLock();
         document.exitPointerLock();
     };
     };
 
 
-    if (DomManagement.IsWindowObjectExist()) {
-        window.addEventListener('vrdisplaypointerrestricted', this._onVRDisplayPointerRestricted, false);
-        window.addEventListener('vrdisplaypointerunrestricted', this._onVRDisplayPointerUnrestricted, false);
+    if (DomManagement.IsWindowObjectExist()) {        
+        let hostWindow = this.getHostWindow();
+        
+        hostWindow.addEventListener('vrdisplaypointerrestricted', this._onVRDisplayPointerRestricted, false);
+        hostWindow.addEventListener('vrdisplaypointerunrestricted', this._onVRDisplayPointerUnrestricted, false);
     }
     }
 };
 };
 
 

+ 25 - 2
src/Engines/engine.ts

@@ -1162,8 +1162,9 @@ export class Engine {
             };
             };
 
 
             if (DomManagement.IsWindowObjectExist()) {
             if (DomManagement.IsWindowObjectExist()) {
-                window.addEventListener("blur", this._onBlur);
-                window.addEventListener("focus", this._onFocus);
+                let hostWindow = this.getHostWindow();
+                hostWindow.addEventListener("blur", this._onBlur);
+                hostWindow.addEventListener("focus", this._onFocus);
             }
             }
 
 
             canvas.addEventListener("pointerout", this._onCanvasPointerOut);
             canvas.addEventListener("pointerout", this._onCanvasPointerOut);
@@ -1697,6 +1698,28 @@ export class Engine {
     }
     }
 
 
     /**
     /**
+     * Gets host window
+     */
+    public getHostWindow(): Window {
+        if (this._renderingCanvas && this._renderingCanvas.ownerDocument && this._renderingCanvas.ownerDocument.defaultView) {
+            return this._renderingCanvas.ownerDocument.defaultView;
+        }
+
+        return window;
+    }
+
+    /**
+     * Gets host document
+     */
+    public getHostDocument(): Document {
+        if (this._renderingCanvas && this._renderingCanvas.ownerDocument) {
+            return this._renderingCanvas.ownerDocument;
+        }
+
+        return document;
+    }
+
+    /**
      * Gets the client rect of the HTML canvas attached with the current webGL context
      * Gets the client rect of the HTML canvas attached with the current webGL context
      * @returns a client rectanglee
      * @returns a client rectanglee
      */
      */

+ 5 - 3
src/Gamepads/gamepadManager.ts

@@ -101,9 +101,11 @@ export class GamepadManager {
                 this._startMonitoringGamepads();
                 this._startMonitoringGamepads();
             }
             }
             // Checking if the gamepad connected event is supported (like in Firefox)
             // Checking if the gamepad connected event is supported (like in Firefox)
-            if (this._gamepadEventSupported) {
-                window.addEventListener('gamepadconnected', this._onGamepadConnectedEvent, false);
-                window.addEventListener('gamepaddisconnected', this._onGamepadDisconnectedEvent, false);
+            if (this._gamepadEventSupported) {                  
+                let hostWindow = this._scene!.getEngine().getHostWindow();
+
+                hostWindow.addEventListener('gamepadconnected', this._onGamepadConnectedEvent, false);
+                hostWindow.addEventListener('gamepaddisconnected', this._onGamepadDisconnectedEvent, false);
             }
             }
             else {
             else {
                 this._startMonitoringGamepads();
                 this._startMonitoringGamepads();

+ 2 - 1
src/Inputs/scene.inputManager.ts

@@ -803,7 +803,8 @@ export class InputManager {
         }
         }
 
 
         if (attachUp) {
         if (attachUp) {
-            window.addEventListener(eventPrefix + "up", <any>this._onPointerUp, false);
+            let hostWindow = scene.getEngine().getHostWindow();
+            hostWindow.addEventListener(eventPrefix + "up", <any>this._onPointerUp, false);
         }
         }
     }
     }
 
 

+ 7 - 6
src/Misc/tools.ts

@@ -555,12 +555,13 @@ export class Tools {
 
 
     /**
     /**
      * Function used to register events at window level
      * Function used to register events at window level
+     * @param windowElement defines the Window object to use
      * @param events defines the events to register
      * @param events defines the events to register
      */
      */
-    public static RegisterTopRootEvents(events: { name: string; handler: Nullable<(e: FocusEvent) => any> }[]): void {
+    public static RegisterTopRootEvents(windowElement: Window, events: { name: string; handler: Nullable<(e: FocusEvent) => any> }[]): void {
         for (var index = 0; index < events.length; index++) {
         for (var index = 0; index < events.length; index++) {
             var event = events[index];
             var event = events[index];
-            window.addEventListener(event.name, <any>event.handler, false);
+            windowElement.addEventListener(event.name, <any>event.handler, false);
 
 
             try {
             try {
                 if (window.parent) {
                 if (window.parent) {
@@ -576,14 +577,14 @@ export class Tools {
      * Function used to unregister events from window level
      * Function used to unregister events from window level
      * @param events defines the events to unregister
      * @param events defines the events to unregister
      */
      */
-    public static UnregisterTopRootEvents(events: { name: string; handler: Nullable<(e: FocusEvent) => any> }[]): void {
+    public static UnregisterTopRootEvents(windowElement: Window, events: { name: string; handler: Nullable<(e: FocusEvent) => any> }[]): void {
         for (var index = 0; index < events.length; index++) {
         for (var index = 0; index < events.length; index++) {
             var event = events[index];
             var event = events[index];
-            window.removeEventListener(event.name, <any>event.handler);
+            windowElement.removeEventListener(event.name, <any>event.handler);
 
 
             try {
             try {
-                if (window.parent) {
-                    window.parent.removeEventListener(event.name, <any>event.handler);
+                if (windowElement.parent) {
+                    windowElement.parent.removeEventListener(event.name, <any>event.handler);
                 }
                 }
             } catch (e) {
             } catch (e) {
                 // Silently fails...
                 // Silently fails...