Exemple #1
0
void CWidgetWindow::onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event)
{
	for(unsigned int i=0;i<touches.size(); i++)
	{
		Touch* pTouch = touches.at(i);
		map<int, __ccMULTITOUCHTARGET>::iterator mitr = m_mMultiTouchKeeper.find(pTouch->getID());
		if( mitr != m_mMultiTouchKeeper.end() )
		{
			if( mitr->second.pWidget )
			{
				if( mitr->second.pWidget->isTouchInterrupted() )
				{
					if( mitr->second.pLongClickedWidgetObject )
					{
						executeTouchCancelledAfterLongClickHandler(mitr->second.pLongClickedWidgetObject, pTouch, mitr->second.fTouchedDuration);
					}
				}
				else
				{
					mitr->second.pWidget->interruptTouchCascade(pTouch, mitr->second.fTouchedDuration);
				}
			}
			m_mMultiTouchKeeper.erase(mitr);
		}
	}
}
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;
}
Exemple #3
0
void CWidgetWindow::onTouchesBegan(const std::vector<Touch*>&touches, Event *unused_event)
{
	for(unsigned int i=0;i<touches.size(); i++)
	{
		Touch* pTouch = touches.at(i);
		if( m_bTouchEnabled && m_bMultiTouchEnabled && _visible &&  _children.size() > 0 )
		{
			Vec2 touchPointInView = convertToNodeSpace(pTouch->getLocation());

				for(int j=_children.size()-1;j>=0;--j){
					Node* pNode = _children.at(j);
					CWidget* pWidget = dynamic_cast<CWidget*>(pNode);
					if( pWidget && pNode->isVisible() && pWidget->isEnabled() && pWidget->isTouchEnabled() )
					{
						bool bSameWidgetBreak = false;
						if( pNode->getBoundingBox().containsPoint(touchPointInView) )
						{
							//make sure it can not happened on the same widget
							map<int, __ccMULTITOUCHTARGET>::iterator mitr = m_mMultiTouchKeeper.begin();
							for(; mitr != m_mMultiTouchKeeper.end(); ++mitr)
							{
								if( mitr->second.pWidget == pWidget )
								{
									bSameWidgetBreak = true;
									break;
								}
							}

							if( bSameWidgetBreak )
							{
								break;
							}

							if( pWidget->executeTouchBeganHandler(pTouch) != eWidgetTouchNone )
							{
								__ccMULTITOUCHTARGET tKeeper;
								tKeeper.fTouchedDuration = 0.000001f;
								tKeeper.pWidget = pWidget;
								tKeeper.pLongClickedWidgetObject = NULL;
								m_mMultiTouchKeeper[pTouch->getID()] = tKeeper;
								return;
							}
						}
					}
				
			}

		}
	}
}
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;
}
void UINumberPicker::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
    do
    {
        CC_BREAK_IF(!isVisible());
        
        if (containsTouchObject(m_pTouches, touch))
        {
            if (m_pTouches.size() == 1 && m_bTouchMoved)
            {
                //手指放开,自由移动
                schedule(schedule_selector(UINumberPicker::deaccelerateScrolling));
            }
            if (m_pTouches.size() == 1 && !m_bTouchMoved)
            {
                //判断当前时一个单击行为,获取当前点击的是哪一个Item;
                PickerCell *cell = itemForPoint(m_tTouchPoint);
                setSelectedItem(cell);
            }
            std::vector<Touch*>::iterator ite = m_pTouches.begin();
            while (ite != m_pTouches.end())
            {
                Touch *ptouch =*ite;
                if (ptouch->getID() == touch->getID())
                {
                    m_pTouches.erase(ite);
                    break;
                }
                ite++;
            }
        }
        if (m_pTouches.size() == 0)
        {
            m_bDragging = false;
            m_bTouchMoved = false;
        }
        m_bIsScrolling = true;
    }
    while (0);
}
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;
}
void LuaTouchEventManager::dispatchingTouchEventReal(const std::vector<Touch*>& touches, Event *pEvent, int event)
{
    LuaEventNode *node = nullptr;
    LuaTouchTargetNode *touchTarget = nullptr;
    Touch *touch = nullptr;

    ssize_t count = _touchingTargets.size();
    //CCLOG("dispatchingTouchEvent COUNT [%zd]", count);
    for (ssize_t i = 0; i < count; ++i)
    {
        touchTarget = _touchingTargets.at(i);

        if (!touchTarget->getNode()->isRunning())
        {
            // target removed from scene, remove it
            //            CCLOG("REMOVE TARGET [%u]", i);
            _touchingTargets.erase(i);
            --count;
            --i;
            continue;
        }

        int touchMode = touchTarget->getTouchMode();
        if (touchMode != LuaEventNode::modeTouchesAllAtOnce)
        {
            touch = touchTarget->findTouch(touches);
            if (!touch)
            {
                // not found touch id for target, skip this target
                continue;
            }
        }

        // try to dispatching event
        Vector<LuaEventNode*> path(10);
        node = touchTarget->getNode();
        do
        {
            path.pushBack(node);
            node = node->getParent();
        } while (node != nullptr /*&& node != this*/);

        // phase: capturing
        // from parent to child
        for (long j = path.size() - 1; j >= 0; --j)
        {
            node = path.at(j);
            if (touchMode == LuaEventNode::modeTouchesAllAtOnce)
            {
                switch (event)
                {
                case CCTOUCHMOVED:
                    node->ccTouchesCaptureMoved(touches, touchTarget->getNode());
                    break;

                case CCTOUCHENDED:
                    node->ccTouchesCaptureEnded(touches, touchTarget->getNode());
                    break;

                case CCTOUCHCANCELLED:
                    node->ccTouchesCaptureCancelled(touches, touchTarget->getNode());
                    break;

                case CCTOUCHADDED:
                    node->ccTouchesCaptureAdded(touches, touchTarget->getNode());
                    break;

                case CCTOUCHREMOVED:
                    node->ccTouchesCaptureRemoved(touches, touchTarget->getNode());
                    break;
                }
            }
            else
            {
                switch (event)
                {
                case CCTOUCHMOVED:
                    node->ccTouchCaptureMoved(touch, touchTarget->getNode());
                    break;

                case CCTOUCHENDED:
                    node->ccTouchCaptureEnded(touch, touchTarget->getNode());
                    break;

                case CCTOUCHCANCELLED:
                    node->ccTouchCaptureCancelled(touch, touchTarget->getNode());
                    break;

                case CCTOUCHREMOVED:
                    if (touch->getID() == touchTarget->getTouchId())
                    {
                        node->ccTouchCaptureEnded(touch, touchTarget->getNode());
                    }
                    break;
                }
            }
        } // for (long j = path.size() - 1; j >= 0; --j)

        // phase: targeting
        node = touchTarget->getNode();
        if (touchMode == LuaEventNode::modeTouchesAllAtOnce)
        {
            switch (event)
            {
            case CCTOUCHMOVED:
                node->ccTouchesMoved(touches, pEvent);
                break;

            case CCTOUCHENDED:
                node->ccTouchesEnded(touches, pEvent);
                break;

            case CCTOUCHCANCELLED:
                node->ccTouchesCancelled(touches, pEvent);
                break;

            case CCTOUCHADDED:
                node->ccTouchesAdded(touches, pEvent);
                break;

            case CCTOUCHREMOVED:
                node->ccTouchesRemoved(touches, pEvent);
                break;
            }
        }
        else
        {
            switch (event)
            {
            case CCTOUCHMOVED:
                node->ccTouchMoved(touch, pEvent);
                break;

            case CCTOUCHENDED:
                node->ccTouchEnded(touch, pEvent);
                break;

            case CCTOUCHCANCELLED:
                node->ccTouchCancelled(touch, pEvent);
                break;

            case CCTOUCHREMOVED:
                if (touch->getID() == touchTarget->getTouchId())
                {
                    node->ccTouchEnded(touch, pEvent);
                    // target touching ended, remove it
                    //CCLOG("REMOVE TARGET [%zd]", i);
                    _touchingTargets.erase(i);
                    --count;
                    --i;
                }
                break;
            }
        }
    }
    //CCLOG("dispatchingTouchEvent end");
}
void LuaTouchEventManager::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
    if (!m_touchDispatchingEnabled || _touchableNodes.size() < 1) return;

    // save touches id
    for (auto it = touches.begin(); it != touches.end(); ++it)
    {
        m_touchingIds.insert(((Touch*)*it)->getID());
    }

    // check current in touching
    if (_touchingTargets.size())
    {
        dispatchingTouchEvent(touches, event, CCTOUCHADDED);
        return;
    }

    // start new touching event
    // sort touchable nodes
    sortAllTouchableNodes(_touchableNodes);

    // find touching target
    bool isTouchable = true;
    LuaEventNode *node = nullptr;
    LuaEventNode *checkTouchableNode = nullptr;
    LuaTouchTargetNode *touchTarget = nullptr;

    for (auto iter = _touchableNodes.begin(); iter != _touchableNodes.end(); ++iter)
    {
        checkTouchableNode = node = *iter;

        // check node is visible and capturing enabled
        isTouchable = true;
        do
        {
            isTouchable = isTouchable
                          && checkTouchableNode->isRunning()
                          && checkTouchableNode->isVisible()
                          && checkTouchableNode->isTouchCaptureEnabled();
            checkTouchableNode = checkTouchableNode->getParent();
        } while (checkTouchableNode && isTouchable);
        if (!isTouchable) continue;

        // prepare for touch testing
        touchTarget = nullptr;
        Node *activeNode = node->getActiveNode();
        if (!activeNode) continue;
        const Rect boundingBox = utils::getCascadeBoundingBox(activeNode);

        // set touch target
        Touch *touch = nullptr;
        for (auto it = touches.begin(); it != touches.end(); ++it)
        {
            touch = (Touch*)*it;
            const Point touchPoint = touch->getLocation();

            if (boundingBox.containsPoint(touchPoint))
            {
                if (!touchTarget)
                {
                    touchTarget = LuaTouchTargetNode::create(node);
                }

                if (touchTarget->getTouchMode() == LuaEventNode::modeTouchesOneByOne)
                {
                    touchTarget->setTouchId(touch->getID());
                    break;
                }
            }
        }

        if (!touchTarget)
        {
            // touch points not in current target, try to next
            continue;
        }

        // try to dispatching event
        Vector<LuaEventNode*> path(10);
        node = touchTarget->getNode();
        do
        {
            path.pushBack(node);
            node = node->getParent();
        } while (node != nullptr /*&& node != this*/);

        // phase: capturing
        // from parent to child
        bool dispatchingContinue = true;
        int touchMode = touchTarget->getTouchMode();
        for (long i = path.size() - 1; dispatchingContinue && i >= 0; --i)
        {
            node = path.at(i);
            if (touchMode == LuaEventNode::modeTouchesAllAtOnce)
            {
                node->ccTouchesCaptureBegan(touches, touchTarget->getNode());
            }
            else
            {
                dispatchingContinue = node->ccTouchCaptureBegan(touchTarget->findTouch(touches), touchTarget->getNode());
            }
        }

        if (!dispatchingContinue)
        {
            // the target stop dispatching, try to next
            continue;
        }

        // phase: targeting
        node = touchTarget->getNode();
        bool ret = true;
        if (touchMode == LuaEventNode::modeTouchesAllAtOnce)
        {
            node->ccTouchesBegan(touches, event);
        }
        else
        {
            ret = node->ccTouchBegan(touchTarget->findTouch(touches), event);
        }

        if (ret)
        {
            _touchingTargets.pushBack(touchTarget);
            //CCLOG("ADD TOUCH TARGET [%p]", touchTarget->getNode()->getNode());
        }

        if (node->isTouchSwallowEnabled())
        {
            // target swallow touch event, stop dispatching
            break;
        }

        // continue dispatching, try to next
    }
}
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);
}
Exemple #10
0
void Viewport::onTouchesBegan(const std::vector<Touch*>& pTouches, Event *pEvent)
{
    unscheduleAll();
    CC_ASSERT(this->m_TargetNode);
//    CC_ASSERT(pTouches);
    
    if (mFingerMap.size() >= 2) {
        isMove = true;
        return;
    }
    
    if( this->mTouchMode == TouchMode_Fling )
    {
        this->m_TargetNode->stopActionByTag(FLINGACTION_TAG);
    }
    this->mTouchMode = TouchMode_None;
    
    
    for ( auto &item: pTouches )
    {
        Touch* curTouch = dynamic_cast<Touch*>(item);
        CC_ASSERT(curTouch);
        if (mFingerMap.size() <= 2) {
            mFingerMap[curTouch->getID()] = curTouch->getLocation();
        }
    }
    
    switch(mFingerMap.size())
    {
        case 1:            
        {
            m_bTouchMoved = false;
            if (this->mTouchMode != TouchMode_ZoomFling) {
                this->mTouchMode = TouchMode_Scroll;
            }

            if (!mForceStopScroll && mMovable) {
                Touch* objTouch = dynamic_cast<Touch*>(*pTouches.begin());
                CC_ASSERT(objTouch);
                // let "beginScroll" fired by first step scroll , make single touch not fire this
                //                beginScroll(objTouch);
                if (mTouchBeginHandler) {
                    pushValueToLua(mTouchBeginHandler, objTouch);
                }
            }
            
            Touch* ptouch =getAnyTouchObject(pTouches);
            mPreviewPos = ptouch->getLocation();
        }
            break;
        case 2:
            if (this->mTouchMode != TouchMode_ZoomFling) {
                this->mTouchMode = TouchMode_Zoom;
            }
            if(!allowZoom){
                return;
            }
            do
            {
                FingerMap::iterator iter = mFingerMap.begin();
                CC_BREAK_IF(iter == mFingerMap.end());
                Vec2 pos1 = (*iter).second;
                CC_BREAK_IF(++iter == mFingerMap.end());
                Vec2 pos2 = (*iter).second;
                beginZoom(pos1, pos2);
            }
            while(0);
            isMove = true;
            break;
        default:
            isMove = true;
            CCLOG("begin:Touch not support with %lu fingers", pTouches.size());
    }
}