2012-02-01 16:55:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 cocos2d-x.org
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
http://www.cocos2d-x.org
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +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:
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
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 "CCLuaEngine.h"
|
2012-02-02 19:13:50 +08:00
|
|
|
#include "cocos2d.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCArray.h"
|
2012-02-02 15:58:10 +08:00
|
|
|
#include "CCScheduler.h"
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaEngine* CCLuaEngine::m_defaultEngine = NULL;
|
|
|
|
|
|
|
|
CCLuaEngine* CCLuaEngine::defaultEngine(void)
|
|
|
|
{
|
|
|
|
if (!m_defaultEngine)
|
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
m_defaultEngine = new CCLuaEngine();
|
|
|
|
m_defaultEngine->init();
|
2012-09-11 15:24:25 +08:00
|
|
|
}
|
|
|
|
return m_defaultEngine;
|
|
|
|
}
|
|
|
|
|
2012-08-25 17:08:33 +08:00
|
|
|
CCLuaEngine::~CCLuaEngine(void)
|
2012-02-07 11:43:29 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_stack);
|
|
|
|
m_defaultEngine = NULL;
|
2012-02-07 11:43:29 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
bool CCLuaEngine::init(void)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
m_stack = CCLuaStack::create();
|
|
|
|
m_stack->retain();
|
2012-02-09 14:07:11 +08:00
|
|
|
return true;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2013-02-04 11:28:09 +08:00
|
|
|
void CCLuaEngine::addSearchPath(const char* path)
|
|
|
|
{
|
|
|
|
m_stack->addSearchPath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLuaEngine::addLuaLoader(lua_CFunction func)
|
|
|
|
{
|
|
|
|
m_stack->addLuaLoader(func);
|
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
void CCLuaEngine::removeScriptObjectByCCObject(CCObject* pObj)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
m_stack->removeScriptObjectByCCObject(pObj);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
void CCLuaEngine::removeScriptHandler(int nHandler)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
m_stack->removeScriptHandler(nHandler);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 19:00:43 +08:00
|
|
|
int CCLuaEngine::executeString(const char *codes)
|
|
|
|
{
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeString(codes);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-02-02 19:00:43 +08:00
|
|
|
}
|
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
int CCLuaEngine::executeScriptFile(const char* filename)
|
|
|
|
{
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeScriptFile(filename);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-04 11:16:59 +08:00
|
|
|
int CCLuaEngine::executeGlobalFunction(const char* functionName)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeGlobalFunction(functionName);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
int CCLuaEngine::executeNodeEvent(CCNode* pNode, int nAction)
|
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
int nHandler = pNode->getScriptHandler();
|
|
|
|
if (!nHandler) return 0;
|
|
|
|
|
|
|
|
switch (nAction)
|
|
|
|
{
|
|
|
|
case kCCNodeOnEnter:
|
|
|
|
m_stack->pushString("enter");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCCNodeOnExit:
|
|
|
|
m_stack->pushString("exit");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCCNodeOnEnterTransitionDidFinish:
|
|
|
|
m_stack->pushString("enterTransitionFinish");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kCCNodeOnExitTransitionDidStart:
|
|
|
|
m_stack->pushString("exitTransitionStart");
|
|
|
|
break;
|
|
|
|
|
2013-04-25 21:29:31 +08:00
|
|
|
case kCCNodeOnCleanup:
|
|
|
|
m_stack->pushString("cleanup");
|
|
|
|
break;
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeMenuItemEvent(CCMenuItem* pMenuItem)
|
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
int nHandler = pMenuItem->getScriptTapHandler();
|
|
|
|
if (!nHandler) return 0;
|
|
|
|
|
|
|
|
m_stack->pushInt(pMenuItem->getTag());
|
|
|
|
m_stack->pushCCObject(pMenuItem, "CCMenuItem");
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 2);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeNotificationEvent(CCNotificationCenter* pNotificationCenter, const char* pszName)
|
|
|
|
{
|
2013-05-24 11:42:27 +08:00
|
|
|
int nHandler = pNotificationCenter->getObserverHandlerByName(pszName);
|
2013-02-02 02:05:52 +08:00
|
|
|
if (!nHandler) return 0;
|
|
|
|
|
|
|
|
m_stack->pushString(pszName);
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeCallFuncActionEvent(CCCallFunc* pAction, CCObject* pTarget/* = NULL*/)
|
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
int nHandler = pAction->getScriptHandler();
|
|
|
|
if (!nHandler) return 0;
|
|
|
|
|
|
|
|
if (pTarget)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
m_stack->pushCCObject(pTarget, "CCNode");
|
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, pTarget ? 1 : 0);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2012-12-10 13:48:27 +08:00
|
|
|
int CCLuaEngine::executeSchedule(int nHandler, float dt, CCNode* pNode/* = NULL*/)
|
|
|
|
{
|
2013-03-18 15:29:53 +08:00
|
|
|
if (!nHandler) return 0;
|
2013-02-02 02:05:52 +08:00
|
|
|
m_stack->pushFloat(dt);
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-12-10 13:48:27 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
int CCLuaEngine::executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch)
|
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
|
2013-04-25 21:29:31 +08:00
|
|
|
if (!pScriptHandlerEntry) return 0;
|
2013-02-02 02:05:52 +08:00
|
|
|
int nHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
if (!nHandler) return 0;
|
|
|
|
|
|
|
|
switch (eventType)
|
|
|
|
{
|
|
|
|
case CCTOUCHBEGAN:
|
|
|
|
m_stack->pushString("began");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CCTOUCHMOVED:
|
|
|
|
m_stack->pushString("moved");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CCTOUCHENDED:
|
|
|
|
m_stack->pushString("ended");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CCTOUCHCANCELLED:
|
|
|
|
m_stack->pushString("cancelled");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CCPoint pt = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
|
|
|
|
m_stack->pushFloat(pt.x);
|
|
|
|
m_stack->pushFloat(pt.y);
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 3);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches)
|
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
|
2013-04-25 21:29:31 +08:00
|
|
|
if (!pScriptHandlerEntry) return 0;
|
2013-02-02 02:05:52 +08:00
|
|
|
int nHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
if (!nHandler) return 0;
|
2012-12-26 18:33:55 +08:00
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
switch (eventType)
|
|
|
|
{
|
|
|
|
case CCTOUCHBEGAN:
|
|
|
|
m_stack->pushString("began");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CCTOUCHMOVED:
|
|
|
|
m_stack->pushString("moved");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CCTOUCHENDED:
|
|
|
|
m_stack->pushString("ended");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CCTOUCHCANCELLED:
|
|
|
|
m_stack->pushString("cancelled");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2012-03-07 20:27:26 +08:00
|
|
|
return 0;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
2012-03-07 20:27:26 +08:00
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
|
|
|
lua_State *L = m_stack->getLuaState();
|
|
|
|
lua_newtable(L);
|
|
|
|
int i = 1;
|
|
|
|
for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); ++it)
|
|
|
|
{
|
|
|
|
CCTouch* pTouch = (CCTouch*)*it;
|
|
|
|
CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView());
|
|
|
|
lua_pushnumber(L, pt.x);
|
|
|
|
lua_rawseti(L, -2, i++);
|
|
|
|
lua_pushnumber(L, pt.y);
|
|
|
|
lua_rawseti(L, -2, i++);
|
2013-04-25 21:29:31 +08:00
|
|
|
lua_pushinteger(L, pTouch->getID());
|
|
|
|
lua_rawseti(L, -2, i++);
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 2);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
int CCLuaEngine::executeLayerKeypadEvent(CCLayer* pLayer, int eventType)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptKeypadHandlerEntry();
|
2013-03-26 04:24:47 +08:00
|
|
|
if (!pScriptHandlerEntry)
|
|
|
|
return 0;
|
2013-02-02 02:05:52 +08:00
|
|
|
int nHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
if (!nHandler) return 0;
|
2012-08-25 15:05:59 +08:00
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
switch (eventType)
|
|
|
|
{
|
|
|
|
case kTypeBackClicked:
|
|
|
|
m_stack->pushString("backClicked");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kTypeMenuClicked:
|
|
|
|
m_stack->pushString("menuClicked");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
int CCLuaEngine::executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptAccelerateHandlerEntry();
|
2013-03-26 04:24:47 +08:00
|
|
|
if (!pScriptHandlerEntry)
|
|
|
|
return 0;
|
2013-02-02 02:05:52 +08:00
|
|
|
int nHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
if (!nHandler) return 0;
|
|
|
|
|
|
|
|
m_stack->pushFloat(pAccelerationValue->x);
|
|
|
|
m_stack->pushFloat(pAccelerationValue->y);
|
|
|
|
m_stack->pushFloat(pAccelerationValue->z);
|
|
|
|
m_stack->pushFloat(pAccelerationValue->timestamp);
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, 4);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
|
|
|
|
2013-02-02 02:05:52 +08:00
|
|
|
int CCLuaEngine::executeEvent(int nHandler, const char* pEventName, CCObject* pEventSource /* = NULL*/, const char* pEventSourceClassName /* = NULL*/)
|
2012-02-07 11:43:29 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
m_stack->pushString(pEventName);
|
|
|
|
if (pEventSource)
|
2012-02-07 11:43:29 +08:00
|
|
|
{
|
2013-02-02 02:05:52 +08:00
|
|
|
m_stack->pushCCObject(pEventSource, pEventSourceClassName ? pEventSourceClassName : "CCObject");
|
2012-02-07 11:43:29 +08:00
|
|
|
}
|
2013-04-25 21:29:31 +08:00
|
|
|
int ret = m_stack->executeFunctionByHandler(nHandler, pEventSource ? 2 : 1);
|
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-02-07 11:43:29 +08:00
|
|
|
}
|
|
|
|
|
2013-04-15 22:50:07 +08:00
|
|
|
bool CCLuaEngine::handleAssert(const char *msg)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
2013-04-15 22:50:07 +08:00
|
|
|
bool ret = m_stack->handleAssert(msg);
|
2013-04-25 21:29:31 +08:00
|
|
|
m_stack->clean();
|
|
|
|
return ret;
|
2012-03-07 20:27:26 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_END
|