axmol/extensions/cocostudio/CCActionManagerEx.cpp

194 lines
5.8 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
2020-10-17 16:32:16 +08:00
Copyright (c) 2013-2017 Chukong Technologies Inc.
2019-11-23 20:27:39 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2019-11-23 20:27:39 +08:00
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 "CCActionManagerEx.h"
#include "CocoLoader.h"
2019-11-23 20:27:39 +08:00
USING_NS_AX;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
namespace cocostudio
{
2019-11-23 20:27:39 +08:00
static ActionManagerEx* sharedActionManager = nullptr;
ActionManagerEx* ActionManagerEx::getInstance()
{
2021-12-25 10:04:45 +08:00
if (!sharedActionManager)
{
2021-12-08 00:11:53 +08:00
sharedActionManager = new ActionManagerEx();
2019-11-23 20:27:39 +08:00
}
return sharedActionManager;
}
void ActionManagerEx::destroyInstance()
{
2021-12-25 10:04:45 +08:00
if (sharedActionManager != nullptr)
2019-11-23 20:27:39 +08:00
{
sharedActionManager->releaseActions();
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(sharedActionManager);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
ActionManagerEx::ActionManagerEx() {}
2019-11-23 20:27:39 +08:00
ActionManagerEx::~ActionManagerEx()
{
_actionDic.clear();
}
2021-12-25 10:04:45 +08:00
void ActionManagerEx::initWithDictionary(const char* jsonName, const rapidjson::Value& dic, Ref* root, int version)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
std::string path = jsonName;
2019-11-23 20:27:39 +08:00
this->_studioVersionNumber = version;
2021-12-25 10:04:45 +08:00
ssize_t pos = path.find_last_of("/");
std::string fileName = path.substr(pos + 1, path.length());
2022-08-08 18:02:17 +08:00
ax::Vector<ActionObject*> actionList;
2019-11-23 20:27:39 +08:00
int actionCount = DICTOOL->getArrayCount_json(dic, "actionlist");
2021-12-25 10:04:45 +08:00
for (int i = 0; i < actionCount; i++)
{
2021-12-08 00:11:53 +08:00
ActionObject* action = new ActionObject();
2019-11-23 20:27:39 +08:00
action->autorelease();
2021-12-25 10:04:45 +08:00
const rapidjson::Value& actionDic = DICTOOL->getDictionaryFromArray_json(dic, "actionlist", i);
action->initWithDictionary(actionDic, root);
2019-11-23 20:27:39 +08:00
actionList.pushBack(action);
}
_actionDic[fileName] = actionList;
}
2021-12-25 10:04:45 +08:00
void ActionManagerEx::initWithBinary(const char* file,
2022-08-08 18:02:17 +08:00
ax::Ref* root,
2021-12-25 10:04:45 +08:00
CocoLoader* cocoLoader,
stExpCocoNode* pCocoNode)
{
std::string path = file;
ssize_t pos = path.find_last_of("/");
std::string fileName = path.substr(pos + 1, path.length());
2022-08-08 18:02:17 +08:00
ax::Vector<ActionObject*> actionList;
2021-12-25 10:04:45 +08:00
stExpCocoNode* stChildArray = pCocoNode->GetChildArray(cocoLoader);
stExpCocoNode* actionNode = nullptr;
for (int i = 0; i < pCocoNode->GetChildNum(); ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
std::string key = stChildArray[i].GetName(cocoLoader);
if (key == "actionlist")
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
actionNode = &stChildArray[i];
break;
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
if (nullptr != actionNode)
{
int actionCount = actionNode->GetChildNum();
for (int i = 0; i < actionCount; ++i)
{
ActionObject* action = new ActionObject();
action->autorelease();
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
action->initWithBinary(cocoLoader, &actionNode->GetChildArray(cocoLoader)[i], root);
actionList.pushBack(action);
}
}
_actionDic[fileName] = actionList;
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
ActionObject* ActionManagerEx::getActionByName(const char* jsonName, const char* actionName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +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);
2019-11-23 20:27:39 +08:00
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;
}
2021-12-25 10:04:45 +08:00
ActionObject* ActionManagerEx::playActionByName(const char* jsonName, const char* actionName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ActionObject* action = getActionByName(jsonName, actionName);
2019-11-23 20:27:39 +08:00
if (action)
{
action->play();
}
return action;
}
2021-12-25 10:04:45 +08:00
ActionObject* ActionManagerEx::playActionByName(const char* jsonName, const char* actionName, CallFunc* func)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ActionObject* action = getActionByName(jsonName, actionName);
2019-11-23 20:27:39 +08:00
if (action)
{
action->play(func);
}
return action;
}
2021-12-25 10:04:45 +08:00
ActionObject* ActionManagerEx::stopActionByName(const char* jsonName, const char* actionName)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
ActionObject* action = getActionByName(jsonName, actionName);
2019-11-23 20:27:39 +08:00
if (action)
{
action->stop();
}
return action;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void ActionManagerEx::releaseActions()
{
for (auto&& iter : _actionDic)
2019-11-23 20:27:39 +08:00
{
2022-08-08 18:02:17 +08:00
ax::Vector<ActionObject*> objList = iter.second;
2021-12-25 10:04:45 +08:00
ssize_t listCount = objList.size();
for (ssize_t i = 0; i < listCount; i++)
{
2019-11-23 20:27:39 +08:00
ActionObject* action = objList.at(i);
2021-12-25 10:04:45 +08:00
if (action != nullptr)
{
2019-11-23 20:27:39 +08:00
action->stop();
}
}
objList.clear();
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_actionDic.clear();
}
int ActionManagerEx::getStudioVersionNumber() const
{
return this->_studioVersionNumber;
}
2021-12-25 10:04:45 +08:00
} // namespace cocostudio