TouchInterceptingLayer.cpp
2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
// TouchInterceptingLayer.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 12.05.2017.
//
//
#include "TouchInterceptingLayer.h"
// create
TouchInterceptingLayer * TouchInterceptingLayer::create(const cocos2d::Color4B& color, bool sceneGraphPriority)
{
TouchInterceptingLayer * layer = new (std::nothrow) TouchInterceptingLayer();
if(layer && layer->initWithColor(color, sceneGraphPriority))
{
layer->autorelease();
return layer;
}
CC_SAFE_DELETE(layer);
return nullptr;
}
// overrides
bool TouchInterceptingLayer::initWithColor(const cocos2d::Color4B& color, bool sceneGraphPriority)
{
if(!LayerColor::initWithColor(color))
{
return false;
}
_onTouchCallback = nullptr;
this->configureTouchListener(sceneGraphPriority);
return true;
}
//void TouchInterceptingLayer::onEnter()
//{
// cocos2d::LayerColor::onEnter();
//}
void TouchInterceptingLayer::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 TouchInterceptingLayer::setOnTouchCallback(const std::function<void(cocos2d::Touch*)>& onTouchCallback)
{
this->_onTouchCallback = onTouchCallback;
}
void TouchInterceptingLayer::setOnTouchMovedCallback(const std::function<void(cocos2d::Touch*)>& onTouchCallback)
{
this->_onTouchMovedCallback = onTouchCallback;
}
void TouchInterceptingLayer::setOnTouchEndedCallback(const std::function<void(cocos2d::Touch*)>& onTouchCallback)
{
this->_onTouchEndedCallback = onTouchCallback;
}
void TouchInterceptingLayer::clearTouchHandlers()
{
_onTouchCallback = nullptr;
_onTouchMovedCallback = nullptr;
_onTouchEndedCallback = nullptr;
}