axmol/cocos/editor-support/cocostudio/CCSSceneReader.cpp

592 lines
19 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
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 "cocostudio/CocoStudio.h"
2014-03-11 17:13:54 +08:00
#include "ui/CocosGUI.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "audio/include/SimpleAudioEngine.h"
#include "base/ObjectFactory.h"
using namespace cocos2d;
using namespace ui;
namespace cocostudio {
SceneReader* SceneReader::s_sharedReader = nullptr;
SceneReader::SceneReader()
: _fnSelector(nullptr)
, _node(nullptr)
, _attachComponent(AttachComponentType::EMPTY_NODE)
{
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAttribute));
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComRender));
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAudio));
2014-10-23 17:54:15 +08:00
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComController));
}
SceneReader::~SceneReader()
{
}
const char* SceneReader::sceneReaderVersion()
{
return "1.0.0.0";
}
cocos2d::Node* SceneReader::createNodeWithSceneFile(const std::string &fileName, AttachComponentType attachComponent /*= AttachComponentType::EMPTY_NODE*/)
{
2014-06-25 11:58:36 +08:00
std::string reDir = fileName;
2014-10-23 17:54:15 +08:00
std::string file_extension = "";
size_t pos = reDir.find_last_of('.');
if (pos != std::string::npos)
{
file_extension = reDir.substr(pos, reDir.length());
std::transform(file_extension.begin(),file_extension.end(), file_extension.begin(), (int(*)(int))toupper);
}
2014-06-25 11:58:36 +08:00
if (file_extension == ".JSON")
{
_node = nullptr;
rapidjson::Document jsonDict;
do {
CC_BREAK_IF(!readJson(fileName, jsonDict));
_node = createObject(jsonDict, nullptr, attachComponent);
TriggerMng::getInstance()->parse(jsonDict);
} while (0);
return _node;
}
else if(file_extension == ".CSB")
{
2014-10-23 17:54:15 +08:00
do {
2015-07-15 12:04:48 +08:00
std::string binaryFilePath = FileUtils::getInstance()->fullPathForFilename(fileName);
2014-10-09 17:19:43 +08:00
auto fileData = FileUtils::getInstance()->getDataFromFile(binaryFilePath);
auto fileDataBytes = fileData.getBytes();
CC_BREAK_IF(fileData.isNull());
CocoLoader tCocoLoader;
if (tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
{
2014-10-23 17:54:15 +08:00
stExpCocoNode *tpRootCocoNode = tCocoLoader.GetRootCocoNode();
rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
if (rapidjson::kObjectType == tType)
{
stExpCocoNode *tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
CC_BREAK_IF(tpRootCocoNode->GetChildNum() == 0);
_node = Node::create();
int nCount = 0;
std::vector<Component*> _vecComs;
ComRender *pRender = nullptr;
std::string key = tpChildArray[15].GetName(&tCocoLoader);
if (key == "components")
{
nCount = tpChildArray[15].GetChildNum();
}
stExpCocoNode *pComponents = tpChildArray[15].GetChildArray(&tCocoLoader);
SerData *data = new (std::nothrow) SerData();
for (int i = 0; i < nCount; i++)
{
stExpCocoNode *subDict = pComponents[i].GetChildArray(&tCocoLoader);
if (subDict == nullptr)
{
continue;
}
std::string key1 = subDict[1].GetName(&tCocoLoader);
const char *comName = subDict[1].GetValue(&tCocoLoader);
Component *pCom = nullptr;
if (key1 == "classname" && comName != nullptr)
{
pCom = createComponent(comName);
}
CCLOG("classname = %s", comName);
if (pCom != nullptr)
{
data->_rData = nullptr;
data->_cocoNode = subDict;
2014-07-02 15:58:42 +08:00
data->_cocoLoader = &tCocoLoader;
2014-10-23 17:54:15 +08:00
if (pCom->serialize(data))
{
ComRender *pTRender = dynamic_cast<ComRender*>(pCom);
if (pTRender != nullptr)
{
pRender = pTRender;
}
else
{
_vecComs.push_back(pCom);
}
}
else
{
CC_SAFE_RELEASE_NULL(pCom);
}
}
2014-06-25 11:58:36 +08:00
if(_fnSelector != nullptr)
{
_fnSelector(pCom, (void*)(data));
}
2014-10-23 17:54:15 +08:00
}
2014-06-25 11:58:36 +08:00
2014-10-23 17:54:15 +08:00
setPropertyFromJsonDict(&tCocoLoader, tpRootCocoNode, _node);
for (std::vector<Component*>::iterator iter = _vecComs.begin(); iter != _vecComs.end(); ++iter)
{
_node->addComponent(*iter);
}
2014-06-25 11:58:36 +08:00
2014-10-23 17:54:15 +08:00
stExpCocoNode *pGameObjects = tpChildArray[11].GetChildArray(&tCocoLoader);
int length = tpChildArray[11].GetChildNum();
for (int i = 0; i < length; ++i)
{
createObject(&tCocoLoader, &pGameObjects[i], _node, attachComponent);
}
TriggerMng::getInstance()->parse(&tCocoLoader, tpChildArray);
}
}
}while (0);
return _node;
2014-06-25 11:58:36 +08:00
}
else
2014-10-23 17:54:15 +08:00
{
log("read file [%s] error!\n", fileName.c_str());
}
return nullptr;
}
bool SceneReader::readJson(const std::string &fileName, rapidjson::Document &doc)
{
2014-06-30 17:15:55 +08:00
bool ret = false;
do {
std::string jsonpath = FileUtils::getInstance()->fullPathForFilename(fileName);
std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);
doc.Parse<0>(contentStr.c_str());
CC_BREAK_IF(doc.HasParseError());
2014-06-30 17:15:55 +08:00
ret = true;
} while (0);
2014-06-30 17:15:55 +08:00
return ret;
}
Node* SceneReader::nodeByTag(Node *parent, int tag)
{
if (parent == nullptr)
{
return nullptr;
}
Node *_retNode = nullptr;
Vector<Node*>& Children = parent->getChildren();
Vector<Node*>::iterator iter = Children.begin();
while (iter != Children.end())
{
Node* pNode = *iter;
if(pNode != nullptr && pNode->getTag() == tag)
{
_retNode = pNode;
break;
}
else
{
_retNode = nodeByTag(pNode, tag);
if (_retNode != nullptr)
{
break;
}
}
++iter;
}
return _retNode;
}
2014-06-04 14:27:14 +08:00
cocos2d::Component* SceneReader::createComponent(const std::string classname)
{
std::string name = this->getComponentClassName(classname);
Ref *object = ObjectFactory::getInstance()->createObject(name);
return dynamic_cast<Component*>(object);
}
std::string SceneReader::getComponentClassName(const std::string &name)
{
std::string comName;
if (name == "CCSprite" || name == "CCTMXTiledMap" || name == "CCParticleSystemQuad" || name == "CCArmature" || name == "GUIComponent")
{
comName = "ComRender";
}
else if (name == "CCComAudio" || name == "CCBackgroundAudio")
{
comName = "ComAudio";
}
else if (name == "CCComController")
{
comName = "ComController";
}
else if (name == "CCComAttribute")
{
comName = "ComAttribute";
}
else if (name == "CCScene")
{
comName = "Scene";
}
else
{
CCASSERT(false, "Unregistered Component!");
}
return comName;
}
Node* SceneReader::createObject(const rapidjson::Value &dict, cocos2d::Node* parent, AttachComponentType attachComponent)
{
const char *className = DICTOOL->getStringValue_json(dict, "classname");
if(strcmp(className, "CCNode") == 0)
{
Node* gb = nullptr;
if(nullptr == parent)
{
gb = Node::create();
}
std::vector<Component*> vecComs;
ComRender *render = nullptr;
int count = DICTOOL->getArrayCount_json(dict, "components");
for (int i = 0; i < count; i++)
{
const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "components", i);
if (!DICTOOL->checkObjectExist_json(subDict))
{
break;
}
const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
2014-06-04 14:27:14 +08:00
Component *com = this->createComponent(comName);
2014-06-25 14:32:38 +08:00
CCLOG("classname = %s", comName);
SerData *data = new (std::nothrow) SerData();
2014-03-26 17:21:09 +08:00
if (com != nullptr)
{
2014-06-25 11:58:36 +08:00
data->_rData = &subDict;
2014-10-23 17:54:15 +08:00
data->_cocoNode = nullptr;
2014-07-02 15:58:42 +08:00
data->_cocoLoader = nullptr;
2014-06-25 11:58:36 +08:00
if (com->serialize(data))
2014-03-26 17:21:09 +08:00
{
ComRender *tRender = dynamic_cast<ComRender*>(com);
if (tRender == nullptr)
{
vecComs.push_back(com);
}
else
{
render = tRender;
}
}
}
2014-06-25 11:58:36 +08:00
CC_SAFE_DELETE(data);
if(_fnSelector != nullptr)
{
2014-06-25 11:58:36 +08:00
_fnSelector(com, data);
}
}
if (parent != nullptr)
{
if (render == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
{
gb = Node::create();
if (render != nullptr)
{
vecComs.push_back(render);
}
}
else
{
gb = render->getNode();
gb->retain();
render->setNode(nullptr);
}
parent->addChild(gb);
}
setPropertyFromJsonDict(dict, gb);
for (std::vector<Component*>::iterator iter = vecComs.begin(); iter != vecComs.end(); ++iter)
{
gb->addComponent(*iter);
}
int length = DICTOOL->getArrayCount_json(dict, "gameobjects");
for (int i = 0; i < length; ++i)
{
const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(dict, "gameobjects", i);
if (!DICTOOL->checkObjectExist_json(subDict))
{
break;
}
createObject(subDict, gb, attachComponent);
}
2015-06-23 13:54:11 +08:00
if(dict.HasMember("CanvasSize"))
{
2015-06-23 13:54:11 +08:00
const rapidjson::Value &canvasSizeDict = DICTOOL->getSubDictionary_json(dict, "CanvasSize");
if (DICTOOL->checkObjectExist_json(canvasSizeDict))
{
int width = DICTOOL->getIntValue_json(canvasSizeDict, "_width");
int height = DICTOOL->getIntValue_json(canvasSizeDict, "_height");
gb->setContentSize(Size(width, height));
}
}
return gb;
}
return nullptr;
}
2014-06-25 11:58:36 +08:00
cocos2d::Node* SceneReader::createObject(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::Node* parent, AttachComponentType attachComponent)
{
2014-06-30 17:15:55 +08:00
const char *className = nullptr;
2014-07-01 16:31:17 +08:00
stExpCocoNode *pNodeArray = cocoNode->GetChildArray(cocoLoader);
2014-06-25 11:58:36 +08:00
std::string Key = pNodeArray[1].GetName(cocoLoader);
if (Key == "classname")
{
2014-07-01 16:31:17 +08:00
className = pNodeArray[1].GetValue(cocoLoader);
2014-06-25 11:58:36 +08:00
}
if(strcmp(className, "CCNode") == 0)
{
2014-06-30 17:15:55 +08:00
Node* gb = nullptr;
2014-06-25 11:58:36 +08:00
std::vector<Component*> _vecComs;
2014-06-30 17:15:55 +08:00
ComRender *pRender = nullptr;
2014-06-25 11:58:36 +08:00
int count = 0;
std::string key = pNodeArray[13].GetName(cocoLoader);
if (key == "components")
{
count = pNodeArray[13].GetChildNum();
}
2014-07-01 16:31:17 +08:00
stExpCocoNode *pComponents = pNodeArray[13].GetChildArray(cocoLoader);
SerData *data = new (std::nothrow) SerData();
2014-06-25 11:58:36 +08:00
for (int i = 0; i < count; ++i)
{
2014-07-01 16:31:17 +08:00
stExpCocoNode *subDict = pComponents[i].GetChildArray(cocoLoader);
2014-06-30 17:15:55 +08:00
if (subDict == nullptr)
2014-06-25 11:58:36 +08:00
{
continue;
}
2014-07-02 10:53:35 +08:00
std::string key1 = subDict[1].GetName(cocoLoader);
2014-07-02 15:37:15 +08:00
const char *comName = subDict[1].GetValue(cocoLoader);
2014-06-25 11:58:36 +08:00
Component *pCom = nullptr;
2014-07-02 10:53:35 +08:00
if (key1 == "classname" && comName != nullptr)
2014-06-25 11:58:36 +08:00
{
pCom = createComponent(comName);
}
CCLOG("classname = %s", comName);
2014-06-30 17:15:55 +08:00
if (pCom != nullptr)
2014-06-25 11:58:36 +08:00
{
2014-06-30 17:15:55 +08:00
data->_rData = nullptr;
2014-06-25 11:58:36 +08:00
data->_cocoNode = subDict;
2014-07-02 15:58:42 +08:00
data->_cocoLoader = cocoLoader;
2014-06-25 11:58:36 +08:00
if (pCom->serialize(data))
{
ComRender *pTRender = dynamic_cast<ComRender*>(pCom);
2014-06-30 17:15:55 +08:00
if (pTRender != nullptr)
2014-06-25 11:58:36 +08:00
{
pRender = pTRender;
}
else
{
_vecComs.push_back(pCom);
}
}
else
{
CC_SAFE_RELEASE_NULL(pCom);
}
}
if(_fnSelector != nullptr)
{
_fnSelector(pCom, (void*)(data));
}
}
CC_SAFE_DELETE(data);
if (parent != nullptr)
{
if (pRender == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
{
2015-07-15 12:04:48 +08:00
gb = Node::create();
2014-06-25 11:58:36 +08:00
if (pRender != nullptr)
{
_vecComs.push_back(pRender);
}
}
else
{
gb = pRender->getNode();
gb->retain();
2014-06-30 17:15:55 +08:00
pRender->setNode(nullptr);
2014-06-25 11:58:36 +08:00
CC_SAFE_RELEASE_NULL(pRender);
}
parent->addChild(gb);
}
setPropertyFromJsonDict(cocoLoader, cocoNode, gb);
for (std::vector<Component*>::iterator iter = _vecComs.begin(); iter != _vecComs.end(); ++iter)
{
gb->addComponent(*iter);
}
2014-07-01 16:31:17 +08:00
stExpCocoNode *pGameObjects = pNodeArray[12].GetChildArray(cocoLoader);
2014-06-30 17:15:55 +08:00
if (pGameObjects != nullptr)
2014-06-25 11:58:36 +08:00
{
int length = pNodeArray[12].GetChildNum();
for (int i = 0; i < length; ++i)
{
createObject(cocoLoader, &pGameObjects[i], gb, attachComponent);
}
}
return gb;
}
return nullptr;
}
void SceneReader::setTarget(const std::function<void(cocos2d::Ref* obj, void* doc)>& selector)
{
_fnSelector = selector;
}
Node* SceneReader::getNodeByTag(int nTag)
{
if (_node == nullptr)
{
return nullptr;
}
if (_node->getTag() == nTag)
{
return _node;
}
return nodeByTag(_node, nTag);
}
void SceneReader::setPropertyFromJsonDict(const rapidjson::Value &root, cocos2d::Node *node)
{
float x = DICTOOL->getFloatValue_json(root, "x");
float y = DICTOOL->getFloatValue_json(root, "y");
node->setPosition(x, y);
const bool bVisible = (DICTOOL->getIntValue_json(root, "visible", 1) != 0);
node->setVisible(bVisible);
int nTag = DICTOOL->getIntValue_json(root, "objecttag", -1);
node->setTag(nTag);
int nZorder = DICTOOL->getIntValue_json(root, "zorder");
node->setLocalZOrder(nZorder);
float fScaleX = DICTOOL->getFloatValue_json(root, "scalex", 1.0);
float fScaleY = DICTOOL->getFloatValue_json(root, "scaley", 1.0);
node->setScaleX(fScaleX);
node->setScaleY(fScaleY);
float fRotationZ = DICTOOL->getFloatValue_json(root, "rotation");
node->setRotation(fRotationZ);
const char *sName = DICTOOL->getStringValue_json(root, "name", "");
node->setName(sName);
}
2014-06-25 11:58:36 +08:00
void SceneReader::setPropertyFromJsonDict(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::Node *node)
{
2014-07-01 16:31:17 +08:00
stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
2014-06-25 11:58:36 +08:00
float x = 0.0f, y = 0.0f, fScaleX = 1.0f, fScaleY = 1.0f, fRotationZ = 1.0f;
bool bVisible = false;
const char *sName = "";
2014-06-25 11:58:36 +08:00
int nTag = 0, nZorder = -1;
for (int i = 0; i < cocoNode->GetChildNum(); ++i)
{
std::string key = stChildArray[i].GetName(cocoLoader);
2014-07-01 16:31:17 +08:00
std::string value = stChildArray[i].GetValue(cocoLoader);
2014-06-25 11:58:36 +08:00
if (key == "x")
{
2014-07-14 20:45:24 +08:00
x = utils::atof(value.c_str());
2014-06-25 11:58:36 +08:00
node->setPositionX(x);
}
else if (key == "y")
{
2014-07-14 20:45:24 +08:00
y = utils::atof(value.c_str());
2014-06-25 11:58:36 +08:00
node->setPositionY(y);
}
else if (key == "visible")
{
2014-10-23 17:54:15 +08:00
bVisible = atoi(value.c_str()) != 0;
2014-06-25 11:58:36 +08:00
node->setVisible(bVisible);
}
else if (key == "objecttag")
{
nTag = atoi(value.c_str());
node->setTag(nTag);
}
else if (key == "zorder")
{
nZorder = atoi(value.c_str());
node->setLocalZOrder(nZorder);
2014-06-25 11:58:36 +08:00
}
else if(key == "scalex")
{
2014-07-14 20:45:24 +08:00
fScaleX = utils::atof(value.c_str());
2014-06-25 11:58:36 +08:00
node->setScaleX(fScaleX);
}
else if(key == "scaley")
{
fScaleY = atof(value.c_str());
node->setScaleY(fScaleY);
}
else if(key == "rotation")
{
2014-07-14 20:45:24 +08:00
fRotationZ = utils::atof(value.c_str());
2014-06-25 11:58:36 +08:00
node->setRotation(fRotationZ);
}
else if(key == "name")
{
sName = value.c_str();
node->setName(sName);
}
2014-06-25 11:58:36 +08:00
}
}
SceneReader* SceneReader::getInstance()
{
if (s_sharedReader == nullptr)
{
s_sharedReader = new (std::nothrow) SceneReader();
}
return s_sharedReader;
}
void SceneReader::destroyInstance()
{
DictionaryHelper::destroyInstance();
TriggerMng::destroyInstance();
CocosDenshion::SimpleAudioEngine::end();
CC_SAFE_DELETE(s_sharedReader);
}
}