UNPKG

dragonbones-tools

Version:
1,344 lines 79.7 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var utils = require("../common/utils"); var geom_1 = require("./geom"); var dbftV23 = require("./dragonBonesFormatV23"); /** * DragonBones format. */ exports.DATA_VERSION_2_3 = "2.3"; exports.DATA_VERSION_3_0 = "3.0"; exports.DATA_VERSION_4_0 = "4.0"; exports.DATA_VERSION_4_5 = "4.5"; exports.DATA_VERSION_5_0 = "5.0"; exports.DATA_VERSION_5_5 = "5.5"; exports.DATA_VERSION_5_6 = "5.6"; exports.DATA_VERSION = exports.DATA_VERSION_5_6; exports.DATA_VERSIONS = [ exports.DATA_VERSION_2_3, exports.DATA_VERSION_3_0, exports.DATA_VERSION_4_0, exports.DATA_VERSION_4_5, exports.DATA_VERSION_5_0, exports.DATA_VERSION_5_5, exports.DATA_VERSION_5_6, ]; var OffsetOrder; (function (OffsetOrder) { OffsetOrder[OffsetOrder["FrameInt"] = 0] = "FrameInt"; OffsetOrder[OffsetOrder["FrameFloat"] = 1] = "FrameFloat"; OffsetOrder[OffsetOrder["Frame"] = 2] = "Frame"; })(OffsetOrder = exports.OffsetOrder || (exports.OffsetOrder = {})); var BinaryOffset; (function (BinaryOffset) { BinaryOffset[BinaryOffset["WeigthBoneCount"] = 0] = "WeigthBoneCount"; BinaryOffset[BinaryOffset["WeigthFloatOffset"] = 1] = "WeigthFloatOffset"; BinaryOffset[BinaryOffset["WeigthBoneIndices"] = 2] = "WeigthBoneIndices"; BinaryOffset[BinaryOffset["GeometryVertexCount"] = 0] = "GeometryVertexCount"; BinaryOffset[BinaryOffset["GeometryTriangleCount"] = 1] = "GeometryTriangleCount"; BinaryOffset[BinaryOffset["GeometryFloatOffset"] = 2] = "GeometryFloatOffset"; BinaryOffset[BinaryOffset["GeometryWeightOffset"] = 3] = "GeometryWeightOffset"; BinaryOffset[BinaryOffset["GeometryVertexIndices"] = 4] = "GeometryVertexIndices"; BinaryOffset[BinaryOffset["TimelineScale"] = 0] = "TimelineScale"; BinaryOffset[BinaryOffset["TimelineOffset"] = 1] = "TimelineOffset"; BinaryOffset[BinaryOffset["TimelineKeyFrameCount"] = 2] = "TimelineKeyFrameCount"; BinaryOffset[BinaryOffset["TimelineFrameValueCount"] = 3] = "TimelineFrameValueCount"; BinaryOffset[BinaryOffset["TimelineFrameValueOffset"] = 4] = "TimelineFrameValueOffset"; BinaryOffset[BinaryOffset["TimelineFrameOffset"] = 5] = "TimelineFrameOffset"; BinaryOffset[BinaryOffset["FramePosition"] = 0] = "FramePosition"; BinaryOffset[BinaryOffset["FrameTweenType"] = 1] = "FrameTweenType"; BinaryOffset[BinaryOffset["FrameTweenEasingOrCurveSampleCount"] = 2] = "FrameTweenEasingOrCurveSampleCount"; BinaryOffset[BinaryOffset["FrameCurveSamples"] = 3] = "FrameCurveSamples"; BinaryOffset[BinaryOffset["ActionFrameActionCount"] = 1] = "ActionFrameActionCount"; BinaryOffset[BinaryOffset["ActionFrameActionIndices"] = 2] = "ActionFrameActionIndices"; BinaryOffset[BinaryOffset["DeformMeshOffset"] = 0] = "DeformMeshOffset"; BinaryOffset[BinaryOffset["DeformCount"] = 1] = "DeformCount"; BinaryOffset[BinaryOffset["DeformValueCount"] = 2] = "DeformValueCount"; BinaryOffset[BinaryOffset["DeformValueOffset"] = 3] = "DeformValueOffset"; BinaryOffset[BinaryOffset["DeformFloatOffset"] = 4] = "DeformFloatOffset"; })(BinaryOffset = exports.BinaryOffset || (exports.BinaryOffset = {})); var ArmatureType; (function (ArmatureType) { ArmatureType[ArmatureType["Armature"] = 0] = "Armature"; ArmatureType[ArmatureType["MovieClip"] = 1] = "MovieClip"; ArmatureType[ArmatureType["Stage"] = 2] = "Stage"; ArmatureType[ArmatureType["ImageSequences"] = 3] = "ImageSequences"; })(ArmatureType = exports.ArmatureType || (exports.ArmatureType = {})); var BoneType; (function (BoneType) { BoneType[BoneType["Bone"] = 0] = "Bone"; BoneType[BoneType["Surface"] = 1] = "Surface"; })(BoneType = exports.BoneType || (exports.BoneType = {})); var DisplayType; (function (DisplayType) { DisplayType[DisplayType["Image"] = 0] = "Image"; DisplayType[DisplayType["Armature"] = 1] = "Armature"; DisplayType[DisplayType["Mesh"] = 2] = "Mesh"; DisplayType[DisplayType["BoundingBox"] = 3] = "BoundingBox"; DisplayType[DisplayType["Path"] = 4] = "Path"; })(DisplayType = exports.DisplayType || (exports.DisplayType = {})); var BoundingBoxType; (function (BoundingBoxType) { BoundingBoxType[BoundingBoxType["Rectangle"] = 0] = "Rectangle"; BoundingBoxType[BoundingBoxType["Ellipse"] = 1] = "Ellipse"; BoundingBoxType[BoundingBoxType["Polygon"] = 2] = "Polygon"; })(BoundingBoxType = exports.BoundingBoxType || (exports.BoundingBoxType = {})); var ActionType; (function (ActionType) { ActionType[ActionType["Play"] = 0] = "Play"; ActionType[ActionType["Frame"] = 10] = "Frame"; ActionType[ActionType["Sound"] = 11] = "Sound"; })(ActionType = exports.ActionType || (exports.ActionType = {})); var BlendMode; (function (BlendMode) { BlendMode[BlendMode["Normal"] = 0] = "Normal"; BlendMode[BlendMode["Add"] = 1] = "Add"; BlendMode[BlendMode["Alpha"] = 2] = "Alpha"; BlendMode[BlendMode["Darken"] = 3] = "Darken"; BlendMode[BlendMode["Difference"] = 4] = "Difference"; BlendMode[BlendMode["Erase"] = 5] = "Erase"; BlendMode[BlendMode["HardLight"] = 6] = "HardLight"; BlendMode[BlendMode["Invert"] = 7] = "Invert"; BlendMode[BlendMode["Layer"] = 8] = "Layer"; BlendMode[BlendMode["Lighten"] = 9] = "Lighten"; BlendMode[BlendMode["Multiply"] = 10] = "Multiply"; BlendMode[BlendMode["Overlay"] = 11] = "Overlay"; BlendMode[BlendMode["Screen"] = 12] = "Screen"; BlendMode[BlendMode["Subtract"] = 13] = "Subtract"; })(BlendMode = exports.BlendMode || (exports.BlendMode = {})); var AnimationType; (function (AnimationType) { AnimationType["Normal"] = "normal"; AnimationType["Tree"] = "tree"; AnimationType["Node"] = "node"; })(AnimationType = exports.AnimationType || (exports.AnimationType = {})); var AnimationBlendType; (function (AnimationBlendType) { AnimationBlendType["None"] = "none"; AnimationBlendType["E1D"] = "1d"; })(AnimationBlendType = exports.AnimationBlendType || (exports.AnimationBlendType = {})); var TimelineType; (function (TimelineType) { TimelineType[TimelineType["Action"] = 0] = "Action"; TimelineType[TimelineType["ZOrder"] = 1] = "ZOrder"; TimelineType[TimelineType["BoneAll"] = 10] = "BoneAll"; TimelineType[TimelineType["BoneTranslate"] = 11] = "BoneTranslate"; TimelineType[TimelineType["BoneRotate"] = 12] = "BoneRotate"; TimelineType[TimelineType["BoneScale"] = 13] = "BoneScale"; TimelineType[TimelineType["Surface"] = 50] = "Surface"; TimelineType[TimelineType["BoneAlpha"] = 60] = "BoneAlpha"; TimelineType[TimelineType["SlotDisplay"] = 20] = "SlotDisplay"; TimelineType[TimelineType["SlotColor"] = 21] = "SlotColor"; TimelineType[TimelineType["SlotDeform"] = 22] = "SlotDeform"; TimelineType[TimelineType["SlotZIndex"] = 23] = "SlotZIndex"; TimelineType[TimelineType["SlotAlpha"] = 24] = "SlotAlpha"; TimelineType[TimelineType["IKConstraint"] = 30] = "IKConstraint"; TimelineType[TimelineType["AnimationProgress"] = 40] = "AnimationProgress"; TimelineType[TimelineType["AnimationWeight"] = 41] = "AnimationWeight"; TimelineType[TimelineType["AnimationParameter"] = 42] = "AnimationParameter"; })(TimelineType = exports.TimelineType || (exports.TimelineType = {})); var TweenType; (function (TweenType) { TweenType[TweenType["None"] = 0] = "None"; TweenType[TweenType["Line"] = 1] = "Line"; TweenType[TweenType["Curve"] = 2] = "Curve"; TweenType[TweenType["QuadIn"] = 3] = "QuadIn"; TweenType[TweenType["QuadOut"] = 4] = "QuadOut"; TweenType[TweenType["QuadInOut"] = 5] = "QuadInOut"; })(TweenType = exports.TweenType || (exports.TweenType = {})); function isDragonBonesString(string) { var testString = string.substr(0, Math.min(200, string.length)); return testString.indexOf("armature") > 0 || testString.indexOf("textureAtlas") > 0; } exports.isDragonBonesString = isDragonBonesString; function getCurvePoint(x1, y1, x2, y2, x3, y3, x4, y4, t, result) { var l_t = 1 - t; var powA = l_t * l_t; var powB = t * t; var kA = l_t * powA; var kB = 3.0 * t * powA; var kC = 3.0 * l_t * powB; var kD = t * powB; result.x = kA * x1 + kB * x2 + kC * x3 + kD * x4; result.y = kA * y1 + kB * y2 + kC * y3 + kD * y4; } exports.getCurvePoint = getCurvePoint; function getCurveEasingValue(t, curve) { var curveCount = curve.length; if (curveCount % 3 === 1) { var stepIndex = -2; while ((stepIndex + 6 < curveCount ? curve[stepIndex + 6] : 1) < t) { // stepIndex + 3 * 2 stepIndex += 6; } var isInCurve = stepIndex >= 0 && stepIndex + 6 < curveCount; var x1 = isInCurve ? curve[stepIndex] : 0.0; var y1 = isInCurve ? curve[stepIndex + 1] : 0.0; var x2 = curve[stepIndex + 2]; var y2 = curve[stepIndex + 3]; var x3 = curve[stepIndex + 4]; var y3 = curve[stepIndex + 5]; var x4 = isInCurve ? curve[stepIndex + 6] : 1.0; var y4 = isInCurve ? curve[stepIndex + 7] : 1.0; var lower = 0.0; var higher = 1.0; while (higher - lower > 0.01) { var percentage = (higher + lower) / 2.0; getCurvePoint(x1, y1, x2, y2, x3, y3, x4, y4, percentage, geom_1.helpPointA); if (t - geom_1.helpPointA.x > 0.0) { lower = percentage; } else { higher = percentage; } } return geom_1.helpPointA.y; } else { var stepIndex = 0; while (curve[stepIndex + 6] < t) { // stepIndex + 3 * 2 stepIndex += 6; } var x1 = curve[stepIndex]; var y1 = curve[stepIndex + 1]; var x2 = curve[stepIndex + 2]; var y2 = curve[stepIndex + 3]; var x3 = curve[stepIndex + 4]; var y3 = curve[stepIndex + 5]; var x4 = curve[stepIndex + 6]; var y4 = curve[stepIndex + 7]; var lower = 0.0; var higher = 1.0; while (higher - lower > 0.01) { var percentage = (higher + lower) / 2.0; getCurvePoint(x1, y1, x2, y2, x3, y3, x4, y4, percentage, geom_1.helpPointA); if (t - geom_1.helpPointA.x > 0.0) { lower = percentage; } else { higher = percentage; } } return geom_1.helpPointA.y; } } exports.getCurveEasingValue = getCurveEasingValue; function samplingEasingCurve(curve, samples, isOmited) { var curveCount = curve.length; if (isOmited) { // The beginning and end vertices are omitted. (0.0, 0.0, ..., 1.0, 1.0) var stepIndex = -2; for (var i = 0, l = samples.length; i < l; ++i) { var t = (i + 1) / (l + 1); while ((stepIndex + 6 < curveCount ? curve[stepIndex + 6] : 1) < t) { // stepIndex + 3 * 2 stepIndex += 6; } var isInCurve = stepIndex >= 0 && stepIndex + 6 < curveCount; var x1 = isInCurve ? curve[stepIndex] : 0.0; var y1 = isInCurve ? curve[stepIndex + 1] : 0.0; var x2 = curve[stepIndex + 2]; var y2 = curve[stepIndex + 3]; var x3 = curve[stepIndex + 4]; var y3 = curve[stepIndex + 5]; var x4 = isInCurve ? curve[stepIndex + 6] : 1.0; var y4 = isInCurve ? curve[stepIndex + 7] : 1.0; var lower = 0.0; var higher = 1.0; while (higher - lower > 0.0001) { var percentage = (higher + lower) / 2.0; getCurvePoint(x1, y1, x2, y2, x3, y3, x4, y4, percentage, geom_1.helpPointA); if (t - geom_1.helpPointA.x > 0.0) { lower = percentage; } else { higher = percentage; } } samples[i] = geom_1.helpPointA.y; } } else { // Full vertices. var stepIndex = 0; for (var i = 0, l = samples.length; i < l; ++i) { if (i === 0) { samples[i] = curve[1]; continue; } else if (i === l - 1) { samples[i] = curve[curveCount - 1]; continue; } var t = i / (l - 1); while (curve[stepIndex + 6] < t) { // stepIndex + 3 * 2 stepIndex += 6; } var x1 = curve[stepIndex]; var y1 = curve[stepIndex + 1]; var x2 = curve[stepIndex + 2]; var y2 = curve[stepIndex + 3]; var x3 = curve[stepIndex + 4]; var y3 = curve[stepIndex + 5]; var x4 = curve[stepIndex + 6]; var y4 = curve[stepIndex + 7]; var lower = 0.0; var higher = 1.0; while (higher - lower > 0.0001) { var percentage = (higher + lower) / 2.0; getCurvePoint(x1, y1, x2, y2, x3, y3, x4, y4, percentage, geom_1.helpPointA); if (t - geom_1.helpPointA.x > 0.0) { lower = percentage; } else { higher = percentage; } } samples[i] = geom_1.helpPointA.y; } } } exports.samplingEasingCurve = samplingEasingCurve; function getEasingValue(tweenType, progress, easing) { var value = progress; switch (tweenType) { case TweenType.QuadIn: value = Math.pow(progress, 2.0); break; case TweenType.QuadOut: value = 1.0 - Math.pow(1.0 - progress, 2.0); break; case TweenType.QuadInOut: value = 0.5 * (1.0 - Math.cos(progress * Math.PI)); break; } return (value - progress) * easing + progress; } exports.getEasingValue = getEasingValue; function getEdgeFormTriangles(triangles) { var edges = []; var lines = {}; var addLine = function (a, b) { if (a > b) { var c_1 = a; a = b; b = c_1; } var k = a + "_" + b; if (k in lines) { lines[k] = null; } else { lines[k] = [a, b]; } }; for (var i = 0, l = triangles.length; i < l; i += 3) { addLine(triangles[i + 0], triangles[i + 1]); addLine(triangles[i + 1], triangles[i + 2]); addLine(triangles[i + 2], triangles[i + 0]); } for (var k in lines) { var line = lines[k]; if (line) { edges.push(line); } } edges.sort(function (a, b) { if (a[0] === b[0]) { return a[1] < b[1] ? -1 : 1; } return a[0] < b[0] ? -1 : 1; }); var last = edges.splice(1, 1)[0]; edges.push(last); var c = last[0]; last[0] = last[1]; last[1] = c; var result = new Array(); for (var _i = 0, edges_1 = edges; _i < edges_1.length; _i++) { var line = edges_1[_i]; result.push(line[0], line[1]); } return result; } exports.getEdgeFormTriangles = getEdgeFormTriangles; function getFrameByPosition(frames, position) { var index = 0; var currentPosition = 0; for (var _i = 0, frames_1 = frames; _i < frames_1.length; _i++) { var frame = frames_1[_i]; if (frame._position >= 0) { if (frame._position === position) { return frame; } else if (frame._position > position) { return frames[index - 1]; } } else { if (currentPosition === position) { return frame; } else if (currentPosition > position) { return frames[index - 1]; } currentPosition += frame.duration; } index++; } return frames[0]; } exports.getFrameByPosition = getFrameByPosition; function getTextureFormTextureAtlases(name, textureAtlases) { for (var _i = 0, textureAtlases_1 = textureAtlases; _i < textureAtlases_1.length; _i++) { var textureAtlas = textureAtlases_1[_i]; var texture = textureAtlas.getTexture(name); if (texture) { return texture; } } return null; } exports.getTextureFormTextureAtlases = getTextureFormTextureAtlases; function oldActionToNewAction(oldAction) { var newAction = new Action(); newAction.type = ActionType.Play; newAction.name = oldAction.gotoAndPlay; return newAction; } exports.oldActionToNewAction = oldActionToNewAction; function mergeActionToAnimation(animation, frame, framePosition, bone, slot, forRuntime) { var frames = animation.frame; var boneName = bone ? bone.name : ""; var slotName = slot ? slot.name : ""; if (frames.length === 0) { var beginFrame = new ActionFrame(); beginFrame.duration = animation.duration; frames.push(beginFrame); } var position = 0; var frameIndex = 0; var insertFrame = null; var prevFrame = null; for (var i = 0, l = frames.length; i < l; ++i) { var eachFrame = frames[i]; if (framePosition === position) { insertFrame = eachFrame; break; } else if (framePosition < position && prevFrame) { prevFrame.duration = framePosition - (position - prevFrame.duration); insertFrame = new ActionFrame(); insertFrame.duration = position - framePosition; frames.splice(i + 1, 0, insertFrame); break; } position += eachFrame.duration; prevFrame = eachFrame; frameIndex++; } if (!insertFrame && prevFrame) { prevFrame.duration = framePosition; insertFrame = new ActionFrame(); insertFrame.duration = position - framePosition; frames.splice(frameIndex, 0, insertFrame); } if (insertFrame) { if (frame instanceof dbftV23.AllFrame || frame instanceof BoneAllFrame) { if (frame.event) { var action = new Action(); action.name = frame.event; action.bone = boneName; if (forRuntime) { action.type = ActionType.Frame; insertFrame.actions.push(action); } else { insertFrame.events.push(action); } } if (frame.sound) { if (forRuntime) { var action = new Action(); action.type = ActionType.Sound; action.name = frame.sound; action.bone = boneName; insertFrame.actions.push(action); } else { insertFrame.sound = frame.sound; } } if (frame.action) { if (forRuntime) { var action = new Action(); action.type = ActionType.Play; action.name = frame.action; action.slot = slotName; insertFrame.actions.push(action); } } } else if (forRuntime) { for (var _i = 0, _a = frame.actions; _i < _a.length; _i++) { var action = _a[_i]; if (action instanceof OldAction) { var newAction = new Action(); newAction.type = ActionType.Play; newAction.name = action.gotoAndPlay; newAction.slot = slotName; insertFrame.actions.push(newAction); } else { action.slot = slotName; insertFrame.actions.push(action); } } } } } exports.mergeActionToAnimation = mergeActionToAnimation; function modifyFramesByPosition(frames) { if (frames.length === 0) { return; } if (frames[0]._position !== 0) { var frame = new frames[0].constructor(); frame.copy(frames[0]); frame._position = 0; if (frame instanceof TweenFrame) { frame.tweenEasing = 0.0; } frames.unshift(frame); } for (var i = 0, l = frames.length; i < l; ++i) { var frame = frames[i]; if (i < l - 1) { frame.duration = frames[i + 1]._position - frame._position; } } } exports.modifyFramesByPosition = modifyFramesByPosition; var BaseData = /** @class */ (function () { function BaseData() { this.extra = null; } BaseData.prototype.clearToBinary = function () { }; return BaseData; }()); exports.BaseData = BaseData; var DragonBones = /** @class */ (function (_super) { __extends(DragonBones, _super); function DragonBones() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.frameRate = 0; _this.name = ""; _this.stage = ""; _this.version = ""; _this.compatibleVersion = ""; _this.armature = []; _this.offset = []; // Binary. _this.tag = []; // Binary. _this.textureAtlas = []; _this.userData = null; return _this; } return DragonBones; }(BaseData)); exports.DragonBones = DragonBones; var UserData = /** @class */ (function (_super) { __extends(UserData, _super); function UserData() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.ints = []; _this.floats = []; _this.strings = []; return _this; } return UserData; }(BaseData)); exports.UserData = UserData; var OldAction = /** @class */ (function (_super) { __extends(OldAction, _super); function OldAction() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.gotoAndPlay = ""; return _this; } return OldAction; }(BaseData)); exports.OldAction = OldAction; var Action = /** @class */ (function (_super) { __extends(Action, _super); function Action() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = ActionType.Play; _this.name = ""; _this.bone = ""; _this.slot = ""; return _this; } return Action; }(UserData)); exports.Action = Action; var Canvas = /** @class */ (function (_super) { __extends(Canvas, _super); function Canvas() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.hasBackground = false; _this.color = -1; _this.x = 0; _this.y = 0; _this.width = 0; _this.height = 0; return _this; } return Canvas; }(BaseData)); exports.Canvas = Canvas; var Armature = /** @class */ (function (_super) { __extends(Armature, _super); function Armature() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = ArmatureType[ArmatureType.Armature].toLowerCase(); _this.frameRate = 0; _this.name = ""; _this.aabb = new geom_1.Rectangle(); _this.bone = []; _this.slot = []; _this.ik = []; _this.path = []; _this.skin = []; _this.animation = []; // Binary. _this.defaultActions = []; _this.actions = []; _this.canvas = null; _this.userData = null; return _this; } Armature.prototype.sortBones = function () { var total = this.bone.length; if (total <= 0) { return; } var sortHelper = this.bone.concat(); var index = 0; var count = 0; this.bone.length = 0; while (count < total) { var bone = sortHelper[index++]; if (index >= total) { index = 0; } if (this.bone.indexOf(bone) >= 0) { continue; } // TODO constraint. if (bone.parent) { var parent_1 = this.getBone(bone.parent); if (!parent_1 || this.bone.indexOf(parent_1) < 0) { continue; } } this.bone.push(bone); count++; } }; Armature.prototype.sortSlots = function () { this.slot.sort(function (a, b) { return a._zOrder > b._zOrder ? 1 : -1; }); }; Armature.prototype.getBone = function (name) { for (var _i = 0, _a = this.bone; _i < _a.length; _i++) { var bone = _a[_i]; if (bone.name === name) { return bone; } } return null; }; Armature.prototype.getSlot = function (name) { for (var _i = 0, _a = this.slot; _i < _a.length; _i++) { var slot = _a[_i]; if (slot.name === name) { return slot; } } return null; }; Armature.prototype.getSkin = function (name) { for (var _i = 0, _a = this.skin; _i < _a.length; _i++) { var skin = _a[_i]; if (skin.name === name) { return skin; } } return null; }; Armature.prototype.getDisplay = function (skinName, slotName, displayName) { var skin = this.getSkin(skinName); if (skin) { var slot = skin.getSlot(slotName); if (slot) { return slot.getDisplay(displayName); } } for (var _i = 0, _a = this.skin; _i < _a.length; _i++) { var skin_1 = _a[_i]; var slot = skin_1.getSlot(slotName); if (slot) { return slot.getDisplay(displayName); } for (var _b = 0, _c = skin_1.slot; _b < _c.length; _b++) { var slot_1 = _c[_b]; for (var _d = 0, _e = slot_1.display; _d < _e.length; _d++) { var display = _e[_d]; if (display && display.name === displayName) { return display; } } } } return null; }; Armature.prototype.getAnimation = function (animationName) { for (var _i = 0, _a = this.animation; _i < _a.length; _i++) { var animation = _a[_i]; if (animation.name === animationName) { return animation; } } return null; }; Armature.prototype.localToGlobal = function () { for (var _i = 0, _a = this.bone; _i < _a.length; _i++) { var bone = _a[_i]; if (!bone._global) { bone._global = new geom_1.Transform(); } bone._global.copyFrom(bone.transform); if (bone.parent) { var parent_2 = this.getBone(bone.parent); if (parent_2 && parent_2._global) { parent_2._global.toMatrix(geom_1.helpMatrixA); if (bone.inheritScale) { if (!bone.inheritRotation) { bone._global.skX -= parent_2._global.skY; bone._global.skY -= parent_2._global.skY; } bone._global.toMatrix(geom_1.helpMatrixB); geom_1.helpMatrixB.concat(geom_1.helpMatrixA); bone._global.fromMatrix(geom_1.helpMatrixB); if (!bone.inheritTranslation) { bone._global.x = bone.transform.x; bone._global.y = bone.transform.y; } } else { if (bone.inheritTranslation) { geom_1.helpMatrixA.transformPoint(bone._global.x, bone._global.y, bone._global, true); } if (bone.inheritRotation) { var dR = parent_2._global.skY; if (parent_2._global.scX < 0.0) { dR += Math.PI; } if (geom_1.helpMatrixA.a * geom_1.helpMatrixA.d - geom_1.helpMatrixA.b * geom_1.helpMatrixA.c < 0.0) { dR -= bone._global.skY * 2.0; if (bone.inheritReflection) { bone._global.skX += Math.PI; } } bone._global.skX += dR; bone._global.skY += dR; } } } } } }; return Armature; }(BaseData)); exports.Armature = Armature; var Bone = /** @class */ (function (_super) { __extends(Bone, _super); function Bone() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = BoneType[BoneType.Bone].toLowerCase(); _this.inheritTranslation = true; _this.inheritRotation = true; _this.inheritScale = true; _this.inheritReflection = true; _this.length = 0.0; _this.alpha = 1.0; _this.name = ""; _this.parent = ""; _this.transform = new geom_1.Transform(); _this.userData = null; _this._global = null; return _this; } return Bone; }(BaseData)); exports.Bone = Bone; var Surface = /** @class */ (function (_super) { __extends(Surface, _super); function Surface(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.type = BoneType[BoneType.Surface].toLowerCase(); _this.offset = -1; // Binary. _this.segmentX = 0; _this.segmentY = 0; _this.vertices = []; _this.weights = []; // _this.bones = []; // if (isDefault) { _this.type = ""; } return _this; } Surface.prototype.clearToBinary = function () { this.vertices.length = 0; }; Object.defineProperty(Surface.prototype, "vertexCount", { get: function () { return (this.segmentX + 1) * (this.segmentY + 1); }, enumerable: true, configurable: true }); return Surface; }(Bone)); exports.Surface = Surface; var Slot = /** @class */ (function (_super) { __extends(Slot, _super); function Slot() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.blendMode = BlendMode[BlendMode.Normal].toLowerCase(); _this.displayIndex = 0; _this.zIndex = 0; _this.alpha = 1.0; _this.name = ""; _this.parent = ""; _this.color = new geom_1.ColorTransform(); _this.actions = []; // Deprecated. _this.userData = null; // _this._zOrder = -1; return _this; } return Slot; }(BaseData)); exports.Slot = Slot; var IKConstraint = /** @class */ (function (_super) { __extends(IKConstraint, _super); function IKConstraint() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.bendPositive = true; _this.chain = 0; _this.weight = 1.00; _this.name = ""; _this.bone = ""; _this.target = ""; return _this; } return IKConstraint; }(BaseData)); exports.IKConstraint = IKConstraint; var PathConstraint = /** @class */ (function (_super) { __extends(PathConstraint, _super); function PathConstraint() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.name = ""; _this.target = ""; _this.bones = []; _this.positionMode = "percent"; _this.spacingMode = "length"; _this.rotateMode = "tangent"; _this.position = 0; _this.spacing = 0; _this.rotateOffset = 0; _this.rotateMix = 0; _this.translateMix = 0; return _this; } return PathConstraint; }(BaseData)); exports.PathConstraint = PathConstraint; var Skin = /** @class */ (function (_super) { __extends(Skin, _super); function Skin() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.name = "default"; _this.slot = []; _this.userData = null; return _this; } Skin.prototype.getSlot = function (name) { for (var _i = 0, _a = this.slot; _i < _a.length; _i++) { var slot = _a[_i]; if (slot.name === name) { return slot; } } return null; }; return Skin; }(BaseData)); exports.Skin = Skin; var SkinSlot = /** @class */ (function (_super) { __extends(SkinSlot, _super); function SkinSlot() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.name = ""; _this.display = []; _this.actions = []; // Deprecated. return _this; } SkinSlot.prototype.getDisplay = function (name) { for (var _i = 0, _a = this.display; _i < _a.length; _i++) { var display = _a[_i]; if (display && display.name === name) { return display; } } return null; }; return SkinSlot; }(BaseData)); exports.SkinSlot = SkinSlot; var Display = /** @class */ (function (_super) { __extends(Display, _super); function Display() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = DisplayType[DisplayType.Image].toLowerCase(); _this.name = ""; _this.transform = new geom_1.Transform(); return _this; } return Display; }(BaseData)); exports.Display = Display; var BoundingBoxDisplay = /** @class */ (function (_super) { __extends(BoundingBoxDisplay, _super); function BoundingBoxDisplay() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.subType = BoundingBoxType[BoundingBoxType.Rectangle].toLowerCase(); _this.color = 0x000000; return _this; } return BoundingBoxDisplay; }(Display)); exports.BoundingBoxDisplay = BoundingBoxDisplay; var ImageDisplay = /** @class */ (function (_super) { __extends(ImageDisplay, _super); function ImageDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.path = ""; _this.pivot = new geom_1.Point(0.5, 0.5); if (!isDefault) { _this.type = DisplayType[DisplayType.Image].toLowerCase(); } return _this; } return ImageDisplay; }(Display)); exports.ImageDisplay = ImageDisplay; var ArmatureDisplay = /** @class */ (function (_super) { __extends(ArmatureDisplay, _super); function ArmatureDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.inheritAnimation = true; _this.path = ""; _this.actions = []; if (!isDefault) { _this.type = DisplayType[DisplayType.Armature].toLowerCase(); } return _this; } return ArmatureDisplay; }(Display)); exports.ArmatureDisplay = ArmatureDisplay; var MeshDisplay = /** @class */ (function (_super) { __extends(MeshDisplay, _super); function MeshDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.offset = -1; // Binary. _this.width = 0; _this.height = 0; _this.path = ""; _this.vertices = []; _this.uvs = []; _this.triangles = []; _this.weights = []; _this.slotPose = []; _this.bonePose = []; _this.glueWeights = []; _this.glueMeshes = []; _this.edges = []; // Nonessential. _this.userEdges = []; // Nonessential. _this._userEdges = true; // TODO _this._boneCount = 0; _this._weightCount = 0; _this._matrix = new geom_1.Matrix(); if (!isDefault) { _this.type = DisplayType[DisplayType.Mesh].toLowerCase(); } return _this; } MeshDisplay.prototype.clearToBinary = function () { this.width = 0; this.height = 0; this.vertices.length = 0; this.uvs.length = 0; this.triangles.length = 0; this.weights.length = 0; this.slotPose.length = 0; this.bonePose.length = 0; this.edges.length = 0; this.userEdges.length = 0; }; MeshDisplay.prototype.getBonePoseOffset = function (boneIndex) { for (var i = 0, l = this.bonePose.length; i < l; i += 7) { if (boneIndex === this.bonePose[i]) { return i; } } throw new Error(); }; return MeshDisplay; }(Display)); exports.MeshDisplay = MeshDisplay; var SharedMeshDisplay = /** @class */ (function (_super) { __extends(SharedMeshDisplay, _super); function SharedMeshDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.inheritDeform = true; _this.path = ""; _this.share = ""; _this.skin = "default"; if (!isDefault) { _this.type = DisplayType[DisplayType.Mesh].toLowerCase(); } return _this; } return SharedMeshDisplay; }(Display)); exports.SharedMeshDisplay = SharedMeshDisplay; var PathDisplay = /** @class */ (function (_super) { __extends(PathDisplay, _super); function PathDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.offset = -1; // Binary. _this.closed = false; _this.constantSpeed = false; _this.vertexCount = 0; _this.vertices = []; _this.lengths = []; _this.weights = []; _this.bones = []; if (!isDefault) { _this.type = DisplayType[DisplayType.Path].toLowerCase(); } return _this; } PathDisplay.prototype.clearToBinary = function () { this.vertexCount = 0; this.vertices.length = 0; this.weights.length = 0; this.bones.length = 0; }; return PathDisplay; }(Display)); exports.PathDisplay = PathDisplay; var RectangleBoundingBoxDisplay = /** @class */ (function (_super) { __extends(RectangleBoundingBoxDisplay, _super); function RectangleBoundingBoxDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.width = 0.00; _this.height = 0.00; if (!isDefault) { _this.type = DisplayType[DisplayType.BoundingBox].toLowerCase(); _this.subType = BoundingBoxType[BoundingBoxType.Rectangle].toLowerCase(); } return _this; } return RectangleBoundingBoxDisplay; }(BoundingBoxDisplay)); exports.RectangleBoundingBoxDisplay = RectangleBoundingBoxDisplay; var EllipseBoundingBoxDisplay = /** @class */ (function (_super) { __extends(EllipseBoundingBoxDisplay, _super); function EllipseBoundingBoxDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.width = 0.00; _this.height = 0.00; if (!isDefault) { _this.type = DisplayType[DisplayType.BoundingBox].toLowerCase(); _this.subType = BoundingBoxType[BoundingBoxType.Ellipse].toLowerCase(); } return _this; } return EllipseBoundingBoxDisplay; }(BoundingBoxDisplay)); exports.EllipseBoundingBoxDisplay = EllipseBoundingBoxDisplay; var PolygonBoundingBoxDisplay = /** @class */ (function (_super) { __extends(PolygonBoundingBoxDisplay, _super); function PolygonBoundingBoxDisplay(isDefault) { if (isDefault === void 0) { isDefault = false; } var _this = _super.call(this) || this; _this.offset = -1; // Binary. _this.vertexCount = 0; _this.vertices = []; _this.weights = []; _this.bones = []; if (!isDefault) { _this.type = DisplayType[DisplayType.BoundingBox].toLowerCase(); _this.subType = BoundingBoxType[BoundingBoxType.Polygon].toLowerCase(); } return _this; } PolygonBoundingBoxDisplay.prototype.clearToBinary = function () { this.vertexCount = 0; // this.vertices.length = 0; this.weights.length = 0; this.bones.length = 0; }; return PolygonBoundingBoxDisplay; }(BoundingBoxDisplay)); exports.PolygonBoundingBoxDisplay = PolygonBoundingBoxDisplay; var Animation = /** @class */ (function (_super) { __extends(Animation, _super); function Animation() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = AnimationType.Normal; _this.blendType = AnimationBlendType.None; _this.duration = 1; _this.playTimes = 1; _this.scale = 1.0; _this.fadeInTime = 0.0; _this.name = "default"; _this.frame = []; // Deprecated. _this.bone = []; // Deprecated. _this.slot = []; // Deprecated. _this.ffd = []; // Deprecated. _this.ik = []; // Deprecated. _this.timeline = []; _this.zOrder = null; return _this; } // Deprecated. Animation.prototype.getSlotTimeline = function (name) { for (var _i = 0, _a = this.slot; _i < _a.length; _i++) { var timeline = _a[_i]; if (timeline.name === name) { return timeline; } } return null; }; // Deprecated. Animation.prototype.getBoneTimeline = function (name) { for (var _i = 0, _a = this.bone; _i < _a.length; _i++) { var timeline = _a[_i]; if (timeline.name === name) { return timeline; } } return null; }; Animation.prototype.getAnimationTimeline = function (name, type) { for (var _i = 0, _a = this.timeline; _i < _a.length; _i++) { var timeline = _a[_i]; if (timeline.type === type && timeline.name === name) { return timeline; } } return null; }; return Animation; }(BaseData)); exports.Animation = Animation; var AnimationBinary = /** @class */ (function (_super) { __extends(AnimationBinary, _super); function AnimationBinary() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = AnimationType.Normal; _this.blendType = AnimationBlendType.None; _this.duration = 0; _this.playTimes = 1; _this.scale = 1.0; _this.fadeInTime = 0.0; _this.name = ""; _this.action = -1; // Deprecated. _this.zOrder = -1; // Deprecated. _this.offset = []; _this.bone = {}; // Deprecated. _this.surface = {}; // Deprecated. _this.slot = {}; // Deprecated. _this.constraint = {}; // Deprecated. _this.timeline = []; return _this; } return AnimationBinary; }(BaseData)); exports.AnimationBinary = AnimationBinary; var Timeline = /** @class */ (function (_super) { __extends(Timeline, _super); function Timeline() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.scale = 1.0; _this.offset = 0.0; _this.name = ""; return _this; } return Timeline; }(BaseData)); exports.Timeline = Timeline; var TypeTimeline = /** @class */ (function (_super) { __extends(TypeTimeline, _super); function TypeTimeline() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.type = TimelineType.Action; _this.frame = []; return _this; } TypeTimeline.prototype.clearToBinary = function () { this.scale = 1.0; this.frame.length = 0; }; return TypeTimeline; }(Timeline)); exports.TypeTimeline = TypeTimeline; /** * @deprecated */ var BoneTimeline = /** @class */ (function (_super) { __extends(BoneTimeline, _super); function BoneTimeline() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.frame = []; // Deprecated. _this.translateFrame = []; _this.rotateFrame = []; _this.scaleFrame = []; return _this; } BoneTimeline.prototype.insertFrame = function (frames, position) { var index = 0; var fromPosition = 0; var progress = 0.0; var from = null; var insert; var to = null; for (var _i = 0, frames_2 = frames; _i < frames_2.length; _i++) { var frame = frames_2[_i]; if (fromPosition === position) { return index; } else if (fromPosition < position && position <= fromPosition + frame.duration) { if (index === frames.length - 1) { } else if (position === fromPosition + frame.duration) { return index + 1; } else { to = frames[index + 1]; } progress = (position - fromPosition) / frame.duration; from = frame; index++; break; } index++; fromPosition += frame.duration; } if (frames === this.frame) { if (!from) { from = new BoneAllFrame(); frames.push(from); } insert = new BoneAllFrame(); } else if (frames === this.translateFrame) { if (!from) { from = new DoubleValueFrame0(); frames.push(from); } insert = new DoubleValueFrame0(); } else if (frames === this.rotateFrame) { if (!from) { from = new BoneRotateFrame(); frames.push(from); } insert = new BoneRotateFrame(); } else if (frames === this.scaleFrame) { if (!from) { from = new DoubleValueFrame1(); frames.push(from); } insert = new DoubleValueFrame1(); } else { return -1; } insert.duration = from.duration - (position - fromPosition); from.duration -= insert.duration; frames.splice(index, 0, insert); if (from instanceof TweenFrame && insert instanceof TweenFrame) { // TODO insert.tweenEasing = from.tweenEasing; //to.curve; progress = from.getTweenProgress(progress); } if (from instanceof BoneAllFrame && insert instanceof BoneAllFrame) { if (to instanceof BoneAllFrame) { insert.transform.x = from.transform.x + (to.transform.x - from.transform.x) * progress; insert.transform.y = from.transform.y + (to.transform.y - from.transform.y) * progress; insert.transform.scX = from.transform.scX + (to.transform.scX - from.transform.scX) * progress; insert.transform.scY = from.transform.scY + (to.transform.scY - from.transform.scY) * progress; if (from.tweenRotate === 0) { insert.tweenRotate = 0; insert.transform.skX = from.transform.skX + geom_1.normalizeDegree(to.transform.skX - from.transform.skX) * progress; insert.transform.skY = from.transform.skY + geom_1.normalizeDegree(to.transform.skY - from.transform.skY) * progress; } else { var tweenRotate = from.tweenRotate; if (tweenRotate > 0 && tweenRotate < 2) { insert.tweenRotate = 1; } else if (tweenRotate < 0 && tweenRotate > -2) { insert.tweenRotate = -1; } else { insert.tweenRotate = Math.floor(tweenRotate * progress); } if (tweenRotate > 0 ? to.transform.skY >= from.transform.skY : to.transform.skY <= from.transform.skY) { tweenRotate = tweenRotate > 0 ? tweenRotate - 1 : tweenRotate + 1; } insert.transform.skX = from.transform.skX + geom_1.normalizeDegree(to.transform.skX - from.transform.skX + 360.0 * tweenRotate) * progress; insert.transform.skY = from.transform.skY + geom_1.normalizeDegree(to.transform.skY - from.transform.skY + 360.0 * tweenRotate) * progress; } } else { insert.transform.copyFrom(from.transform); } } else if (from instanceof DoubleValueFrame0 && insert instanceof DoubleValueFrame0) { if (to instanceof DoubleValueFram