2014-01-07 11:47:11 +08:00
|
|
|
/****************************************************************************
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "editor-support/cocostudio/TriggerObj.h"
|
2016-04-18 15:09:21 +08:00
|
|
|
#include "base/CCEventListenerCustom.h"
|
2013-12-25 17:40:54 +08:00
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace cocostudio {
|
|
|
|
|
|
|
|
BaseTriggerCondition::BaseTriggerCondition(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseTriggerCondition::~BaseTriggerCondition(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseTriggerCondition::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseTriggerCondition::detect()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseTriggerCondition::serialize(const rapidjson::Value &val)
|
|
|
|
{
|
|
|
|
}
|
2014-06-25 11:58:36 +08:00
|
|
|
|
|
|
|
void BaseTriggerCondition::serialize(cocostudio::CocoLoader *cocoLoader, cocostudio::stExpCocoNode *cocoNode)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-12-25 17:40:54 +08:00
|
|
|
|
|
|
|
void BaseTriggerCondition::removeAll()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseTriggerAction::BaseTriggerAction(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseTriggerAction::~BaseTriggerAction(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseTriggerAction::init()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseTriggerAction::done()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseTriggerAction::serialize(const rapidjson::Value &val)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-25 11:58:36 +08:00
|
|
|
void BaseTriggerAction::serialize(cocostudio::CocoLoader *cocoLoader, cocostudio::stExpCocoNode *cocoNode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-12-25 17:40:54 +08:00
|
|
|
void BaseTriggerAction::removeAll()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerObj::TriggerObj(void)
|
2013-12-25 19:46:09 +08:00
|
|
|
:_id(UINT_MAX)
|
2013-12-30 20:57:46 +08:00
|
|
|
,_enabled(true)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TriggerObj::~TriggerObj(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TriggerObj::init()
|
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
return true;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TriggerObj* TriggerObj::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TriggerObj * pRet = new (std::nothrow) TriggerObj();
|
2013-12-25 17:40:54 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TriggerObj::detect()
|
|
|
|
{
|
2014-03-04 23:55:36 +08:00
|
|
|
if (!_enabled || _cons.empty())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-30 20:57:46 +08:00
|
|
|
|
2014-03-11 22:02:55 +08:00
|
|
|
bool ret = false;
|
2013-12-25 19:46:09 +08:00
|
|
|
|
2013-12-30 20:57:46 +08:00
|
|
|
for (const auto& con : _cons)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2014-03-11 22:02:55 +08:00
|
|
|
ret = ret || con->detect();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-30 20:57:46 +08:00
|
|
|
return ret;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerObj::done()
|
|
|
|
{
|
2014-03-04 23:55:36 +08:00
|
|
|
if (!_enabled || _acts.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-12-25 17:40:54 +08:00
|
|
|
|
2013-12-30 20:57:46 +08:00
|
|
|
for (const auto& act : _acts)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
act->done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerObj::removeAll()
|
|
|
|
{
|
2013-12-30 20:57:46 +08:00
|
|
|
for (const auto& con : _cons)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
con->removeAll();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2013-12-30 20:57:46 +08:00
|
|
|
|
|
|
|
for (const auto& act : _acts)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-25 19:46:09 +08:00
|
|
|
act->removeAll();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
2014-03-04 23:55:36 +08:00
|
|
|
|
|
|
|
for (const auto& lis : _listeners)
|
|
|
|
{
|
|
|
|
TriggerMng::getInstance()->removeEventListener(lis);
|
|
|
|
}
|
2013-12-25 19:46:09 +08:00
|
|
|
|
|
|
|
_cons.clear();
|
|
|
|
_acts.clear();
|
2014-03-04 23:55:36 +08:00
|
|
|
_listeners.clear();
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerObj::serialize(const rapidjson::Value &val)
|
|
|
|
{
|
2014-03-04 23:55:36 +08:00
|
|
|
_id = (unsigned int)(DICTOOL->getIntValue_json(val, "id"));
|
2013-12-25 17:40:54 +08:00
|
|
|
int count = DICTOOL->getArrayCount_json(val, "conditions");
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(val, "conditions", i);
|
|
|
|
const char *classname = DICTOOL->getStringValue_json(subDict, "classname");
|
2013-12-26 01:17:04 +08:00
|
|
|
if (classname == nullptr)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BaseTriggerCondition *con = dynamic_cast<BaseTriggerCondition*>(ObjectFactory::getInstance()->createObject(classname));
|
2013-12-30 15:26:51 +08:00
|
|
|
if(con == nullptr)
|
|
|
|
{
|
|
|
|
CCLOG("class %s can not be implemented!", classname);
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(con != nullptr, "con can't be nullptr!");
|
2013-12-30 15:26:51 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(con != nullptr, "con can't be nullptr!");
|
2013-12-25 17:40:54 +08:00
|
|
|
con->serialize(subDict);
|
2014-03-04 23:55:36 +08:00
|
|
|
con->init();
|
2013-12-25 19:46:09 +08:00
|
|
|
_cons.pushBack(con);
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2014-03-04 23:55:36 +08:00
|
|
|
count = DICTOOL->getArrayCount_json(val, "actions");
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value &subDict = DICTOOL->getSubDictionary_json(val, "actions", i);
|
|
|
|
const char *classname = DICTOOL->getStringValue_json(subDict, "classname");
|
|
|
|
if (classname == nullptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BaseTriggerAction *act = dynamic_cast<BaseTriggerAction*>(ObjectFactory::getInstance()->createObject(classname));
|
|
|
|
if(act == nullptr)
|
2013-12-30 15:26:51 +08:00
|
|
|
{
|
|
|
|
CCLOG("class %s can not be implemented!", classname);
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(act != nullptr, "act can't be nullptr!");
|
2013-12-30 15:26:51 +08:00
|
|
|
}
|
2014-03-04 23:55:36 +08:00
|
|
|
act->serialize(subDict);
|
|
|
|
act->init();
|
|
|
|
_acts.pushBack(act);
|
|
|
|
}
|
|
|
|
|
|
|
|
int length = DICTOOL->getArrayCount_json(val, "events");
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
const rapidjson::Value &sub = DICTOOL->getSubDictionary_json(val, "events", i);
|
|
|
|
int event = DICTOOL->getIntValue_json(sub, "id");
|
|
|
|
if (event < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
char buf[10];
|
2014-03-04 23:55:36 +08:00
|
|
|
sprintf(buf, "%d", event);
|
|
|
|
std::string custom_event_name(buf);
|
|
|
|
|
2014-03-12 14:07:34 +08:00
|
|
|
EventListenerCustom* listener = EventListenerCustom::create(custom_event_name, [=](EventCustom* evt){
|
2014-03-04 23:55:36 +08:00
|
|
|
if (detect())
|
|
|
|
{
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
2014-03-12 14:07:34 +08:00
|
|
|
_listeners.pushBack(listener);
|
|
|
|
TriggerMng::getInstance()->addEventListenerWithFixedPriority(listener, 1);
|
2014-03-04 23:55:36 +08:00
|
|
|
}
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 11:58:36 +08:00
|
|
|
|
2014-07-16 14:50:35 +08:00
|
|
|
void TriggerObj::serialize(cocostudio::CocoLoader *pCocoLoader, cocostudio::stExpCocoNode *pCocoNode)
|
|
|
|
{
|
|
|
|
int length = pCocoNode->GetChildNum();
|
|
|
|
int count = 0;
|
|
|
|
int num = 0;
|
|
|
|
stExpCocoNode *pTriggerObjArray = pCocoNode->GetChildArray(pCocoLoader);
|
|
|
|
for (int i0 = 0; i0 < length; ++i0)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
std::string key = pTriggerObjArray[i0].GetName(pCocoLoader);
|
|
|
|
const char* str0 = pTriggerObjArray[i0].GetValue(pCocoLoader);
|
|
|
|
if (key.compare("id") == 0)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
if (str0 != nullptr)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
_id = atoi(str0);
|
2014-06-25 11:58:36 +08:00
|
|
|
}
|
2014-07-16 14:50:35 +08:00
|
|
|
}
|
|
|
|
else if (key.compare("conditions") == 0)
|
|
|
|
{
|
|
|
|
count = pTriggerObjArray[i0].GetChildNum();
|
|
|
|
stExpCocoNode *pConditionsArray = pTriggerObjArray[i0].GetChildArray(pCocoLoader);
|
|
|
|
for (int i1 = 0; i1 < count; ++i1)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
num = pConditionsArray[i1].GetChildNum();
|
|
|
|
stExpCocoNode *pConditionArray = pConditionsArray[i1].GetChildArray(pCocoLoader);
|
|
|
|
const char *classname = pConditionArray[0].GetValue(pCocoLoader);
|
|
|
|
if (classname == nullptr)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
continue;
|
2014-06-25 11:58:36 +08:00
|
|
|
}
|
2014-07-16 14:50:35 +08:00
|
|
|
BaseTriggerCondition *con = dynamic_cast<BaseTriggerCondition*>(ObjectFactory::getInstance()->createObject(classname));
|
|
|
|
CCAssert(con != nullptr, "class named classname can not implement!");
|
|
|
|
con->serialize(pCocoLoader, &pConditionArray[1]);
|
|
|
|
con->init();
|
|
|
|
_cons.pushBack(con);
|
2014-06-25 11:58:36 +08:00
|
|
|
}
|
2014-07-16 14:50:35 +08:00
|
|
|
}
|
|
|
|
else if (key.compare("actions") == 0)
|
|
|
|
{
|
|
|
|
count = pTriggerObjArray[i0].GetChildNum();
|
|
|
|
stExpCocoNode *pActionsArray = pTriggerObjArray[i0].GetChildArray(pCocoLoader);
|
|
|
|
for (int i2 = 0; i2 < count; ++i2)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
num = pActionsArray[i2].GetChildNum();
|
|
|
|
stExpCocoNode *pActionArray = pActionsArray[i2].GetChildArray(pCocoLoader);
|
|
|
|
const char *classname = pActionArray[0].GetValue(pCocoLoader);
|
|
|
|
if (classname == nullptr)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
continue;
|
2014-06-25 11:58:36 +08:00
|
|
|
}
|
2014-07-16 14:50:35 +08:00
|
|
|
BaseTriggerAction *act = dynamic_cast<BaseTriggerAction*>(ObjectFactory::getInstance()->createObject(classname));
|
|
|
|
CCAssert(act != nullptr, "class named classname can not implement!");
|
|
|
|
act->serialize(pCocoLoader, &pActionArray[1]);
|
|
|
|
act->init();
|
|
|
|
_acts.pushBack(act);
|
2014-06-25 11:58:36 +08:00
|
|
|
}
|
2014-07-16 14:50:35 +08:00
|
|
|
}
|
|
|
|
else if (key.compare("events") == 0)
|
|
|
|
{
|
|
|
|
count = pTriggerObjArray[i0].GetChildNum();
|
|
|
|
stExpCocoNode *pEventsArray = pTriggerObjArray[i0].GetChildArray(pCocoLoader);
|
|
|
|
for (int i3 = 0; i3 < count; ++i3)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
num = pEventsArray[i3].GetChildNum();
|
|
|
|
stExpCocoNode *pEventArray = pEventsArray[i3].GetChildArray(pCocoLoader);
|
|
|
|
const char *str1 = pEventArray[0].GetValue(pCocoLoader);
|
|
|
|
if (str1 == nullptr)
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int event = atoi(str1);
|
|
|
|
if (event < 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-16 14:02:55 +08:00
|
|
|
char buf[10];
|
2014-07-16 14:50:35 +08:00
|
|
|
sprintf(buf, "%d", event);
|
|
|
|
std::string custom_event_name(buf);
|
|
|
|
|
|
|
|
EventListenerCustom* listener = EventListenerCustom::create(custom_event_name, [=](EventCustom* evt){
|
|
|
|
if (detect())
|
2014-06-25 11:58:36 +08:00
|
|
|
{
|
2014-07-16 14:50:35 +08:00
|
|
|
done();
|
2014-06-25 11:58:36 +08:00
|
|
|
}
|
2014-07-16 14:50:35 +08:00
|
|
|
});
|
|
|
|
_listeners.pushBack(listener);
|
|
|
|
TriggerMng::getInstance()->addEventListenerWithFixedPriority(listener, 1);
|
2014-06-25 11:58:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 14:50:35 +08:00
|
|
|
}
|
2014-06-25 11:58:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-12-25 17:40:54 +08:00
|
|
|
unsigned int TriggerObj::getId()
|
|
|
|
{
|
2014-03-04 23:55:36 +08:00
|
|
|
return _id;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-30 20:57:46 +08:00
|
|
|
void TriggerObj::setEnabled(bool enabled)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2014-03-04 23:55:36 +08:00
|
|
|
_enabled = enabled;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|