/**
* The MIT License (MIT)
*
* Copyright (c) 2012-2017 DragonBones team and other contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
namespace DragonBones
{
///
/// - The armature data.
///
/// DragonBones 3.0
/// en_US
///
/// - 骨架数据。
///
/// DragonBones 3.0
/// zh_CN
public class ArmatureData : BaseObject
{
///
public ArmatureType type;
///
/// - The animation frame rate.
///
/// DragonBones 3.0
/// en_US
///
/// - 动画帧率。
///
/// DragonBones 3.0
/// zh_CN
public uint frameRate;
///
public uint cacheFrameRate;
///
public float scale;
///
/// - The armature name.
///
/// DragonBones 3.0
/// en_US
///
/// - 骨架名称。
///
/// DragonBones 3.0
/// zh_CN
public string name;
///
public readonly Rectangle aabb = new Rectangle();
///
/// - The names of all the animation data.
///
/// DragonBones 3.0
/// en_US
///
/// - 所有的动画数据名称。
///
/// DragonBones 3.0
/// zh_CN
public readonly List animationNames = new List();
///
public readonly List sortedBones = new List();
///
public readonly List sortedSlots = new List();
///
public readonly List defaultActions = new List();
///
public readonly List actions = new List();
///
public readonly Dictionary bones = new Dictionary();
///
public readonly Dictionary slots = new Dictionary();
///
public readonly Dictionary constraints = new Dictionary();
///
public readonly Dictionary skins = new Dictionary();
///
public readonly Dictionary animations = new Dictionary();
///
/// - The default skin data.
///
/// DragonBones 4.5
/// en_US
///
/// - 默认插槽数据。
///
/// DragonBones 4.5
/// zh_CN
public SkinData defaultSkin = null;
///
/// - The default animation data.
///
/// DragonBones 4.5
/// en_US
///
/// - 默认动画数据。
///
/// DragonBones 4.5
/// zh_CN
public AnimationData defaultAnimation = null;
///
public CanvasData canvas = null; // Initial value.
///
public UserData userData = null; // Initial value.
///
public DragonBonesData parent;
///
protected override void _OnClear()
{
foreach (var action in this.defaultActions)
{
action.ReturnToPool();
}
foreach (var action in this.actions)
{
action.ReturnToPool();
}
foreach (var k in this.bones.Keys)
{
this.bones[k].ReturnToPool();
}
foreach (var k in this.slots.Keys)
{
this.slots[k].ReturnToPool();
}
foreach (var k in this.constraints.Keys)
{
this.constraints[k].ReturnToPool();
}
foreach (var k in this.skins.Keys)
{
this.skins[k].ReturnToPool();
}
foreach (var k in this.animations.Keys)
{
this.animations[k].ReturnToPool();
}
if (this.canvas != null)
{
this.canvas.ReturnToPool();
}
if (this.userData != null)
{
this.userData.ReturnToPool();
}
this.type = ArmatureType.Armature;
this.frameRate = 0;
this.cacheFrameRate = 0;
this.scale = 1.0f;
this.name = "";
this.aabb.Clear();
this.animationNames.Clear();
this.sortedBones.Clear();
this.sortedSlots.Clear();
this.defaultActions.Clear();
this.actions.Clear();
this.bones.Clear();
this.slots.Clear();
this.constraints.Clear();
this.skins.Clear();
this.animations.Clear();
this.defaultSkin = null;
this.defaultAnimation = null;
this.canvas = null;
this.userData = null;
this.parent = null; //
}
///
///
public void SortBones()
{
var total = this.sortedBones.Count;
if (total <= 0)
{
return;
}
var sortHelper = this.sortedBones.ToArray();
var index = 0;
var count = 0;
this.sortedBones.Clear();
while (count < total)
{
var bone = sortHelper[index++];
if (index >= total)
{
index = 0;
}
if (this.sortedBones.Contains(bone))
{
continue;
}
var flag = false;
foreach (var constraint in this.constraints.Values)
{
// Wait constraint.
if (constraint.root == bone && !this.sortedBones.Contains(constraint.target))
{
flag = true;
break;
}
}
if (flag)
{
continue;
}
if (bone.parent != null && !this.sortedBones.Contains(bone.parent))
{
// Wait parent.
continue;
}
this.sortedBones.Add(bone);
count++;
}
}
///
///
public void CacheFrames(uint frameRate)
{
if (this.cacheFrameRate > 0)
{
// TODO clear cache.
return;
}
this.cacheFrameRate = frameRate;
foreach (var k in this.animations.Keys)
{
this.animations[k].CacheFrames(this.cacheFrameRate);
}
}
///
///
public int SetCacheFrame(Matrix globalTransformMatrix, TransformDB transform)
{
var dataArray = this.parent.cachedFrames;
var arrayOffset = dataArray.Count;
dataArray.ResizeList(arrayOffset + 10, 0.0f);
dataArray[arrayOffset] = globalTransformMatrix.a;
dataArray[arrayOffset + 1] = globalTransformMatrix.b;
dataArray[arrayOffset + 2] = globalTransformMatrix.c;
dataArray[arrayOffset + 3] = globalTransformMatrix.d;
dataArray[arrayOffset + 4] = globalTransformMatrix.tx;
dataArray[arrayOffset + 5] = globalTransformMatrix.ty;
dataArray[arrayOffset + 6] = transform.rotation;
dataArray[arrayOffset + 7] = transform.skew;
dataArray[arrayOffset + 8] = transform.scaleX;
dataArray[arrayOffset + 9] = transform.scaleY;
return arrayOffset;
}
///
///
public void GetCacheFrame(Matrix globalTransformMatrix, TransformDB transform, int arrayOffset)
{
var dataArray = this.parent.cachedFrames;
globalTransformMatrix.a = dataArray[arrayOffset];
globalTransformMatrix.b = dataArray[arrayOffset + 1];
globalTransformMatrix.c = dataArray[arrayOffset + 2];
globalTransformMatrix.d = dataArray[arrayOffset + 3];
globalTransformMatrix.tx = dataArray[arrayOffset + 4];
globalTransformMatrix.ty = dataArray[arrayOffset + 5];
transform.rotation = dataArray[arrayOffset + 6];
transform.skew = dataArray[arrayOffset + 7];
transform.scaleX = dataArray[arrayOffset + 8];
transform.scaleY = dataArray[arrayOffset + 9];
transform.x = globalTransformMatrix.tx;
transform.y = globalTransformMatrix.ty;
}
///
///
public void AddBone(BoneData value)
{
if (value != null && !string.IsNullOrEmpty(value.name))
{
if (this.bones.ContainsKey(value.name))
{
Helper.Assert(false, "Same bone: " + value.name);
this.bones[value.name].ReturnToPool();
}
this.bones[value.name] = value;
this.sortedBones.Add(value);
}
}
///
///
public void AddSlot(SlotData value)
{
if (value != null && !string.IsNullOrEmpty(value.name))
{
if (this.slots.ContainsKey(value.name))
{
Helper.Assert(false, "Same slot: " + value.name);
this.slots[value.name].ReturnToPool();
}
this.slots[value.name] = value;
this.sortedSlots.Add(value);
}
}
///
///
public void AddConstraint(ConstraintData value)
{
if (value != null && !string.IsNullOrEmpty(value.name))
{
if (this.constraints.ContainsKey(value.name))
{
Helper.Assert(false, "Same constraint: " + value.name);
this.slots[value.name].ReturnToPool();
}
this.constraints[value.name] = value;
}
}
///
///
public void AddSkin(SkinData value)
{
if (value != null && !string.IsNullOrEmpty(value.name))
{
if (this.skins.ContainsKey(value.name))
{
Helper.Assert(false, "Same slot: " + value.name);
this.skins[value.name].ReturnToPool();
}
value.parent = this;
this.skins[value.name] = value;
if (this.defaultSkin == null)
{
this.defaultSkin = value;
}
if (value.name == "default")
{
this.defaultSkin = value;
}
}
}
///
///
public void AddAnimation(AnimationData value)
{
if (value != null && !string.IsNullOrEmpty(value.name))
{
if (this.animations.ContainsKey(value.name))
{
Helper.Assert(false, "Same animation: " + value.name);
this.animations[value.name].ReturnToPool();
}
value.parent = this;
this.animations[value.name] = value;
this.animationNames.Add(value.name);
if (this.defaultAnimation == null)
{
this.defaultAnimation = value;
}
}
}
///
///
internal void AddAction(ActionData value, bool isDefault)
{
if (isDefault)
{
this.defaultActions.Add(value);
}
else
{
this.actions.Add(value);
}
}
///
/// - Get a specific done data.
///
/// - The bone name.
/// DragonBones 3.0
/// en_US
///
/// - 获取特定的骨骼数据。
///
/// - 骨骼名称。
/// DragonBones 3.0
/// zh_CN
public BoneData GetBone(string boneName)
{
return (!string.IsNullOrEmpty(boneName) && bones.ContainsKey(boneName)) ? bones[boneName] : null;
}
///
/// - Get a specific slot data.
///
/// - The slot name.
/// DragonBones 3.0
/// en_US
///
/// - 获取特定的插槽数据。
///
/// - 插槽名称。
/// DragonBones 3.0
/// zh_CN
public SlotData GetSlot(string slotName)
{
return (!string.IsNullOrEmpty(slotName) && slots.ContainsKey(slotName)) ? slots[slotName] : null;
}
///
public ConstraintData GetConstraint(string constraintName)
{
return this.constraints.ContainsKey(constraintName) ? this.constraints[constraintName] : null;
}
///
/// - Get a specific skin data.
///
/// - The skin name.
/// DragonBones 3.0
/// en_US
///
/// - 获取特定皮肤数据。
///
/// - 皮肤名称。
/// DragonBones 3.0
/// zh_CN
public SkinData GetSkin(string skinName)
{
return !string.IsNullOrEmpty(skinName) ? (skins.ContainsKey(skinName) ? skins[skinName] : null) : defaultSkin;
}
///
public MeshDisplayData GetMesh(string skinName, string slotName, string meshName)
{
var skin = this.GetSkin(skinName);
if (skin == null)
{
return null;
}
return skin.GetDisplay(slotName, meshName) as MeshDisplayData;
}
///
/// - Get a specific animation data.
///
/// - The animation animationName.
/// DragonBones 3.0
/// en_US
///
/// - 获取特定的动画数据。
///
/// - 动画名称。
/// DragonBones 3.0
/// zh_CN
public AnimationData GetAnimation(string animationName)
{
return !string.IsNullOrEmpty(animationName) ? (animations.ContainsKey(animationName) ? animations[animationName] : null) : defaultAnimation;
}
}
///
/// - The bone data.
///
/// DragonBones 3.0
/// en_US
///
/// - 骨骼数据。
///
/// DragonBones 3.0
/// zh_CN
public class BoneData : BaseObject
{
///
public bool inheritTranslation;
///
public bool inheritRotation;
///
public bool inheritScale;
///
public bool inheritReflection;
///
/// - The bone length.
///
/// DragonBones 3.0
/// en_US
///
/// - 骨骼长度。
///
/// DragonBones 3.0
/// zh_CN
public float length;
///
/// - The bone name.
///
/// DragonBones 3.0
/// en_US
///
/// - 骨骼名称。
///
/// DragonBones 3.0
/// zh_CN
public string name;
///
public readonly TransformDB transform = new TransformDB();
///
public UserData userData = null; // Initial value.
///
/// - The parent bone data.
///
/// DragonBones 3.0
/// en_US
///
/// - 父骨骼数据。
///
/// DragonBones 3.0
/// zh_CN
public BoneData parent = null;
///
protected override void _OnClear()
{
if (this.userData != null)
{
this.userData.ReturnToPool();
}
this.inheritTranslation = false;
this.inheritRotation = false;
this.inheritScale = false;
this.inheritReflection = false;
this.length = 0.0f;
this.name = "";
this.transform.Identity();
this.userData = null;
this.parent = null;
}
}
///
///
public class SurfaceData : BoneData
{
public float vertexCountX;
public float vertexCountY;
public readonly List vertices = new List();
///
protected override void _OnClear()
{
base._OnClear();
this.vertexCountX = 0;
this.vertexCountY = 0;
this.vertices.Clear();
}
}
///
/// - The slot data.
///
/// DragonBones 3.0
/// en_US
///
/// - 插槽数据。
///
/// DragonBones 3.0
/// zh_CN
public class SlotData : BaseObject
{
///
///
public static readonly ColorTransform DEFAULT_COLOR = new ColorTransform();
///
///
public static ColorTransform CreateColor()
{
return new ColorTransform();
}
///
public BlendMode blendMode;
///
public int displayIndex;
///
public int zOrder;
///
/// - The slot name.
///
/// DragonBones 3.0
/// en_US
///
/// - 插槽名称。
///
/// DragonBones 3.0
/// zh_CN
public string name;
///
public ColorTransform color = null; // Initial value.
///
public UserData userData = null; // Initial value.
///
/// - The parent bone data.
///
/// DragonBones 3.0
/// en_US
///
/// - 父骨骼数据。
///
/// DragonBones 3.0
/// zh_CN
public BoneData parent;
///
protected override void _OnClear()
{
if (this.userData != null)
{
this.userData.ReturnToPool();
}
this.blendMode = BlendMode.Normal;
this.displayIndex = 0;
this.zOrder = 0;
this.name = "";
this.color = null; //
this.userData = null;
this.parent = null; //
}
}
}