From 413f0ae9b94d83db03a9c1f4315520dd3cdb6def Mon Sep 17 00:00:00 2001 From: Vaios Kalpias-Ilias Date: Thu, 4 Jun 2015 12:22:03 +0300 Subject: [PATCH] Added pc.Application#destroy --- src/core/core_fullscreen.js | 10 +-- src/framework/components/component_system.js | 10 +++ src/framework/framework_application.js | 68 ++++++++++++++++++++ src/resources/resources_loader.js | 11 ++++ 4 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/core/core_fullscreen.js b/src/core/core_fullscreen.js index 29542862c83..b4cfabd9376 100644 --- a/src/core/core_fullscreen.js +++ b/src/core/core_fullscreen.js @@ -34,16 +34,16 @@ Element.prototype.requestFullscreen = Element.prototype.requestFullscreen || Element.prototype.webkitRequestFullscreen || Element.prototype.msRequestFullscreen || - function () {}; + function () {}; } document.exitFullscreen = document.exitFullscreen || document.webkitExitFullscreen || document.mozCancelFullScreen || document.msExitFullscreen; - if (!document.fullscreenElement) { + if (!document.hasOwnProperty('fullscreenElement')) { Object.defineProperty(document, 'fullscreenElement', { - enumerable: true, - configurable: false, + enumerable: true, + configurable: false, get: function () { return document.webkitCurrentFullScreenElement || document.webkitFullscreenElement || @@ -53,7 +53,7 @@ }); } - if (!document.fullscreenEnabled) { + if (!document.hasOwnProperty('fullscreenEnabled')) { Object.defineProperty(document, 'fullscreenEnabled', { enumerable: true, configurable: false, diff --git a/src/framework/components/component_system.js b/src/framework/components/component_system.js index 94db573c3bf..7ddee6676ee 100644 --- a/src/framework/components/component_system.js +++ b/src/framework/components/component_system.js @@ -152,11 +152,21 @@ pc.extend(pc, function () { component.onEnable(); } } + }; // Add event support pc.events.attach(ComponentSystem); + ComponentSystem.destroy = function () { + ComponentSystem.off('initialize'); + ComponentSystem.off('postInitialize'); + ComponentSystem.off('toolsUpdate'); + ComponentSystem.off('update'); + ComponentSystem.off('fixedUpdate'); + ComponentSystem.off('postUpdate'); + }; + return { ComponentSystem: ComponentSystem }; diff --git a/src/framework/framework_application.js b/src/framework/framework_application.js index 002d4d5cdbb..367a2cea6e6 100644 --- a/src/framework/framework_application.js +++ b/src/framework/framework_application.js @@ -508,6 +508,10 @@ pc.extend(pc, function () { * the next tick. Override this if you have a custom Application. */ tick: function () { + if (!this.graphicsDevice) { + return; + } + Application._currentApplication = this; // Submit a request to queue up a new animation frame immediately @@ -776,6 +780,70 @@ pc.extend(pc, function () { } else { self.scene.setSkybox(null); } + }, + + /** + * @function + * @name pc.Application#destroy + * @description Destroys application and removes all event listeners + */ + destroy: function () { + Application._applications[this.graphicsDevice.canvas.id] = null; + + this.off('librariesloaded'); + document.removeEventListener('visibilitychange'); + document.removeEventListener('mozvisibilitychange'); + document.removeEventListener('msvisibilitychange'); + document.removeEventListener('webkitvisibilitychange'); + + if (this.mouse) { + this.mouse.off('mouseup'); + this.mouse.off('mousedown'); + this.mouse.off('mousewheel'); + this.mouse.off('mousemove'); + + this.mouse = null; + } + + if (this.keyboard) { + this.keyboard.off("keydown"); + this.keyboard.off("keyup"); + this.keyboard.off("keypress"); + + this.keyboard = null; + } + + if (this.touch) { + this.touch.off('touchstart'); + this.touch.off('touchend'); + this.touch.off('touchmove'); + this.touch.off('touchcancel'); + + this.touch = null; + } + + if (this.controller) { + this.controller = null; + } + + this.root.destroy(); + + pc.ComponentSystem.destroy(); + + this.loader.destroy(); + this.loader = null; + + this.scene = null; + + this.systems = []; + this.context = null; + + this.graphicsDevice = null; + + this.renderer = null; + this._audioManager = null; + + pc.net.http = new pc.net.Http(); } }; diff --git a/src/resources/resources_loader.js b/src/resources/resources_loader.js index 6f45d17dd6f..53484137d91 100644 --- a/src/resources/resources_loader.js +++ b/src/resources/resources_loader.js @@ -117,6 +117,17 @@ pc.extend(pc, function () { if (this._cache[url + type]) { return this._cache[url + type]; } + }, + + /** + * @function + * @name pc.ResourceLoader#destroy + * @description Destroys resource loader + */ + destroy: function () { + this._handlers = {}; + this._requests = {}; + this._cache = {}; } };