-
Notifications
You must be signed in to change notification settings - Fork 13
/
SneakyButton.cpp
114 lines (98 loc) · 2.65 KB
/
SneakyButton.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
#include "SneakyButton.h"
using namespace cocos2d;
void SneakyButton::onEnterTransitionDidFinish()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);
}
void SneakyButton::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}
bool SneakyButton::initWithRect(CCRect rect)
{
bool pRet = false;
//if(CCSprite::init()){
bounds = CCRectMake(0, 0, rect.size.width, rect.size.height);
center = CCPointMake(rect.size.width/2, rect.size.height/2);
status = 1; //defaults to enabled
active = false;
value = 0;
isHoldable = 0;
isToggleable = 0;
radius = 32.0f;
rateLimit = 1.0f/120.0f;
setPosition(rect.origin); //not sure about this
pRet = true;
//}
return pRet;
}
void SneakyButton::limiter(float delta)
{
value = 0;
this->unschedule(schedule_selector(SneakyButton::limiter));
active = false;
}
void SneakyButton::setRadius(float r)
{
radius = r;
radiusSq = r*r;
}
bool SneakyButton::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
if (active) return false;
CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
location = this->convertToNodeSpace(location);
//Do a fast rect check before doing a circle hit check:
if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
return false;
}else{
float dSq = location.x*location.x + location.y*location.y;
if(radiusSq > dSq){
active = true;
if (!isHoldable && !isToggleable){
value = 1;
this->schedule(schedule_selector(SneakyButton::limiter), rateLimit);
}
if (isHoldable) value = 1;
if (isToggleable) value = !value;
return true;
}
}
return false;
}
void SneakyButton::ccTouchMoved(CCTouch *touch, CCEvent *event)
{
if (!active) return;
CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
location = this->convertToNodeSpace(location);
//Do a fast rect check before doing a circle hit check:
if(location.x < -radius || location.x > radius || location.y < -radius || location.y > radius){
return;
}else{
float dSq = location.x*location.x + location.y*location.y;
if(radiusSq > dSq){
if (isHoldable) value = 1;
}
else {
if (isHoldable) value = 0; active = false;
}
}
}
void SneakyButton::ccTouchEnded(CCTouch *touch, CCEvent *event)
{
if (!active) return;
if (isHoldable) value = 0;
if (isHoldable||isToggleable) active = false;
}
void SneakyButton::ccTouchCancelled(CCTouch *touch, CCEvent *event)
{
this->ccTouchEnded(touch, event);
}
void SneakyButton::touchDelegateRelease()
{
this->release();
}
void SneakyButton::touchDelegateRetain()
{
this->retain();
}