123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583 |
- /****************************************************************************
- Copyright (c) 2013-2017 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 "editor-support/cocostudio/CocoStudio.h"
- #include "ui/CocosGUI.h"
- #include "audio/include/SimpleAudioEngine.h"
- #include "base/ObjectFactory.h"
- #include "base/ccUtils.h"
- #include "platform/CCFileUtils.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));
- 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*/)
- {
- std::string fileExtension = cocos2d::FileUtils::getInstance()->getFileExtension(fileName);
- if (fileExtension == ".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(fileExtension == ".csb")
- {
- do {
- std::string binaryFilePath = FileUtils::getInstance()->fullPathForFilename(fileName);
- auto fileData = FileUtils::getInstance()->getDataFromFile(binaryFilePath);
- auto fileDataBytes = fileData.getBytes();
- CC_BREAK_IF(fileData.isNull());
- CocoLoader tCocoLoader;
- if (tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
- {
- 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);
- }
- if (pCom != nullptr)
- {
- data->_rData = nullptr;
- data->_cocoNode = subDict;
- data->_cocoLoader = &tCocoLoader;
- 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);
- }
- }
- if(_fnSelector != nullptr)
- {
- _fnSelector(pCom, (void*)(data));
- }
- }
-
- setPropertyFromJsonDict(&tCocoLoader, tpRootCocoNode, _node);
- for (std::vector<Component*>::iterator iter = _vecComs.begin(); iter != _vecComs.end(); ++iter)
- {
- _node->addComponent(*iter);
- }
-
- 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;
- }
- else
- {
- log("read file [%s] error!\n", fileName.c_str());
- }
- return nullptr;
- }
- bool SceneReader::readJson(const std::string &fileName, rapidjson::Document &doc)
- {
- 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());
- ret = true;
- } while (0);
- 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;
- }
-
- 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 == ComAudio::COMPONENT_NAME || name == "CCBackgroundAudio")
- {
- comName = "ComAudio";
- }
- else if (name == ComController::COMPONENT_NAME)
- {
- comName = "ComController";
- }
- else if (name == ComAttribute::COMPONENT_NAME)
- {
- 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");
- Component *com = this->createComponent(comName);
- SerData *data = new (std::nothrow) SerData();
- if (com != nullptr)
- {
- data->_rData = &subDict;
- data->_cocoNode = nullptr;
- data->_cocoLoader = nullptr;
- if (com->serialize(data))
- {
- ComRender *tRender = dynamic_cast<ComRender*>(com);
- if (tRender == nullptr)
- {
- vecComs.push_back(com);
- }
- else
- {
- render = tRender;
- }
- }
- }
- CC_SAFE_DELETE(data);
- if(_fnSelector != nullptr)
- {
- _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);
- }
-
- if(dict.HasMember("CanvasSize"))
- {
- 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;
- }
-
-
- cocos2d::Node* SceneReader::createObject(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::Node* parent, AttachComponentType attachComponent)
- {
- const char *className = nullptr;
- stExpCocoNode *pNodeArray = cocoNode->GetChildArray(cocoLoader);
- std::string Key = pNodeArray[1].GetName(cocoLoader);
- if (Key == "classname")
- {
- className = pNodeArray[1].GetValue(cocoLoader);
- }
- if(strcmp(className, "CCNode") == 0)
- {
- Node* gb = nullptr;
- std::vector<Component*> _vecComs;
- ComRender *pRender = nullptr;
- int count = 0;
- std::string key = pNodeArray[13].GetName(cocoLoader);
- if (key == "components")
- {
- count = pNodeArray[13].GetChildNum();
- }
- stExpCocoNode *pComponents = pNodeArray[13].GetChildArray(cocoLoader);
- SerData *data = new (std::nothrow) SerData();
- for (int i = 0; i < count; ++i)
- {
- stExpCocoNode *subDict = pComponents[i].GetChildArray(cocoLoader);
- if (subDict == nullptr)
- {
- continue;
- }
- std::string key1 = subDict[1].GetName(cocoLoader);
- const char *comName = subDict[1].GetValue(cocoLoader);
- Component *pCom = nullptr;
- if (key1 == "classname" && comName != nullptr)
- {
- pCom = createComponent(comName);
- }
- if (pCom != nullptr)
- {
- data->_rData = nullptr;
- data->_cocoNode = subDict;
- data->_cocoLoader = cocoLoader;
- 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);
- }
- }
- if(_fnSelector != nullptr)
- {
- _fnSelector(pCom, (void*)(data));
- }
- }
- CC_SAFE_DELETE(data);
-
- if (parent != nullptr)
- {
- if (pRender == nullptr || attachComponent == AttachComponentType::EMPTY_NODE)
- {
- gb = Node::create();
- if (pRender != nullptr)
- {
- _vecComs.push_back(pRender);
- }
- }
- else
- {
- gb = pRender->getNode();
- gb->retain();
- pRender->setNode(nullptr);
- 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);
- }
-
- stExpCocoNode *pGameObjects = pNodeArray[12].GetChildArray(cocoLoader);
- if (pGameObjects != nullptr)
- {
- 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);
- }
-
-
- void SceneReader::setPropertyFromJsonDict(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::Node *node)
- {
- stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
- float x = 0.0f, y = 0.0f, fScaleX = 1.0f, fScaleY = 1.0f, fRotationZ = 1.0f;
- bool bVisible = false;
- const char *sName = "";
- int nTag = 0, nZorder = -1;
-
- for (int i = 0; i < cocoNode->GetChildNum(); ++i)
- {
- std::string key = stChildArray[i].GetName(cocoLoader);
- std::string value = stChildArray[i].GetValue(cocoLoader);
-
- if (key == "x")
- {
- x = utils::atof(value.c_str());
- node->setPositionX(x);
- }
- else if (key == "y")
- {
- y = utils::atof(value.c_str());
- node->setPositionY(y);
- }
- else if (key == "visible")
- {
- bVisible = atoi(value.c_str()) != 0;
- 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);
- }
- else if(key == "scalex")
- {
- fScaleX = utils::atof(value.c_str());
- node->setScaleX(fScaleX);
- }
- else if(key == "scaley")
- {
- fScaleY = atof(value.c_str());
- node->setScaleY(fScaleY);
- }
- else if(key == "rotation")
- {
- fRotationZ = utils::atof(value.c_str());
- node->setRotation(fRotationZ);
- }
- else if(key == "name")
- {
- sName = value.c_str();
- node->setName(sName);
- }
- }
- }
- 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);
- }
- }
|