// // HTouchInterceptingLayer.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 12.05.2017. // // #include "HTouchInterceptingLayer.h" // create HTouchInterceptingLayer * HTouchInterceptingLayer::create(const cocos2d::Color4B& color, bool sceneGraphPriority) { HTouchInterceptingLayer * layer = new (std::nothrow) HTouchInterceptingLayer(); if(layer && layer->initWithColor(color, sceneGraphPriority)) { layer->autorelease(); return layer; } CC_SAFE_DELETE(layer); return nullptr; } // overrides bool HTouchInterceptingLayer::initWithColor(const cocos2d::Color4B& color, bool sceneGraphPriority) { if(!LayerColor::initWithColor(color)) { return false; } _onTouchCallback = nullptr; this->configureTouchListener(sceneGraphPriority); return true; } //void HTouchInterceptingLayer::onEnter() //{ // cocos2d::LayerColor::onEnter(); //} void HTouchInterceptingLayer::configureTouchListener(bool sceneGraphPriority) { auto touchListener = cocos2d::EventListenerTouchOneByOne::create(); touchListener->setSwallowTouches(true); touchListener->onTouchBegan = [&](cocos2d::Touch* touch, cocos2d::Event* event){ if(_onTouchCallback){ _onTouchCallback(touch); } return true; }; touchListener->onTouchMoved = [&](cocos2d::Touch* touch, cocos2d::Event* event){ if(_onTouchMovedCallback){ _onTouchMovedCallback(touch); } }; touchListener->onTouchEnded = [&](cocos2d::Touch* touch, cocos2d::Event* event){ if(_onTouchEndedCallback){ _onTouchEndedCallback(touch); } }; if(sceneGraphPriority){ _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); } else { _eventDispatcher->addEventListenerWithFixedPriority(touchListener, TouchEventListenerPriority); } } //setters void HTouchInterceptingLayer::setOnTouchCallback(const std::function& onTouchCallback) { this->_onTouchCallback = onTouchCallback; } void HTouchInterceptingLayer::setOnTouchMovedCallback(const std::function& onTouchCallback) { this->_onTouchMovedCallback = onTouchCallback; } void HTouchInterceptingLayer::setOnTouchEndedCallback(const std::function& onTouchCallback) { this->_onTouchEndedCallback = onTouchCallback; } void HTouchInterceptingLayer::clearTouchHandlers() { _onTouchCallback = nullptr; _onTouchMovedCallback = nullptr; _onTouchEndedCallback = nullptr; }