int LuaEngine::handleTouchesEvent(void* data) { if (NULL == data) return 0; TouchesScriptData* touchesScriptData = static_cast<TouchesScriptData*>(data); if (NULL == touchesScriptData->nativeObject || NULL == touchesScriptData->touches) return 0; int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchesScriptData->nativeObject, ScriptHandlerMgr::kTouchesHandler); if (0 == handler) return 0; switch (touchesScriptData->actionType) { case CCTOUCHBEGAN: _stack->pushString("began"); break; case CCTOUCHMOVED: _stack->pushString("moved"); break; case CCTOUCHENDED: _stack->pushString("ended"); break; case CCTOUCHCANCELLED: _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 (SetIterator it = touchesScriptData->touches->begin(); it != touchesScriptData->touches->end(); ++it) { Touch* pTouch = static_cast<Touch*>(*it); Point 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++); } ret = _stack->executeFunctionByHandler(handler, 2); _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) { cocos2d::Vec2 pt = pDirector->convertToGL(touch->getLocationInView()); 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::executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches) { TouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry(); if (!pScriptHandlerEntry) return 0; int nHandler = pScriptHandlerEntry->getHandler(); if (!nHandler) return 0; switch (eventType) { case CCTOUCHBEGAN: _stack->pushString("began"); break; case CCTOUCHMOVED: _stack->pushString("moved"); break; case CCTOUCHENDED: _stack->pushString("ended"); break; case CCTOUCHCANCELLED: _stack->pushString("cancelled"); break; default: return 0; } Director* pDirector = Director::sharedDirector(); lua_State *L = _stack->getLuaState(); lua_newtable(L); int i = 1; for (SetIterator it = pTouches->begin(); it != pTouches->end(); ++it) { Touch* pTouch = (Touch*)*it; Point 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 = _stack->executeFunctionByHandler(nHandler, 2); _stack->clean(); return ret; }
int LuaEngine::executeNodeTouchesEvent(Node* pNode, int eventType, const std::vector<Touch*>& touches, int phase) { CCScriptEventListenersForEvent *listeners = getListeners(pNode, phase == NODE_TOUCH_CAPTURING_PHASE ? NODE_TOUCH_CAPTURE_EVENT : NODE_TOUCH_EVENT); if (!listeners) return 1; _stack->clean(); LuaValueDict event; switch (eventType) { case CCTOUCHBEGAN: event["name"] = LuaValue::stringValue("began"); break; case CCTOUCHMOVED: event["name"] = LuaValue::stringValue("moved"); break; case CCTOUCHENDED: event["name"] = LuaValue::stringValue("ended"); break; case CCTOUCHCANCELLED: event["name"] = LuaValue::stringValue("cancelled"); break; case CCTOUCHADDED: event["name"] = LuaValue::stringValue("added"); break; case CCTOUCHREMOVED: event["name"] = LuaValue::stringValue("removed"); break; default: return 0; } event["mode"] = LuaValue::intValue((int)Touch::DispatchMode::ALL_AT_ONCE); switch (phase) { case NODE_TOUCH_CAPTURING_PHASE: event["phase"] = LuaValue::stringValue("capturing"); break; case NODE_TOUCH_TARGETING_PHASE: event["phase"] = LuaValue::stringValue("targeting"); break; default: event["phase"] = LuaValue::stringValue("unknown"); } LuaValueDict points; Director* pDirector = Director::getInstance(); char touchId[16]; for (auto touchIt = touches.begin(); touchIt != touches.end(); ++touchIt) { LuaValueDict point; Touch* pTouch = (Touch*)*touchIt; sprintf(touchId, "%d", pTouch->getID()); point["id"] = LuaValue::stringValue(touchId); const Point pt = pDirector->convertToGL(pTouch->getLocationInView()); point["x"] = LuaValue::floatValue(pt.x); point["y"] = LuaValue::floatValue(pt.y); const Point prev = pDirector->convertToGL(pTouch->getPreviousLocationInView()); point["prevX"] = LuaValue::floatValue(prev.x); point["prevY"] = LuaValue::floatValue(prev.y); points[touchId] = LuaValue::dictValue(point); } event["points"] = LuaValue::dictValue(points); _stack->pushLuaValueDict(event); bool flagNeedClean = false; for (auto it=listeners->begin(); it!=listeners->end(); ++it) { if ((*it)->removed) { flagNeedClean = true; continue; } _stack->copyValue(1); _stack->executeFunctionByHandler((*it)->listener, 1); _stack->settop(1); } cleanListeners(listeners, pNode, flagNeedClean); _stack->clean(); return 1; }
int LuaEventNode::executeScriptTouchHandler(int nEventType, const std::vector<Touch*>& touches, int phase /* = NODE_TOUCH_TARGETING_PHASE */) { auto stack = initExecParam(this->getActiveNode(), phase); if (!stack) { return 0; } LuaValueDict event; switch (nEventType) { case CCTOUCHBEGAN: event["name"] = LuaValue::stringValue("began"); break; case CCTOUCHMOVED: event["name"] = LuaValue::stringValue("moved"); break; case CCTOUCHENDED: event["name"] = LuaValue::stringValue("ended"); break; case CCTOUCHCANCELLED: event["name"] = LuaValue::stringValue("cancelled"); break; case CCTOUCHADDED: event["name"] = LuaValue::stringValue("added"); break; case CCTOUCHREMOVED: event["name"] = LuaValue::stringValue("removed"); break; default: return 0; } event["mode"] = LuaValue::intValue((int)Touch::DispatchMode::ALL_AT_ONCE); switch (phase) { case NODE_TOUCH_CAPTURING_PHASE: event["phase"] = LuaValue::stringValue("capturing"); break; case NODE_TOUCH_TARGETING_PHASE: event["phase"] = LuaValue::stringValue("targeting"); break; default: event["phase"] = LuaValue::stringValue("unknown"); } LuaValueDict points; Director* pDirector = Director::getInstance(); char touchId[16]; for (auto touchIt = touches.begin(); touchIt != touches.end(); ++touchIt) { LuaValueDict point; Touch* pTouch = (Touch*)*touchIt; sprintf(touchId, "%d", pTouch->getID()); point["id"] = LuaValue::stringValue(touchId); const Point pt = pDirector->convertToGL(pTouch->getLocationInView()); point["x"] = LuaValue::floatValue(pt.x); point["y"] = LuaValue::floatValue(pt.y); const Point prev = pDirector->convertToGL(pTouch->getPreviousLocationInView()); point["prevX"] = LuaValue::floatValue(prev.x); point["prevY"] = LuaValue::floatValue(prev.y); points[touchId] = LuaValue::dictValue(point); } event["points"] = LuaValue::dictValue(points); return callNodeEventDispatcher(stack, event); }