// // CCBlockTouchLayer.cpp // cocos2d_libs // // Created by zhuge on 2023/3/16. // #include "CCBlockTouchLayer.h" #include "base/CCDirector.h" #include "base/CCEventDispatcher.h" #include "base/CCEventListenerTouch.h" cocos2d::CCBlockTouchLayer::CCBlockTouchLayer() { } cocos2d::CCBlockTouchLayer::~CCBlockTouchLayer() { } bool cocos2d::CCBlockTouchLayer::init() { if (!cocos2d::Layer::init()) { return false; } _registTouch(); return true; } void cocos2d::CCBlockTouchLayer::_registTouch() { auto listener = cocos2d::EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [this](cocos2d::Touch*, cocos2d::Event*) -> bool { if (this->isVisible() && this->hasVisibleParents()) { // block all touches if visible return true; } else { return false; } }; cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); } bool cocos2d::CCBlockTouchLayer::hasVisibleParents(){ auto parent = this->getParent(); for( auto c = parent; c != nullptr; c = c->getParent() ) { if( !c->isVisible() ) { return false; } } return true; }