axmol/extensions/cocostudio/WidgetReader/ParticleReader/ParticleReader.cpp

225 lines
7.6 KiB
C++
Raw Normal View History

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.
****************************************************************************/
#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"
#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;
}
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)
{
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();
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());
}
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
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:
{
if (FileUtils::getInstance()->isFileExist(path))
2014-11-21 15:15:38 +08:00
{
fileExist = true;
}
else
{
errorFilePath = path;
fileExist = false;
2014-11-21 15:15:38 +08:00
}
break;
}
default:
break;
}
if (fileExist)
{
2020-08-04 00:14:35 +08:00
particle = ParticleSystemQuad::create(path);
if (particle)
{
setPropsWithFlatBuffers(particle, (Table*)particleOptions);
particle->setPositionType(ParticleSystem::PositionType::GROUPED);
}
}
else
{
2020-08-04 00:14:35 +08:00
Node* node = Node::create();
setPropsWithFlatBuffers(node, (Table*)particleOptions);
return node;
}
2014-11-21 15:15:38 +08:00
return particle;
}
}