UNPKG

laya-coreui-es

Version:

laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改

296 lines (295 loc) 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Scene = void 0; const Node_1 = require("./Node"); const Const_1 = require("../Const"); const Sprite_1 = require("./Sprite"); const Event_1 = require("../events/Event"); const SceneLoader_1 = require("../net/SceneLoader"); const Resource_1 = require("../resource/Resource"); const Handler_1 = require("../utils/Handler"); const SceneUtils_1 = require("../utils/SceneUtils"); const ILaya_1 = require("../../ILaya"); const URL_1 = require("../net/URL"); class Scene extends Sprite_1.Sprite { constructor(createChildren = true) { super(); this.autoDestroyAtClosed = false; this.url = null; this._viewCreated = false; this._idMap = null; this._$componentType = "Scene"; Scene.unDestroyedScenes.push(this); this._scene = this; if (createChildren) this.createChildren(); } createChildren() { } static setUIMap(url) { let uimap = ILaya_1.ILaya.loader.getRes(url); if (uimap) { for (let key in uimap) { ILaya_1.ILaya.Loader.loadedMap[URL_1.URL.formatURL(key + ".scene")] = uimap[key]; } } else { throw "请提前加载uimap的json,再使用该接口设置!"; } } loadScene(path) { var url = path.indexOf(".") > -1 ? path : path + ".scene"; var view = ILaya_1.ILaya.loader.getRes(url); if (view) { this.createView(view); } else { this._setBit(Const_1.Const.NOT_READY, true); ILaya_1.ILaya.loader.resetProgress(); var loader = new SceneLoader_1.SceneLoader(); loader.on(Event_1.Event.COMPLETE, this, this._onSceneLoaded, [url]); loader.load(url); } } _onSceneLoaded(url) { this.createView(ILaya_1.ILaya.Loader.getRes(url)); } createView(view) { if (view && !this._viewCreated) { this._viewCreated = true; SceneUtils_1.SceneUtils.createByData(this, view); } } getNodeByID(id) { if (this._idMap) return this._idMap[id]; return null; } open(closeOther = true, param = null) { if (closeOther) Scene.closeAll(); Scene.root.addChild(this); this.onOpened(param); } onOpened(param) { } close(type = null) { this.onClosed(type); if (this.autoDestroyAtClosed) this.destroy(); else this.removeSelf(); } onClosed(type = null) { } destroy(destroyChild = true) { this._idMap = null; super.destroy(destroyChild); var list = Scene.unDestroyedScenes; for (var i = list.length - 1; i > -1; i--) { if (list[i] === this) { list.splice(i, 1); return; } } } set scaleX(value) { if (super.get_scaleX() == value) return; super.set_scaleX(value); this.event(Event_1.Event.RESIZE); } get scaleX() { return super.scaleX; } set scaleY(value) { if (super.get_scaleY() == value) return; super.set_scaleY(value); this.event(Event_1.Event.RESIZE); } get scaleY() { return super.scaleY; } get width() { if (this._width) return this._width; var max = 0; for (var i = this.numChildren - 1; i > -1; i--) { var comp = this.getChildAt(i); if (comp._visible) { max = Math.max(comp._x + comp.width * comp.scaleX, max); } } return max; } set width(value) { if (super.get_width() == value) return; super.set_width(value); this.callLater(this._sizeChanged); } get height() { if (this._height) return this._height; var max = 0; for (var i = this.numChildren - 1; i > -1; i--) { var comp = this.getChildAt(i); if (comp._visible) { max = Math.max(comp._y + comp.height * comp.scaleY, max); } } return max; } set height(value) { if (super.get_height() == value) return; super.set_height(value); this.callLater(this._sizeChanged); } _sizeChanged() { this.event(Event_1.Event.RESIZE); } static get root() { if (!Scene._root) { Scene._root = ILaya_1.ILaya.stage.addChild(new Sprite_1.Sprite()); Scene._root.name = "root"; ILaya_1.ILaya.stage.on("resize", null, () => { Scene._root.size(ILaya_1.ILaya.stage.width, ILaya_1.ILaya.stage.height); Scene._root.event(Event_1.Event.RESIZE); }); Scene._root.size(ILaya_1.ILaya.stage.width, ILaya_1.ILaya.stage.height); Scene._root.event(Event_1.Event.RESIZE); } return Scene._root; } get timer() { return this._timer || ILaya_1.ILaya.timer; } set timer(value) { this._timer = value; } static load(url, complete = null, progress = null) { ILaya_1.ILaya.loader.resetProgress(); var loader = new SceneLoader_1.SceneLoader(); loader.on(Event_1.Event.PROGRESS, null, onProgress); loader.once(Event_1.Event.COMPLETE, null, create); loader.load(url); function onProgress(value) { if (Scene._loadPage) Scene._loadPage.event("progress", value); progress && progress.runWith(value); } function create() { loader.off(Event_1.Event.PROGRESS, null, onProgress); var obj = ILaya_1.ILaya.Loader.getRes(url); if (!obj) throw "Can not find scene:" + url; if (!obj.props) throw "Scene data is error:" + url; var runtime = obj.props.runtime ? obj.props.runtime : obj.type; var clas = ILaya_1.ILaya.ClassUtils.getClass(runtime); if (obj.props.renderType == "instance") { var scene = clas.instance || (clas.instance = new clas()); } else { scene = new clas(); } if (scene && scene instanceof Node_1.Node) { scene.url = url; if (scene._viewCreated) { complete && complete.runWith(scene); } else { scene.on("onViewCreated", null, function () { complete && complete.runWith(scene); }); scene.createView(obj); } Scene.hideLoadingPage(); } else { throw "Can not find scene:" + runtime; } } } static open(url, closeOther = true, param = null, complete = null, progress = null) { if (param instanceof Handler_1.Handler) { var temp = complete; complete = param; param = temp; } Scene.showLoadingPage(); Scene.load(url, Handler_1.Handler.create(null, this._onSceneLoaded, [closeOther, complete, param]), progress); } static _onSceneLoaded(closeOther, complete, param, scene) { scene.open(closeOther, param); if (complete) complete.runWith(scene); } static close(url, name = "") { var flag = false; var list = Scene.unDestroyedScenes; for (var i = 0, n = list.length; i < n; i++) { var scene = list[i]; if (scene && scene.parent && scene.url === url && scene.name == name) { scene.close(); flag = true; } } return flag; } static closeAll() { var root = Scene.root; for (var i = 0, n = root.numChildren; i < n; i++) { var scene = root.getChildAt(0); if (scene instanceof Scene) scene.close(); else scene.removeSelf(); } } static destroy(url, name = "") { var flag = false; var list = [].concat(Scene.unDestroyedScenes); for (var i = 0, n = list.length; i < n; i++) { var scene = list[i]; if (scene.url === url && scene.name == name && !scene.destroyed) { scene.destroy(); flag = true; } } return flag; } static gc() { Resource_1.Resource.destroyUnusedResources(); } static setLoadingPage(loadPage) { if (Scene._loadPage != loadPage) { Scene._loadPage = loadPage; } } static showLoadingPage(param = null, delay = 500) { if (Scene._loadPage) { ILaya_1.ILaya.systemTimer.clear(null, Scene._showLoading); ILaya_1.ILaya.systemTimer.clear(null, Scene._hideLoading); ILaya_1.ILaya.systemTimer.once(delay, null, Scene._showLoading, [param], false); } } static _showLoading(param) { ILaya_1.ILaya.stage.addChild(Scene._loadPage); Scene._loadPage.onOpened(param); } static _hideLoading() { Scene._loadPage.close(); } static hideLoadingPage(delay = 500) { if (Scene._loadPage) { ILaya_1.ILaya.systemTimer.clear(null, Scene._showLoading); ILaya_1.ILaya.systemTimer.clear(null, Scene._hideLoading); ILaya_1.ILaya.systemTimer.once(delay, null, Scene._hideLoading); } } } exports.Scene = Scene; Scene.unDestroyedScenes = [];