/
DialogLayer.cpp
106 lines (85 loc) · 2.26 KB
/
DialogLayer.cpp
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
103
104
105
106
#include "DialogLayer.h"
USING_NS_CC;
DialogLayer::DialogLayer():
mMenu(NULL),
mMenuItemArray(NULL),
mTouchedMenu(false)
{
}
DialogLayer::~DialogLayer()
{
}
bool DialogLayer::init()
{
bool bRet = false;
do {
CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 125)));
onInitDialog();
initMenu();
bRet = true;
} while (0);
return bRet;
}
void DialogLayer::pushMenu(CCMenuItem *pMenuItem)
{
if (!mMenuItemArray) {
mMenuItemArray = CCArray::create();
}
mMenuItemArray->addObject(pMenuItem);
}
bool DialogLayer::initMenu()
{
if (mMenuItemArray && mMenuItemArray->count() > 0) {
if (!mMenu) {
mMenu = CCMenu::createWithArray(mMenuItemArray);
mMenu->setPosition(CCPointZero);
addChild(mMenu);
}
}
return true;
}
void DialogLayer::onEnter()
{
CCLayerColor::onEnter();
// 屏蔽所有priority比自己大的消息
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority - 1, true);
}
void DialogLayer::onExit()
{
CCLayerColor::onExit();
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
bool DialogLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
// 因为拦截了所有消息(包括按钮) 所以需要将消息手动传给模态对话框上的按钮
if (mMenu) {
mTouchedMenu = mMenu->ccTouchBegan(pTouch, pEvent);
}
return true;
}
void DialogLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (mTouchedMenu) {
if (mMenu) {
mMenu->ccTouchMoved(pTouch, pEvent);
}
}
}
void DialogLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (mTouchedMenu) {
if (mMenu) {
mMenu->ccTouchEnded(pTouch, pEvent);
}
mTouchedMenu = false;
}
}
void DialogLayer::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
if (mTouchedMenu) {
if (mMenu) {
mMenu->ccTouchEnded(pTouch, pEvent);
}
mTouchedMenu = false;
}
}