2014-11-21 15:15:38 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "WidgetReader/ParticleReader/ParticleReader.h"
|
2014-11-21 15:15:38 +08:00
|
|
|
|
2016-04-18 15:09:21 +08:00
|
|
|
#include "base/ccTypes.h"
|
2019-11-25 01:35:26 +08:00
|
|
|
#include "base/ccUtils.h"
|
2016-04-18 15:09:21 +08:00
|
|
|
#include "2d/CCParticleSystemQuad.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CSParseBinary_generated.h"
|
|
|
|
#include "WidgetReader/NodeReader/NodeReader.h"
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
#include "flatbuffers/flatbuffers.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
using namespace flatbuffers;
|
|
|
|
|
|
|
|
namespace cocostudio
|
|
|
|
{
|
|
|
|
IMPLEMENT_CLASS_NODE_READER_INFO(ParticleReader)
|
|
|
|
|
|
|
|
ParticleReader::ParticleReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ParticleReader::~ParticleReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static ParticleReader* _instanceParticleReader = nullptr;
|
|
|
|
|
|
|
|
ParticleReader* ParticleReader::getInstance()
|
|
|
|
{
|
|
|
|
if (!_instanceParticleReader)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_instanceParticleReader = new ParticleReader();
|
2014-11-21 15:15:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _instanceParticleReader;
|
|
|
|
}
|
|
|
|
|
2015-03-31 14:56:24 +08:00
|
|
|
void ParticleReader::purge()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_instanceParticleReader);
|
|
|
|
}
|
|
|
|
|
2015-03-30 16:46:33 +08:00
|
|
|
void ParticleReader::destroyInstance()
|
2014-11-21 15:15:38 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_instanceParticleReader);
|
|
|
|
}
|
|
|
|
|
2019-11-24 23:15:56 +08:00
|
|
|
Offset<Table> ParticleReader::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;
|
|
|
|
|
2015-04-08 16:54:58 +08:00
|
|
|
cocos2d::BlendFunc blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
// child elements
|
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();
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
if (name == "FileData")
|
|
|
|
{
|
2019-11-24 23:15:56 +08:00
|
|
|
auto 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 = 0;
|
|
|
|
}
|
|
|
|
else if (name == "Plist")
|
|
|
|
{
|
|
|
|
plistFile = value;
|
|
|
|
}
|
|
|
|
|
2019-11-24 23:15:56 +08:00
|
|
|
attribute = attribute.next_attribute();
|
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
|
|
|
auto 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();
|
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
|
|
|
}
|
|
|
|
|
2019-11-25 01:35:26 +08:00
|
|
|
flatbuffers::BlendFunc f_blendFunc(utils::toGLBlendFactor(blendFunc.src), utils::toGLBlendFactor(blendFunc.dst));
|
2015-04-08 16:54:58 +08:00
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto options = CreateParticleSystemOptions(*builder,
|
|
|
|
nodeOptions,
|
|
|
|
CreateResourceData(*builder,
|
|
|
|
builder->CreateString(path),
|
|
|
|
builder->CreateString(plistFile),
|
2015-04-08 16:54:58 +08:00
|
|
|
resourceType),
|
|
|
|
&f_blendFunc);
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
return *(Offset<Table>*)(&options);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleReader::setPropsWithFlatBuffers(cocos2d::Node *node,
|
|
|
|
const flatbuffers::Table *particleOptions)
|
|
|
|
{
|
2015-07-08 16:13:38 +08:00
|
|
|
auto particle = dynamic_cast<ParticleSystemQuad*>(node);
|
2014-11-21 15:15:38 +08:00
|
|
|
auto options = (ParticleSystemOptions*)particleOptions;
|
2015-04-08 16:54:58 +08:00
|
|
|
|
|
|
|
auto f_blendFunc = options->blendFunc();
|
2015-07-08 16:13:38 +08:00
|
|
|
if (particle && f_blendFunc)
|
2015-04-08 16:54:58 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
particle->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto nodeReader = NodeReader::getInstance();
|
|
|
|
nodeReader->setPropsWithFlatBuffers(node, (Table*)options->nodeOptions());
|
|
|
|
}
|
|
|
|
|
2014-11-26 11:50:42 +08:00
|
|
|
Node* ParticleReader::createNodeWithFlatBuffers(const flatbuffers::Table *particleOptions)
|
2014-11-21 15:15:38 +08:00
|
|
|
{
|
|
|
|
ParticleSystemQuad* particle = nullptr;
|
|
|
|
|
|
|
|
auto options = (ParticleSystemOptions*)particleOptions;
|
2020-08-04 10:55:30 +08:00
|
|
|
auto fileNameDataDic = (options->fileNameData());
|
2014-11-21 15:15:38 +08:00
|
|
|
|
2014-12-17 19:00:24 +08:00
|
|
|
bool fileExist = false;
|
2020-08-04 10:55:30 +08:00
|
|
|
std::string errorFilePath;
|
|
|
|
std::string path = fileNameDataDic->path()->c_str();
|
|
|
|
int resourceType = fileNameDataDic->resourceType();
|
2014-11-21 15:15:38 +08:00
|
|
|
switch (resourceType)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
2014-12-17 19:00:24 +08:00
|
|
|
if (FileUtils::getInstance()->isFileExist(path))
|
2014-11-21 15:15:38 +08:00
|
|
|
{
|
2014-12-17 19:00:24 +08:00
|
|
|
fileExist = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errorFilePath = path;
|
|
|
|
fileExist = false;
|
2014-11-21 15:15:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-12-17 19:00:24 +08:00
|
|
|
if (fileExist)
|
|
|
|
{
|
2020-08-04 00:14:35 +08:00
|
|
|
particle = ParticleSystemQuad::create(path);
|
2014-12-17 19:00:24 +08:00
|
|
|
if (particle)
|
|
|
|
{
|
|
|
|
setPropsWithFlatBuffers(particle, (Table*)particleOptions);
|
2015-01-14 10:24:44 +08:00
|
|
|
particle->setPositionType(ParticleSystem::PositionType::GROUPED);
|
2014-12-17 19:00:24 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-08 16:13:38 +08:00
|
|
|
else
|
|
|
|
{
|
2020-08-04 00:14:35 +08:00
|
|
|
Node* node = Node::create();
|
2015-07-08 16:13:38 +08:00
|
|
|
setPropsWithFlatBuffers(node, (Table*)particleOptions);
|
|
|
|
return node;
|
|
|
|
}
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
return particle;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|