2013-09-20 22:23:13 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-26 16:29:59 +08:00
|
|
|
|
|
|
|
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-20 22:23:13 +08:00
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "editor-support/cocostudio/CCActionManagerEx.h"
|
|
|
|
#include "editor-support/cocostudio/CocoLoader.h"
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-12-26 16:29:59 +08:00
|
|
|
using namespace cocos2d;
|
2013-10-15 18:00:03 +08:00
|
|
|
|
|
|
|
namespace cocostudio {
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-12-26 23:40:50 +08:00
|
|
|
static ActionManagerEx* sharedActionManager = nullptr;
|
2013-09-20 22:23:13 +08:00
|
|
|
|
2013-12-27 10:43:49 +08:00
|
|
|
ActionManagerEx* ActionManagerEx::getInstance()
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-26 16:29:59 +08:00
|
|
|
if (!sharedActionManager) {
|
2014-08-28 07:31:57 +08:00
|
|
|
sharedActionManager = new (std::nothrow) ActionManagerEx();
|
2013-12-26 16:29:59 +08:00
|
|
|
}
|
|
|
|
return sharedActionManager;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-27 17:21:13 +08:00
|
|
|
void ActionManagerEx::destroyInstance()
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2014-07-17 17:24:41 +08:00
|
|
|
if(sharedActionManager != nullptr)
|
|
|
|
{
|
|
|
|
sharedActionManager->releaseActions();
|
|
|
|
CC_SAFE_DELETE(sharedActionManager);
|
|
|
|
}
|
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ActionManagerEx::ActionManagerEx()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ActionManagerEx::~ActionManagerEx()
|
|
|
|
{
|
2013-12-27 10:43:49 +08:00
|
|
|
_actionDic.clear();
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ActionManagerEx::initWithDictionary(const char* jsonName,const rapidjson::Value &dic, Ref* root)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
std::string path = jsonName;
|
2013-12-25 19:46:09 +08:00
|
|
|
ssize_t pos = path.find_last_of("/");
|
2013-09-20 22:23:13 +08:00
|
|
|
std::string fileName = path.substr(pos+1,path.length());
|
2013-12-25 19:46:09 +08:00
|
|
|
cocos2d::Vector<ActionObject*> actionList;
|
2013-09-20 22:23:13 +08:00
|
|
|
int actionCount = DICTOOL->getArrayCount_json(dic, "actionlist");
|
2013-12-26 16:29:59 +08:00
|
|
|
for (int i=0; i<actionCount; i++) {
|
2014-08-28 07:31:57 +08:00
|
|
|
ActionObject* action = new (std::nothrow) ActionObject();
|
2014-03-25 11:41:16 +08:00
|
|
|
action->autorelease();
|
2013-12-24 20:33:55 +08:00
|
|
|
const rapidjson::Value &actionDic = DICTOOL->getDictionaryFromArray_json(dic, "actionlist", i);
|
2013-12-26 16:29:59 +08:00
|
|
|
action->initWithDictionary(actionDic,root);
|
|
|
|
actionList.pushBack(action);
|
|
|
|
}
|
2014-09-26 11:29:48 +08:00
|
|
|
_actionDic[fileName] = actionList;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2014-06-18 15:55:01 +08:00
|
|
|
|
|
|
|
void ActionManagerEx::initWithBinary(const char* file,
|
|
|
|
cocos2d::Ref *root,
|
2014-06-23 10:02:09 +08:00
|
|
|
CocoLoader* cocoLoader,
|
2014-06-18 15:55:01 +08:00
|
|
|
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;
|
|
|
|
|
2014-07-01 16:31:17 +08:00
|
|
|
stExpCocoNode *stChildArray = pCocoNode->GetChildArray(cocoLoader);
|
2014-06-23 10:12:54 +08:00
|
|
|
stExpCocoNode *actionNode = nullptr;
|
2014-06-18 15:55:01 +08:00
|
|
|
for (int i=0; i < pCocoNode->GetChildNum(); ++i) {
|
2014-06-23 10:02:09 +08:00
|
|
|
std::string key = stChildArray[i].GetName(cocoLoader);
|
2014-06-18 15:55:01 +08:00
|
|
|
if (key == "actionlist") {
|
|
|
|
actionNode = &stChildArray[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-23 10:12:54 +08:00
|
|
|
if (nullptr != actionNode)
|
2014-06-18 15:55:01 +08:00
|
|
|
{
|
|
|
|
int actionCount = actionNode->GetChildNum();
|
|
|
|
for (int i = 0; i < actionCount; ++i) {
|
2014-08-28 07:31:57 +08:00
|
|
|
ActionObject* action = new (std::nothrow) ActionObject();
|
2014-06-18 15:55:01 +08:00
|
|
|
action->autorelease();
|
|
|
|
|
2014-10-12 08:58:28 +08:00
|
|
|
action->initWithBinary(cocoLoader, &actionNode->GetChildArray(cocoLoader)[i], root);
|
2014-06-18 15:55:01 +08:00
|
|
|
|
|
|
|
actionList.pushBack(action);
|
|
|
|
}
|
|
|
|
}
|
2014-09-26 11:29:48 +08:00
|
|
|
_actionDic[fileName] = actionList;
|
2014-06-18 15:55:01 +08:00
|
|
|
|
|
|
|
}
|
2013-09-20 22:23:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
ActionObject* ActionManagerEx::getActionByName(const char* jsonName,const char* actionName)
|
|
|
|
{
|
2014-08-14 20:10:51 +08:00
|
|
|
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);
|
2013-12-27 10:43:49 +08:00
|
|
|
if (iterator == _actionDic.end())
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-26 23:40:50 +08:00
|
|
|
return nullptr;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
2013-12-26 16:29:59 +08:00
|
|
|
auto actionList = iterator->second;
|
2013-12-25 19:46:09 +08:00
|
|
|
for (int i = 0; i < actionList.size(); i++)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
ActionObject* action = actionList.at(i);
|
2013-09-20 22:23:13 +08:00
|
|
|
if (strcmp(actionName, action->getName()) == 0)
|
|
|
|
{
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
}
|
2013-12-26 23:40:50 +08:00
|
|
|
return nullptr;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-11-19 00:57:22 +08:00
|
|
|
ActionObject* ActionManagerEx::playActionByName(const char* jsonName,const char* actionName)
|
2013-09-20 22:23:13 +08:00
|
|
|
{
|
|
|
|
ActionObject* action = getActionByName(jsonName,actionName);
|
|
|
|
if (action)
|
|
|
|
{
|
|
|
|
action->play();
|
|
|
|
}
|
2013-11-19 00:57:22 +08:00
|
|
|
return action;
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 20:33:55 +08:00
|
|
|
ActionObject* ActionManagerEx::playActionByName(const char* jsonName,const char* actionName, CallFunc* func)
|
|
|
|
{
|
|
|
|
ActionObject* action = getActionByName(jsonName,actionName);
|
|
|
|
if (action)
|
|
|
|
{
|
|
|
|
action->play(func);
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
2014-12-11 02:57:13 +08:00
|
|
|
|
|
|
|
ActionObject* ActionManagerEx::stopActionByName(const char* jsonName,const char* actionName)
|
|
|
|
{
|
|
|
|
ActionObject* action = getActionByName(jsonName,actionName);
|
|
|
|
if (action)
|
|
|
|
{
|
|
|
|
action->stop();
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
2014-03-07 11:58:54 +08:00
|
|
|
|
2013-09-20 22:23:13 +08:00
|
|
|
void ActionManagerEx::releaseActions()
|
|
|
|
{
|
2014-03-07 11:58:54 +08:00
|
|
|
std::unordered_map<std::string, cocos2d::Vector<ActionObject*>>::iterator iter;
|
|
|
|
for (iter = _actionDic.begin(); iter != _actionDic.end(); iter++)
|
|
|
|
{
|
|
|
|
cocos2d::Vector<ActionObject*> objList = iter->second;
|
2014-08-21 16:21:23 +08:00
|
|
|
ssize_t listCount = objList.size();
|
|
|
|
for (ssize_t i = 0; i < listCount; i++) {
|
2014-07-17 17:24:41 +08:00
|
|
|
ActionObject* action = objList.at(i);
|
|
|
|
if (action != nullptr) {
|
|
|
|
action->stop();
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 11:58:54 +08:00
|
|
|
objList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
_actionDic.clear();
|
2013-09-20 22:23:13 +08:00
|
|
|
}
|
|
|
|
|
2014-03-25 11:41:16 +08:00
|
|
|
}
|