UNPKG

@awayjs/stage

Version:
149 lines (148 loc) 6.08 kB
import { __extends } from "tslib"; import { EventDispatcher, ArgumentError } from '@awayjs/core'; import { ContextMode } from '../base/ContextMode'; import { ContextGLProfile } from '../base/ContextGLProfile'; import { StageEvent } from '../events/StageEvent'; import { Stage } from '../Stage'; /** * The StageManager class provides a multiton object that handles management for Stage objects. * * @see away.base.Stage */ var StageManager = /** @class */ (function (_super) { __extends(StageManager, _super); /** * Creates a new StageManager class. * @param stage The Stage object that contains the Stage objects to be managed. * @private */ function StageManager() { var _this = _super.call(this) || this; _this._stages = new Array(StageManager.STAGE_MAX_QUANTITY); _this._onContextCreatedDelegate = function (event) { return _this.onContextCreated(event); }; return _this; } /** * Gets a StageManager instance for the given Stage object. * @param stage The Stage object that contains the Stage objects to be managed. * @return The StageManager instance for the given Stage object. */ StageManager.getInstance = function () { if (this._instance == null) this._instance = new StageManager(); return this._instance; }; /** * Requests the Stage for the given index. * * @param index The index of the requested Stage. * @param forceSoftware Whether to force software mode even if hardware acceleration is available. * @param profile The compatibility profile, an enumeration of ContextProfile * @return The Stage for the given index. */ StageManager.prototype.getStageAt = function (index, forceSoftware, profile, mode, alpha) { if (forceSoftware === void 0) { forceSoftware = false; } if (profile === void 0) { profile = ContextGLProfile.BASELINE; } if (mode === void 0) { mode = ContextMode.AUTO; } if (alpha === void 0) { alpha = false; } if (index < 0 || index >= StageManager.STAGE_MAX_QUANTITY) throw new ArgumentError('Index is out of bounds [0..' + StageManager.STAGE_MAX_QUANTITY + ']'); if (!this._stages[index]) { StageManager._numStages++; if (document && StageManager.htmlCanvas == null) { var canvas = document.createElement('canvas'); canvas.id = 'stage' + index; document.body.appendChild(canvas); } else if (StageManager.htmlCanvas) { var canvas = StageManager.htmlCanvas; } var stage = this._stages[index] = new Stage(canvas, index, this, forceSoftware, profile); stage.addEventListener(StageEvent.CONTEXT_CREATED, this._onContextCreatedDelegate); stage.requestContext(forceSoftware, profile, mode, alpha); } return this._stages[index]; }; /** * Removes a Stage from the manager. * @param stage * @private */ StageManager.prototype.iRemoveStage = function (stage) { StageManager._numStages--; stage.removeEventListener(StageEvent.CONTEXT_CREATED, this._onContextCreatedDelegate); this._stages[stage.stageIndex] = null; }; /** * Get the next available stage. An error is thrown if there are no StageProxies available * @param forceSoftware Whether to force software mode even if hardware acceleration is available. * @param profile The compatibility profile, an enumeration of ContextProfile * @return The allocated stage */ StageManager.prototype.getFreeStage = function (forceSoftware, profile, mode, alpha) { if (forceSoftware === void 0) { forceSoftware = false; } if (profile === void 0) { profile = ContextGLProfile.BASELINE; } if (mode === void 0) { mode = ContextMode.AUTO; } if (alpha === void 0) { alpha = false; } var i = 0; var len = this._stages.length; while (i < len) { if (!this._stages[i]) return this.getStageAt(i, forceSoftware, profile, mode, alpha); ++i; } return null; }; Object.defineProperty(StageManager.prototype, "hasFreeStage", { /** * Checks if a new stage can be created and managed by the class. * @return true if there is one slot free for a new stage */ get: function () { return StageManager._numStages < StageManager.STAGE_MAX_QUANTITY ? true : false; }, enumerable: false, configurable: true }); Object.defineProperty(StageManager.prototype, "numSlotsFree", { /** * Returns the amount of stage objects that can be created and managed by the class * @return the amount of free slots */ get: function () { return StageManager.STAGE_MAX_QUANTITY - StageManager._numStages; }, enumerable: false, configurable: true }); Object.defineProperty(StageManager.prototype, "numSlotsUsed", { /** * Returns the amount of Stage objects currently managed by the class. * @return the amount of slots used */ get: function () { return StageManager._numStages; }, enumerable: false, configurable: true }); Object.defineProperty(StageManager.prototype, "numSlotsTotal", { /** * The maximum amount of Stage objects that can be managed by the class */ get: function () { return this._stages.length; }, enumerable: false, configurable: true }); StageManager.prototype.onContextCreated = function (event) { //var stage:Stage = <Stage> e.target; //document.body.appendChild(stage.canvas) }; StageManager.STAGE_MAX_QUANTITY = 8; StageManager.htmlCanvas = null; StageManager._numStages = 0; return StageManager; }(EventDispatcher)); export { StageManager };