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 "SpriteReader.h"
|
|
|
|
|
|
|
|
#include "cocostudio/CSParseBinary_generated.h"
|
2014-11-21 18:08:09 +08:00
|
|
|
#include "cocostudio/FlatBuffersSerialize.h"
|
|
|
|
#include "cocostudio/WidgetReader/NodeReader/NodeReader.h"
|
2014-11-21 15:15:38 +08:00
|
|
|
|
2014-12-14 17:14:16 +08:00
|
|
|
#include "tinyxml2.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(SpriteReader)
|
|
|
|
|
|
|
|
SpriteReader::SpriteReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SpriteReader::~SpriteReader()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static SpriteReader* _instanceSpriteReader = nullptr;
|
|
|
|
|
|
|
|
SpriteReader* SpriteReader::getInstance()
|
|
|
|
{
|
|
|
|
if (!_instanceSpriteReader)
|
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
_instanceSpriteReader = new (std::nothrow) SpriteReader();
|
2014-11-21 15:15:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _instanceSpriteReader;
|
|
|
|
}
|
|
|
|
|
2015-03-31 14:56:24 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Offset<Table> SpriteReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *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;
|
|
|
|
|
2015-04-08 16:54:58 +08:00
|
|
|
cocos2d::BlendFunc blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
// FileData
|
|
|
|
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
|
|
|
|
while (child)
|
|
|
|
{
|
|
|
|
std::string name = child->Name();
|
|
|
|
|
|
|
|
if (name == "FileData")
|
|
|
|
{
|
|
|
|
std::string texture = "";
|
|
|
|
std::string texturePng = "";
|
|
|
|
|
|
|
|
const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
|
|
|
|
|
|
|
|
while (attribute)
|
|
|
|
{
|
|
|
|
name = attribute->Name();
|
|
|
|
std::string value = attribute->Value();
|
|
|
|
|
|
|
|
if (name == "Path")
|
|
|
|
{
|
|
|
|
path = value;
|
|
|
|
}
|
|
|
|
else if (name == "Type")
|
|
|
|
{
|
|
|
|
resourceType = getResourceType(value);
|
|
|
|
}
|
|
|
|
else if (name == "Plist")
|
|
|
|
{
|
|
|
|
plistFile = value;
|
|
|
|
texture = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
attribute = attribute->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
{
|
|
|
|
const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
|
|
|
|
|
|
|
|
while (attribute)
|
|
|
|
{
|
|
|
|
name = attribute->Name();
|
|
|
|
std::string value = attribute->Value();
|
|
|
|
|
|
|
|
if (name == "Src")
|
|
|
|
{
|
|
|
|
blendFunc.src = atoi(value.c_str());
|
|
|
|
}
|
|
|
|
else if (name == "Dst")
|
|
|
|
{
|
|
|
|
blendFunc.dst = atoi(value.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
attribute = attribute->Next();
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
child = child->NextSiblingElement();
|
|
|
|
}
|
2015-04-08 16:54:58 +08:00
|
|
|
|
|
|
|
flatbuffers::BlendFunc f_blendFunc(blendFunc.src, 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),
|
|
|
|
&f_blendFunc);
|
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;
|
|
|
|
|
2015-08-20 11:27:01 +08:00
|
|
|
auto nodeReader = NodeReader::getInstance();
|
|
|
|
nodeReader->setPropsWithFlatBuffers(node, (Table*)(options->nodeOptions()));
|
2014-11-21 15:15:38 +08:00
|
|
|
|
|
|
|
auto fileNameData = options->fileNameData();
|
|
|
|
|
|
|
|
int resourceType = fileNameData->resourceType();
|
2014-12-17 19:00:24 +08:00
|
|
|
std::string path = fileNameData->path()->c_str();
|
|
|
|
|
|
|
|
bool fileExist = false;
|
|
|
|
std::string errorFilePath = "";
|
|
|
|
|
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
|
|
|
{
|
|
|
|
sprite->setTexture(path);
|
2014-12-17 19:00:24 +08:00
|
|
|
fileExist = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errorFilePath = path;
|
|
|
|
fileExist = false;
|
2014-11-21 15:15:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
2014-12-17 19:00:24 +08:00
|
|
|
std::string plist = fileNameData->plistFile()->c_str();
|
|
|
|
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
|
|
|
|
if (spriteFrame)
|
|
|
|
{
|
|
|
|
sprite->setSpriteFrame(spriteFrame);
|
|
|
|
fileExist = true;
|
|
|
|
}
|
|
|
|
else
|
2014-11-21 15:15:38 +08:00
|
|
|
{
|
2014-12-17 19:00:24 +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;
|
|
|
|
}
|
|
|
|
fileExist = false;
|
2014-11-21 15:15:38 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-05-22 18:21:59 +08:00
|
|
|
//if (!fileExist)
|
|
|
|
//{
|
|
|
|
// auto label = Label::create();
|
|
|
|
// label->setString(__String::createWithFormat("%s missed", errorFilePath.c_str())->getCString());
|
|
|
|
// sprite->addChild(label);
|
|
|
|
//}
|
2014-11-21 15:15:38 +08:00
|
|
|
|
2015-04-08 16:54:58 +08:00
|
|
|
auto f_blendFunc = options->blendFunc();
|
|
|
|
if (f_blendFunc)
|
|
|
|
{
|
|
|
|
cocos2d::BlendFunc blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
blendFunc.src = f_blendFunc->src();
|
|
|
|
blendFunc.dst = f_blendFunc->dst();
|
|
|
|
sprite->setBlendFunc(blendFunc);
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:15:38 +08:00
|
|
|
auto nodeOptions = options->nodeOptions();
|
|
|
|
|
|
|
|
GLubyte alpha = (GLubyte)nodeOptions->color()->a();
|
|
|
|
GLubyte red = (GLubyte)nodeOptions->color()->r();
|
|
|
|
GLubyte green = (GLubyte)nodeOptions->color()->g();
|
|
|
|
GLubyte blue = (GLubyte)nodeOptions->color()->b();
|
|
|
|
|
|
|
|
if (alpha != 255)
|
|
|
|
{
|
|
|
|
sprite->setOpacity(alpha);
|
|
|
|
}
|
|
|
|
if (red != 255 || green != 255 || blue != 255)
|
|
|
|
{
|
|
|
|
sprite->setColor(Color3B(red, green, blue));
|
|
|
|
}
|
|
|
|
|
2015-01-23 09:02:33 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-11-26 11:50:42 +08:00
|
|
|
Node* SpriteReader::createNodeWithFlatBuffers(const flatbuffers::Table *spriteOptions)
|
2014-11-21 15:15:38 +08:00
|
|
|
{
|
|
|
|
Sprite* sprite = Sprite::create();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-12-14 17:14:16 +08:00
|
|
|
}
|