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"
|
2013-12-22 22:10:21 +08:00
|
|
|
#include "tolua_fix.h"
|
2012-02-02 19:13:50 +08:00
|
|
|
#include "cocos2d.h"
|
2013-10-16 17:19:02 +08:00
|
|
|
#include "CCArray.h"
|
2012-02-02 15:58:10 +08:00
|
|
|
#include "CCScheduler.h"
|
2013-10-16 17:19:02 +08:00
|
|
|
#include "extensions/GUI/CCControlExtension/CCControl.h"
|
2013-07-27 00:28:06 +08:00
|
|
|
#include "LuaOpengl.h"
|
2013-12-22 22:10:21 +08:00
|
|
|
#include "lua_cocos2dx_manual.hpp"
|
2013-09-04 14:02:22 +08:00
|
|
|
#include "lua_cocos2dx_extension_manual.h"
|
2013-11-09 22:58:40 +08:00
|
|
|
#include "lua_cocos2dx_coco_studio_manual.hpp"
|
2014-01-03 10:39:19 +08:00
|
|
|
#include "lua_cocos2dx_gui_manual.hpp"
|
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
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaEngine* LuaEngine::_defaultEngine = NULL;
|
2012-09-11 15:24:25 +08:00
|
|
|
|
2013-07-22 13:42:58 +08:00
|
|
|
LuaEngine* LuaEngine::getInstance(void)
|
2012-09-11 15:24:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_defaultEngine)
|
2012-09-11 15:24:25 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
_defaultEngine = new LuaEngine();
|
2013-06-15 14:03:30 +08:00
|
|
|
_defaultEngine->init();
|
2012-09-11 15:24:25 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
return _defaultEngine;
|
2012-09-11 15:24:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
LuaEngine::~LuaEngine(void)
|
2012-02-07 11:43:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_stack);
|
|
|
|
_defaultEngine = NULL;
|
2012-02-07 11:43:29 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool LuaEngine::init(void)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
_stack = LuaStack::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
_stack->retain();
|
2013-08-28 15:11:19 +08:00
|
|
|
executeScriptFile("DeprecatedEnum.lua");
|
|
|
|
executeScriptFile("DeprecatedClass.lua");
|
2013-07-18 23:27:07 +08:00
|
|
|
executeScriptFile("Deprecated.lua");
|
2012-02-09 14:07:11 +08:00
|
|
|
return true;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaEngine::addSearchPath(const char* path)
|
2013-02-04 11:28:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stack->addSearchPath(path);
|
2013-02-04 11:28:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaEngine::addLuaLoader(lua_CFunction func)
|
2013-02-04 11:28:09 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stack->addLuaLoader(func);
|
2013-02-04 11:28:09 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaEngine::removeScriptObjectByObject(Object* pObj)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
_stack->removeScriptObjectByObject(pObj);
|
2013-07-15 10:38:16 +08:00
|
|
|
ScriptHandlerMgr::getInstance()->removeObjectAllHandlers(pObj);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
void LuaEngine::removeScriptHandler(int nHandler)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stack->removeScriptHandler(nHandler);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeString(const char *codes)
|
2012-02-02 19:00:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int ret = _stack->executeString(codes);
|
|
|
|
_stack->clean();
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2012-02-02 19:00:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeScriptFile(const char* filename)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int ret = _stack->executeScriptFile(filename);
|
|
|
|
_stack->clean();
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeGlobalFunction(const char* functionName)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int ret = _stack->executeGlobalFunction(functionName);
|
|
|
|
_stack->clean();
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeNodeEvent(Node* pNode, int nAction)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
return 0;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeMenuItemEvent(MenuItem* pMenuItem)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
return 0;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
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;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_stack->pushString(pszName);
|
|
|
|
int ret = _stack->executeFunctionByHandler(nHandler, 1);
|
|
|
|
_stack->clean();
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeCallFuncActionEvent(CallFunc* pAction, Object* pTarget/* = NULL*/)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
return 0;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeSchedule(int nHandler, float dt, Node* pNode/* = NULL*/)
|
2012-12-10 13:48:27 +08:00
|
|
|
{
|
2013-03-18 15:29:53 +08:00
|
|
|
if (!nHandler) return 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
_stack->pushFloat(dt);
|
|
|
|
int ret = _stack->executeFunctionByHandler(nHandler, 1);
|
|
|
|
_stack->clean();
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2012-12-10 13:48:27 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
return 0;
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
return 0;
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeLayerKeypadEvent(Layer* pLayer, int eventType)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
return 0;
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
return 0;
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::executeEvent(int nHandler, const char* pEventName, Object* pEventSource /* = NULL*/, const char* pEventSourceClassName /* = NULL*/)
|
2012-02-07 11:43:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_stack->pushString(pEventName);
|
2013-02-02 02:05:52 +08:00
|
|
|
if (pEventSource)
|
2012-02-07 11:43:29 +08:00
|
|
|
{
|
2013-06-20 14:33:59 +08:00
|
|
|
_stack->pushObject(pEventSource, pEventSourceClassName ? pEventSourceClassName : "CCObject");
|
2012-02-07 11:43:29 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
int ret = _stack->executeFunctionByHandler(nHandler, pEventSource ? 2 : 1);
|
|
|
|
_stack->clean();
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2012-02-07 11:43:29 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
bool LuaEngine::handleAssert(const char *msg)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
bool ret = _stack->handleAssert(msg);
|
|
|
|
_stack->clean();
|
2013-04-25 21:29:31 +08:00
|
|
|
return ret;
|
2012-03-07 20:27:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:33:59 +08:00
|
|
|
int LuaEngine::reallocateScriptHandler(int nHandler)
|
2013-06-05 15:17:00 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int nRet = _stack->reallocateScriptHandler(nHandler);
|
|
|
|
_stack->clean();
|
2013-06-05 15:17:00 +08:00
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
|
2013-12-30 21:04:00 +08:00
|
|
|
bool LuaEngine::parseConfig(ConfigType type, const std::string& str)
|
|
|
|
{
|
2014-01-02 12:00:37 +08:00
|
|
|
lua_getglobal(_stack->getLuaState(), "__onParseConfig");
|
|
|
|
if (!lua_isfunction(_stack->getLuaState(), -1))
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", "__onParseConfig");
|
|
|
|
lua_pop(_stack->getLuaState(), 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_stack->pushInt((int)type);
|
|
|
|
_stack->pushString(str.c_str());
|
|
|
|
|
|
|
|
return _stack->executeFunction(2);
|
2013-12-30 21:04:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int LuaEngine::sendEvent(ScriptEvent* evt)
|
2013-07-01 15:04:14 +08:00
|
|
|
{
|
2013-07-22 17:12:53 +08:00
|
|
|
if (NULL == evt)
|
2013-07-01 15:04:14 +08:00
|
|
|
return 0;
|
2013-07-22 17:12:53 +08:00
|
|
|
|
|
|
|
switch (evt->type)
|
2013-07-01 15:04:14 +08:00
|
|
|
{
|
2013-07-02 15:23:51 +08:00
|
|
|
case kNodeEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleNodeEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-04 15:44:42 +08:00
|
|
|
case kMenuClickedEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleMenuClickedEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-02 15:23:51 +08:00
|
|
|
case kCallFuncEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleCallFuncActionEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-02 15:23:51 +08:00
|
|
|
case kScheduleEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleScheduler(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kTouchEvent:
|
|
|
|
{
|
|
|
|
return handleTouchEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-04 15:44:42 +08:00
|
|
|
case kTouchesEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleTouchesEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-04 15:44:42 +08:00
|
|
|
case kKeypadEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleKeypadEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-02 15:23:51 +08:00
|
|
|
case kAccelerometerEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleAccelerometerEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-02 15:23:51 +08:00
|
|
|
case kCommonEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handleCommonEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-16 09:55:06 +08:00
|
|
|
case kControlEvent:
|
2013-07-22 17:12:53 +08:00
|
|
|
{
|
|
|
|
return handlerControlEvent(evt->data);
|
|
|
|
}
|
|
|
|
break;
|
2013-07-01 15:04:14 +08:00
|
|
|
default:
|
2013-07-22 17:12:53 +08:00
|
|
|
break;
|
2013-07-01 15:04:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
int LuaEngine::handleNodeEvent(void* data)
|
2013-07-01 15:04:14 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* basicScriptData = (BasicScriptData*)data;
|
|
|
|
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
|
2013-07-01 15:04:14 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::NODE);
|
2013-07-01 15:04:14 +08:00
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
int action = *((int*)(basicScriptData->value));
|
2013-07-01 15:04:14 +08:00
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case kNodeOnEnter:
|
|
|
|
_stack->pushString("enter");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kNodeOnExit:
|
|
|
|
_stack->pushString("exit");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kNodeOnEnterTransitionDidFinish:
|
|
|
|
_stack->pushString("enterTransitionFinish");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kNodeOnExitTransitionDidStart:
|
|
|
|
_stack->pushString("exitTransitionStart");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kNodeOnCleanup:
|
|
|
|
_stack->pushString("cleanup");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 1);
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
int LuaEngine::handleMenuClickedEvent(void* data)
|
2013-07-01 15:04:14 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == data)
|
2013-07-01 15:04:14 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
BasicScriptData* basicScriptData = (BasicScriptData*)data;
|
|
|
|
if (NULL == basicScriptData->nativeObject)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
MenuItem* menuItem = static_cast<MenuItem*>(basicScriptData->nativeObject);
|
2013-07-01 15:04:14 +08:00
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(menuItem, ScriptHandlerMgr::HandlerType::MENU_CLICKED);
|
2013-07-01 15:04:14 +08:00
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
_stack->pushInt(menuItem->getTag());
|
2013-08-14 21:35:55 +08:00
|
|
|
_stack->pushObject(menuItem, "MenuItem");
|
2013-07-01 15:04:14 +08:00
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 2);
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
int LuaEngine::handleCallFuncActionEvent(void* data)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
2013-07-04 17:54:02 +08:00
|
|
|
if (NULL == basicScriptData->nativeObject)
|
2013-07-02 15:23:51 +08:00
|
|
|
return 0;
|
2013-07-15 10:38:16 +08:00
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler =ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::CALLFUNC);
|
2013-07-04 15:44:42 +08:00
|
|
|
|
2013-07-10 15:04:00 +08:00
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
2013-07-15 10:38:16 +08:00
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
Object* target = static_cast<Object*>(basicScriptData->value);
|
2013-07-02 15:23:51 +08:00
|
|
|
if (NULL != target)
|
|
|
|
{
|
2013-08-14 21:35:55 +08:00
|
|
|
_stack->pushObject(target, "Node");
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
|
|
|
int ret = _stack->executeFunctionByHandler(handler, target ? 1 : 0);
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngine::handleScheduler(void* data)
|
|
|
|
{
|
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
SchedulerScriptData* schedulerInfo = static_cast<SchedulerScriptData*>(data);
|
2013-07-02 15:23:51 +08:00
|
|
|
|
|
|
|
_stack->pushFloat(schedulerInfo->elapse);
|
|
|
|
int ret = _stack->executeFunctionByHandler(schedulerInfo->handler, 1);
|
|
|
|
_stack->clean();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
int LuaEngine::handleKeypadEvent(void* data)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == data)
|
2013-07-02 15:23:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
KeypadScriptData* keypadScriptData = static_cast<KeypadScriptData*>(data);
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == keypadScriptData->nativeObject)
|
2013-07-02 15:23:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(keypadScriptData->nativeObject, ScriptHandlerMgr::HandlerType::KEYPAD);
|
2013-07-16 09:55:06 +08:00
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-20 20:09:39 +08:00
|
|
|
EventKeyboard::KeyCode action = keypadScriptData->actionType;
|
2013-09-17 23:21:45 +08:00
|
|
|
|
|
|
|
switch(action)
|
|
|
|
{
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventKeyboard::KeyCode::KEY_BACKSPACE:
|
2013-09-17 23:21:45 +08:00
|
|
|
_stack->pushString("backClicked");
|
|
|
|
break;
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventKeyboard::KeyCode::KEY_MENU:
|
2013-09-17 23:21:45 +08:00
|
|
|
_stack->pushString("menuClicked");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-07-16 09:55:06 +08:00
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 1);
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
2013-07-04 15:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngine::handleAccelerometerEvent(void* data)
|
|
|
|
{
|
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::ACCELEROMETER);
|
2013-07-15 10:38:16 +08:00
|
|
|
if (0 == handler)
|
2013-07-04 15:44:42 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
Acceleration* accelerationValue = static_cast<Acceleration*>(basicScriptData->value);
|
2013-07-04 15:44:42 +08:00
|
|
|
_stack->pushFloat(accelerationValue->x);
|
|
|
|
_stack->pushFloat(accelerationValue->y);
|
|
|
|
_stack->pushFloat(accelerationValue->z);
|
|
|
|
_stack->pushFloat(accelerationValue->timestamp);
|
2013-07-15 10:38:16 +08:00
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 4);
|
2013-07-04 15:44:42 +08:00
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngine::handleCommonEvent(void* data)
|
|
|
|
{
|
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
CommonScriptData* commonInfo = static_cast<CommonScriptData*>(data);
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == commonInfo->eventName || 0 == commonInfo->handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
_stack->pushString(commonInfo->eventName);
|
|
|
|
if (NULL != commonInfo->eventSource)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL != commonInfo->eventSourceClassName && strlen(commonInfo->eventSourceClassName) > 0)
|
|
|
|
{
|
|
|
|
_stack->pushObject(commonInfo->eventSource, commonInfo->eventSourceClassName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-04 14:02:22 +08:00
|
|
|
_stack->pushObject(commonInfo->eventSource, "Object");
|
2013-07-04 15:44:42 +08:00
|
|
|
}
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2013-07-04 15:44:42 +08:00
|
|
|
int ret = _stack->executeFunctionByHandler(commonInfo->handler, commonInfo->eventSource ? 2 : 1);
|
2013-07-02 15:23:51 +08:00
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
int LuaEngine::handleTouchEvent(void* data)
|
|
|
|
{
|
|
|
|
if (NULL == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
TouchScriptData* touchScriptData = static_cast<TouchScriptData*>(data);
|
|
|
|
if (NULL == touchScriptData->nativeObject || NULL == touchScriptData->touch)
|
|
|
|
return 0;
|
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchScriptData->nativeObject, ScriptHandlerMgr::HandlerType::TOUCHES);
|
2013-07-22 17:12:53 +08:00
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (touchScriptData->actionType)
|
|
|
|
{
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::BEGAN:
|
2013-07-22 17:12:53 +08:00
|
|
|
_stack->pushString("began");
|
|
|
|
break;
|
|
|
|
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::MOVED:
|
2013-07-22 17:12:53 +08:00
|
|
|
_stack->pushString("moved");
|
|
|
|
break;
|
|
|
|
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::ENDED:
|
2013-07-22 17:12:53 +08:00
|
|
|
_stack->pushString("ended");
|
|
|
|
break;
|
|
|
|
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::CANCELLED:
|
2013-07-22 17:12:53 +08:00
|
|
|
_stack->pushString("cancelled");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
Touch* touch = touchScriptData->touch;
|
|
|
|
if (NULL != touch) {
|
|
|
|
const Point pt = Director::getInstance()->convertToGL(touch->getLocationInView());
|
|
|
|
_stack->pushFloat(pt.x);
|
|
|
|
_stack->pushFloat(pt.y);
|
|
|
|
ret = _stack->executeFunctionByHandler(handler, 3);
|
|
|
|
}
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-04 15:44:42 +08:00
|
|
|
int LuaEngine::handleTouchesEvent(void* data)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
if (NULL == data)
|
2013-07-02 15:23:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
TouchesScriptData* touchesScriptData = static_cast<TouchesScriptData*>(data);
|
2013-09-12 21:12:46 +08:00
|
|
|
if (NULL == touchesScriptData->nativeObject || touchesScriptData->touches.size() == 0)
|
2013-07-04 15:44:42 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-04 22:17:26 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchesScriptData->nativeObject, ScriptHandlerMgr::HandlerType::TOUCHES);
|
2013-07-02 15:23:51 +08:00
|
|
|
|
2013-07-15 10:38:16 +08:00
|
|
|
if (0 == handler)
|
2013-07-02 15:23:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-16 09:55:06 +08:00
|
|
|
switch (touchesScriptData->actionType)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::BEGAN:
|
2013-07-02 15:23:51 +08:00
|
|
|
_stack->pushString("began");
|
|
|
|
break;
|
|
|
|
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::MOVED:
|
2013-07-02 15:23:51 +08:00
|
|
|
_stack->pushString("moved");
|
|
|
|
break;
|
|
|
|
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::ENDED:
|
2013-07-02 15:23:51 +08:00
|
|
|
_stack->pushString("ended");
|
|
|
|
break;
|
|
|
|
|
2013-09-20 20:09:39 +08:00
|
|
|
case EventTouch::EventCode::CANCELLED:
|
2013-07-02 15:23:51 +08:00
|
|
|
_stack->pushString("cancelled");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director* pDirector = Director::getInstance();
|
2013-07-04 15:44:42 +08:00
|
|
|
lua_State *L = _stack->getLuaState();
|
|
|
|
int ret = 0;
|
2013-07-22 17:12:53 +08:00
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
int i = 1;
|
2013-09-12 21:12:46 +08:00
|
|
|
for (auto& touch : touchesScriptData->touches)
|
2013-07-04 15:44:42 +08:00
|
|
|
{
|
2013-09-12 21:12:46 +08:00
|
|
|
Point pt = pDirector->convertToGL(touch->getLocationInView());
|
2013-07-22 17:12:53 +08:00
|
|
|
lua_pushnumber(L, pt.x);
|
|
|
|
lua_rawseti(L, -2, i++);
|
|
|
|
lua_pushnumber(L, pt.y);
|
|
|
|
lua_rawseti(L, -2, i++);
|
2013-09-12 21:12:46 +08:00
|
|
|
lua_pushinteger(L, touch->getID());
|
2013-07-22 17:12:53 +08:00
|
|
|
lua_rawseti(L, -2, i++);
|
2013-07-04 15:44:42 +08:00
|
|
|
}
|
2013-07-22 17:12:53 +08:00
|
|
|
ret = _stack->executeFunctionByHandler(handler, 2);
|
|
|
|
|
2013-07-02 15:23:51 +08:00
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-16 09:55:06 +08:00
|
|
|
int LuaEngine::handlerControlEvent(void* data)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
if ( NULL == data )
|
2013-07-02 15:23:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
2013-07-16 09:55:06 +08:00
|
|
|
if (NULL == basicScriptData->nativeObject)
|
2013-07-15 10:38:16 +08:00
|
|
|
return 0;
|
2013-07-04 15:44:42 +08:00
|
|
|
|
2013-07-16 09:55:06 +08:00
|
|
|
int controlEvents = *((int*)(basicScriptData->value));
|
2013-07-02 15:23:51 +08:00
|
|
|
|
2013-07-16 09:55:06 +08:00
|
|
|
int handler = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < kControlEventTotalNumber; i++)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
2013-07-16 09:55:06 +08:00
|
|
|
if ((controlEvents & (1 << i)))
|
|
|
|
{
|
2013-09-04 22:17:26 +08:00
|
|
|
ScriptHandlerMgr::HandlerType controlHandler = ScriptHandlerMgr::HandlerType((int)ScriptHandlerMgr::HandlerType::CONTROL_TOUCH_DOWN + i);
|
|
|
|
handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, controlHandler);
|
2013-07-02 15:23:51 +08:00
|
|
|
|
2013-07-16 09:55:06 +08:00
|
|
|
if (0 != handler)
|
|
|
|
{
|
2013-08-22 10:16:57 +08:00
|
|
|
_stack->pushObject((Object*)basicScriptData->nativeObject, "Object");
|
|
|
|
_stack->pushInt(controlEvents);
|
|
|
|
ret = _stack->executeFunctionByHandler(handler, 2);
|
2013-07-16 09:55:06 +08:00
|
|
|
_stack->clean();
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2013-07-16 09:55:06 +08:00
|
|
|
|
|
|
|
return ret;
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2013-07-15 10:38:16 +08:00
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleEventAcc(void* data)
|
2013-12-22 22:10:21 +08:00
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::EVENT_ACC);
|
2013-12-22 22:10:21 +08:00
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lua_State* L = _stack->getLuaState();
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
LuaEventAccelerationData* eventListennerAcc = static_cast<LuaEventAccelerationData*>(basicScriptData->value);
|
2013-12-22 22:10:21 +08:00
|
|
|
toluafix_pushusertype_ccobject(L, eventListennerAcc->event->_ID, &(eventListennerAcc->event->_luaID), (void*)(eventListennerAcc->event),"Event");
|
|
|
|
Acceleration* accleration = static_cast<Acceleration*>(eventListennerAcc->acc);
|
|
|
|
lua_pushnumber(L,accleration->x);
|
|
|
|
lua_pushnumber(L,accleration->y);
|
|
|
|
lua_pushnumber(L,accleration->z);
|
|
|
|
lua_pushnumber(L,accleration->timestamp);
|
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 5);
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleEventKeyboard(ScriptHandlerMgr::HandlerType type, void* data)
|
2013-12-22 22:10:21 +08:00
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
LuaEventKeyboarData* keyboardData = static_cast<LuaEventKeyboarData*>(basicScriptData->value);
|
2013-12-22 22:10:21 +08:00
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type);
|
2013-12-22 22:10:21 +08:00
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lua_State* L = _stack->getLuaState();
|
|
|
|
lua_pushinteger(L, keyboardData->keyCode);
|
|
|
|
toluafix_pushusertype_ccobject(L, keyboardData->event->_ID, &(keyboardData->event->_luaID), (void*)(keyboardData->event),"Event");
|
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 2);
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleEventTouch(ScriptHandlerMgr::HandlerType type, void* data)
|
2013-12-22 22:10:21 +08:00
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaEventTouchData* touchData = static_cast<LuaEventTouchData*>(basicScriptData->value);
|
|
|
|
if (nullptr == touchData->touch || nullptr == touchData->event)
|
2013-12-22 22:10:21 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type);
|
2013-12-22 22:10:21 +08:00
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
Touch* touch = touchData->touch;
|
2013-12-22 22:10:21 +08:00
|
|
|
if (NULL != touch) {
|
2013-12-27 10:35:44 +08:00
|
|
|
_stack->pushObject(touchData->touch, "Touch");
|
|
|
|
_stack->pushObject(touchData->event, "Event");
|
2013-12-22 22:10:21 +08:00
|
|
|
ret = _stack->executeFunctionByHandler(handler, 2);
|
|
|
|
}
|
|
|
|
_stack->clean();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleEventTouches(ScriptHandlerMgr::HandlerType type,void* data)
|
2013-12-22 22:10:21 +08:00
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaEventTouchesData * touchesData = static_cast<LuaEventTouchesData*>(basicScriptData->value);
|
|
|
|
if (nullptr == touchesData->event || touchesData->touches.size() == 0)
|
2013-12-22 22:10:21 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicScriptData->nativeObject, type);
|
2013-12-22 22:10:21 +08:00
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lua_State *L = _stack->getLuaState();
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
int i = 1;
|
2013-12-27 10:35:44 +08:00
|
|
|
for (auto& touch : touchesData->touches)
|
|
|
|
{
|
|
|
|
_stack->pushInt(i);
|
|
|
|
_stack->pushObject(touch, "Touch");
|
2013-12-22 22:10:21 +08:00
|
|
|
lua_rawset(L, -3);
|
|
|
|
++i;
|
|
|
|
}
|
2013-12-27 10:35:44 +08:00
|
|
|
_stack->pushObject(touchesData->event, "Event");
|
2013-12-22 22:10:21 +08:00
|
|
|
|
|
|
|
ret = _stack->executeFunctionByHandler(handler, 2);
|
|
|
|
_stack->clean();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleEventMouse(ScriptHandlerMgr::HandlerType type, void* data)
|
2013-12-22 22:10:21 +08:00
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaEventMouseData * mouseData = static_cast<LuaEventMouseData*>(basicScriptData->value);
|
|
|
|
if (nullptr == mouseData->event )
|
2013-12-22 22:10:21 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicScriptData->nativeObject, type);
|
2013-12-22 22:10:21 +08:00
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
_stack->pushObject(mouseData->event, "Event");
|
2013-12-22 22:10:21 +08:00
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 1);
|
|
|
|
_stack->clean();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleEvenCustom(void* data)
|
2013-12-22 22:10:21 +08:00
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
BasicScriptData * basicData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (NULL == basicData->nativeObject || nullptr == basicData->value )
|
2013-12-22 22:10:21 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
EventCustom* eventCustom = static_cast<EventCustom*>(basicData->value);
|
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicData->nativeObject, ScriptHandlerMgr::HandlerType::EVENT_CUSTIOM);
|
2013-12-22 22:10:21 +08:00
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lua_State* L = _stack->getLuaState();
|
|
|
|
toluafix_pushusertype_ccobject(L, eventCustom->_ID, &(eventCustom->_luaID), (void*)(eventCustom),"EventCustom");
|
|
|
|
int ret = _stack->executeFunctionByHandler(handler, 1);
|
|
|
|
_stack->clean();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-26 19:43:49 +08:00
|
|
|
int LuaEngine::handleEvent(ScriptHandlerMgr::HandlerType type,void* data)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL:
|
|
|
|
case ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE:
|
|
|
|
{
|
|
|
|
return handleTableViewEvent(type, data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS:
|
|
|
|
case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR:
|
|
|
|
case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS:
|
|
|
|
{
|
|
|
|
return handleAssetsManagerEvent(type, data);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-27 10:35:44 +08:00
|
|
|
case ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER:
|
|
|
|
{
|
|
|
|
return handleStudioEventListener(type, data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::ARMATURE_EVENT:
|
|
|
|
{
|
|
|
|
return handleArmatureWrapper(type, data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_ACC:
|
|
|
|
{
|
|
|
|
return handleEventAcc(data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_KEYBOARD_PRESSED:
|
2013-12-27 12:05:12 +08:00
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_KEYBOARD_RELEASED:
|
2013-12-27 10:35:44 +08:00
|
|
|
{
|
|
|
|
return handleEventKeyboard(type,data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_CUSTIOM:
|
|
|
|
{
|
|
|
|
return handleEvenCustom(data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_BEGAN:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_MOVED:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_ENDED:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_CANCELLED:
|
|
|
|
{
|
|
|
|
return handleEventTouch(type, data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_BEGAN:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_MOVED:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_ENDED:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_CANCELLED:
|
|
|
|
{
|
|
|
|
return handleEventTouches(type, data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_DOWN:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_UP:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_MOVE:
|
|
|
|
case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_SCROLL:
|
|
|
|
{
|
|
|
|
return handleEventMouse(type, data);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-26 19:43:49 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngine::handleEvent(ScriptHandlerMgr::HandlerType type, void* data, int numResults, const std::function<void(lua_State*,int)>& func)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS:
|
|
|
|
{
|
2013-12-27 10:35:44 +08:00
|
|
|
return handleTableViewEvent(type, data, numResults,func);
|
2013-12-26 19:43:49 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngine::handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data)
|
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* eventData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == eventData->nativeObject || nullptr == eventData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaTableViewEventData* tableViewData = static_cast<LuaTableViewEventData*>(eventData->value);
|
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, type);
|
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Object* obj = static_cast<Object*>(eventData->nativeObject);
|
|
|
|
if (nullptr == obj)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL:
|
|
|
|
case ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM:
|
|
|
|
{
|
|
|
|
toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView");
|
|
|
|
ret = _stack->executeFunctionByHandler(handler, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT:
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE:
|
|
|
|
{
|
|
|
|
Object* cellObject = static_cast<Object*>(tableViewData->value);
|
|
|
|
if (nullptr == cellObject) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView");
|
|
|
|
toluafix_pushusertype_ccobject(_stack->getLuaState(), cellObject->_ID, &(cellObject->_luaID), (void*)(cellObject),"TableViewCell");
|
|
|
|
ret = _stack->executeFunctionByHandler(handler, 2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleTableViewEvent(ScriptHandlerMgr::HandlerType handlerType,void* data, int numResults, const std::function<void(lua_State*,int)>& func)
|
2013-12-26 19:43:49 +08:00
|
|
|
{
|
|
|
|
if (nullptr == data || numResults <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* eventData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == eventData->nativeObject || nullptr == eventData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaTableViewEventData* tableViewData = static_cast<LuaTableViewEventData*>(eventData->value);
|
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, handlerType);
|
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Object* obj = static_cast<Object*>(eventData->nativeObject);
|
|
|
|
if (nullptr == obj)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
switch (handlerType)
|
|
|
|
{
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX:
|
|
|
|
{
|
|
|
|
toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView");
|
2013-12-27 11:17:11 +08:00
|
|
|
_stack->pushLong(*((ssize_t*)tableViewData->value));
|
2013-12-26 19:43:49 +08:00
|
|
|
ret = _stack->executeFunction(handler, 2, 2, func);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX:
|
|
|
|
{
|
|
|
|
toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView");
|
2013-12-27 11:17:11 +08:00
|
|
|
_stack->pushLong(*((ssize_t*)tableViewData->value));
|
2013-12-26 19:43:49 +08:00
|
|
|
ret = _stack->executeFunction(handler, 2, 1, func);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS:
|
|
|
|
{
|
|
|
|
toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"TableView");
|
|
|
|
ret = _stack->executeFunction(handler, 1, 1, func);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngine::handleAssetsManagerEvent(ScriptHandlerMgr::HandlerType type,void* data)
|
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* eventData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == eventData->nativeObject || nullptr == eventData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaAssetsManagerEventData* assetsManagerData = static_cast<LuaAssetsManagerEventData*>(eventData->value);
|
|
|
|
|
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, type);
|
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS:
|
|
|
|
case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR:
|
|
|
|
{
|
|
|
|
_stack->pushInt(assetsManagerData->value);
|
|
|
|
ret = _stack->executeFunctionByHandler(handler, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS:
|
|
|
|
{
|
|
|
|
ret = _stack->executeFunctionByHandler(handler, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 10:35:44 +08:00
|
|
|
int LuaEngine::handleStudioEventListener(ScriptHandlerMgr::HandlerType type,void* data)
|
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* eventData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == eventData->nativeObject || nullptr == eventData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaStudioEventListenerData* listenerData = static_cast<LuaStudioEventListenerData*>(eventData->value);
|
|
|
|
|
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER);
|
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
_stack->pushObject(listenerData->objTarget, "Object");
|
|
|
|
_stack->pushInt(listenerData->eventType);
|
|
|
|
|
|
|
|
_stack->executeFunctionByHandler(handler, 2);
|
|
|
|
_stack->clean();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaEngine::handleArmatureWrapper(ScriptHandlerMgr::HandlerType type,void* data)
|
|
|
|
{
|
|
|
|
if (nullptr == data)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicScriptData* eventData = static_cast<BasicScriptData*>(data);
|
|
|
|
if (nullptr == eventData->nativeObject || nullptr == eventData->value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LuaArmatureWrapperEventData* wrapperData = static_cast<LuaArmatureWrapperEventData*>(eventData->value);
|
|
|
|
|
|
|
|
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, ScriptHandlerMgr::HandlerType::ARMATURE_EVENT);
|
|
|
|
|
|
|
|
if (0 == handler)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (wrapperData->eventType)
|
|
|
|
{
|
|
|
|
case LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::MOVEMENT_EVENT:
|
|
|
|
{
|
|
|
|
LuaArmatureMovementEventData* movementData = static_cast<LuaArmatureMovementEventData*>(wrapperData->eventData);
|
|
|
|
|
|
|
|
_stack->pushObject(movementData->objTarget, "Armature");
|
|
|
|
_stack->pushInt(movementData->movementType);
|
|
|
|
_stack->pushString(movementData->movementID.c_str());
|
|
|
|
_stack->executeFunctionByHandler(handler, 3);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::FRAME_EVENT:
|
|
|
|
{
|
|
|
|
LuaArmatureFrameEventData* frameData = static_cast<LuaArmatureFrameEventData*>(wrapperData->eventData);
|
|
|
|
|
|
|
|
_stack->pushObject(frameData->objTarget, "Bone");
|
|
|
|
_stack->pushString(frameData->frameEventName.c_str());
|
|
|
|
_stack->pushInt(frameData->originFrameIndex);
|
|
|
|
_stack->pushInt(frameData->currentFrameIndex);
|
|
|
|
_stack->executeFunctionByHandler(handler, 4);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::FILE_ASYNC:
|
|
|
|
{
|
|
|
|
_stack->pushFloat(*(float*)wrapperData->eventData);
|
|
|
|
_stack->executeFunctionByHandler(handler, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_stack->clean();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_END
|