Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions cocos/base/CCEventDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,30 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event)
//
if (oneByOneListeners)
{
auto mutableTouchesIter = mutableTouches.begin();
if (event->getEventCode() == EventTouch::EventCode::BEGAN)
{
auto fixedPriorityListeners = oneByOneListeners->getFixedPriorityListeners();
auto sceneGraphPriorityListeners = oneByOneListeners->getSceneGraphPriorityListeners();
if (fixedPriorityListeners != nullptr)
{
for (auto l : (*fixedPriorityListeners))
{
EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);
listener->_claimedTouches.clear();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dadidzf , if clear _claimedTouches, it may break the multi-touch logic of using EventListenerTouchOneByOne. I will check TouchTest to see whether it works after this fix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed that this patch breaks cpp-tests/63:Touches

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dumganhar Thanks, Do you know why it breaks? I test the cpp-tests/63, it is ok ! it seems multi-touch do not use _claimedTouches

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dadidzf , just test it on iOS or Android mobile phones. Using multi-touch on the white blocks. The second time you touch the blocks, it will not be responsive.

}
}

if (sceneGraphPriorityListeners != nullptr)
{
for (auto l : (*sceneGraphPriorityListeners))
{
EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);
listener->_claimedTouches.clear();
}
}
}

auto mutableTouchesIter = mutableTouches.begin();

for (auto& touches : originalTouches)
{
Expand Down Expand Up @@ -1004,7 +1027,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event)
}
}
}
else if (listener->_claimedTouches.size() > 0
else if (!listener->_claimedTouches.empty()
&& ((removedIter = std::find(listener->_claimedTouches.begin(), listener->_claimedTouches.end(), touches)) != listener->_claimedTouches.end()))
{
isClaimed = true;
Expand Down Expand Up @@ -1081,7 +1104,7 @@ void EventDispatcher::dispatchTouchEvent(EventTouch* event)
//
// process standard handlers 2nd
//
if (allAtOnceListeners && mutableTouches.size() > 0)
if (allAtOnceListeners && !mutableTouches.empty())
{

auto onTouchesEvent = [&](EventListener* l) -> bool{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ EventDispatcherTests::EventDispatcherTests()
ADD_TEST_CASE(PauseResumeTargetTest);
ADD_TEST_CASE(Issue4129);
ADD_TEST_CASE(Issue4160);
ADD_TEST_CASE(Issue17476);
ADD_TEST_CASE(DanglingNodePointersTest);
ADD_TEST_CASE(RegisterAndUnregisterWhileEventHanldingTest);
ADD_TEST_CASE(WindowEventsTest);
Expand Down Expand Up @@ -1209,6 +1210,92 @@ std::string Issue4160::subtitle() const
return "Touch the red block twice \n should not crash and the red one couldn't be touched";
}

// Issue17476
Issue17476::Issue17476()
{
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();

auto labelFirst = Label::createWithSystemFont("First\nClick\nHere\nThe\nIntersect", "Arial", 15);
labelFirst->setPosition(origin + Vec2(size.width / 2, size.height / 2));
addChild(labelFirst);

auto sprite1 = Sprite::create();
sprite1->setTexture("Images/CyanSquare.png");
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-25, 0));
addChild(sprite1, -10);

auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(false);
listener1->onTouchBegan = [=](Touch* touch, Event* event) {
auto ptInSprite = sprite1->convertToNodeSpace(touch->getLocation());
if (sprite1->getTextureRect().containsPoint(ptInSprite))
{
CCLOG("Left Square On TouchBegan !");
sprite1->setColor(Color3B::RED);
return true;
}
else
{
return false;
}
};

listener1->onTouchEnded = [=](Touch* touch, Event* event) {
CCLOG("Left Square On TouchEnded !");
sprite1->setColor(Color3B::WHITE);
event->stopPropagation();
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);

auto sprite2 = Sprite::create();
sprite2->setTexture("Images/MagentaSquare.png");
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(25, 0));
addChild(sprite2, -20);

auto listener2 = EventListenerTouchOneByOne::create();
listener2->setSwallowTouches(false);
listener2->onTouchBegan = [=](Touch* touch, Event* event) {
auto ptInSprite = sprite2->convertToNodeSpace(touch->getLocation());
if (sprite2->getTextureRect().containsPoint(ptInSprite))
{
CCLOG("Right Square On TouchBegan !");
sprite2->setColor(Color3B::RED);
return true;
}
else
{
return false;
}
};

listener2->onTouchEnded = [=](Touch* touch, Event* event) {
CCLOG("Right Square On TouchEnded !");
sprite2->setColor(Color3B::WHITE);
event->stopPropagation();
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, sprite2);

auto labelSecond = Label::createWithSystemFont("Second Step click Here, outside the squares", \
"Arial", 15);
labelSecond->setPosition(origin + Vec2(size.width / 2, size.height / 2) + Vec2(0, -90));
addChild(labelSecond);
}

Issue17476::~Issue17476()
{
}

std::string Issue17476::title() const
{
return "Issue 17476: _claimedTouches not cleared!";
}

std::string Issue17476::subtitle() const
{
return "First Click the intersection of the two squares, Then click outside, you will find that the bottom square responsed to the second click !";
}

// DanglingNodePointersTest
class DanglingNodePointersTestSprite : public Sprite
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ class Issue4160 : public EventDispatcherTestDemo
private:
};

class Issue17476 : public EventDispatcherTestDemo
{
public:
CREATE_FUNC(Issue17476);
Issue17476();
virtual ~Issue17476();

virtual std::string title() const override;
virtual std::string subtitle() const override;

private:
};

class DanglingNodePointersTest : public EventDispatcherTestDemo
{
public:
Expand Down