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"
|
|
|
|
#include "gui/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;
|
|
|
|
using namespace gui;
|
|
|
|
|
|
|
|
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-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-01-03 19:30:47 +08:00
|
|
|
cocos2d::Node* SceneReader::createNodeWithSceneFile(const std::string &fileName)
|
|
|
|
{
|
|
|
|
rapidjson::Document jsonDict;
|
|
|
|
do {
|
|
|
|
CC_BREAK_IF(!readJson(fileName, jsonDict));
|
|
|
|
_node = createObject(jsonDict, nullptr);
|
|
|
|
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-01-03 19:30:47 +08:00
|
|
|
Node* SceneReader::createObject(const rapidjson::Value &dict, cocos2d::Node* parent)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gb = Node::create();
|
|
|
|
parent->addChild(gb);
|
|
|
|
}
|
|
|
|
|
|
|
|
setPropertyFromJsonDict(dict, gb);
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (com != NULL)
|
|
|
|
{
|
|
|
|
if (com->serialize((void*)(&subDict)))
|
|
|
|
{
|
|
|
|
gb->addComponent(com);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-06 13:27:19 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(com);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
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-01-04 12:11:10 +08:00
|
|
|
void SceneReader::setTarget(const std::function<void(cocos2d::Object* 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
|
|
|
}
|