HTouchInterceptingLayer.cpp
2.58 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
//
//  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<void(cocos2d::Touch*)>& onTouchCallback)
{
    this->_onTouchCallback = onTouchCallback;
}
void HTouchInterceptingLayer::setOnTouchMovedCallback(const std::function<void(cocos2d::Touch*)>& onTouchCallback)
{
    this->_onTouchMovedCallback = onTouchCallback;
}
void HTouchInterceptingLayer::setOnTouchEndedCallback(const std::function<void(cocos2d::Touch*)>& onTouchCallback)
{
    this->_onTouchEndedCallback = onTouchCallback;
}
void HTouchInterceptingLayer::clearTouchHandlers()
{
    _onTouchCallback = nullptr;
    _onTouchMovedCallback = nullptr;
    _onTouchEndedCallback = nullptr;
}