axmol/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp

1059 lines
32 KiB
C++
Raw Normal View History

2014-03-10 14:04:58 +08:00
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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 "scripting/lua-bindings/manual/CCLuaEngine.h"
#include "scripting/lua-bindings/manual/tolua_fix.h"
2016-04-18 15:09:21 +08:00
2014-03-10 14:04:58 +08:00
#include "extensions/GUI/CCControlExtension/CCControl.h"
#include "scripting/lua-bindings/manual/cocos2d/LuaOpengl.h"
#include "scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.hpp"
#include "scripting/lua-bindings/manual/extension/lua_cocos2dx_extension_manual.h"
#include "scripting/lua-bindings/manual/cocostudio/lua_cocos2dx_coco_studio_manual.hpp"
#include "scripting/lua-bindings/manual/ui/lua_cocos2dx_ui_manual.hpp"
2016-04-18 15:09:21 +08:00
#include "2d/CCMenuItem.h"
#include "base/CCDirector.h"
#include "base/CCEventCustom.h"
2014-03-10 14:04:58 +08:00
2015-05-21 13:46:43 +08:00
#if _MSC_VER > 1800
#pragma comment(lib,"lua51-2015.lib")
#else
#pragma comment(lib,"lua51.lib")
#endif
2014-03-10 14:04:58 +08:00
NS_CC_BEGIN
LuaEngine* LuaEngine::_defaultEngine = nullptr;
2014-03-10 14:04:58 +08:00
LuaEngine* LuaEngine::getInstance(void)
{
if (!_defaultEngine)
{
_defaultEngine = new (std::nothrow) LuaEngine();
2014-03-10 14:04:58 +08:00
_defaultEngine->init();
}
return _defaultEngine;
}
LuaEngine::~LuaEngine(void)
{
CC_SAFE_RELEASE(_stack);
_defaultEngine = nullptr;
2014-03-10 14:04:58 +08:00
}
bool LuaEngine::init(void)
{
_stack = LuaStack::create();
_stack->retain();
return true;
}
void LuaEngine::addSearchPath(const char* path)
{
_stack->addSearchPath(path);
}
void LuaEngine::addLuaLoader(lua_CFunction func)
{
_stack->addLuaLoader(func);
}
void LuaEngine::removeScriptObjectByObject(Ref* pObj)
{
_stack->removeScriptObjectByObject(pObj);
ScriptHandlerMgr::getInstance()->removeObjectAllHandlers(pObj);
}
void LuaEngine::removeScriptHandler(int nHandler)
{
_stack->removeScriptHandler(nHandler);
}
int LuaEngine::executeString(const char *codes)
{
int ret = _stack->executeString(codes);
_stack->clean();
return ret;
}
int LuaEngine::executeScriptFile(const char* filename)
{
int ret = _stack->executeScriptFile(filename);
_stack->clean();
return ret;
}
int LuaEngine::executeGlobalFunction(const char* functionName)
{
int ret = _stack->executeGlobalFunction(functionName);
_stack->clean();
return ret;
}
int LuaEngine::executeNodeEvent(Node* pNode, int nAction)
{
return 0;
}
int LuaEngine::executeMenuItemEvent(MenuItem* pMenuItem)
{
return 0;
}
2014-03-11 17:35:09 +08:00
int LuaEngine::executeNotificationEvent(__NotificationCenter* pNotificationCenter, const char* pszName)
2014-03-10 14:04:58 +08:00
{
int nHandler = pNotificationCenter->getObserverHandlerByName(pszName);
if (!nHandler) return 0;
_stack->pushString(pszName);
int ret = _stack->executeFunctionByHandler(nHandler, 1);
_stack->clean();
return ret;
}
int LuaEngine::executeCallFuncActionEvent(CallFunc* pAction, Ref* pTarget/* = NULL*/)
{
return 0;
}
int LuaEngine::executeSchedule(int nHandler, float dt, Node* pNode/* = NULL*/)
{
if (!nHandler) return 0;
_stack->pushFloat(dt);
int ret = _stack->executeFunctionByHandler(nHandler, 1);
_stack->clean();
return ret;
}
int LuaEngine::executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch)
{
return 0;
}
2014-03-11 17:35:09 +08:00
int LuaEngine::executeLayerTouchesEvent(Layer* pLayer, int eventType, __Set *pTouches)
2014-03-10 14:04:58 +08:00
{
return 0;
}
int LuaEngine::executeLayerKeypadEvent(Layer* pLayer, int eventType)
{
return 0;
}
int LuaEngine::executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue)
{
return 0;
}
int LuaEngine::executeEvent(int nHandler, const char* pEventName, Ref* pEventSource /* = NULL*/, const char* pEventSourceClassName /* = NULL*/)
{
_stack->pushString(pEventName);
if (pEventSource)
{
_stack->pushObject(pEventSource, pEventSourceClassName ? pEventSourceClassName : "cc.Ref");
}
int ret = _stack->executeFunctionByHandler(nHandler, pEventSource ? 2 : 1);
_stack->clean();
return ret;
}
bool LuaEngine::handleAssert(const char *msg)
{
bool ret = _stack->handleAssert(msg);
_stack->clean();
return ret;
}
int LuaEngine::reallocateScriptHandler(int nHandler)
{
int nRet = _stack->reallocateScriptHandler(nHandler);
_stack->clean();
return nRet;
}
bool LuaEngine::parseConfig(ConfigType type, const std::string& str)
{
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);
}
int LuaEngine::sendEvent(ScriptEvent* evt)
{
if (NULL == evt)
return 0;
switch (evt->type)
{
case kNodeEvent:
{
return handleNodeEvent(evt->data);
}
break;
case kMenuClickedEvent:
{
return handleMenuClickedEvent(evt->data);
}
break;
case kCallFuncEvent:
{
return handleCallFuncActionEvent(evt->data);
}
break;
case kScheduleEvent:
{
return handleScheduler(evt->data);
}
break;
case kTouchEvent:
{
return handleTouchEvent(evt->data);
}
break;
case kTouchesEvent:
{
return handleTouchesEvent(evt->data);
}
break;
case kKeypadEvent:
{
return handleKeypadEvent(evt->data);
}
break;
case kAccelerometerEvent:
{
return handleAccelerometerEvent(evt->data);
}
break;
case kCommonEvent:
{
return handleCommonEvent(evt->data);
}
break;
case kControlEvent:
{
return handlerControlEvent(evt->data);
}
break;
default:
break;
}
return 0;
}
int LuaEngine::handleNodeEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = (BasicScriptData*)data;
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::NODE);
if (0 == handler)
return 0;
int action = *((int*)(basicScriptData->value));
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;
}
int LuaEngine::handleMenuClickedEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = (BasicScriptData*)data;
if (NULL == basicScriptData->nativeObject)
return 0;
MenuItem* menuItem = static_cast<MenuItem*>(basicScriptData->nativeObject);
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(menuItem, ScriptHandlerMgr::HandlerType::MENU_CLICKED);
if (0 == handler)
return 0;
_stack->pushInt(menuItem->getTag());
_stack->pushObject(menuItem, "cc.MenuItem");
int ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
return ret;
}
int LuaEngine::handleCallFuncActionEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
if (NULL == basicScriptData->nativeObject)
return 0;
int handler =ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::CALLFUNC);
if (0 == handler)
return 0;
Ref* target = static_cast<Ref*>(basicScriptData->value);
if (NULL != target)
{
_stack->pushObject(target, "cc.Node");
}
int ret = _stack->executeFunctionByHandler(handler, target ? 1 : 0);
_stack->clean();
return ret;
}
int LuaEngine::handleScheduler(void* data)
{
if (NULL == data)
return 0;
SchedulerScriptData* schedulerInfo = static_cast<SchedulerScriptData*>(data);
_stack->pushFloat(schedulerInfo->elapse);
int ret = _stack->executeFunctionByHandler(schedulerInfo->handler, 1);
_stack->clean();
return ret;
}
int LuaEngine::handleKeypadEvent(void* data)
{
if (NULL == data)
return 0;
KeypadScriptData* keypadScriptData = static_cast<KeypadScriptData*>(data);
if (NULL == keypadScriptData->nativeObject)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(keypadScriptData->nativeObject, ScriptHandlerMgr::HandlerType::KEYPAD);
if (0 == handler)
return 0;
EventKeyboard::KeyCode action = keypadScriptData->actionType;
switch(action)
{
case EventKeyboard::KeyCode::KEY_BACKSPACE:
_stack->pushString("backClicked");
break;
case EventKeyboard::KeyCode::KEY_MENU:
_stack->pushString("menuClicked");
break;
default:
break;
}
int ret = _stack->executeFunctionByHandler(handler, 1);
_stack->clean();
return ret;
}
int LuaEngine::handleAccelerometerEvent(void* data)
{
if (NULL == data)
return 0;
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::ACCELEROMETER);
if (0 == handler)
return 0;
Acceleration* accelerationValue = static_cast<Acceleration*>(basicScriptData->value);
_stack->pushFloat(accelerationValue->x);
_stack->pushFloat(accelerationValue->y);
_stack->pushFloat(accelerationValue->z);
_stack->pushFloat(accelerationValue->timestamp);
int ret = _stack->executeFunctionByHandler(handler, 4);
_stack->clean();
return ret;
}
int LuaEngine::handleCommonEvent(void* data)
{
if (NULL == data)
return 0;
CommonScriptData* commonInfo = static_cast<CommonScriptData*>(data);
if (NULL == commonInfo->eventName || 0 == commonInfo->handler)
return 0;
_stack->pushString(commonInfo->eventName);
if (NULL != commonInfo->eventSource)
{
if (NULL != commonInfo->eventSourceClassName && strlen(commonInfo->eventSourceClassName) > 0)
{
_stack->pushObject(commonInfo->eventSource, commonInfo->eventSourceClassName);
}
else
{
_stack->pushObject(commonInfo->eventSource, "cc.Ref");
}
}
int ret = _stack->executeFunctionByHandler(commonInfo->handler, commonInfo->eventSource ? 2 : 1);
_stack->clean();
return ret;
}
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;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchScriptData->nativeObject, ScriptHandlerMgr::HandlerType::TOUCHES);
if (0 == handler)
return 0;
switch (touchScriptData->actionType)
{
case EventTouch::EventCode::BEGAN:
_stack->pushString("began");
break;
case EventTouch::EventCode::MOVED:
_stack->pushString("moved");
break;
case EventTouch::EventCode::ENDED:
_stack->pushString("ended");
break;
case EventTouch::EventCode::CANCELLED:
_stack->pushString("cancelled");
break;
default:
return 0;
}
int ret = 0;
Touch* touch = touchScriptData->touch;
if (NULL != touch) {
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
const cocos2d::Vec2 pt = Director::getInstance()->convertToGL(touch->getLocationInView());
2014-03-10 14:04:58 +08:00
_stack->pushFloat(pt.x);
_stack->pushFloat(pt.y);
ret = _stack->executeFunctionByHandler(handler, 3);
}
_stack->clean();
return ret;
}
int LuaEngine::handleTouchesEvent(void* data)
{
if (NULL == data)
return 0;
TouchesScriptData* touchesScriptData = static_cast<TouchesScriptData*>(data);
if (NULL == touchesScriptData->nativeObject || touchesScriptData->touches.size() == 0)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchesScriptData->nativeObject, ScriptHandlerMgr::HandlerType::TOUCHES);
if (0 == handler)
return 0;
switch (touchesScriptData->actionType)
{
case EventTouch::EventCode::BEGAN:
_stack->pushString("began");
break;
case EventTouch::EventCode::MOVED:
_stack->pushString("moved");
break;
case EventTouch::EventCode::ENDED:
_stack->pushString("ended");
break;
case EventTouch::EventCode::CANCELLED:
_stack->pushString("cancelled");
break;
default:
return 0;
}
Director* pDirector = Director::getInstance();
lua_State *L = _stack->getLuaState();
int ret = 0;
lua_newtable(L);
int i = 1;
for (auto& touch : touchesScriptData->touches)
{
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
cocos2d::Vec2 pt = pDirector->convertToGL(touch->getLocationInView());
2014-03-10 14:04:58 +08:00
lua_pushnumber(L, pt.x);
lua_rawseti(L, -2, i++);
lua_pushnumber(L, pt.y);
lua_rawseti(L, -2, i++);
lua_pushinteger(L, touch->getID());
lua_rawseti(L, -2, i++);
}
ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
return ret;
}
int LuaEngine::handlerControlEvent(void* data)
{
if ( NULL == data )
return 0;
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
if (NULL == basicScriptData->nativeObject)
return 0;
int controlEvents = *((int*)(basicScriptData->value));
int handler = 0;
int ret = 0;
for (int i = 0; i < kControlEventTotalNumber; i++)
{
if ((controlEvents & (1 << i)))
{
ScriptHandlerMgr::HandlerType controlHandler = ScriptHandlerMgr::HandlerType((int)ScriptHandlerMgr::HandlerType::CONTROL_TOUCH_DOWN + i);
handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, controlHandler);
if (0 != handler)
{
_stack->pushObject((Ref*)basicScriptData->nativeObject, "cc.Ref");
_stack->pushInt(controlEvents);
ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
}
}
}
return ret;
}
int LuaEngine::handleEventAcc(void* data)
{
if (nullptr == data)
return 0;
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::EVENT_ACC);
if (0 == handler)
return 0;
lua_State* L = _stack->getLuaState();
LuaEventAccelerationData* eventListennerAcc = static_cast<LuaEventAccelerationData*>(basicScriptData->value);
toluafix_pushusertype_ccobject(L, eventListennerAcc->event->_ID, &(eventListennerAcc->event->_luaID), (void*)(eventListennerAcc->event),"cc.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;
}
int LuaEngine::handleEventKeyboard(ScriptHandlerMgr::HandlerType type, void* data)
{
if (nullptr == data)
return 0;
BasicScriptData* basicScriptData = static_cast<BasicScriptData*>(data);
if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value)
return 0;
LuaEventKeyboarData* keyboardData = static_cast<LuaEventKeyboarData*>(basicScriptData->value);
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type);
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),"cc.Event");
int ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
return ret;
}
int LuaEngine::handleEventTouch(ScriptHandlerMgr::HandlerType type, void* data)
{
if (nullptr == data)
return 0;
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)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type);
if (0 == handler)
return 0;
int ret = 0;
Touch* touch = touchData->touch;
if (NULL != touch) {
_stack->pushObject(touchData->touch, "cc.Touch");
_stack->pushObject(touchData->event, "cc.Event");
ret = _stack->executeFunctionByHandler(handler, 2);
}
_stack->clean();
return ret;
}
int LuaEngine::handleEventTouches(ScriptHandlerMgr::HandlerType type,void* data)
{
if (nullptr == data)
return 0;
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)
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicScriptData->nativeObject, type);
if (0 == handler)
return 0;
lua_State *L = _stack->getLuaState();
int ret = 0;
lua_newtable(L);
int i = 1;
for (auto& touch : touchesData->touches)
{
_stack->pushInt(i);
_stack->pushObject(touch, "cc.Touch");
lua_rawset(L, -3);
++i;
}
_stack->pushObject(touchesData->event, "cc.Event");
ret = _stack->executeFunctionByHandler(handler, 2);
_stack->clean();
return ret;
}
int LuaEngine::handleEventMouse(ScriptHandlerMgr::HandlerType type, void* data)
{
if (nullptr == data)
return 0;
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 )
return 0;
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicScriptData->nativeObject, type);
if (0 == handler)
return 0;
_stack->pushObject(mouseData->event, "cc.Event");
int ret = _stack->executeFunctionByHandler(handler, 1);
_stack->clean();
return ret;
}
int LuaEngine::handleEvenCustom(void* data)
{
if (nullptr == data)
return 0;
BasicScriptData * basicData = static_cast<BasicScriptData*>(data);
if (NULL == basicData->nativeObject || nullptr == basicData->value )
return 0;
EventCustom* eventCustom = static_cast<EventCustom*>(basicData->value);
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicData->nativeObject, ScriptHandlerMgr::HandlerType::EVENT_CUSTIOM);
if (0 == handler)
return 0;
lua_State* L = _stack->getLuaState();
toluafix_pushusertype_ccobject(L, eventCustom->_ID, &(eventCustom->_luaID), (void*)(eventCustom),"cc.EventCustom");
int ret = _stack->executeFunctionByHandler(handler, 1);
_stack->clean();
return ret;
}
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;
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:
case ScriptHandlerMgr::HandlerType::EVENT_KEYBOARD_RELEASED:
{
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;
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:
{
return handleTableViewEvent(type, data, numResults,func);
}
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;
Ref* obj = static_cast<Ref*>(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),"cc.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:
{
Ref* cellObject = static_cast<Ref*>(tableViewData->value);
if (nullptr == cellObject) {
break;
}
toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"cc.TableView");
toluafix_pushusertype_ccobject(_stack->getLuaState(), cellObject->_ID, &(cellObject->_luaID), (void*)(cellObject),"cc.TableViewCell");
ret = _stack->executeFunctionByHandler(handler, 2);
}
break;
default:
break;
}
return ret;
}
int LuaEngine::handleTableViewEvent(ScriptHandlerMgr::HandlerType handlerType,void* data, int numResults, const std::function<void(lua_State*,int)>& func)
{
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;
Ref* obj = static_cast<Ref*>(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),"cc.TableView");
_stack->pushLong(*((ssize_t*)tableViewData->value));
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),"cc.TableView");
_stack->pushLong(*((ssize_t*)tableViewData->value));
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),"cc.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;
}
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, "ccs.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, "ccs.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;
}
int LuaEngine::reload(const char* moduleFileName)
{
return _stack->reload(moduleFileName);
}
NS_CC_END