2021-12-26 23:26:34 +08:00
|
|
|
#include "ArmatureData.h"
|
2020-10-18 00:27:23 +08:00
|
|
|
#include "UserData.h"
|
|
|
|
#include "DragonBonesData.h"
|
|
|
|
#include "ConstraintData.h"
|
|
|
|
#include "CanvasData.h"
|
|
|
|
#include "SkinData.h"
|
|
|
|
#include "DisplayData.h"
|
|
|
|
#include "AnimationData.h"
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
void ArmatureData::_onClear()
|
|
|
|
{
|
|
|
|
for (const auto action : defaultActions)
|
|
|
|
{
|
|
|
|
action->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto action : actions)
|
|
|
|
{
|
|
|
|
action->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& pair : bones)
|
|
|
|
{
|
|
|
|
pair.second->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& pair : slots)
|
|
|
|
{
|
|
|
|
pair.second->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& pair : constraints)
|
|
|
|
{
|
|
|
|
pair.second->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& pair : skins)
|
|
|
|
{
|
|
|
|
pair.second->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& pair : animations)
|
|
|
|
{
|
|
|
|
pair.second->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canvas != nullptr)
|
|
|
|
{
|
|
|
|
canvas->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userData != nullptr)
|
|
|
|
{
|
|
|
|
userData->returnToPool();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
type = ArmatureType::Armature;
|
|
|
|
frameRate = 0;
|
2020-10-18 00:27:23 +08:00
|
|
|
cacheFrameRate = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
scale = 1.0f;
|
|
|
|
name = "";
|
2020-10-18 00:27:23 +08:00
|
|
|
aabb.clear();
|
|
|
|
animationNames.clear();
|
|
|
|
sortedBones.clear();
|
|
|
|
sortedSlots.clear();
|
|
|
|
defaultActions.clear();
|
|
|
|
actions.clear();
|
|
|
|
bones.clear();
|
|
|
|
slots.clear();
|
|
|
|
constraints.clear();
|
|
|
|
skins.clear();
|
|
|
|
animations.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
parent = nullptr;
|
|
|
|
defaultSkin = nullptr;
|
2020-10-18 00:27:23 +08:00
|
|
|
defaultAnimation = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
canvas = nullptr;
|
|
|
|
userData = nullptr;
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::sortBones()
|
|
|
|
{
|
|
|
|
const auto total = sortedBones.size();
|
|
|
|
if (total <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto sortHelper = sortedBones; // Copy.
|
|
|
|
unsigned index = 0;
|
|
|
|
unsigned count = 0;
|
2020-10-18 00:27:23 +08:00
|
|
|
sortedBones.clear();
|
|
|
|
while (count < total)
|
|
|
|
{
|
|
|
|
const auto bone = sortHelper[index++];
|
|
|
|
if (index >= total)
|
|
|
|
{
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::find(sortedBones.cbegin(), sortedBones.cend(), bone) != sortedBones.cend())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flag = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& pair : constraints)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto constrait = pair.second;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (constrait->root == bone &&
|
|
|
|
std::find(sortedBones.cbegin(), sortedBones.cend(), constrait->target) == sortedBones.cend())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
flag = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (flag)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bone->parent != nullptr &&
|
|
|
|
std::find(sortedBones.cbegin(), sortedBones.cend(), bone->parent) == sortedBones.cend())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sortedBones.push_back(bone);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::cacheFrames(unsigned value)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cacheFrameRate > value) // TODO clear cache.
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheFrameRate = value;
|
|
|
|
for (const auto& pair : animations)
|
|
|
|
{
|
|
|
|
pair.second->cacheFrames(cacheFrameRate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ArmatureData::setCacheFrame(const Matrix& globalTransformMatrix, const Transform& transform)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& dataArray = *&parent->cachedFrames;
|
2020-10-18 00:27:23 +08:00
|
|
|
auto arrayOffset = dataArray.size();
|
|
|
|
|
|
|
|
dataArray.resize(arrayOffset + 10);
|
2021-12-25 10:04:45 +08:00
|
|
|
dataArray[arrayOffset] = globalTransformMatrix.a;
|
2020-10-18 00:27:23 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::getCacheFrame(Matrix& globalTransformMatrix, Transform& transform, unsigned arrayOffset) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& dataArray = *&parent->cachedFrames;
|
|
|
|
globalTransformMatrix.a = dataArray[arrayOffset];
|
|
|
|
globalTransformMatrix.b = dataArray[arrayOffset + 1];
|
|
|
|
globalTransformMatrix.c = dataArray[arrayOffset + 2];
|
|
|
|
globalTransformMatrix.d = dataArray[arrayOffset + 3];
|
2020-10-18 00:27:23 +08:00
|
|
|
globalTransformMatrix.tx = dataArray[arrayOffset + 4];
|
|
|
|
globalTransformMatrix.ty = dataArray[arrayOffset + 5];
|
2021-12-25 10:04:45 +08:00
|
|
|
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;
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::addBone(BoneData* value)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bones.find(value->name) != bones.cend())
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
DRAGONBONES_ASSERT(false, "Same bone: " + value->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bones[value->name] = value;
|
|
|
|
sortedBones.push_back(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::addSlot(SlotData* value)
|
|
|
|
{
|
|
|
|
if (slots.find(value->name) != slots.cend())
|
|
|
|
{
|
|
|
|
DRAGONBONES_ASSERT(false, "Same slot: " + value->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
slots[value->name] = value;
|
|
|
|
sortedSlots.push_back(value);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ArmatureData::addConstraint(ConstraintData* value)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
if (constraints.find(value->name) != constraints.cend())
|
|
|
|
{
|
|
|
|
DRAGONBONES_ASSERT(false, "Same constaint: " + value->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
constraints[value->name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::addSkin(SkinData* value)
|
|
|
|
{
|
|
|
|
if (skins.find(value->name) != skins.cend())
|
|
|
|
{
|
|
|
|
DRAGONBONES_ASSERT(false, "Same skin: " + value->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
value->parent = this;
|
2020-10-18 00:27:23 +08:00
|
|
|
skins[value->name] = value;
|
|
|
|
|
|
|
|
if (defaultSkin == nullptr)
|
|
|
|
{
|
|
|
|
defaultSkin = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::addAnimation(AnimationData* value)
|
|
|
|
{
|
|
|
|
if (animations.find(value->name) != animations.cend())
|
|
|
|
{
|
|
|
|
DRAGONBONES_ASSERT(false, "Same animation: " + value->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
value->parent = this;
|
2020-10-18 00:27:23 +08:00
|
|
|
animations[value->name] = value;
|
|
|
|
animationNames.push_back(value->name);
|
|
|
|
if (defaultAnimation == nullptr)
|
|
|
|
{
|
|
|
|
defaultAnimation = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArmatureData::addAction(ActionData* value, bool isDefault)
|
|
|
|
{
|
|
|
|
if (isDefault)
|
|
|
|
{
|
|
|
|
defaultActions.push_back(value);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
actions.push_back(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
MeshDisplayData* ArmatureData::getMesh(std::string_view skinName,
|
|
|
|
std::string_view slotName,
|
|
|
|
std::string_view meshName) const
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto skin = getSkin(skinName);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (skin == nullptr)
|
|
|
|
{
|
2020-10-18 00:27:23 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<MeshDisplayData*>(skin->getDisplay(slotName, meshName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoneData::_onClear()
|
|
|
|
{
|
|
|
|
if (userData != nullptr)
|
|
|
|
{
|
|
|
|
userData->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
inheritTranslation = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
inheritRotation = false;
|
|
|
|
inheritScale = false;
|
|
|
|
inheritReflection = false;
|
|
|
|
length = 0.0f;
|
|
|
|
name = "";
|
2020-10-18 00:27:23 +08:00
|
|
|
transform.identity();
|
2021-12-25 10:04:45 +08:00
|
|
|
parent = nullptr;
|
2020-10-18 00:27:23 +08:00
|
|
|
userData = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColorTransform SlotData::DEFAULT_COLOR;
|
|
|
|
ColorTransform* SlotData::createColor()
|
|
|
|
{
|
|
|
|
return new ColorTransform();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlotData::_onClear()
|
|
|
|
{
|
|
|
|
if (userData != nullptr)
|
|
|
|
{
|
|
|
|
userData->returnToPool();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (color != nullptr && color != &DEFAULT_COLOR)
|
|
|
|
{
|
|
|
|
delete color;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
blendMode = BlendMode::Normal;
|
2020-10-18 00:27:23 +08:00
|
|
|
displayIndex = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
zOrder = 0;
|
|
|
|
name = "";
|
|
|
|
parent = nullptr;
|
|
|
|
color = nullptr;
|
|
|
|
userData = nullptr;
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_END
|