-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathShopMenu.cpp
More file actions
164 lines (131 loc) · 3.36 KB
/
Copy pathShopMenu.cpp
File metadata and controls
164 lines (131 loc) · 3.36 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// ShopMenu.cpp
// IronPig
//
// Created by firedragonpzy on 13-9-3.
//
//
#include "ShopMenu.h"
USING_NS_CC;
ShopMenu* ShopMenu::create()
{
return ShopMenu::create(NULL, NULL);
}
ShopMenu * ShopMenu::create(CCMenuItem* item, ...)
{
va_list args;
va_start(args,item);
ShopMenu *pRet = ShopMenu::createWithItems(item, args);
va_end(args);
return pRet;
}
ShopMenu* ShopMenu::createWithItems(CCMenuItem* item, va_list args)
{
CCArray* pArray = NULL;
if( item )
{
pArray = CCArray::create(item, NULL);
CCMenuItem *i = va_arg(args, CCMenuItem*);
while(i)
{
pArray->addObject(i);
i = va_arg(args, CCMenuItem*);
}
}
return ShopMenu::createWithArray(pArray);
}
ShopMenu* ShopMenu::createWithArray(CCArray* pArrayOfItems)
{
ShopMenu *pRet = new ShopMenu();
if (pRet && pRet->initWithArray(pArrayOfItems))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
ShopMenu* ShopMenu::create(CCArray* pArrayOfItems)
{
ShopMenu *pRet = new ShopMenu();
if (pRet && pRet->initWithArray(pArrayOfItems))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
void ShopMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
}
bool ShopMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
CC_UNUSED_PARAM(event);
if (m_eState != kCCMenuStateWaiting || ! m_bVisible || !isEnabled())
{
return false;
}
if (!isEnabled()) {
return true;
}
for (CCNode *c = this->m_pParent; c != NULL; c = c->getParent())
{
if (c->isVisible() == false)
{
return false;
}
}
m_pSelectedItem = this->itemForTouch(touch);
if (m_pSelectedItem)
{
m_eState = kCCMenuStateTrackingTouch;
if (!m_pSelectedItem->isEnabled()) {
return true;
}
m_pSelectedItem->selected();
return true;
}
return false;
}
void ShopMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
m_pLastItem = m_pSelectedItem;
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
((ShopItem*)m_pSelectedItem)->activate();
m_eState = kCCMenuStateWaiting;
}
CCMenuItem* ShopMenu::itemForTouch(CCTouch *touch)
{
CCPoint touchLocation = touch->getLocation();
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCMenuItem* pChild = dynamic_cast<CCMenuItem*>(pObject);
if (pChild && pChild->isVisible())
{
CCPoint local = pChild->convertToNodeSpace(touchLocation);
CCRect r = pChild->rect();
r.origin = CCPointZero;
if (r.containsPoint(local))
{
if ( m_pLastItem && m_pLastItem->isEnabled()) {
m_pLastItem->unselected();
}
return pChild;
}
}
}
}
return NULL;
}