void TouchEventDispatchingPerfTest::generateTestFunctions()
{
    TestFunction testFunctions[] = {
        { "OneByOne-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchOneByOne::create();
                listener->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listener->onTouchMoved = [](Touch* touch, Event* event){};
                listener->onTouchEnded = [](Touch* touch, Event* event){};

                // Create new touchable nodes
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;

            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);

            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "OneByOne-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchOneByOne::create();
                listener->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listener->onTouchMoved = [](Touch* touch, Event* event){};
                listener->onTouchEnded = [](Touch* touch, Event* event){};
                
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listener->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "AllAtOnce-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchAllAtOnce::create();
                listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                // Create new touchable nodes
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "AllAtOnce-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchAllAtOnce::create();
                listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listener->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "TouchModeMix-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listenerOneByOne = EventListenerTouchOneByOne::create();
                listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){};
                listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){};
                
                auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create();
                listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                int i = 0;
                // Create new touchable nodes
                for (; i < this->_quantityOfNodes/2; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listenerOneByOne->clone(), node);
                }
                
                for (; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listenerAllAtOnce->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "TouchModeMix-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listenerOneByOne = EventListenerTouchOneByOne::create();
                listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){};
                listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){};
                
                auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create();
                listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                int i = 0;

                for (; i < this->_quantityOfNodes/2; ++i)
                {
                    auto l = listenerOneByOne->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                for (; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listenerAllAtOnce->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
    };
    
    for (const auto& func : testFunctions)
    {
        _testFunctions.push_back(func);
    }
}
void TouchesPerformTest3::onEnter()
{
    PerformBasicLayer::onEnter();
    
    
    auto s = Director::getInstance()->getWinSize();
    
    // add title
    auto label = Label::createWithTTF(title().c_str(), "fonts/arial.ttf", 32);
    addChild(label, 1);
    label->setPosition(Vec2(s.width/2, s.height-50));
    
#define TOUCH_PROFILER_NAME  "TouchProfileName"
#define TOUCHABLE_NODE_NUM 1000
    
    srand((unsigned)time(nullptr));
    
    for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i)
    {
        int zorder = rand() % TOUCHABLE_NODE_NUM;
        auto layer = new (std::nothrow) TouchableLayer();
        
        auto listener = EventListenerTouchOneByOne::create();
        listener->onTouchBegan = CC_CALLBACK_2(TouchableLayer::onTouchBegan, layer);
        listener->onTouchMoved = CC_CALLBACK_2(TouchableLayer::onTouchMoved, layer);
        listener->onTouchEnded = CC_CALLBACK_2(TouchableLayer::onTouchEnded, layer);
        listener->onTouchCancelled = CC_CALLBACK_2(TouchableLayer::onTouchCancelled, layer);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, layer);
        
        addChild(layer, zorder);
        layer->release();
    }
    
    auto emitEventlabel = Label::createWithSystemFont("Emit Touch Event", "", 24);
    auto menuItem = MenuItemLabel::create(emitEventlabel, [this](Ref* sender){
        
        CC_PROFILER_PURGE_ALL();
        
        std::vector<Touch*> touches;
        for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i)
        {
            Touch* touch = new (std::nothrow) Touch();
            touch->setTouchInfo(i, 10, (i+1) * 10);
            touches.push_back(touch);
        }
        
        EventTouch event;
        event.setEventCode(EventTouch::EventCode::BEGAN);
        event.setTouches(touches);
        
        for (int i = 0; i < 100; ++i)
        {
            CC_PROFILER_START(TOUCH_PROFILER_NAME);
            
            _eventDispatcher->dispatchEvent(&event);
            
            CC_PROFILER_STOP(TOUCH_PROFILER_NAME);
        }
        
        CC_PROFILER_DISPLAY_TIMERS();
        
        for (auto& touch : touches)
        {
            touch->release();
        }
    });
    
    menuItem->setPosition(Vec2(0, -20));
    auto menu = Menu::create(menuItem, nullptr);
    addChild(menu);
}
示例#3
0
/**Handles an event Called first by OnEvent
* @param  pEvent Pointer to an EventType abstract base class describing the event
* @return bool Return false to stop propagating this event 
*/
bool GameMap::EarlyOnEvent (EventType* pEvent )
{
	Vector2 pos(0,0);

	if(pEvent && pEvent->getType() == EVENT_TOUCH)
	{
		EventTouch* evTouch = dynamic_cast<EventTouch*>(pEvent);
		if(evTouch)
		{
			if(pCollider->isColliding(evTouch->getX(), evTouch->getY()) || (evTouch->getTouchType() == TOUCH_END))
			{
				//adjust touch event coords to local space
				getTransform().GetPosition(pos);
				pos.setX(evTouch->getX() - pos.getX());
				pos.setY(evTouch->getY() - pos.getY());

				int tX = pos.getX() / m_tileSize;
				int tY = pos.getY() / m_tileSize;

				if((tX >= 0 && tY >=0 &&
					tX < m_tilesX && tY < m_tilesY ) || (evTouch->getTouchType() == TOUCH_END))
				{
					//decide what to do with the touch for various states
					switch(m_jewelMoveState)
					{
					case IDLE:
						if(evTouch->getTouchType() == TOUCH_START)
						{
							m_sourceJewelTile.x = tX;
							m_sourceJewelTile.y = tY;
							m_jewelMoveState = SOURCE_JEWEL_SET;
						}
						break;
					case SOURCE_JEWEL_SET:
						if(evTouch->getTouchType() == TOUCH_START)
						{	
							int dx,dy;
							dx = tX - m_sourceJewelTile.x;
							dy = tY - m_sourceJewelTile.y;
							/* only up/down left/right neighbours allowed */
							if((dx == 0 && (dy == 1 || dy == -1 )) || 
							   (dy == 0 && (dx == 1 || dx == -1 ))) 
							{
								m_destJewelTile.x = tX;
								m_destJewelTile.y = tY;
								m_jewelMoveState = SWITCHING_JEWELS;
							}
							else /* a new source tile is set */
							{
								m_sourceJewelTile.x = tX;
								m_sourceJewelTile.y = tY;
							}
						}
						else if(evTouch->getTouchType() == TOUCH_END)
						{
							if(m_sourceJewelTile.x != tX ||
							   m_sourceJewelTile.y != tY)
							{
								//start from the source tile
								m_destJewelTile = m_sourceJewelTile;

								Vector2 centerSource(m_sourceJewelTile.x*m_tileSize + m_tileSize/2, 
													 m_sourceJewelTile.y*m_tileSize + m_tileSize/2);
								int dx, dy;
								//compute the absolute movement from the source point
								dx = centerSource.getX() > pos.getX() ? centerSource.getX() - pos.getX() : 
																		pos.getX() - centerSource.getX();

								dy = centerSource.getY() > pos.getY() ? centerSource.getY() - pos.getY() : 
																		pos.getY() - centerSource.getY();
								//determine if the movement was predominantly horizontal or vertical
								if(dx > dy)
								{
									if(centerSource.getX() > pos.getX())
									{
										m_destJewelTile.x--;
									}
									else
									{
										m_destJewelTile.x++;
									}
								}
								else
								{
									if(centerSource.getY() > pos.getY())
									{
										m_destJewelTile.y--;
									}
									else
									{
										m_destJewelTile.y++;
									}								
								}

								//in the end, if we have a valid tile we start switching the jewels
								if( m_destJewelTile.x >= 0 && m_destJewelTile.x < m_tilesX &&
									m_destJewelTile.y >= 0 && m_destJewelTile.y < m_tilesY )
								{
									m_jewelMoveState = SWITCHING_JEWELS;
								}
							}
						}

						//if the state changed to that of switching the jewels, start the switch
						if (m_jewelMoveState == SWITCHING_JEWELS)
						{
							pJewelsMap[m_sourceJewelTile.x][m_sourceJewelTile.y]->MoveTo(
								Vector2(m_destJewelTile.x*m_tileSize, m_destJewelTile.y*m_tileSize), kJewelSwitchSpeed);
							pJewelsMap[m_destJewelTile.x][m_destJewelTile.y]->MoveTo(
								Vector2(m_sourceJewelTile.x*m_tileSize, m_sourceJewelTile.y*m_tileSize), kJewelSwitchSpeed);
						}

						break;
					
					default:
						break;
					};
					//pJewelsMap[tX][tY]->ScaleTo(Vector2(0.5, 0.5), 2);	
					//pJewelsMap[tX][tY]->MoveTo(Vector2(tX*m_tileSize+m_tileSize/8, tY*m_tileSize+m_tileSize/8), 3);	
				}

				return false;
			}
		}
			
	}

	//not interested in this event let it bubble
	return true;
}
示例#4
0
void TouchesPerformTest3::onEnter()
{
    PerformBasicLayer::onEnter();
    
    
    auto s = Director::getInstance()->getWinSize();
    
    // add title
    auto label = LabelTTF::create(title().c_str(), "Arial", 32);
    addChild(label, 1);
    label->setPosition(Point(s.width/2, s.height-50));
    
#define TOUCH_PROFILER_NAME  "TouchProfileName"
#define TOUCHABLE_NODE_NUM 1000
    
    srand(time(nullptr));
    
    for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i)
    {
        int zorder = rand() % TOUCHABLE_NODE_NUM;
        auto layer = new TouchableLayer();
        layer->setTouchEnabled(true);
        layer->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
        addChild(layer, zorder);
        layer->release();
    }
    
    auto emitEventlabel = LabelTTF::create("Emit Touch Event", "", 24);
    auto menuItem = MenuItemLabel::create(emitEventlabel, [](Object* sender){
        
        CC_PROFILER_PURGE_ALL();
        
        std::vector<Touch*> touches;
        for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i)
        {
            Touch* touch = new Touch();
            touch->setTouchInfo(i, 10, (i+1) * 10);
            touches.push_back(touch);
        }
        
        EventTouch event;
        event.setEventCode(EventTouch::EventCode::BEGAN);
        event.setTouches(touches);
        
        auto dispatcher = EventDispatcher::getInstance();
        
        for (int i = 0; i < 100; ++i)
        {
            CC_PROFILER_START(TOUCH_PROFILER_NAME);
            
            dispatcher->dispatchEvent(&event, false);
            
            CC_PROFILER_STOP(TOUCH_PROFILER_NAME);
        }
        
        CC_PROFILER_DISPLAY_TIMERS();
        
        for (auto& touch : touches)
        {
            touch->release();
        }
    });
    
    menuItem->setPosition(Point(0, -20));
    auto menu = Menu::create(menuItem, NULL);
    addChild(menu);
}