axmol/extensions/cocostudio/WidgetReader/SpriteReader/SpriteReader.cpp

373 lines
12 KiB
C++
Raw Normal View History

2014-11-21 15:15:38 +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.
2014-11-21 15:15:38 +08:00
http://www.cocos2d-x.org
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/SpriteReader/SpriteReader.h"
2014-11-21 15:15:38 +08:00
2019-11-25 01:35:26 +08:00
#include "base/ccUtils.h"
2016-04-18 15:09:21 +08:00
#include "2d/CCSprite.h"
#include "2d/CCSpriteFrameCache.h"
#include "platform/CCFileUtils.h"
#include "CSParseBinary_generated.h"
#include "FlatBuffersSerialize.h"
#include "WidgetReader/NodeReader/NodeReader.h"
2014-11-21 15:15:38 +08:00
2016-04-18 15:09:21 +08:00
2014-11-21 15:15:38 +08:00
#include "flatbuffers/flatbuffers.h"
2020-08-04 00:14:35 +08:00
#include "renderer/CCColorizer.h"
2014-11-21 15:15:38 +08:00
USING_NS_CC;
using namespace flatbuffers;
namespace cocostudio
{
IMPLEMENT_CLASS_NODE_READER_INFO(SpriteReader)
SpriteReader::SpriteReader()
{
}
SpriteReader::~SpriteReader()
{
}
static SpriteReader* _instanceSpriteReader = nullptr;
SpriteReader* SpriteReader::getInstance()
{
if (!_instanceSpriteReader)
{
_instanceSpriteReader = new (std::nothrow) SpriteReader();
2014-11-21 15:15:38 +08:00
}
return _instanceSpriteReader;
}
void SpriteReader::purge()
{
CC_SAFE_DELETE(_instanceSpriteReader);
}
2015-03-30 16:46:33 +08:00
void SpriteReader::destroyInstance()
2014-11-21 15:15:38 +08:00
{
CC_SAFE_DELETE(_instanceSpriteReader);
}
2019-11-24 23:15:56 +08:00
Offset<Table> SpriteReader::createOptionsWithFlatBuffers(pugi::xml_node objectData,
2014-11-21 15:15:38 +08:00
flatbuffers::FlatBufferBuilder *builder)
{
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
2020-08-04 10:55:30 +08:00
std::string path;
std::string plistFile;
2014-11-21 15:15:38 +08:00
int resourceType = 0;
2019-11-24 23:15:56 +08:00
bool intelliShadingEnabled = false;
cocos2d::Vec3 hsv;
cocos2d::Vec3 filter;
2014-11-21 15:15:38 +08:00
2015-04-08 16:54:58 +08:00
cocos2d::BlendFunc blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED;
2019-11-24 23:15:56 +08:00
// attributes
auto attribute = objectData.first_attribute();
while (attribute)
{
std::string attriname = attribute.name();
std::string value = attribute.value();
if (attriname == "IntelliShadingEnabled")
{
intelliShadingEnabled = (value == "True") ? true : false;
break;
}
attribute = attribute.next_attribute();
}
2014-11-21 15:15:38 +08:00
// FileData
2019-11-24 23:15:56 +08:00
auto child = objectData.first_child();
2014-11-21 15:15:38 +08:00
while (child)
{
2019-11-24 23:15:56 +08:00
std::string name = child.name();
pugi::xml_attribute attribute;
2014-11-21 15:15:38 +08:00
if (name == "FileData")
{
2020-08-04 10:55:30 +08:00
std::string texture;
std::string texturePng;
2014-11-21 15:15:38 +08:00
2019-11-24 23:15:56 +08:00
attribute = child.first_attribute();
2014-11-21 15:15:38 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
name = attribute.name();
std::string value = attribute.value();
2014-11-21 15:15:38 +08:00
if (name == "Path")
{
path = value;
}
else if (name == "Type")
{
resourceType = getResourceType(value);
}
else if (name == "Plist")
{
plistFile = value;
texture = value;
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +08:00
}
if (resourceType == 1)
{
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
2014-12-11 19:15:04 +08:00
fbs->_textures.push_back(builder->CreateString(texture));
2014-11-21 15:15:38 +08:00
}
}
2015-04-08 16:54:58 +08:00
else if (name == "BlendFunc")
{
2019-11-24 23:15:56 +08:00
attribute = child.first_attribute();
2015-04-08 16:54:58 +08:00
while (attribute)
{
2019-11-24 23:15:56 +08:00
name = attribute.name();
std::string value = attribute.value();
2015-04-08 16:54:58 +08:00
if (name == "Src")
{
2019-11-25 01:35:26 +08:00
blendFunc.src = utils::toBackendBlendFactor(atoi(value.c_str()));
2015-04-08 16:54:58 +08:00
}
else if (name == "Dst")
{
2019-11-25 01:35:26 +08:00
blendFunc.dst = utils::toBackendBlendFactor(atoi(value.c_str()));
2015-04-08 16:54:58 +08:00
}
2019-11-24 23:15:56 +08:00
attribute = attribute.next_attribute();
}
}
else if (name == "HSV") {
attribute = child.first_attribute();
while (attribute)
{
name = attribute.name();
std::string value = attribute.value();
if (name == "X")
{
hsv.x = atof(value.c_str());
}
else if (name == "Y")
{
hsv.y = atof(value.c_str());
}
else if (name == "Z")
{
hsv.z = atof(value.c_str());
}
attribute = attribute.next_attribute();
}
}
else if (name == "FilterRGB") {
attribute = child.first_attribute();
while (attribute)
{
name = attribute.name();
std::string value = attribute.value();
if (name == "X")
{
filter.x = atof(value.c_str());
}
else if (name == "Y")
{
filter.y = atof(value.c_str());
}
else if (name == "Z")
{
filter.z = atof(value.c_str());
}
attribute = attribute.next_attribute();
2015-04-08 16:54:58 +08:00
}
}
2014-11-21 15:15:38 +08:00
2019-11-24 23:15:56 +08:00
child = child.next_sibling();
2014-11-21 15:15:38 +08:00
}
2015-04-08 16:54:58 +08:00
2019-11-25 01:35:26 +08:00
flatbuffers::BlendFunc f_blendFunc(utils::toGLBlendFactor(blendFunc.src), utils::toGLBlendFactor(blendFunc.dst));
2014-11-21 15:15:38 +08:00
auto options = CreateSpriteOptions(*builder,
nodeOptions,
CreateResourceData(*builder,
builder->CreateString(path),
builder->CreateString(plistFile),
2015-04-08 16:54:58 +08:00
resourceType),
2019-11-24 23:15:56 +08:00
&f_blendFunc,
intelliShadingEnabled,
(const FVec3*)&hsv,
(const FVec3*)&filter);
2014-11-21 15:15:38 +08:00
return *(Offset<Table>*)(&options);
}
void SpriteReader::setPropsWithFlatBuffers(cocos2d::Node *node,
const flatbuffers::Table* spriteOptions)
{
Sprite *sprite = static_cast<Sprite*>(node);
auto options = (SpriteOptions*)spriteOptions;
auto nodeReader = NodeReader::getInstance();
nodeReader->setPropsWithFlatBuffers(node, (Table*)(options->nodeOptions()));
2014-11-21 15:15:38 +08:00
2020-08-04 10:55:30 +08:00
auto fileNameDataDic = (options->fileNameData());
2014-11-21 15:15:38 +08:00
2020-08-04 10:55:30 +08:00
int resourceType = fileNameDataDic->resourceType();
std::string path = fileNameDataDic->path()->c_str();
2020-08-04 10:55:30 +08:00
std::string errorFilePath;
2014-11-21 15:15:38 +08:00
switch (resourceType)
{
case 0:
{
if (FileUtils::getInstance()->isFileExist(path))
2014-11-21 15:15:38 +08:00
{
sprite->setTexture(path);
}
else
{
errorFilePath = path;
2014-11-21 15:15:38 +08:00
}
break;
}
case 1:
{
2020-08-04 10:55:30 +08:00
std::string plist = fileNameDataDic->plistFile()->c_str();
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
if (spriteFrame)
{
sprite->setSpriteFrame(spriteFrame);
}
else
2014-11-21 15:15:38 +08:00
{
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;
}
2014-11-21 15:15:38 +08:00
}
break;
}
default:
break;
}
2015-04-08 16:54:58 +08:00
auto f_blendFunc = options->blendFunc();
if (f_blendFunc)
{
cocos2d::BlendFunc blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED;
2019-11-25 01:35:26 +08:00
blendFunc.src = utils::toBackendBlendFactor(f_blendFunc->src());
blendFunc.dst = utils::toBackendBlendFactor(f_blendFunc->dst());
2015-04-08 16:54:58 +08:00
sprite->setBlendFunc(blendFunc);
}
2014-11-21 15:15:38 +08:00
auto nodeOptions = options->nodeOptions();
2019-11-25 01:35:26 +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();
2014-11-21 15:15:38 +08:00
if (alpha != 255)
{
sprite->setOpacity(alpha);
}
if (red != 255 || green != 255 || blue != 255)
{
sprite->setColor(Color3B(red, green, blue));
}
bool flipX = nodeOptions->flipX() != 0;
bool flipY = nodeOptions->flipY() != 0;
2014-11-21 15:15:38 +08:00
if(flipX != false)
sprite->setFlippedX(flipX);
if(flipY != false)
sprite->setFlippedY(flipY);
2019-11-24 23:15:56 +08:00
if (options->intelliShadingEnabled()) {
auto hsv = options->hsv();
auto filter = options->filter();
if (hsv != nullptr && filter != nullptr) {
2020-08-04 00:14:35 +08:00
Colorizer::enableNodeIntelliShading(sprite,
2019-11-24 23:15:56 +08:00
Vec3(hsv->x(), hsv->y(), hsv->z()),
Vec3(filter->x(), filter->y(), filter->z()));
}
}
2014-11-21 15:15:38 +08:00
}
Node* SpriteReader::createNodeWithFlatBuffers(const flatbuffers::Table *spriteOptions)
2014-11-21 15:15:38 +08:00
{
2020-08-04 00:14:35 +08:00
Sprite* sprite = Sprite::create();
2014-11-21 15:15:38 +08:00
setPropsWithFlatBuffers(sprite, (Table*)spriteOptions);
return sprite;
}
int SpriteReader::getResourceType(std::string key)
{
if(key == "Normal" || key == "Default")
{
return 0;
}
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
if(fbs->_isSimulator)
{
if(key == "MarkedSubImage")
{
return 0;
}
}
return 1;
}
}