2021-12-31 12:12:40 +08:00
|
|
|
#include "CCFactory.h"
|
2020-10-18 00:27:23 +08:00
|
|
|
#include "CCTextureAtlasData.h"
|
|
|
|
#include "CCArmatureDisplay.h"
|
|
|
|
#include "CCSlot.h"
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
DragonBones* CCFactory::_dragonBonesInstance = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
CCFactory* CCFactory::_factory = nullptr;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
TextureAtlasData* CCFactory::_buildTextureAtlasData(TextureAtlasData* textureAtlasData, void* textureAtlas) const
|
|
|
|
{
|
|
|
|
if (textureAtlasData != nullptr)
|
|
|
|
{
|
|
|
|
const auto pos = _prevPath.find_last_of("/");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto basePath = _prevPath.substr(0, pos + 1);
|
2020-10-18 00:27:23 +08:00
|
|
|
textureAtlasData->imagePath = basePath + textureAtlasData->imagePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textureAtlas != nullptr)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
static_cast<CCTextureAtlasData*>(textureAtlasData)
|
2022-07-11 17:50:21 +08:00
|
|
|
->setRenderTexture(static_cast<axis::Texture2D*>(textureAtlas));
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
const auto textureCache = axis::Director::getInstance()->getTextureCache();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto texture = textureCache->getTextureForKey(textureAtlasData->imagePath);
|
2020-10-18 00:27:23 +08:00
|
|
|
if (texture == nullptr)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
const auto defaultPixelFormat = axis::Texture2D::getDefaultAlphaPixelFormat();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto pixelFormat = defaultPixelFormat;
|
2020-10-18 00:27:23 +08:00
|
|
|
#if COCOS2D_VERSION >= 0x00040000
|
|
|
|
switch (textureAtlasData->format)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGBA8888:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::backend::PixelFormat::RGBA8;
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::BGRA8888:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::backend::PixelFormat::BGRA8;
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGBA4444:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::backend::PixelFormat::RGBA4;
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGB888:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::backend::PixelFormat::RGB8;
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGB565:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::backend::PixelFormat::RGB565;
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGBA5551:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::backend::PixelFormat::RGB5A1;
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::DEFAULT:
|
|
|
|
default:
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
switch (textureAtlasData->format)
|
|
|
|
{
|
|
|
|
case TextureFormat::RGBA8888:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::Texture2D::PixelFormat::RGBA8;
|
2020-10-18 00:27:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::BGRA8888:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::Texture2D::PixelFormat::BGRA8;
|
2020-10-18 00:27:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGBA4444:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::Texture2D::PixelFormat::RGBA4;
|
2020-10-18 00:27:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGB888:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::Texture2D::PixelFormat::RGB8;
|
2020-10-18 00:27:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGB565:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::Texture2D::PixelFormat::RGB565;
|
2020-10-18 00:27:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGBA5551:
|
2022-07-11 17:50:21 +08:00
|
|
|
pixelFormat = axis::Texture2D::PixelFormat::RGB5A1;
|
2020-10-18 00:27:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::DEFAULT:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-13 21:06:17 +08:00
|
|
|
texture = textureCache->addImage(textureAtlasData->imagePath, pixelFormat);
|
2020-10-18 00:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static_cast<CCTextureAtlasData*>(textureAtlasData)->setRenderTexture(texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textureAtlasData = BaseObject::borrowObject<CCTextureAtlasData>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return textureAtlasData;
|
|
|
|
}
|
|
|
|
|
|
|
|
Armature* CCFactory::_buildArmature(const BuildArmaturePackage& dataPackage) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto armature = BaseObject::borrowObject<Armature>();
|
2020-10-18 00:27:23 +08:00
|
|
|
const auto armatureDisplay = CCArmatureDisplay::create();
|
|
|
|
|
|
|
|
armatureDisplay->retain();
|
|
|
|
armatureDisplay->setCascadeOpacityEnabled(true);
|
|
|
|
armatureDisplay->setCascadeColorEnabled(true);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
armature->init(dataPackage.armature, armatureDisplay, armatureDisplay, _dragonBones);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
return armature;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slot* CCFactory::_buildSlot(const BuildArmaturePackage& dataPackage, const SlotData* slotData, Armature* armature) const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto slot = BaseObject::borrowObject<CCSlot>();
|
2020-10-18 00:27:23 +08:00
|
|
|
const auto rawDisplay = DBCCSprite::create();
|
|
|
|
|
|
|
|
rawDisplay->setCascadeOpacityEnabled(true);
|
|
|
|
rawDisplay->setCascadeColorEnabled(true);
|
2022-07-11 17:50:21 +08:00
|
|
|
rawDisplay->setAnchorPoint(axis::Vec2::ZERO);
|
2020-10-18 00:27:23 +08:00
|
|
|
rawDisplay->setLocalZOrder(slotData->zOrder);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
slot->init(slotData, armature, rawDisplay, rawDisplay);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
DragonBonesData* CCFactory::loadDragonBonesData(std::string_view filePath, std::string_view name, float scale)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
if (!name.empty())
|
|
|
|
{
|
|
|
|
const auto existedData = getDragonBonesData(name);
|
|
|
|
if (existedData)
|
|
|
|
{
|
|
|
|
return existedData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
const auto fullpath = axis::FileUtils::getInstance()->fullPathForFilename(filePath);
|
|
|
|
if (axis::FileUtils::getInstance()->isFileExist(filePath))
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto pos = fullpath.find(".json");
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
const auto data = axis::FileUtils::getInstance()->getStringFromFile(filePath);
|
2020-10-18 00:27:23 +08:00
|
|
|
|
|
|
|
return parseDragonBonesData(data.c_str(), name, scale);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if COCOS2D_VERSION >= 0x00031200
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Data cocos2dData;
|
|
|
|
axis::FileUtils::getInstance()->getContents(fullpath, &cocos2dData);
|
2020-10-18 00:27:23 +08:00
|
|
|
#else
|
2022-07-11 17:50:21 +08:00
|
|
|
const auto cocos2dData = axis::FileUtils::getInstance()->getDataFromFile(fullpath);
|
2020-10-18 00:27:23 +08:00
|
|
|
#endif
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto binary = (unsigned char*)malloc(sizeof(unsigned char) * cocos2dData.getSize());
|
2020-10-18 00:27:23 +08:00
|
|
|
memcpy(binary, cocos2dData.getBytes(), cocos2dData.getSize());
|
|
|
|
const auto data = parseDragonBonesData((char*)binary, name, scale);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
TextureAtlasData* CCFactory::loadTextureAtlasData(std::string_view filePath, std::string_view name, float scale)
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
_prevPath = axis::FileUtils::getInstance()->fullPathForFilename(filePath);
|
|
|
|
const auto data = axis::FileUtils::getInstance()->getStringFromFile(_prevPath);
|
2020-10-18 00:27:23 +08:00
|
|
|
if (data.empty())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<CCTextureAtlasData*>(BaseFactory::parseTextureAtlasData(data.c_str(), nullptr, name, scale));
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
CCArmatureDisplay* CCFactory::buildArmatureDisplay(std::string_view armatureName,
|
|
|
|
std::string_view dragonBonesName,
|
|
|
|
std::string_view skinName,
|
|
|
|
std::string_view textureAtlasName) const
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto armature = buildArmature(armatureName, dragonBonesName, skinName, textureAtlasName);
|
|
|
|
if (armature != nullptr)
|
|
|
|
{
|
|
|
|
_dragonBones->getClock()->add(armature);
|
|
|
|
|
|
|
|
return static_cast<CCArmatureDisplay*>(armature->getDisplay());
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::Sprite* CCFactory::getTextureDisplay(std::string_view textureName, std::string_view dragonBonesName) const
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto textureData = static_cast<CCTextureData*>(_getTextureData(dragonBonesName, textureName));
|
|
|
|
if (textureData != nullptr && textureData->spriteFrame != nullptr)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
const auto display = axis::Sprite::createWithSpriteFrame(textureData->spriteFrame);
|
2020-10-18 00:27:23 +08:00
|
|
|
return display;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_END
|