2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
2019-11-25 01:35:26 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
Copyright (c) simdsoft.com, @HALX99.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "WidgetReader/SpriteReader/SpriteReader.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Utils.h"
|
|
|
|
#include "2d/Sprite.h"
|
|
|
|
#include "2d/SpriteFrameCache.h"
|
|
|
|
#include "platform/FileUtils.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CSParseBinary_generated.h"
|
|
|
|
#include "FlatBuffersSerialize.h"
|
|
|
|
#include "WidgetReader/NodeReader/NodeReader.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "flatbuffers/flatbuffers.h"
|
2023-08-31 17:57:36 +08:00
|
|
|
|
|
|
|
#include "renderer/backend/ProgramManager.h"
|
|
|
|
#include "renderer/backend/ProgramState.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
using namespace flatbuffers;
|
|
|
|
|
|
|
|
namespace cocostudio
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
IMPLEMENT_CLASS_NODE_READER_INFO(SpriteReader)
|
|
|
|
|
|
|
|
SpriteReader::SpriteReader() {}
|
|
|
|
|
|
|
|
SpriteReader::~SpriteReader() {}
|
|
|
|
|
|
|
|
static SpriteReader* _instanceSpriteReader = nullptr;
|
|
|
|
|
|
|
|
SpriteReader* SpriteReader::getInstance()
|
|
|
|
{
|
|
|
|
if (!_instanceSpriteReader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_instanceSpriteReader = new SpriteReader();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
return _instanceSpriteReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteReader::purge()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_instanceSpriteReader);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteReader::destroyInstance()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(_instanceSpriteReader);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Offset<Table> SpriteReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
|
|
|
|
flatbuffers::FlatBufferBuilder* builder)
|
|
|
|
{
|
|
|
|
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
|
|
|
|
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
std::string plistFile;
|
|
|
|
int resourceType = 0;
|
|
|
|
bool intelliShadingEnabled = false;
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::Vec3 hsv;
|
|
|
|
ax::Vec3 filter;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::BlendFunc blendFunc = ax::BlendFunc::ALPHA_PREMULTIPLIED;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// attributes
|
|
|
|
auto attribute = objectData.first_attribute();
|
|
|
|
while (attribute)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view attriname = attribute.name();
|
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (attriname == "IntelliShadingEnabled")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
intelliShadingEnabled = (value == "True") ? true : false;
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
attribute = attribute.next_attribute();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// FileData
|
|
|
|
auto child = objectData.first_child();
|
|
|
|
while (child)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view name = child.name();
|
2021-12-25 10:04:45 +08:00
|
|
|
pugi::xml_attribute attribute;
|
|
|
|
if (name == "FileData")
|
2019-11-24 23:15:56 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string texture;
|
|
|
|
std::string texturePng;
|
2019-11-24 23:15:56 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
attribute = child.first_attribute();
|
|
|
|
|
|
|
|
while (attribute)
|
2019-11-24 23:15:56 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
name = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +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();
|
2019-11-24 23:15:56 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (resourceType == 1)
|
|
|
|
{
|
|
|
|
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
|
2022-08-09 09:54:53 +08:00
|
|
|
fbs->_textures.emplace_back(builder->CreateString(texture));
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-24 23:15:56 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "BlendFunc")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
attribute = child.first_attribute();
|
|
|
|
while (attribute)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
name = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (name == "Src")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
blendFunc.src = utils::toBackendBlendFactor(atoi(value.data()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "Dst")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
blendFunc.dst = utils::toBackendBlendFactor(atoi(value.data()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
attribute = attribute.next_attribute();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else if (name == "HSV")
|
|
|
|
{
|
|
|
|
attribute = child.first_attribute();
|
|
|
|
while (attribute)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
name = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (name == "X")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
hsv.x = atof(value.data());
|
2019-11-24 23:15:56 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "Y")
|
2019-11-24 23:15:56 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
hsv.y = atof(value.data());
|
2019-11-24 23:15:56 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "Z")
|
2019-11-24 23:15:56 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
hsv.z = atof(value.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
attribute = attribute.next_attribute();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "FilterRGB")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
attribute = child.first_attribute();
|
|
|
|
while (attribute)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
name = attribute.name();
|
2021-12-31 15:49:45 +08:00
|
|
|
std::string_view value = attribute.value();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (name == "X")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
filter.x = atof(value.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "Y")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
filter.y = atof(value.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "Z")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 15:49:45 +08:00
|
|
|
filter.z = atof(value.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
attribute = attribute.next_attribute();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
child = child.next_sibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
flatbuffers::BlendFunc f_blendFunc(utils::toGLBlendFactor(blendFunc.src), utils::toGLBlendFactor(blendFunc.dst));
|
|
|
|
|
|
|
|
auto options = CreateSpriteOptions(
|
|
|
|
*builder, nodeOptions,
|
|
|
|
CreateResourceData(*builder, builder->CreateString(path), builder->CreateString(plistFile), resourceType),
|
|
|
|
&f_blendFunc, intelliShadingEnabled, (const FVec3*)&hsv, (const FVec3*)&filter);
|
|
|
|
|
|
|
|
return *(Offset<Table>*)(&options);
|
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
void SpriteReader::setPropsWithFlatBuffers(ax::Node* node, const flatbuffers::Table* spriteOptions)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
Sprite* sprite = static_cast<Sprite*>(node);
|
|
|
|
auto options = (SpriteOptions*)spriteOptions;
|
|
|
|
|
|
|
|
auto nodeReader = NodeReader::getInstance();
|
|
|
|
nodeReader->setPropsWithFlatBuffers(node, (Table*)(options->nodeOptions()));
|
|
|
|
|
|
|
|
auto fileNameDataDic = (options->fileNameData());
|
|
|
|
|
|
|
|
int resourceType = fileNameDataDic->resourceType();
|
|
|
|
std::string path = fileNameDataDic->path()->c_str();
|
|
|
|
|
|
|
|
std::string errorFilePath;
|
|
|
|
|
|
|
|
switch (resourceType)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
if (FileUtils::getInstance()->isFileExist(path))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
sprite->setTexture(path);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
errorFilePath = path;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
std::string plist = fileNameDataDic->plistFile()->c_str();
|
2022-08-03 09:53:53 +08:00
|
|
|
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->findFrame(path);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (spriteFrame)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
sprite->setSpriteFrame(spriteFrame);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (FileUtils::getInstance()->isFileExist(plist))
|
|
|
|
{
|
|
|
|
ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
|
|
|
|
ValueMap metadata = value["metadata"].asValueMap();
|
|
|
|
std::string textureFileName = metadata["textureFileName"].asString();
|
|
|
|
if (!FileUtils::getInstance()->isFileExist(textureFileName))
|
|
|
|
{
|
|
|
|
errorFilePath = textureFileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errorFilePath = plist;
|
2019-11-24 23:15:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto f_blendFunc = options->blendFunc();
|
|
|
|
if (f_blendFunc)
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::BlendFunc blendFunc = ax::BlendFunc::ALPHA_PREMULTIPLIED;
|
2021-12-25 10:04:45 +08:00
|
|
|
blendFunc.src = utils::toBackendBlendFactor(f_blendFunc->src());
|
|
|
|
blendFunc.dst = utils::toBackendBlendFactor(f_blendFunc->dst());
|
|
|
|
sprite->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nodeOptions = options->nodeOptions();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (alpha != 255)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
sprite->setOpacity(alpha);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (red != 255 || green != 255 || blue != 255)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
sprite->setColor(Color3B(red, green, blue));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool flipX = nodeOptions->flipX() != 0;
|
|
|
|
bool flipY = nodeOptions->flipY() != 0;
|
|
|
|
|
|
|
|
if (flipX != false)
|
|
|
|
sprite->setFlippedX(flipX);
|
|
|
|
if (flipY != false)
|
|
|
|
sprite->setFlippedY(flipY);
|
|
|
|
|
|
|
|
if (options->intelliShadingEnabled())
|
|
|
|
{
|
|
|
|
auto hsv = options->hsv();
|
|
|
|
auto filter = options->filter();
|
|
|
|
if (hsv != nullptr && filter != nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2023-08-31 17:57:36 +08:00
|
|
|
auto prog = ProgramManager::getInstance()->getBuiltinProgram(ProgramType::HSV);
|
|
|
|
auto ps = new backend::ProgramState(prog);
|
|
|
|
sprite->setProgramState(ps, true);
|
|
|
|
Vec3 axhsv{hsv->x(), hsv->y(), hsv->z()};
|
|
|
|
ps->setUniform(ps->getUniformLocation("u_hsv"), &axhsv, sizeof(axhsv));
|
|
|
|
ps->updateBatchId();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* SpriteReader::createNodeWithFlatBuffers(const flatbuffers::Table* spriteOptions)
|
|
|
|
{
|
|
|
|
Sprite* sprite = Sprite::create();
|
|
|
|
|
|
|
|
setPropsWithFlatBuffers(sprite, (Table*)spriteOptions);
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2021-12-31 15:49:45 +08:00
|
|
|
int SpriteReader::getResourceType(std::string_view key)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (key == "Normal" || key == "Default")
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
|
|
|
|
if (fbs->_isSimulator)
|
|
|
|
{
|
|
|
|
if (key == "MarkedSubImage")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|