2013-09-09 19:51:26 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "cocostudio/CocoStudio.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
#include "ui/CocosGUI.h"
|
2014-01-03 01:14:12 +08:00
|
|
|
#include "SimpleAudioEngine.h"
|
2014-01-05 02:22:32 +08:00
|
|
|
#include "ObjectFactory.h"
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace cocos2d;
|
2014-02-24 18:56:45 +08:00
|
|
|
using namespace ui;
|
2013-10-15 18:00:03 +08:00
|
|
|
|
|
|
|
namespace cocostudio {
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
SceneReader* SceneReader::s_sharedReader = nullptr;
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
SceneReader::SceneReader()
|
|
|
|
: _fnSelector(nullptr)
|
|
|
|
, _node(nullptr)
|
2014-03-26 17:55:35 +08:00
|
|
|
, _attachComponent(AttachComponentType::EMPTY_NODE)
|
2014-01-03 19:30:47 +08:00
|
|
|
{
|
2014-01-05 02:22:32 +08:00
|
|
|
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAttribute));
|
|
|
|
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComRender));
|
|
|
|
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComAudio));
|
|
|
|
ObjectFactory::getInstance()->registerType(CREATE_CLASS_COMPONENT_INFO(ComController));
|
2014-01-03 19:30:47 +08:00
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
SceneReader::~SceneReader()
|
|
|
|
{
|
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
const char* SceneReader::sceneReaderVersion()
|
|
|
|
{
|
|
|
|
return "1.0.0.0";
|
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-03-26 17:55:35 +08:00
|
|
|
cocos2d::Node* SceneReader::createNodeWithSceneFile(const std::string &fileName, AttachComponentType attachComponent /*= AttachComponentType::EMPTY_NODE*/)
|
2014-01-03 19:30:47 +08:00
|
|
|
{
|
|
|
|
rapidjson::Document jsonDict;
|
|
|
|
do {
|
|
|
|
CC_BREAK_IF(!readJson(fileName, jsonDict));
|
2014-03-26 16:39:32 +08:00
|
|
|
_node = createObject(jsonDict, nullptr, attachComponent);
|
2014-01-03 19:30:47 +08:00
|
|
|
TriggerMng::getInstance()->parse(jsonDict);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return _node;
|
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
bool SceneReader::readJson(const std::string &fileName, rapidjson::Document &doc)
|
|
|
|
{
|
|
|
|
bool bRet = 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());
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
return bRet;
|
|
|
|
}
|
2013-12-24 20:33:55 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-12-24 20:33:55 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
return _retNode;
|
|
|
|
}
|
2013-12-24 20:33:55 +08:00
|
|
|
|
|
|
|
|
2014-03-26 16:39:32 +08:00
|
|
|
Node* SceneReader::createObject(const rapidjson::Value &dict, cocos2d::Node* parent, AttachComponentType attachComponent)
|
2014-01-03 19:30:47 +08:00
|
|
|
{
|
|
|
|
const char *className = DICTOOL->getStringValue_json(dict, "classname");
|
|
|
|
if(strcmp(className, "CCNode") == 0)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2014-01-03 19:30:47 +08:00
|
|
|
Node* gb = nullptr;
|
|
|
|
if(nullptr == parent)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2014-01-03 19:30:47 +08:00
|
|
|
gb = Node::create();
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:39:32 +08:00
|
|
|
std::vector<Component*> vecComs;
|
|
|
|
ComRender *render = nullptr;
|
2014-01-03 19:30:47 +08:00
|
|
|
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))
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2014-01-03 19:30:47 +08:00
|
|
|
break;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
2014-01-03 19:30:47 +08:00
|
|
|
const char *comName = DICTOOL->getStringValue_json(subDict, "classname");
|
2014-01-05 02:22:32 +08:00
|
|
|
Component *com = ObjectFactory::getInstance()->createComponent(comName);
|
2014-03-26 17:21:09 +08:00
|
|
|
if (com != nullptr)
|
|
|
|
{
|
|
|
|
if (com->serialize((void*)(&subDict)))
|
|
|
|
{
|
2014-03-26 17:55:35 +08:00
|
|
|
ComRender *tRender = dynamic_cast<ComRender*>(com);
|
|
|
|
if (tRender == nullptr)
|
2014-03-26 16:39:32 +08:00
|
|
|
{
|
|
|
|
vecComs.push_back(com);
|
|
|
|
}
|
2014-03-26 17:55:35 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
render = tRender;
|
|
|
|
}
|
2014-01-24 11:50:58 +08:00
|
|
|
}
|
2014-03-26 16:39:32 +08:00
|
|
|
}
|
2014-01-05 02:22:32 +08:00
|
|
|
if(_fnSelector != nullptr)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2014-01-05 02:22:32 +08:00
|
|
|
_fnSelector(com, (void*)(&subDict));
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
2014-01-03 19:30:47 +08:00
|
|
|
}
|
|
|
|
|
2014-03-26 16:39:32 +08:00
|
|
|
if (parent != nullptr)
|
|
|
|
{
|
2014-03-26 17:55:35 +08:00
|
|
|
if (render == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
|
2014-03-26 16:39:32 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-03-26 16:39:32 +08:00
|
|
|
createObject(subDict, gb, attachComponent);
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
return gb;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
2014-01-03 19:30:47 +08:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void SceneReader::setTarget(const std::function<void(cocos2d::Ref* obj, void* doc)>& selector)
|
2014-01-03 19:30:47 +08:00
|
|
|
{
|
|
|
|
_fnSelector = selector;
|
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
Node* SceneReader::getNodeByTag(int nTag)
|
|
|
|
{
|
|
|
|
if (_node == nullptr)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2014-01-03 19:30:47 +08:00
|
|
|
return nullptr;
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
2014-01-03 19:30:47 +08:00
|
|
|
if (_node->getTag() == nTag)
|
2013-11-15 09:45:03 +08:00
|
|
|
{
|
2014-01-03 19:30:47 +08:00
|
|
|
return _node;
|
2013-11-15 09:45:03 +08:00
|
|
|
}
|
2014-01-03 19:30:47 +08:00
|
|
|
return nodeByTag(_node, nTag);
|
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2014-01-03 19:30:47 +08:00
|
|
|
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(Point(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");
|
2014-01-19 03:35:27 +08:00
|
|
|
node->setLocalZOrder(nZorder);
|
2014-01-03 19:30:47 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
SceneReader* SceneReader::getInstance()
|
|
|
|
{
|
|
|
|
if (s_sharedReader == nullptr)
|
2013-09-09 19:51:26 +08:00
|
|
|
{
|
2014-01-03 19:30:47 +08:00
|
|
|
s_sharedReader = new SceneReader();
|
2013-09-09 19:51:26 +08:00
|
|
|
}
|
2014-01-03 19:30:47 +08:00
|
|
|
return s_sharedReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneReader::destroyInstance()
|
|
|
|
{
|
|
|
|
DictionaryHelper::destroyInstance();
|
|
|
|
TriggerMng::destroyInstance();
|
|
|
|
CocosDenshion::SimpleAudioEngine::end();
|
|
|
|
CC_SAFE_DELETE(s_sharedReader);
|
|
|
|
}
|
2013-09-09 19:51:26 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|