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)
|
|
|
|
->setRenderTexture(static_cast<cocos2d::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
|
|
|
{
|
|
|
|
const auto textureCache = cocos2d::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)
|
|
|
|
{
|
|
|
|
const auto defaultPixelFormat = cocos2d::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:
|
|
|
|
pixelFormat = cocos2d::backend::PixelFormat::RGBA8;
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::BGRA8888:
|
|
|
|
pixelFormat = cocos2d::backend::PixelFormat::BGRA8;
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGBA4444:
|
|
|
|
pixelFormat = cocos2d::backend::PixelFormat::RGBA4;
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGB888:
|
|
|
|
pixelFormat = cocos2d::backend::PixelFormat::RGB8;
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGB565:
|
|
|
|
pixelFormat = cocos2d::backend::PixelFormat::RGB565;
|
|
|
|
break;
|
2020-10-18 00:27:23 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case TextureFormat::RGBA5551:
|
|
|
|
pixelFormat = cocos2d::backend::PixelFormat::RGB5A1;
|
|
|
|
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:
|
|
|
|
pixelFormat = cocos2d::Texture2D::PixelFormat::RGBA8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::BGRA8888:
|
|
|
|
pixelFormat = cocos2d::Texture2D::PixelFormat::BGRA8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGBA4444:
|
|
|
|
pixelFormat = cocos2d::Texture2D::PixelFormat::RGBA4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGB888:
|
|
|
|
pixelFormat = cocos2d::Texture2D::PixelFormat::RGB8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGB565:
|
|
|
|
pixelFormat = cocos2d::Texture2D::PixelFormat::RGB565;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::RGBA5551:
|
|
|
|
pixelFormat = cocos2d::Texture2D::PixelFormat::RGB5A1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextureFormat::DEFAULT:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cocos2d::Texture2D::setDefaultAlphaPixelFormat(pixelFormat);
|
|
|
|
texture = textureCache->addImage(textureAtlasData->imagePath);
|
|
|
|
if (texture != nullptr)
|
|
|
|
{
|
|
|
|
cocos2d::Texture2D::setDefaultAlphaPixelFormat(defaultPixelFormat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
rawDisplay->setAnchorPoint(cocos2d::Vec2::ZERO);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto fullpath = cocos2d::FileUtils::getInstance()->fullPathForFilename(filePath);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (cocos2d::FileUtils::getInstance()->isFileExist(filePath))
|
2020-10-18 00:27:23 +08:00
|
|
|
{
|
|
|
|
const auto pos = fullpath.find(".json");
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
const auto data = cocos2d::FileUtils::getInstance()->getStringFromFile(filePath);
|
|
|
|
|
|
|
|
return parseDragonBonesData(data.c_str(), name, scale);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if COCOS2D_VERSION >= 0x00031200
|
|
|
|
cocos2d::Data cocos2dData;
|
|
|
|
cocos2d::FileUtils::getInstance()->getContents(fullpath, &cocos2dData);
|
|
|
|
#else
|
|
|
|
const auto cocos2dData = cocos2d::FileUtils::getInstance()->getDataFromFile(fullpath);
|
|
|
|
#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
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_prevPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(filePath);
|
2020-10-18 00:27:23 +08:00
|
|
|
const auto data = cocos2d::FileUtils::getInstance()->getStringFromFile(_prevPath);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
cocos2d::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)
|
|
|
|
{
|
|
|
|
const auto display = cocos2d::Sprite::createWithSpriteFrame(textureData->spriteFrame);
|
|
|
|
return display;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DRAGONBONES_NAMESPACE_END
|