mirror of https://github.com/axmolengine/axmol.git
Add interface that create node with flat buffers object for simulator
This commit is contained in:
parent
a76e2e430d
commit
c3365a4654
|
@ -37,6 +37,8 @@ THE SOFTWARE.
|
|||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#include "cocostudio/FlatBuffersSerialize.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
using namespace cocos2d;
|
||||
|
@ -690,6 +692,41 @@ Frame* ActionTimelineCache::loadTextureFrameWithFlatBuffers(const flatbuffers::T
|
|||
|
||||
return frame;
|
||||
}
|
||||
|
||||
ActionTimeline* ActionTimelineCache::createActionWithFlatBuffersForSimulator(const std::string& fileName)
|
||||
{
|
||||
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
|
||||
fbs->_isSimulator = true;
|
||||
auto builder = fbs->createFlatBuffersWithXMLFileForSimulator(fileName);
|
||||
|
||||
ActionTimeline* action = ActionTimeline::create();
|
||||
|
||||
auto csparsebinary = GetCSParseBinary(builder->GetBufferPointer());
|
||||
auto nodeAction = csparsebinary->action();
|
||||
|
||||
action = ActionTimeline::create();
|
||||
|
||||
int duration = nodeAction->duration();
|
||||
action->setDuration(duration);
|
||||
|
||||
float speed = nodeAction->speed();
|
||||
action->setTimeSpeed(speed);
|
||||
|
||||
auto timeLines = nodeAction->timeLines();
|
||||
int timelineLength = timeLines->size();
|
||||
for (int i = 0; i < timelineLength; i++)
|
||||
{
|
||||
auto timelineFlatBuf = timeLines->Get(i);
|
||||
Timeline* timeline = loadTimelineWithFlatBuffers(timelineFlatBuf);
|
||||
|
||||
if (timeline)
|
||||
action->addTimeline(timeline);
|
||||
}
|
||||
|
||||
fbs->deleteFlatBufferBuilder();
|
||||
|
||||
return action;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,6 +79,8 @@ public:
|
|||
ActionTimeline* createActionWithFlatBuffersFile(const std::string& fileName);
|
||||
ActionTimeline* loadAnimationActionWithFlatBuffersFile(const std::string& fileName);
|
||||
|
||||
ActionTimeline* createActionWithFlatBuffersForSimulator(const std::string& fileName);
|
||||
|
||||
protected:
|
||||
|
||||
Timeline* loadTimeline(const rapidjson::Value& json);
|
||||
|
|
|
@ -61,6 +61,8 @@
|
|||
#include "flatbuffers/flatbuffers.h"
|
||||
#include "flatbuffers/util.h"
|
||||
|
||||
#include "cocostudio/FlatBuffersSerialize.h"
|
||||
|
||||
#include "cocostudio/WidgetCallBackHandlerProtocol.h"
|
||||
|
||||
#include <fstream>
|
||||
|
@ -1060,4 +1062,139 @@ void CSLoader::registReaderObject(const std::string &className,
|
|||
ObjectFactory::getInstance()->registerType(t);
|
||||
}
|
||||
|
||||
Node* CSLoader::createNodeWithFlatBuffersForSimulator(const std::string& filename)
|
||||
{
|
||||
FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
|
||||
fbs->_isSimulator = true;
|
||||
FlatBufferBuilder* builder = fbs->createFlatBuffersWithXMLFileForSimulator(filename);
|
||||
|
||||
auto csparsebinary = GetCSParseBinary(builder->GetBufferPointer());
|
||||
|
||||
// decode plist
|
||||
auto textures = csparsebinary->textures();
|
||||
auto texturePngs = csparsebinary->texturePngs();
|
||||
int textureSize = csparsebinary->textures()->size();
|
||||
// CCLOG("textureSize = %d", textureSize);
|
||||
for (int i = 0; i < textureSize; ++i)
|
||||
{
|
||||
std::string texture = textures->Get(i)->c_str();
|
||||
std::string texturePng = texturePngs->Get(i)->c_str();
|
||||
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(texture,
|
||||
texturePng);
|
||||
}
|
||||
|
||||
auto nodeTree = csparsebinary->nodeTree();
|
||||
Node* node = nodeWithFlatBuffersForSimulator(nodeTree);
|
||||
|
||||
_rootNode = nullptr;
|
||||
|
||||
fbs->deleteFlatBufferBuilder();
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* CSLoader::nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree *nodetree)
|
||||
{
|
||||
Node* node = nullptr;
|
||||
|
||||
std::string classname = nodetree->classname()->c_str();
|
||||
CCLOG("classname = %s", classname.c_str());
|
||||
|
||||
auto options = nodetree->options();
|
||||
|
||||
if (classname == "ProjectNode")
|
||||
{
|
||||
auto reader = ProjectNodeReader::getInstance();
|
||||
auto projectNodeOptions = (ProjectNodeOptions*)options->data();
|
||||
std::string filePath = projectNodeOptions->fileName()->c_str();
|
||||
CCLOG("filePath = %s", filePath.c_str());
|
||||
|
||||
if (filePath != "" && FileUtils::getInstance()->isFileExist(filePath))
|
||||
{
|
||||
node = createNodeWithFlatBuffersForSimulator(filePath);
|
||||
reader->setPropsWithFlatBuffers(node, options->data());
|
||||
|
||||
cocostudio::timeline::ActionTimeline* action = cocostudio::timeline::ActionTimelineCache::getInstance()->createActionWithFlatBuffersForSimulator(filePath);
|
||||
if (action)
|
||||
{
|
||||
node->runAction(action);
|
||||
action->gotoFrameAndPlay(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (classname == "SimpleAudio")
|
||||
{
|
||||
node = Node::create();
|
||||
auto reader = ComAudioReader::getInstance();
|
||||
Component* component = reader->createComAudioWithFlatBuffers(options->data());
|
||||
if (component)
|
||||
{
|
||||
node->addComponent(component);
|
||||
reader->setPropsWithFlatBuffers(node, options->data());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string readername = getGUIClassName(classname);
|
||||
readername.append("Reader");
|
||||
|
||||
NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
|
||||
node = reader->createNodeWithFlatBuffers(options->data());
|
||||
|
||||
Widget* widget = dynamic_cast<Widget*>(node);
|
||||
if (widget)
|
||||
{
|
||||
std::string callbackName = widget->getCallbackName();
|
||||
std::string callbackType = widget->getCallbackType();
|
||||
|
||||
bindCallback(callbackName, callbackType, widget, _rootNode);
|
||||
}
|
||||
|
||||
if (_rootNode == nullptr)
|
||||
{
|
||||
_rootNode = node;
|
||||
}
|
||||
// _loadingNodeParentHierarchy.push_back(node);
|
||||
}
|
||||
|
||||
auto children = nodetree->children();
|
||||
int size = children->size();
|
||||
CCLOG("size = %d", size);
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
auto subNodeTree = children->Get(i);
|
||||
Node* child = nodeWithFlatBuffersForSimulator(subNodeTree);
|
||||
CCLOG("child = %p", child);
|
||||
if (child)
|
||||
{
|
||||
PageView* pageView = dynamic_cast<PageView*>(node);
|
||||
ListView* listView = dynamic_cast<ListView*>(node);
|
||||
if (pageView)
|
||||
{
|
||||
Layout* layout = dynamic_cast<Layout*>(child);
|
||||
if (layout)
|
||||
{
|
||||
pageView->addPage(layout);
|
||||
}
|
||||
}
|
||||
else if (listView)
|
||||
{
|
||||
Widget* widget = dynamic_cast<Widget*>(child);
|
||||
if (widget)
|
||||
{
|
||||
listView->pushBackCustomItem(widget);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
node->addChild(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// _loadingNodeParentHierarchy.pop_back();
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -98,6 +98,9 @@ public:
|
|||
void registReaderObject(const std::string& className,
|
||||
ObjectFactory::Instance ins);
|
||||
|
||||
cocos2d::Node* createNodeWithFlatBuffersForSimulator(const std::string& filename);
|
||||
cocos2d::Node* nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree* nodetree);
|
||||
|
||||
protected:
|
||||
|
||||
cocos2d::Node* loadNode(const rapidjson::Value& json);
|
||||
|
|
|
@ -369,6 +369,259 @@ Offset<NodeTree> FlatBuffersSerialize::createNodeTree(const tinyxml2::XMLElement
|
|||
options,
|
||||
_builder->CreateString(customClassName));
|
||||
}
|
||||
|
||||
FlatBufferBuilder* FlatBuffersSerialize::createFlatBuffersWithXMLFileForSimulator(const std::string &xmlFileName)
|
||||
{
|
||||
std::string inFullpath = FileUtils::getInstance()->fullPathForFilename(xmlFileName).c_str();
|
||||
|
||||
// xml read
|
||||
if (!FileUtils::getInstance()->isFileExist(inFullpath))
|
||||
{
|
||||
CCLOG(".csd file doesn not exists ");
|
||||
}
|
||||
|
||||
ssize_t size;
|
||||
std::string content =(char*)FileUtils::getInstance()->getFileData(inFullpath, "r", &size);
|
||||
|
||||
// xml parse
|
||||
tinyxml2::XMLDocument* document = new tinyxml2::XMLDocument();
|
||||
document->Parse(content.c_str());
|
||||
|
||||
const tinyxml2::XMLElement* rootElement = document->RootElement();// Root
|
||||
CCLOG("rootElement name = %s", rootElement->Name());
|
||||
|
||||
const tinyxml2::XMLElement* element = rootElement->FirstChildElement();
|
||||
|
||||
bool serializeEnabled = false;
|
||||
std::string rootType = "";
|
||||
|
||||
while (element)
|
||||
{
|
||||
CCLOG("entity name = %s", element->Name());
|
||||
|
||||
if (strcmp("Content", element->Name()) == 0)
|
||||
{
|
||||
const tinyxml2::XMLAttribute* attribute = element->FirstAttribute();
|
||||
|
||||
//
|
||||
if (!attribute)
|
||||
{
|
||||
serializeEnabled = true;
|
||||
rootType = "NodeObjectData";
|
||||
}
|
||||
}
|
||||
|
||||
if (serializeEnabled)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
const tinyxml2::XMLElement* child = element->FirstChildElement();
|
||||
if (child)
|
||||
{
|
||||
element = child;
|
||||
}
|
||||
else
|
||||
{
|
||||
element = element->NextSiblingElement();
|
||||
}
|
||||
}
|
||||
|
||||
if (serializeEnabled)
|
||||
{
|
||||
_builder = new FlatBufferBuilder();
|
||||
|
||||
Offset<NodeTree> nodeTree;
|
||||
Offset<NodeAction> aciton;
|
||||
|
||||
|
||||
const tinyxml2::XMLElement* child = element->FirstChildElement();
|
||||
|
||||
while (child)
|
||||
{
|
||||
std::string name = child->Name();
|
||||
|
||||
if (name == "Animation") // action
|
||||
{
|
||||
const tinyxml2::XMLElement* animation = child;
|
||||
aciton = createNodeAction(animation);
|
||||
}
|
||||
else if (name == "ObjectData") // nodeTree
|
||||
{
|
||||
const tinyxml2::XMLElement* objectData = child;
|
||||
nodeTree = createNodeTreeForSimulator(objectData, rootType);
|
||||
}
|
||||
|
||||
child = child->NextSiblingElement();
|
||||
}
|
||||
|
||||
auto csparsebinary = CreateCSParseBinary(*_builder,
|
||||
_builder->CreateVector(_textures),
|
||||
_builder->CreateVector(_texturePngs),
|
||||
nodeTree,
|
||||
aciton);
|
||||
_builder->Finish(csparsebinary);
|
||||
|
||||
_textures.clear();
|
||||
_texturePngs.clear();
|
||||
}
|
||||
return _builder;
|
||||
}
|
||||
|
||||
Offset<NodeTree> FlatBuffersSerialize::createNodeTreeForSimulator(const tinyxml2::XMLElement *objectData,
|
||||
std::string classType)
|
||||
{
|
||||
std::string classname = classType.substr(0, classType.find("ObjectData"));
|
||||
CCLOG("classname = %s", classname.c_str());
|
||||
|
||||
std::string name = "";
|
||||
|
||||
Offset<Options> options;
|
||||
std::vector<Offset<NodeTree>> children;
|
||||
|
||||
if (classname == "ProjectNode")
|
||||
{
|
||||
auto projectNodeOptions = createProjectNodeOptionsForSimulator(objectData);
|
||||
options = CreateOptions(*_builder, *(Offset<Table>*)(&projectNodeOptions));
|
||||
}
|
||||
else if (classname == "SimpleAudio")
|
||||
{
|
||||
auto reader = ComAudioReader::getInstance();
|
||||
options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string readername = getGUIClassName(classname);
|
||||
readername.append("Reader");
|
||||
|
||||
NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
|
||||
options = CreateOptions(*_builder, reader->createOptionsWithFlatBuffers(objectData, _builder));
|
||||
}
|
||||
|
||||
|
||||
// children
|
||||
bool containChildrenElement = false;
|
||||
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
|
||||
|
||||
while (child)
|
||||
{
|
||||
CCLOG("child name = %s", child->Name());
|
||||
|
||||
if (strcmp("Children", child->Name()) == 0)
|
||||
{
|
||||
containChildrenElement = true;
|
||||
break;
|
||||
}
|
||||
|
||||
child = child->NextSiblingElement();
|
||||
}
|
||||
|
||||
if (containChildrenElement)
|
||||
{
|
||||
child = child->FirstChildElement();
|
||||
CCLOG("element name = %s", child->Name());
|
||||
|
||||
while (child)
|
||||
{
|
||||
const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
|
||||
bool bHasType = false;
|
||||
while (attribute)
|
||||
{
|
||||
std::string attriname = attribute->Name();
|
||||
std::string value = attribute->Value();
|
||||
|
||||
if (attriname == "ctype")
|
||||
{
|
||||
children.push_back(createNodeTreeForSimulator(child, value));
|
||||
|
||||
bHasType = true;
|
||||
break;
|
||||
}
|
||||
|
||||
attribute = attribute->Next();
|
||||
}
|
||||
|
||||
if(!bHasType)
|
||||
{
|
||||
children.push_back(createNodeTreeForSimulator(child, "NodeObjectData"));
|
||||
}
|
||||
|
||||
child = child->NextSiblingElement();
|
||||
}
|
||||
}
|
||||
//
|
||||
|
||||
|
||||
std::string customClassName = "";
|
||||
const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
|
||||
while (attribute)
|
||||
{
|
||||
std::string attriname = attribute->Name();
|
||||
std::string value = attribute->Value();
|
||||
|
||||
if (attriname == "CustomClassName")
|
||||
{
|
||||
customClassName = value;
|
||||
break;
|
||||
}
|
||||
|
||||
attribute = attribute->Next();
|
||||
}
|
||||
|
||||
return CreateNodeTree(*_builder,
|
||||
_builder->CreateString(classname),
|
||||
_builder->CreateVector(children),
|
||||
options,
|
||||
_builder->CreateString(customClassName));
|
||||
}
|
||||
|
||||
Offset<ProjectNodeOptions> FlatBuffersSerialize::createProjectNodeOptionsForSimulator(const tinyxml2::XMLElement *objectData)
|
||||
{
|
||||
auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, _builder);
|
||||
auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
|
||||
|
||||
std::string filename = "";
|
||||
|
||||
// FileData
|
||||
const tinyxml2::XMLElement* child = objectData->FirstChildElement();
|
||||
while (child)
|
||||
{
|
||||
std::string name = child->Name();
|
||||
|
||||
if (name == "FileData")
|
||||
{
|
||||
const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
|
||||
|
||||
while (attribute)
|
||||
{
|
||||
name = attribute->Name();
|
||||
std::string value = attribute->Value();
|
||||
|
||||
if (name == "Path")
|
||||
{
|
||||
filename = value;
|
||||
}
|
||||
|
||||
attribute = attribute->Next();
|
||||
}
|
||||
}
|
||||
|
||||
child = child->NextSiblingElement();
|
||||
}
|
||||
|
||||
return CreateProjectNodeOptions(*_builder,
|
||||
nodeOptions,
|
||||
_builder->CreateString(filename));
|
||||
}
|
||||
|
||||
void FlatBuffersSerialize::deleteFlatBufferBuilder()
|
||||
{
|
||||
if (_builder != nullptr)
|
||||
{
|
||||
_builder->Clear();
|
||||
CC_SAFE_DELETE(_builder);
|
||||
}
|
||||
}
|
||||
|
||||
int FlatBuffersSerialize::getResourceType(std::string key)
|
||||
{
|
||||
|
|
|
@ -110,6 +110,13 @@ public:
|
|||
flatbuffers::Offset<flatbuffers::TimeLineTextureFrame> createTimeLineTextureFrame(const tinyxml2::XMLElement* objectData);
|
||||
/**/
|
||||
|
||||
flatbuffers::FlatBufferBuilder* createFlatBuffersWithXMLFileForSimulator(const std::string& xmlFileName);
|
||||
flatbuffers::Offset<flatbuffers::NodeTree> createNodeTreeForSimulator(const tinyxml2::XMLElement* objectData,
|
||||
std::string classType);
|
||||
flatbuffers::Offset<flatbuffers::ProjectNodeOptions> createProjectNodeOptionsForSimulator(const tinyxml2::XMLElement* objectData);
|
||||
|
||||
void deleteFlatBufferBuilder();
|
||||
|
||||
int getResourceType(std::string key);
|
||||
std::string getGUIClassName(const std::string &name);
|
||||
std::string getWidgetReaderClassName(cocos2d::ui::Widget *widget);
|
||||
|
|
Loading…
Reference in New Issue