/
SneakyJoystickSkinnedBase.cpp
122 lines (107 loc) · 2.08 KB
/
SneakyJoystickSkinnedBase.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
#include "SneakyJoystickSkinnedBase.h"
bool SneakJoystickSkinnedBase::init()
{
if (!Layer::init())
{
return false;
}
this->backgroundSprite = nullptr;
this->thumbSprite = nullptr;
this->joystick = nullptr;
schedule(schedule_selector(SneakJoystickSkinnedBase::updatePositions));
return true;
}
SneakJoystickSkinnedBase::~SneakJoystickSkinnedBase()
{
if (backgroundSprite)
{
backgroundSprite->release();
backgroundSprite = nullptr;
}
if (thumbSprite)
{
thumbSprite->release();
thumbSprite = nullptr;
}
if (joystick)
{
joystick->release();
joystick = nullptr;
}
}
void SneakJoystickSkinnedBase::updatePositions(float delta)
{
if (joystick && thumbSprite)
{
thumbSprite->setPosition(joystick->getStickPosition());
}
}
void SneakJoystickSkinnedBase::setContentSize(Size s)
{
Layer::setContentSize(s);
backgroundSprite->setContentSize(s);
}
void SneakJoystickSkinnedBase::setBackgroundSprite(Sprite* bg)
{
if (backgroundSprite)
{
if (backgroundSprite->getParent())
{
backgroundSprite->getParent()->removeChild(backgroundSprite,true);
}
backgroundSprite->release();
}
bg->retain();
backgroundSprite = bg;
if (bg)
{
this->addChild(backgroundSprite,0);
this->setContentSize(backgroundSprite->getContentSize());
}
}
void SneakJoystickSkinnedBase::setThumbSprite(Sprite* thumb)
{
if (thumbSprite)
{
if (thumbSprite->getParent())
{
thumbSprite->getParent()->removeChild(thumbSprite,true);
}
thumbSprite->release();
}
thumb->retain();
thumbSprite = thumb;
if (thumb)
{
this->addChild(thumbSprite,1);
}
}
void SneakJoystickSkinnedBase::setJoystick(SneakJoystick* joys)
{
if (joystick)
{
if (joystick->getParent())
{
joystick->getParent()->removeChild(joystick,true);
}
joystick->release();
}
joys->retain();
joystick = joys;
if (joys)
{
this->addChild(joys,2);
if (thumbSprite)
{
joystick->setThumbRadius(thumbSprite->getBoundingBox().size.width/2);
}
else
{
joystick->setThumbRadius(0);
}
if (backgroundSprite)
{
joystick->setJoystickRadius(backgroundSprite->getBoundingBox().size.width/2);
}
}
}