UNPKG

@robotlegsjs/phaser

Version:

RobotlegsJS integration with Phaser Scene Manager

135 lines 6.12 kB
"use strict"; // ------------------------------------------------------------------------------ // Copyright (c) 2017-present, RobotlegsJS. All Rights Reserved. // // NOTICE: You are permitted to use, modify, and distribute this file // in accordance with the terms of the license agreement accompanying it. // ------------------------------------------------------------------------------ Object.defineProperty(exports, "__esModule", { value: true }); exports.SceneRegistry = void 0; var tslib_1 = require("tslib"); var core_1 = require("@robotlegsjs/core"); var SceneManagerBinding_1 = require("./SceneManagerBinding"); var SceneManagerBindingEvent_1 = require("./SceneManagerBindingEvent"); var SceneRegistryEvent_1 = require("./SceneRegistryEvent"); /** * @private */ var SceneRegistry = /** @class */ (function (_super) { tslib_1.__extends(SceneRegistry, _super); function SceneRegistry() { /*============================================================================*/ /* Private Properties */ /*============================================================================*/ var _this = _super !== null && _super.apply(this, arguments) || this; _this._rootBindings = []; _this._bindings = []; _this._bindingBySceneManager = new Map(); return _this; } Object.defineProperty(SceneRegistry.prototype, "rootBindings", { /*============================================================================*/ /* Public Properties */ /*============================================================================*/ /** * @private */ get: function () { return this._rootBindings; }, enumerable: false, configurable: true }); Object.defineProperty(SceneRegistry.prototype, "bindings", { /** * @private */ get: function () { return this._bindings; }, enumerable: false, configurable: true }); /*============================================================================*/ /* Public Functions */ /*============================================================================*/ /** * @private */ SceneRegistry.prototype.addSceneManager = function (sceneManager) { var binding = this._bindingBySceneManager.get(sceneManager); if (!binding) { binding = this._createBinding(sceneManager); this._bindingBySceneManager.set(sceneManager, binding); } return binding; }; /** * @private */ SceneRegistry.prototype.removeSceneManager = function (sceneManager) { var binding = this._bindingBySceneManager.get(sceneManager); if (binding) { this._removeBinding(binding); } return binding; }; /** * Finds the closest parent binding for a given display object * * @private */ SceneRegistry.prototype.findParentBinding = function (target) { return null; }; /** * @private */ SceneRegistry.prototype.getBinding = function (sceneManager) { return this._bindingBySceneManager.get(sceneManager); }; /*============================================================================*/ /* Private Functions */ /*============================================================================*/ SceneRegistry.prototype._createBinding = function (sceneManager) { var binding = new SceneManagerBinding_1.SceneManagerBinding(sceneManager); this._bindings.push(binding); // Add a listener so that we can remove this binding when it has no handlers binding.addEventListener(SceneManagerBindingEvent_1.SceneManagerBindingEvent.BINDING_EMPTY, this._onBindingEmpty, this); // If the new binding doesn't have a parent it is a Root binding.parent = this.findParentBinding(sceneManager); if (binding.parent == null) { this._addRootBinding(binding); } this.dispatchEvent(new SceneRegistryEvent_1.SceneRegistryEvent(SceneRegistryEvent_1.SceneRegistryEvent.SCENE_MANAGER_ADD, binding.sceneManager)); return binding; }; SceneRegistry.prototype._removeBinding = function (binding) { // Remove the binding itself this._bindingBySceneManager.delete(binding.sceneManager); var index = this._bindings.indexOf(binding); this._bindings.splice(index, 1); // Drop the empty binding listener binding.removeEventListener(SceneManagerBindingEvent_1.SceneManagerBindingEvent.BINDING_EMPTY, this._onBindingEmpty, this); if (!binding.parent) { // This binding didn't have a parent, so it was a Root this._removeRootBinding(binding); } this.dispatchEvent(new SceneRegistryEvent_1.SceneRegistryEvent(SceneRegistryEvent_1.SceneRegistryEvent.SCENE_MANAGER_REMOVE, binding.sceneManager)); }; SceneRegistry.prototype._addRootBinding = function (binding) { this._rootBindings.push(binding); this.dispatchEvent(new SceneRegistryEvent_1.SceneRegistryEvent(SceneRegistryEvent_1.SceneRegistryEvent.ROOT_SCENE_MANAGER_ADD, binding.sceneManager)); }; SceneRegistry.prototype._removeRootBinding = function (binding) { var index = this._rootBindings.indexOf(binding); this._rootBindings.splice(index, 1); this.dispatchEvent(new SceneRegistryEvent_1.SceneRegistryEvent(SceneRegistryEvent_1.SceneRegistryEvent.ROOT_SCENE_MANAGER_REMOVE, binding.sceneManager)); }; SceneRegistry.prototype._onBindingEmpty = function (event) { this._removeBinding(event.target); }; return SceneRegistry; }(core_1.EventDispatcher)); exports.SceneRegistry = SceneRegistry; //# sourceMappingURL=SceneRegistry.js.map