-
Notifications
You must be signed in to change notification settings - Fork 70
/
SneakyButtonSkinnedBase.cpp
172 lines (157 loc) · 3.6 KB
/
SneakyButtonSkinnedBase.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
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
163
164
165
166
167
168
169
170
171
172
#include "SneakyButtonSkinnedBase.h"
using namespace cocos2d;
SneakyButtonSkinnedBase::~SneakyButtonSkinnedBase()
{
if (defaultSprite)
{
defaultSprite->release();
defaultSprite = NULL;
}
if (activatedSprite)
{
activatedSprite->release();
activatedSprite = NULL;
}
if (disabledSprite)
{
disabledSprite->release();
disabledSprite = NULL;
}
if (pressSprite)
{
pressSprite->release();
pressSprite = NULL;
}
if (button)
{
button->release();
button = NULL;
}
}
bool SneakyButtonSkinnedBase::init() //Possible errors here
{
bool pRet = false;
if(CCLayer::init()){
this->defaultSprite = NULL;
//defaultSprite->retain();
this->activatedSprite = NULL;
//activatedSprite->retain();
this->disabledSprite = NULL;
//disabledSprite->retain();
this->pressSprite = NULL;
//pressSprite->retain();
this->button = NULL;
//button->retain();
this->schedule(schedule_selector(SneakyButtonSkinnedBase::watchSelf));
pRet = true;
}
return pRet;
}
void SneakyButtonSkinnedBase::watchSelf(ccTime delta) //Be Careful Here
{
if (!this->button->getStatus()){
if(disabledSprite){
disabledSprite->setIsVisible(true);
}
else {
disabledSprite->setIsVisible(false);
}
}
else {
if(!this->button->getIsActive()){
pressSprite->setIsVisible(false);
if(this->button->getValue() == 0){
activatedSprite->setIsVisible(false);
if(defaultSprite){
defaultSprite->setIsVisible(true);
}
}
else {
activatedSprite->setIsVisible(true);
}
}
else {
defaultSprite->setIsVisible(false);
if(pressSprite){
pressSprite->setIsVisible(true);
}
}
}
}
void SneakyButtonSkinnedBase::setContentSize(CCSize s)
{
CCLayer::setContentSize(s);
defaultSprite->setContentSize(s);
//button->setRadius(s.width/2);
}
void SneakyButtonSkinnedBase::setDefaultSprite(CCSprite *aSprite)
{
if(defaultSprite){
if(defaultSprite->getParent())
defaultSprite->getParent()->removeChild(defaultSprite, true);
defaultSprite->release();
}
aSprite->retain();
defaultSprite = aSprite; //Check again here
if(aSprite){
this->addChild(defaultSprite, 0);
this->setContentSize(defaultSprite->getContentSize());
}
}
void SneakyButtonSkinnedBase::setActivatedSprite(CCSprite *aSprite)
{
if(activatedSprite){
if(activatedSprite->getParent())
activatedSprite->getParent()->removeChild(activatedSprite, true);
activatedSprite->release();
}
aSprite->retain();
activatedSprite = aSprite;
if(aSprite){
this->addChild(activatedSprite, 1);
this->setContentSize(activatedSprite->getContentSize());
}
}
void SneakyButtonSkinnedBase::setDisabledSprite(CCSprite *aSprite)
{
if(disabledSprite){
if(disabledSprite->getParent())
disabledSprite->getParent()->removeChild(disabledSprite, true);
disabledSprite->release();
}
aSprite->retain();
disabledSprite = aSprite;
if(aSprite){
this->addChild(disabledSprite, 2);
this->setContentSize(disabledSprite->getContentSize());
}
}
void SneakyButtonSkinnedBase::setPressSprite(CCSprite *aSprite)
{
if(pressSprite){
if(pressSprite->getParent())
pressSprite->getParent()->removeChild(pressSprite, true);
pressSprite->release();
}
aSprite->retain();
pressSprite = aSprite;
if(aSprite){
this->addChild(pressSprite, 3);
this->setContentSize(pressSprite->getContentSize());
}
}
void SneakyButtonSkinnedBase::setButton(SneakyButton *aButton)
{
if(button){
if(button->getParent())
button->getParent()->removeChild(button, true);
button->release();
}
aButton->retain();
button = aButton;
if(aButton){
this->addChild(button, 4);
if(defaultSprite)
button->setRadius(defaultSprite->boundingBox().size.width/2);
}
}