UNPKG

laya-coreui-es

Version:

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

271 lines (270 loc) 8.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Clip = void 0; const UIComponent_1 = require("./UIComponent"); const AutoBitmap_1 = require("./AutoBitmap"); const UIUtils_1 = require("./UIUtils"); const Styles_1 = require("./Styles"); const Const_1 = require("../Const"); const Event_1 = require("../events/Event"); const Loader_1 = require("../net/Loader"); const Texture_1 = require("../resource/Texture"); const Handler_1 = require("../utils/Handler"); const Utils_1 = require("../utils/Utils"); const WeakObject_1 = require("../utils/WeakObject"); const ILaya_1 = require("../../ILaya"); class Clip extends UIComponent_1.UIComponent { constructor(url = null, clipX = 1, clipY = 1) { super(); this._clipX = 1; this._clipY = 1; this._clipWidth = 0; this._clipHeight = 0; this._interval = 50; this._index = 0; this._toIndex = -1; this._clipX = clipX; this._clipY = clipY; this.skin = url; } destroy(destroyChild = true) { super.destroy(true); this._bitmap && this._bitmap.destroy(); this._bitmap = null; this._sources = null; } dispose() { this.destroy(true); ILaya_1.ILaya.loader.clearRes(this._skin); } createChildren() { this.graphics = this._bitmap = new AutoBitmap_1.AutoBitmap(); } _onDisplay(e) { if (this._isPlaying) { if (this._getBit(Const_1.Const.DISPLAYED_INSTAGE)) this.play(); else this.stop(); } else if (this._autoPlay) { this.play(); } } get skin() { return this._skin; } set skin(value) { if (this._skin != value) { this._skin = value; if (value) { if (!Loader_1.Loader.getRes(value)) { ILaya_1.ILaya.loader.load(this._skin, Handler_1.Handler.create(this, this._skinLoaded), null, Loader_1.Loader.IMAGE, 1); } else { this._skinLoaded(); } } else { this._bitmap.source = null; } } } _skinLoaded() { this._setClipChanged(); this._sizeChanged(); this.event(Event_1.Event.LOADED); } get clipX() { return this._clipX; } set clipX(value) { this._clipX = value || 1; this._setClipChanged(); } get clipY() { return this._clipY; } set clipY(value) { this._clipY = value || 1; this._setClipChanged(); } get clipWidth() { return this._clipWidth; } set clipWidth(value) { this._clipWidth = value; this._setClipChanged(); } get clipHeight() { return this._clipHeight; } set clipHeight(value) { this._clipHeight = value; this._setClipChanged(); } changeClip() { this._clipChanged = false; if (!this._skin || this.destroyed) return; var img = Loader_1.Loader.getRes(this._skin); if (img) { this.loadComplete(this._skin, img); } else { ILaya_1.ILaya.loader.load(this._skin, Handler_1.Handler.create(this, this.loadComplete, [this._skin])); } } loadComplete(url, img) { if (url === this._skin && img) { var w = this._clipWidth || Math.ceil(img.sourceWidth / this._clipX); var h = this._clipHeight || Math.ceil(img.sourceHeight / this._clipY); var key = this._skin + w + h; var clips = WeakObject_1.WeakObject.I.get(key); if (!Utils_1.Utils.isOkTextureList(clips)) { clips = null; } if (clips) this._sources = clips; else { this._sources = []; for (var i = 0; i < this._clipY; i++) { for (var j = 0; j < this._clipX; j++) { this._sources.push(Texture_1.Texture.createFromTexture(img, w * j, h * i, w, h)); } } WeakObject_1.WeakObject.I.set(key, this._sources); } this.index = this._index; this.event(Event_1.Event.LOADED); this.onCompResize(); } } get sources() { return this._sources; } set sources(value) { this._sources = value; this.index = this._index; this.event(Event_1.Event.LOADED); } get group() { return this._group; } set group(value) { if (value && this._skin) Loader_1.Loader.setGroup(this._skin, value); this._group = value; } set width(value) { super.width = value; this._bitmap.width = value; } get width() { return super.width; } set height(value) { super.height = value; this._bitmap.height = value; } get height() { return super.height; } measureWidth() { this.runCallLater(this.changeClip); return this._bitmap.width; } measureHeight() { this.runCallLater(this.changeClip); return this._bitmap.height; } get sizeGrid() { if (this._bitmap.sizeGrid) return this._bitmap.sizeGrid.join(","); return null; } set sizeGrid(value) { this._bitmap.sizeGrid = UIUtils_1.UIUtils.fillArray(Styles_1.Styles.defaultSizeGrid, value, Number); } get index() { return this._index; } set index(value) { this._index = value; this._bitmap && this._sources && (this._bitmap.source = this._sources[value]); this.event(Event_1.Event.CHANGE); } get total() { this.runCallLater(this.changeClip); return this._sources ? this._sources.length : 0; } get autoPlay() { return this._autoPlay; } set autoPlay(value) { if (this._autoPlay != value) { this._autoPlay = value; value ? this.play() : this.stop(); } } get interval() { return this._interval; } set interval(value) { if (this._interval != value) { this._interval = value; if (this._isPlaying) this.play(); } } get isPlaying() { return this._isPlaying; } set isPlaying(value) { this._isPlaying = value; } play(from = 0, to = -1) { this._isPlaying = true; this.index = from; this._toIndex = to; this._index++; ILaya_1.ILaya.timer.loop(this.interval, this, this._loop); this.on(Event_1.Event.DISPLAY, this, this._onDisplay); this.on(Event_1.Event.UNDISPLAY, this, this._onDisplay); } _loop() { if (this._visible && this._sources) { this._index++; if (this._toIndex > -1 && this._index >= this._toIndex) this.stop(); else if (this._index >= this._sources.length) this._index = 0; this.index = this._index; } } stop() { this._isPlaying = false; ILaya_1.ILaya.timer.clear(this, this._loop); this.event(Event_1.Event.COMPLETE); } set dataSource(value) { this._dataSource = value; if (typeof (value) == 'number' || typeof (value) == 'string') this.index = parseInt(value); else super.dataSource = value; } get dataSource() { return super.dataSource; } get bitmap() { return this._bitmap; } _setClipChanged() { if (!this._clipChanged) { this._clipChanged = true; this.callLater(this.changeClip); } } } exports.Clip = Clip;