123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /****************************************************************************
- 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/CCActionManagerEx.h"
- #include "editor-support/cocostudio/CocoLoader.h"
- using namespace cocos2d;
- namespace cocostudio {
- static ActionManagerEx* sharedActionManager = nullptr;
- ActionManagerEx* ActionManagerEx::getInstance()
- {
- if (!sharedActionManager) {
- sharedActionManager = new (std::nothrow) ActionManagerEx();
- }
- return sharedActionManager;
- }
- void ActionManagerEx::destroyInstance()
- {
- if(sharedActionManager != nullptr)
- {
- sharedActionManager->releaseActions();
- CC_SAFE_DELETE(sharedActionManager);
- }
- }
- ActionManagerEx::ActionManagerEx()
- {
- }
- ActionManagerEx::~ActionManagerEx()
- {
- _actionDic.clear();
- }
- void ActionManagerEx::initWithDictionary(const char* jsonName,const rapidjson::Value &dic, Ref* root, int version)
- {
- std::string path = jsonName;
- this->_studioVersionNumber = version;
- ssize_t pos = path.find_last_of("/");
- std::string fileName = path.substr(pos+1,path.length());
- cocos2d::Vector<ActionObject*> actionList;
- int actionCount = DICTOOL->getArrayCount_json(dic, "actionlist");
- for (int i=0; i<actionCount; i++) {
- ActionObject* action = new (std::nothrow) ActionObject();
- action->autorelease();
- const rapidjson::Value &actionDic = DICTOOL->getDictionaryFromArray_json(dic, "actionlist", i);
- action->initWithDictionary(actionDic,root);
- actionList.pushBack(action);
- }
- _actionDic[fileName] = actionList;
- }
-
- void ActionManagerEx::initWithBinary(const char* file,
- cocos2d::Ref *root,
- CocoLoader* cocoLoader,
- stExpCocoNode* pCocoNode)
- {
- std::string path = file;
- ssize_t pos = path.find_last_of("/");
- std::string fileName = path.substr(pos+1,path.length());
- cocos2d::Vector<ActionObject*> actionList;
-
- stExpCocoNode *stChildArray = pCocoNode->GetChildArray(cocoLoader);
- stExpCocoNode *actionNode = nullptr;
- for (int i=0; i < pCocoNode->GetChildNum(); ++i) {
- std::string key = stChildArray[i].GetName(cocoLoader);
- if (key == "actionlist") {
- actionNode = &stChildArray[i];
- break;
- }
- }
- if (nullptr != actionNode)
- {
- int actionCount = actionNode->GetChildNum();
- for (int i = 0; i < actionCount; ++i) {
- ActionObject* action = new (std::nothrow) ActionObject();
- action->autorelease();
-
- action->initWithBinary(cocoLoader, &actionNode->GetChildArray(cocoLoader)[i], root);
-
- actionList.pushBack(action);
- }
- }
- _actionDic[fileName] = actionList;
-
- }
- ActionObject* ActionManagerEx::getActionByName(const char* jsonName,const char* actionName)
- {
- std::string path = jsonName;
- ssize_t pos = path.find_last_of("/");
- std::string fileName = path.substr(pos+1,path.length());
- auto iterator = _actionDic.find(fileName);
- if (iterator == _actionDic.end())
- {
- return nullptr;
- }
- auto actionList = iterator->second;
- for (int i = 0; i < actionList.size(); i++)
- {
- ActionObject* action = actionList.at(i);
- if (strcmp(actionName, action->getName()) == 0)
- {
- return action;
- }
- }
- return nullptr;
- }
- ActionObject* ActionManagerEx::playActionByName(const char* jsonName,const char* actionName)
- {
- ActionObject* action = getActionByName(jsonName,actionName);
- if (action)
- {
- action->play();
- }
- return action;
- }
- ActionObject* ActionManagerEx::playActionByName(const char* jsonName,const char* actionName, CallFunc* func)
- {
- ActionObject* action = getActionByName(jsonName,actionName);
- if (action)
- {
- action->play(func);
- }
- return action;
- }
- ActionObject* ActionManagerEx::stopActionByName(const char* jsonName,const char* actionName)
- {
- ActionObject* action = getActionByName(jsonName,actionName);
- if (action)
- {
- action->stop();
- }
- return action;
- }
-
- void ActionManagerEx::releaseActions()
- {
- for (auto& iter : _actionDic)
- {
- cocos2d::Vector<ActionObject*> objList = iter.second;
- ssize_t listCount = objList.size();
- for (ssize_t i = 0; i < listCount; i++) {
- ActionObject* action = objList.at(i);
- if (action != nullptr) {
- action->stop();
- }
- }
- objList.clear();
- }
-
- _actionDic.clear();
- }
- int ActionManagerEx::getStudioVersionNumber() const
- {
- return this->_studioVersionNumber;
- }
- }
|