2013-12-25 17:40:54 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-25 17:40:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "TriggerMng.h"
|
2013-12-30 20:57:46 +08:00
|
|
|
#include "json/filestream.h"
|
|
|
|
#include "json/prettywriter.h"
|
|
|
|
#include "json/stringbuffer.h"
|
2013-12-25 17:40:54 +08:00
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace cocostudio {
|
|
|
|
|
2013-12-27 17:21:13 +08:00
|
|
|
TriggerMng* TriggerMng::_sharedTriggerMng = nullptr;
|
2013-12-25 17:40:54 +08:00
|
|
|
|
|
|
|
TriggerMng::TriggerMng(void)
|
2013-12-27 20:54:05 +08:00
|
|
|
: _movementDispatches(new std::unordered_map<Armature*, ArmatureMovementDispatcher*>)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerMng::~TriggerMng(void)
|
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
removeAll();
|
2013-12-25 19:46:09 +08:00
|
|
|
_eventTriggers.clear();
|
|
|
|
_triggerObjs.clear();
|
2013-12-25 17:40:54 +08:00
|
|
|
|
|
|
|
removeAllArmatureMovementCallBack();
|
|
|
|
CC_SAFE_DELETE(_movementDispatches);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* TriggerMng::triggerMngVersion()
|
|
|
|
{
|
|
|
|
return "1.0.0.0";
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerMng* TriggerMng::getInstance()
|
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
if (nullptr == _sharedTriggerMng)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
_sharedTriggerMng = new TriggerMng();
|
|
|
|
}
|
|
|
|
return _sharedTriggerMng;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerMng::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedTriggerMng);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerMng::parse(const rapidjson::Value &root)
|
|
|
|
{
|
|
|
|
CCLOG("%s", triggerMngVersion());
|
2013-12-30 15:26:51 +08:00
|
|
|
int count = DICTOOL->getArrayCount_json(root, "Triggers");
|
2014-02-20 16:40:46 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-12-30 20:57:46 +08:00
|
|
|
ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
bool useBindings = engine != nullptr;
|
2013-12-30 21:06:57 +08:00
|
|
|
|
2013-12-30 20:57:46 +08:00
|
|
|
if (useBindings)
|
|
|
|
{
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(root, "Triggers");
|
|
|
|
rapidjson::StringBuffer buffer;
|
|
|
|
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
|
|
|
|
subDict.Accept(writer);
|
|
|
|
|
|
|
|
engine->parseConfig(ScriptEngineProtocol::ConfigType::COCOSTUDIO, buffer.GetString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif // #if CC_ENABLE_SCRIPT_BINDING
|
2013-12-30 20:57:46 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(root, "Triggers", i);
|
|
|
|
TriggerObj *obj = TriggerObj::create();
|
|
|
|
obj->serialize(subDict);
|
|
|
|
auto &vInt = obj->getEvents();
|
|
|
|
for (const auto& e : vInt)
|
|
|
|
{
|
|
|
|
add((unsigned int)e, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
_triggerObjs.insert(std::pair<unsigned int, TriggerObj*>(obj->getId(), obj));
|
|
|
|
}
|
|
|
|
}
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-26 01:17:04 +08:00
|
|
|
cocos2d::Vector<TriggerObj*>* TriggerMng::get(unsigned int event) const
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2014-02-17 22:10:45 +08:00
|
|
|
CCASSERT(event != 0, "Argument must be larger than 0");
|
2013-12-25 19:46:09 +08:00
|
|
|
|
2013-12-27 20:54:05 +08:00
|
|
|
auto iter = _eventTriggers.find(event);
|
2013-12-26 01:17:04 +08:00
|
|
|
if (iter == _eventTriggers.end())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return iter->second;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TriggerObj* TriggerMng::getTriggerObj(unsigned int id) const
|
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
auto iter = _triggerObjs.find(id);
|
2013-12-26 01:17:04 +08:00
|
|
|
if (iter == _triggerObjs.end())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return iter->second;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-27 20:54:05 +08:00
|
|
|
bool TriggerMng::add(unsigned int event, TriggerObj *obj)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
bool ret = false;
|
2014-02-17 22:10:45 +08:00
|
|
|
CCASSERT(obj != nullptr, "Argument must be non-nil");
|
2013-12-25 17:40:54 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
auto iterator = _eventTriggers.find(event);
|
|
|
|
if (iterator == _eventTriggers.end())
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
auto array = new cocos2d::Vector<TriggerObj*>();
|
|
|
|
array->pushBack(obj);
|
|
|
|
_eventTriggers.insert(std::make_pair(event, array));
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2013-12-25 19:46:09 +08:00
|
|
|
else
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-26 01:17:04 +08:00
|
|
|
Vector<TriggerObj*>* temp = iterator->second;
|
2013-12-27 20:54:05 +08:00
|
|
|
if(temp->find(obj) == temp->end())
|
2013-12-25 19:46:09 +08:00
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
temp->pushBack(obj);
|
2013-12-25 19:46:09 +08:00
|
|
|
}
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2013-12-27 20:54:05 +08:00
|
|
|
ret = true;
|
2013-12-25 17:40:54 +08:00
|
|
|
} while(0);
|
2013-12-27 20:54:05 +08:00
|
|
|
return ret;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerMng::removeAll(void)
|
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
auto etIter = _eventTriggers.begin();
|
|
|
|
for (;etIter != _eventTriggers.end(); ++etIter)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
for (auto toIter = etIter->second->begin(); toIter != etIter->second->end(); ++toIter)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
(*toIter)->removeAll();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2013-12-27 20:54:05 +08:00
|
|
|
etIter->second->clear();
|
|
|
|
CC_SAFE_DELETE(etIter->second);
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2013-12-25 19:46:09 +08:00
|
|
|
_eventTriggers.clear();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TriggerMng::remove(unsigned int event)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
2014-02-17 22:10:45 +08:00
|
|
|
CCASSERT(event != 0, "event must be larger than 0");
|
2013-12-25 17:40:54 +08:00
|
|
|
do
|
2013-12-25 19:46:09 +08:00
|
|
|
{
|
|
|
|
auto iterator = _eventTriggers.find(event);
|
|
|
|
if(iterator != _eventTriggers.end())
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
for(auto &obj : *iterator->second)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
obj->removeAll();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2013-12-26 01:17:04 +08:00
|
|
|
iterator->second->clear();
|
|
|
|
CC_SAFE_DELETE(iterator->second);
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2013-12-25 19:46:09 +08:00
|
|
|
|
|
|
|
_eventTriggers.erase(event);
|
|
|
|
|
2013-12-25 17:40:54 +08:00
|
|
|
bRet = true;
|
|
|
|
} while(0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TriggerMng::remove(unsigned int event, TriggerObj *Obj)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
2014-02-17 22:10:45 +08:00
|
|
|
CCASSERT(event != 0, "event must be larger than 0");
|
|
|
|
CCASSERT(Obj != 0, "TriggerObj must be not 0");
|
2013-12-25 17:40:54 +08:00
|
|
|
do
|
2013-12-25 19:46:09 +08:00
|
|
|
{
|
|
|
|
auto iterator = _eventTriggers.find(event);
|
|
|
|
if(iterator != _eventTriggers.end())
|
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
for(auto &triobj : *iterator->second)
|
2013-12-25 19:46:09 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
if (triobj != nullptr && triobj == Obj)
|
2013-12-25 19:46:09 +08:00
|
|
|
{
|
|
|
|
triobj->removeAll();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-26 01:17:04 +08:00
|
|
|
iterator->second->eraseObject(Obj);
|
2013-12-25 19:46:09 +08:00
|
|
|
}
|
|
|
|
|
2013-12-25 17:40:54 +08:00
|
|
|
bRet = true;
|
|
|
|
} while(0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TriggerMng::removeTriggerObj(unsigned int id)
|
|
|
|
{
|
|
|
|
TriggerObj *obj = getTriggerObj(id);
|
2013-12-27 17:21:13 +08:00
|
|
|
if (obj == nullptr)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-27 20:54:05 +08:00
|
|
|
auto &_vInt = obj->getEvents();
|
|
|
|
for (auto iter = _vInt.begin(); iter != _vInt.end(); ++iter)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
remove(*iter, obj);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TriggerMng::isEmpty(void) const
|
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
return _eventTriggers.empty();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void TriggerMng::addArmatureMovementCallBack(Armature *pAr, Ref *pTarget, SEL_MovementEventCallFunc mecf)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
if (pAr == nullptr || _movementDispatches == nullptr || pTarget == nullptr || mecf == nullptr)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:54:05 +08:00
|
|
|
auto iter = _movementDispatches->find(pAr);
|
2013-12-27 17:21:13 +08:00
|
|
|
ArmatureMovementDispatcher *amd = nullptr;
|
2013-12-25 17:40:54 +08:00
|
|
|
if (iter == _movementDispatches->end())
|
|
|
|
{
|
|
|
|
amd = new ArmatureMovementDispatcher();
|
2013-12-26 01:17:04 +08:00
|
|
|
pAr->getAnimation()->setMovementEventCallFunc(CC_CALLBACK_0(ArmatureMovementDispatcher::animationEvent, amd, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
2013-12-30 15:26:51 +08:00
|
|
|
amd->addAnimationEventCallBack(pTarget, mecf);
|
2013-12-27 20:54:05 +08:00
|
|
|
_movementDispatches->insert(std::make_pair(pAr, amd));
|
2013-12-26 01:17:04 +08:00
|
|
|
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
amd = iter->second;
|
2013-12-30 15:26:51 +08:00
|
|
|
amd->addAnimationEventCallBack(pTarget, mecf);
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void TriggerMng::removeArmatureMovementCallBack(Armature *pAr, Ref *pTarget, SEL_MovementEventCallFunc mecf)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
if (pAr == nullptr || _movementDispatches == nullptr || pTarget == nullptr || mecf == nullptr)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 17:21:13 +08:00
|
|
|
|
|
|
|
auto iter =_movementDispatches->find(pAr);
|
|
|
|
ArmatureMovementDispatcher *amd = nullptr;
|
2013-12-25 17:40:54 +08:00
|
|
|
if (iter == _movementDispatches->end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
amd = iter->second;
|
|
|
|
amd->removeAnnimationEventCallBack(pTarget, mecf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerMng::removeArmatureAllMovementCallBack(Armature *pAr)
|
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
if (pAr == nullptr)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-27 17:21:13 +08:00
|
|
|
auto iter = _movementDispatches->find(pAr);
|
2013-12-25 17:40:54 +08:00
|
|
|
if (iter == _movementDispatches->end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(iter->second);
|
|
|
|
_movementDispatches->erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerMng::removeAllArmatureMovementCallBack()
|
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
auto iter = _movementDispatches->begin();
|
2013-12-25 17:40:54 +08:00
|
|
|
while (iter != _movementDispatches->end())
|
|
|
|
{
|
|
|
|
removeArmatureAllMovementCallBack(iter->first);
|
|
|
|
}
|
|
|
|
_movementDispatches->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ArmatureMovementDispatcher::ArmatureMovementDispatcher(void)
|
2013-12-26 01:17:04 +08:00
|
|
|
: _mapEventAnimation(nullptr)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
_mapEventAnimation = new std::unordered_map<Ref*, SEL_MovementEventCallFunc> ;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ArmatureMovementDispatcher::~ArmatureMovementDispatcher(void)
|
|
|
|
{
|
|
|
|
_mapEventAnimation->clear();
|
|
|
|
CC_SAFE_DELETE(_mapEventAnimation);
|
|
|
|
}
|
|
|
|
|
2013-12-26 01:17:04 +08:00
|
|
|
void ArmatureMovementDispatcher::animationEvent(Armature *armature, MovementEventType movementType, const std::string& movementID)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
for (auto iter = _mapEventAnimation->begin(); iter != _mapEventAnimation->end(); ++iter)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
(iter->first->*iter->second)(armature, movementType, movementID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ArmatureMovementDispatcher::addAnimationEventCallBack(Ref *pTarget, SEL_MovementEventCallFunc mecf)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-27 17:21:13 +08:00
|
|
|
_mapEventAnimation->insert(std::make_pair(pTarget, mecf));
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void ArmatureMovementDispatcher::removeAnnimationEventCallBack(Ref *pTarget, SEL_MovementEventCallFunc mecf)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
_mapEventAnimation->erase(pTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|