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) { const cocos2d::Vec2 pt = Director::getInstance()->convertToGL(touch->getLocationInView()); _stack->pushFloat(pt.x); _stack->pushFloat(pt.y); const cocos2d::Vec2 prev_pt = Director::getInstance()->convertToGL(touch->getPreviousLocationInView()); _stack->pushFloat(prev_pt.x); _stack->pushFloat(prev_pt.y); ret = _stack->executeFunctionByHandler(handler, 5); } _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); }