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

215 lines
6.4 KiB
C++
Raw Normal View History

2014-11-21 15:15:38 +08:00
/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
2021-12-25 10:04:45 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2021-12-25 10:04:45 +08:00
2014-11-21 15:15:38 +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
2014-11-21 15:15:38 +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
2014-11-21 15:15:38 +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.
****************************************************************************/
#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_AX;
2014-11-21 15:15:38 +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)
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
_instanceParticleReader = new ParticleReader();
2014-11-21 15:15:38 +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-08-08 18:02:17 +08:00
ax::BlendFunc blendFunc = ax::BlendFunc::ALPHA_PREMULTIPLIED;
2021-12-25 10:04:45 +08:00
// child elements
auto child = objectData.first_child();
while (child)
2014-11-21 15:15:38 +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")
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
auto attribute = child.first_attribute();
while (attribute)
2014-11-21 15:15:38 +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")
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
path = value;
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Type")
2015-04-08 16:54:58 +08:00
{
2021-12-25 10:04:45 +08:00
resourceType = 0;
2015-04-08 16:54:58 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "Plist")
{
plistFile = value;
}
attribute = attribute.next_attribute();
2015-04-08 16:54:58 +08:00
}
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
else if (name == "BlendFunc")
2014-11-21 15:15:38 +08:00
{
2021-12-25 10:04:45 +08:00
auto attribute = child.first_attribute();
while (attribute)
2014-11-21 15:15:38 +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")
2014-11-21 15:15:38 +08:00
{
2021-12-31 15:49:45 +08:00
blendFunc.src = utils::toBackendBlendFactor(atoi(value.data()));
}
2021-12-25 10:04:45 +08:00
else if (name == "Dst")
{
2021-12-31 15:49:45 +08:00
blendFunc.dst = utils::toBackendBlendFactor(atoi(value.data()));
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
attribute = attribute.next_attribute();
2014-11-21 15:15:38 +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-08-08 18:02:17 +08:00
void ParticleReader::setPropsWithFlatBuffers(ax::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-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());
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))
{
2021-12-25 10:04:45 +08:00
fileExist = true;
}
else
{
2021-12-25 10:04:45 +08:00
errorFilePath = path;
fileExist = false;
}
2021-12-25 10:04:45 +08:00
break;
2014-11-21 15:15:38 +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;
2014-11-21 15:15:38 +08:00
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio