2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2014 cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-10 09:47:41 +08:00
|
|
|
https://axis-project.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/ParticleReader/ParticleReader.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "base/ccTypes.h"
|
2019-11-25 01:35:26 +08:00
|
|
|
#include "base/ccUtils.h"
|
2019-11-23 20:27:39 +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"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#include "flatbuffers/flatbuffers.h"
|
|
|
|
|
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(ParticleReader)
|
|
|
|
|
|
|
|
ParticleReader::ParticleReader() {}
|
|
|
|
|
|
|
|
ParticleReader::~ParticleReader() {}
|
|
|
|
|
|
|
|
static ParticleReader* _instanceParticleReader = nullptr;
|
|
|
|
|
|
|
|
ParticleReader* ParticleReader::getInstance()
|
|
|
|
{
|
|
|
|
if (!_instanceParticleReader)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_instanceParticleReader = new ParticleReader();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
return _instanceParticleReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleReader::purge()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(_instanceParticleReader);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleReader::destroyInstance()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(_instanceParticleReader);
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Offset<Table> ParticleReader::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;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::BlendFunc blendFunc = axis::BlendFunc::ALPHA_PREMULTIPLIED;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// child elements
|
|
|
|
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
|
|
|
|
|
|
|
if (name == "FileData")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto 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 == "Path")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
path = value;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "Type")
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
resourceType = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (name == "Plist")
|
|
|
|
{
|
|
|
|
plistFile = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
attribute = attribute.next_attribute();
|
2019-11-23 20:27:39 +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
|
|
|
auto 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
|
|
|
|
|
|
|
child = child.next_sibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
flatbuffers::BlendFunc f_blendFunc(utils::toGLBlendFactor(blendFunc.src), utils::toGLBlendFactor(blendFunc.dst));
|
|
|
|
|
|
|
|
auto options = CreateParticleSystemOptions(
|
|
|
|
*builder, nodeOptions,
|
|
|
|
CreateResourceData(*builder, builder->CreateString(path), builder->CreateString(plistFile), resourceType),
|
|
|
|
&f_blendFunc);
|
|
|
|
|
|
|
|
return *(Offset<Table>*)(&options);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void ParticleReader::setPropsWithFlatBuffers(axis::Node* node, const flatbuffers::Table* particleOptions)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
auto particle = dynamic_cast<ParticleSystemQuad*>(node);
|
|
|
|
auto options = (ParticleSystemOptions*)particleOptions;
|
|
|
|
|
|
|
|
auto f_blendFunc = options->blendFunc();
|
|
|
|
if (particle && f_blendFunc)
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::BlendFunc blendFunc = axis::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());
|
|
|
|
particle->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto nodeReader = NodeReader::getInstance();
|
|
|
|
nodeReader->setPropsWithFlatBuffers(node, (Table*)options->nodeOptions());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* ParticleReader::createNodeWithFlatBuffers(const flatbuffers::Table* particleOptions)
|
|
|
|
{
|
|
|
|
ParticleSystemQuad* particle = nullptr;
|
|
|
|
|
|
|
|
auto options = (ParticleSystemOptions*)particleOptions;
|
|
|
|
auto fileNameDataDic = (options->fileNameData());
|
|
|
|
|
|
|
|
bool fileExist = false;
|
|
|
|
std::string errorFilePath;
|
|
|
|
std::string path = fileNameDataDic->path()->c_str();
|
|
|
|
int resourceType = fileNameDataDic->resourceType();
|
|
|
|
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
|
|
|
fileExist = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
errorFilePath = path;
|
|
|
|
fileExist = false;
|
2019-11-23 20:27:39 +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;
|
|
|
|
}
|
|
|
|
if (fileExist)
|
|
|
|
{
|
|
|
|
particle = ParticleSystemQuad::create(path);
|
|
|
|
if (particle)
|
|
|
|
{
|
|
|
|
setPropsWithFlatBuffers(particle, (Table*)particleOptions);
|
|
|
|
particle->setPositionType(ParticleSystem::PositionType::GROUPED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Node* node = Node::create();
|
|
|
|
setPropsWithFlatBuffers(node, (Table*)particleOptions);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return particle;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
} // namespace cocostudio
|