axmol/extensions/cocostudio/WidgetReader/SpineSkeletonReader/SpineSkeletonReader.cpp

268 lines
8.2 KiB
C++
Raw Normal View History

2019-11-24 23:15:56 +08:00
/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-24 23:15:56 +08:00
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.
****************************************************************************/
#include "WidgetReader/SpineSkeletonReader/SpineSkeletonReader.h"
2022-07-16 10:43:05 +08:00
#if defined(AX_BUILD_WITH_SPINE)
2021-12-25 10:04:45 +08:00
# include "SpineSkeletonDataCache.h"
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
# include "2d/CCSprite.h"
# include "2d/CCSpriteFrameCache.h"
# include "platform/CCFileUtils.h"
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
# include "CSParseBinary_generated.h"
# include "FlatBuffersSerialize.h"
# include "WidgetReader/NodeReader/NodeReader.h"
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
# include "flatbuffers/flatbuffers.h"
2019-11-24 23:15:56 +08:00
USING_NS_AX;
2019-11-24 23:15:56 +08:00
using namespace flatbuffers;
namespace cocostudio
{
2021-12-25 10:04:45 +08:00
IMPLEMENT_CLASS_NODE_READER_INFO(SpineSkeletonReader)
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
SpineSkeletonReader::SpineSkeletonReader() {}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
SpineSkeletonReader::~SpineSkeletonReader() {}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
static SpineSkeletonReader* _instanceSpriteReader = nullptr;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
SpineSkeletonReader* SpineSkeletonReader::getInstance()
{
if (!_instanceSpriteReader)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
_instanceSpriteReader = new SpineSkeletonReader();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return _instanceSpriteReader;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void SpineSkeletonReader::purge()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_instanceSpriteReader);
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void SpineSkeletonReader::destroyInstance()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_instanceSpriteReader);
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Offset<Table> SpineSkeletonReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
flatbuffers::FlatBufferBuilder* builder)
{
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string dataFile;
std::string atlasFile;
std::string animation;
std::string skin = "default";
bool loop = false;
float timeScale = 1.0f;
2019-11-24 23:15:56 +08:00
2022-08-08 18:02:17 +08:00
ax::BlendFunc blendFunc = ax::BlendFunc::ALPHA_PREMULTIPLIED;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// attributes
auto attribute = objectData.first_attribute();
while (attribute)
{
2021-12-31 15:49:45 +08:00
std::string_view name = attribute.name();
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (name == "DataFile")
{
dataFile = std::move(value);
}
else if (name == "AtlasFile")
{
atlasFile = std::move(value);
}
else if (name == "Animation")
{
animation = std::move(value);
}
else if (name == "Skin")
{
skin = std::move(value);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Loop")
{
loop = (value == "True" ? true : false);
}
else if (name == "TimeScale")
{
2021-12-31 15:49:45 +08:00
timeScale = atof(value.data());
2021-12-25 10:04:45 +08:00
}
attribute = attribute.next_attribute();
}
# if 0
2019-11-24 23:15:56 +08:00
// FileData
auto child = objectData.first_child();
while (child)
{
2021-12-31 15:49:45 +08:00
std::string_view name = child.name();
2019-11-24 23:15:56 +08:00
if (name == "DataFile")
{
2020-08-04 10:55:30 +08:00
std::string texture;
std::string texturePng;
2019-11-24 23:15:56 +08:00
auto attribute = child.first_attribute();
while (attribute)
{
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
if (name == "Path")
{
path = value;
}
else if (name == "Type")
{
resourceType = getResourceType(value);
}
else if (name == "Plist")
{
plistFile = value;
texture = value;
}
attribute = attribute.next_attribute();
}
if (resourceType == 1)
{
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
fbs->_textures.emplace_back(builder->CreateString(texture));
2019-11-24 23:15:56 +08:00
}
}
else if (name == "BlendFunc")
{
auto attribute = child.first_attribute();
while (attribute)
{
name = attribute.name();
2021-12-31 15:49:45 +08:00
std::string_view value = attribute.value();
2019-11-24 23:15:56 +08:00
if (name == "Src")
{
2021-12-31 15:49:45 +08:00
blendFunc.src = atoi(value.data());
2019-11-24 23:15:56 +08:00
}
else if (name == "Dst")
{
2021-12-31 15:49:45 +08:00
blendFunc.dst = atoi(value.data());
2019-11-24 23:15:56 +08:00
}
attribute = attribute.next_attribute();
}
}
child = child.next_sibling();
}
2021-12-25 10:04:45 +08:00
# endif
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
flatbuffers::BlendFunc f_blendFunc(utils::toGLBlendFactor(blendFunc.src), utils::toGLBlendFactor(blendFunc.dst));
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto options = CreateSpineSkeletonOptions(*builder, nodeOptions, builder->CreateString(dataFile),
builder->CreateString(atlasFile), builder->CreateString(animation),
builder->CreateString(skin), loop, timeScale);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return *(Offset<Table>*)(&options);
}
2019-11-24 23:15:56 +08:00
2022-08-08 18:02:17 +08:00
void SpineSkeletonReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* spriteOptions)
2021-12-25 10:04:45 +08:00
{
Node** ppResult = (Node**)(node);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto options = (SpineSkeletonOptions*)spriteOptions;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto sharedData = SpineSkeletonDataCache::getInstance()->addData(options->dataFile()->c_str(),
options->atlasFile()->c_str(), 1.0f /*TODO:*/);
auto implNode = spine::SkeletonAnimation::createWithData(sharedData->data);
implNode->setAnimation(0, options->animation()->c_str(), options->loop());
implNode->setSkin(options->animation()->c_str());
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
*ppResult = implNode;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
auto nodeReader = NodeReader::getInstance();
nodeReader->setPropsWithFlatBuffers(implNode, (Table*)(options->nodeOptions()));
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// set Node Options
auto nodeOptions = options->nodeOptions();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
uint8_t alpha = (uint8_t)nodeOptions->color()->a();
uint8_t red = (uint8_t)nodeOptions->color()->r();
uint8_t green = (uint8_t)nodeOptions->color()->g();
uint8_t blue = (uint8_t)nodeOptions->color()->b();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
implNode->setScaleX(nodeOptions->scale()->scaleX());
implNode->setScaleY(nodeOptions->scale()->scaleY());
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
implNode->setColor(Color3B(red, green, blue));
implNode->setOpacity(alpha);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/*bool flipX = nodeOptions->flipX() != 0;
bool flipY = nodeOptions->flipY() != 0;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (flipX != false)
sprite->setFlippedX(flipX);
if (flipY != false)
sprite->setFlippedY(flipY);*/
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Node* SpineSkeletonReader::createNodeWithFlatBuffers(const flatbuffers::Table* spriteOptions)
{
Node* node = nullptr;
// #Hack
setPropsWithFlatBuffers((Node*)(&node), (Table*)spriteOptions);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
return node;
}
2021-12-31 15:49:45 +08:00
int SpineSkeletonReader::getResourceType(std::string_view key)
2021-12-25 10:04:45 +08:00
{
if (key == "Normal" || key == "Default")
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
return 0;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
if (fbs->_isSimulator)
{
if (key == "MarkedSubImage")
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
return 0;
2019-11-24 23:15:56 +08:00
}
}
2021-12-25 10:04:45 +08:00
return 1;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio
#endif