axmol/scripting/lua/cocos2dx_support/CCLuaEngine.cpp

335 lines
9.3 KiB
C++
Raw Normal View History

2012-02-01 16:55:46 +08:00
/****************************************************************************
Copyright (c) 2011 cocos2d-x.org
2012-02-01 16:55:46 +08:00
http://www.cocos2d-x.org
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-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-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"
#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
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();
}
return m_defaultEngine;
}
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);
}
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
}
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)
{
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)
{
int ret = m_stack->executeScriptFile(filename);
m_stack->clean();
return ret;
2012-02-01 16:55:46 +08:00
}
int CCLuaEngine::executeGlobalFunction(const char* functionName)
2012-02-01 16:55:46 +08:00
{
int ret = m_stack->executeGlobalFunction(functionName);
m_stack->clean();
return ret;
2012-02-01 16:55:46 +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;
case kCCNodeOnCleanup:
m_stack->pushString("cleanup");
break;
2013-02-02 02:05:52 +08:00
default:
return 0;
}
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
m_stack->clean();
return ret;
}
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");
int ret = m_stack->executeFunctionByHandler(nHandler, 2);
m_stack->clean();
return ret;
}
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);
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
m_stack->clean();
return ret;
}
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)
{
2013-02-02 02:05:52 +08:00
m_stack->pushCCObject(pTarget, "CCNode");
}
int ret = m_stack->executeFunctionByHandler(nHandler, pTarget ? 1 : 0);
m_stack->clean();
return ret;
}
int CCLuaEngine::executeSchedule(int nHandler, float dt, CCNode* pNode/* = NULL*/)
{
if (!nHandler) return 0;
2013-02-02 02:05:52 +08:00
m_stack->pushFloat(dt);
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
m_stack->clean();
return ret;
}
int CCLuaEngine::executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch)
{
2013-02-02 02:05:52 +08:00
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
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);
int ret = m_stack->executeFunctionByHandler(nHandler, 3);
m_stack->clean();
return ret;
}
int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches)
{
2013-02-02 02:05:52 +08:00
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
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:
return 0;
2012-02-01 16:55:46 +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++);
lua_pushinteger(L, pTouch->getID());
lua_rawseti(L, -2, i++);
}
int ret = m_stack->executeFunctionByHandler(nHandler, 2);
m_stack->clean();
return ret;
}
2013-02-02 02:05:52 +08:00
int CCLuaEngine::executeLayerKeypadEvent(CCLayer* pLayer, int eventType)
{
2013-02-02 02:05:52 +08:00
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptKeypadHandlerEntry();
if (!pScriptHandlerEntry)
return 0;
2013-02-02 02:05:52 +08:00
int nHandler = pScriptHandlerEntry->getHandler();
if (!nHandler) return 0;
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;
}
int ret = m_stack->executeFunctionByHandler(nHandler, 1);
m_stack->clean();
return ret;
}
2013-02-02 02:05:52 +08:00
int CCLuaEngine::executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue)
{
2013-02-02 02:05:52 +08:00
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptAccelerateHandlerEntry();
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);
int ret = m_stack->executeFunctionByHandler(nHandler, 4);
m_stack->clean();
return ret;
}
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
}
int ret = m_stack->executeFunctionByHandler(nHandler, pEventSource ? 2 : 1);
m_stack->clean();
return ret;
2012-02-07 11:43:29 +08:00
}
bool CCLuaEngine::handleAssert(const char *msg)
{
bool ret = m_stack->handleAssert(msg);
m_stack->clean();
return ret;
}
int CCLuaEngine::reallocateScriptHandler(int nHandler)
{
int nRet = m_stack->reallocateScriptHandler(nHandler);
m_stack->clean();
return nRet;
}
2012-02-09 14:07:11 +08:00
NS_CC_END