forked from lshain/SneakyInput-Cocos2dx-3.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SneakyJoystick.cpp
168 lines (138 loc) · 4.02 KB
/
SneakyJoystick.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
#include "SneakyJoystick.h"
using namespace cocos2d;
#define SJ_PI 3.14159265359f
#define SJ_PI_X_2 6.28318530718f
#define SJ_RAD2DEG 180.0f/SJ_PI
#define SJ_DEG2RAD SJ_PI/180.0f
SneakyJoystick::~SneakyJoystick()
{
}
bool SneakyJoystick::initWithRect(CCRect rect)
{
bool pRet = false;
//if(CCSprite::init()){
stickPosition = CCPointZero;
degrees = 0.0f;
velocity = CCPointZero;
autoCenter = true;
isDPad = false;
hasDeadzone = false;
numberOfDirections = 4;
this->setJoystickRadius(rect.size.width/2);
this->setThumbRadius(32.0f);
this->setDeadRadius(0.0f);
//Cocos node stuff
setPosition(rect.origin);
pRet = true;
//}
return pRet;
}
void SneakyJoystick::onEnterTransitionDidFinish()
{
mEventListenerTouch = EventListenerTouchOneByOne::create();
mEventListenerTouch->onTouchBegan = CC_CALLBACK_2(SneakyJoystick::onTouchBegan, this);
mEventListenerTouch->onTouchMoved = CC_CALLBACK_2(SneakyJoystick::onTouchMoved, this);
mEventListenerTouch->onTouchEnded = CC_CALLBACK_2(SneakyJoystick::onTouchEnded, this);
mEventListenerTouch->onTouchCancelled = CC_CALLBACK_2(SneakyJoystick::onTouchCancelled, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(mEventListenerTouch, this);
}
void SneakyJoystick::onExit()
{
Director::getInstance()->getEventDispatcher()->removeEventListener(mEventListenerTouch);
}
float round(float r) {
return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}
void SneakyJoystick::updateVelocity(CCPoint point)
{
// Calculate distance and angle from the center.
float dx = point.x;
float dy = point.y;
float dSq = dx * dx + dy * dy;
if(dSq <= deadRadiusSq){
velocity = CCPointZero;
degrees = 0.0f;
stickPosition = point;
return;
}
float angle = atan2f(dy, dx); // in radians
if(angle < 0){
angle += SJ_PI_X_2;
}
float cosAngle;
float sinAngle;
if(isDPad){
float anglePerSector = 360.0f / numberOfDirections * SJ_DEG2RAD;
angle = round(angle/anglePerSector) * anglePerSector;
}
cosAngle = cosf(angle);
sinAngle = sinf(angle);
// NOTE: Velocity goes from -1.0 to 1.0.
if (dSq > joystickRadiusSq || isDPad) {
dx = cosAngle * joystickRadius;
dy = sinAngle * joystickRadius;
}
velocity = CCPointMake(dx/joystickRadius, dy/joystickRadius);
degrees = angle * SJ_RAD2DEG;
// Update the thumb's position
stickPosition = ccp(dx, dy);
}
void SneakyJoystick::setIsDPad(bool b)
{
isDPad = b;
if(isDPad){
hasDeadzone = true;
this->setDeadRadius(10.0f);
}
}
void SneakyJoystick::setJoystickRadius(float r)
{
joystickRadius = r;
joystickRadiusSq = r*r;
}
void SneakyJoystick::setThumbRadius(float r)
{
thumbRadius = r;
thumbRadiusSq = r*r;
}
void SneakyJoystick::setDeadRadius(float r)
{
deadRadius = r;
deadRadiusSq = r*r;
}
bool SneakyJoystick::onTouchBegan(Touch* touch, Event* event)
{
CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
//if([background containsPoint:[background convertToNodeSpace:location]]){
location = this->convertToNodeSpace(location);
//Do a fast rect check before doing a circle hit check:
if(location.x < -joystickRadius || location.x > joystickRadius || location.y < -joystickRadius || location.y > joystickRadius){
return false;
}else{
float dSq = location.x*location.x + location.y*location.y;
if(joystickRadiusSq > dSq){
this->updateVelocity(location);
return true;
}
}
return false;
}
void SneakyJoystick::onTouchMoved(Touch* touch, Event* event)
{
CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
location = this->convertToNodeSpace(location);
this->updateVelocity(location);
}
void SneakyJoystick::onTouchEnded(Touch* touch, Event* event)
{
CCPoint location = CCPointZero;
if(!autoCenter){
CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
location = this->convertToNodeSpace(location);
}
this->updateVelocity(location);
}
void SneakyJoystick::onTouchCancelled(Touch* touch, Event* event)
{
this->onTouchEnded(touch, event);
}