commonui-lib-test
Version:
"#common ui lib test"
136 lines (135 loc) • 4.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Templet = Laya.Templet;
var Skeleton = Laya.Skeleton;
var Handler = Laya.Handler;
var Event = Laya.Event;
/**
* @api {class} SkeletonHandler
* @apiName SkeletonHandler
* @apiGroup SkeletonHandler
* @apiDescription Skeleton Handler
*/
class SkeletonHandler {
// private mAniPath: string;
// private mAniName: string;
constructor(root, aniMode) {
this.mRoot = root;
this.mAniMode = aniMode || 0;
}
/**
* @api {function} loadWithTemplet
* @apiName loadWithTemplet
* @apiGroup SkeletonHandler
* @apiDescription load skeleton with Laya.Templet
*/
loadWithTemplet(skeletonData) {
return __awaiter(this, void 0, void 0, function* () {
let promise = new Promise(resolve => {
let args = this.combinArgs(skeletonData);
this.mFactory = new Templet();
this.mFactory.on(Event.COMPLETE, this, () => resolve(this.parseTempletComplete(args[0])), args);
this.mFactory.loadAni(skeletonData.url);
});
return promise;
});
}
/**
* @api {function} loadWithSkeleton
* @apiName loadWithSkeleton
* @apiGroup SkeletonHandler
* @apiDescription load skeleton with Laya.Skeleton
*/
loadWithSkeleton(skeletonData) {
return __awaiter(this, void 0, void 0, function* () {
let promise = new Promise(resolve => {
let args = this.combinArgs(skeletonData);
this.mArmature = new Skeleton();
this.mArmature.load(skeletonData.url, Handler.create(this, () => resolve(this.parseSkeletonComplete(args[0])), args), this.mAniMode);
});
return promise;
});
}
/**
* @api {function} dispose
* @apiName dispose
* @apiGroup SkeletonHandler
* @apiDescription dispose SkeletonHandler
*/
dispose() {
if (this.mFactory)
this.mFactory.destroy();
if (this.mArmature)
this.mArmature.destroy(false);
this.mRoot = null;
this.mFactory = null;
this.mArmature = null;
}
/**
* @api {function} parseTempletComplete
* @apiName parseTempletComplete
* @apiGroup SkeletonHandler
* @apiDescription on load skeleton with Laya.Templet complete
*/
parseTempletComplete(evt) {
let { pivot, scale, aniName, loop, autoPlay } = evt;
if (!this.mRoot)
return;
this.mArmature = this.mFactory.buildArmature(this.mAniMode);
this.mRoot.displayObject.addChild(this.mArmature);
this.mArmature.pivot(pivot.x, pivot.y).scale(scale.x, scale.y).pos(this.mRoot.width / 2, this.mRoot.height / 2);
if (autoPlay) {
this.showDragonBoneAni(aniName, loop);
}
else {
this.mArmature.stop();
}
}
/**
* @api {function} parseSkeletonComplete
* @apiName parseSkeletonComplete
* @apiGroup SkeletonHandler
* @apiDescription on load skeleton with Laya.Skeleton complete
*/
parseSkeletonComplete(evt) {
let { pivot, scale, aniName, loop, autoPlay } = evt;
if (!this.mRoot)
return;
this.mRoot.displayObject.addChild(this.mArmature);
this.mArmature.pivot(pivot.x, pivot.y).scale(scale.x, scale.y).pos(this.mRoot.width / 2, this.mRoot.height / 2);
if (autoPlay) {
this.showDragonBoneAni(aniName, loop);
}
else {
this.mArmature.stop();
}
}
/**
* @api {function} showDragonBoneAni
* @apiName showDragonBoneAni
* @apiGroup SkeletonHandler
* @apiDescription show dragonbone animation winth name or index
*/
showDragonBoneAni(aniName, loop) {
if (this.mArmature) {
this.mArmature.play(aniName, loop);
}
}
/**
* @api {function} combinArgs
* @apiName combinArgs
* @apiGroup SkeletonHandler
* @apiDescription combin loader args
*/
combinArgs(skeletonData) {
let args = {
pivot: skeletonData.pivot || { x: 1, y: 1 },
scale: skeletonData.scale || { x: 1, y: 1 },
aniName: skeletonData.aniName || 0,
loop: skeletonData.loop || false,
autoPlay: skeletonData.autoPlay || false
};
return [args];
}
}
exports.default = SkeletonHandler;