1+ #include " JoystickNode.hpp"
2+
3+ bool JoystickNode::init () {
4+ if (!CCMenu::init ()) return false ;
5+ setContentSize ({100 , 100 });
6+
7+ m_bg = CCSprite::createWithSpriteFrameName (" d_circle_02_001.png" );
8+ m_bg->setScale (getContentSize ().width / m_bg->getContentSize ().width );
9+ m_bg->setOpacity (165 );
10+ m_bg->setColor ({189 , 189 , 189 });
11+ addChildAtPosition (m_bg, Anchor::Center);
12+
13+ m_center = CCSprite::createWithSpriteFrameName (" d_circle_02_001.png" );
14+ m_center->setScale (getContentSize ().width / m_center->getContentSize ().width / 3 );
15+ m_center->setOpacity (236 );
16+ m_center->setZOrder (1 );
17+ m_center->setColor ({0 , 0 , 0 });
18+ addChildAtPosition (m_center, Anchor::Center);
19+
20+ registerWithTouchDispatcher ();
21+
22+ return true ;
23+ }
24+
25+ JoystickNode *JoystickNode::create () {
26+ auto ret = new JoystickNode;
27+ if (!ret->init ()) {
28+ delete ret;
29+ return nullptr ;
30+ }
31+ ret->autorelease ();
32+ return ret;
33+ }
34+
35+ bool JoystickNode::ccTouchBegan (CCTouch *touch, CCEvent *event) {
36+ if (getScaledContentSize ().width / 2 < ccpDistance (getPosition (), touch->getLocation ())) {
37+ return false ;
38+ }
39+
40+ // m_offset = m_center->getPosition() - convertToNodeSpace(touch->getLocation());
41+
42+ return true ;
43+ }
44+
45+ void handleInput (GJBaseGameLayer *layer, CCPoint input, CCPoint old) {
46+ if (old.x == 1 ) {
47+ layer->handleButton (false , 3 , true );
48+ } else if (old.x == -1 ) {
49+ layer->handleButton (false , 2 , true );
50+ }
51+ if (old.y == 1 ) {
52+ layer->handleButton (false , 3 , false );
53+ } else if (old.y == -1 ) {
54+ layer->handleButton (false , 2 , false );
55+ }
56+
57+ if (input.x == 1 ) {
58+ layer->handleButton (true , 3 , true );
59+ } else if (input.x == -1 ) {
60+ layer->handleButton (true , 2 , true );
61+ }
62+ if (input.y == 1 ) {
63+ layer->handleButton (true , 3 , false );
64+ } else if (input.y == -1 ) {
65+ layer->handleButton (true , 2 , false );
66+ }
67+ }
68+
69+ void JoystickNode::ccTouchEnded (CCTouch *touch, CCEvent *event) {
70+ if (auto pl = PlayLayer::get ()) {
71+ handleInput (pl, {0 , 0 }, m_currentInput);
72+ }
73+
74+ m_currentInput = CCPoint{0 , 0 };
75+
76+ m_center->setPosition (getScaledContentSize () / 2 );
77+ }
78+
79+ void JoystickNode::ccTouchMoved (CCTouch *touch, CCEvent *event) {
80+ auto pos = convertToNodeSpace (touch->getLocation ());
81+
82+ auto fromCenter = pos - getScaledContentSize () / 2 ;
83+
84+ if (fromCenter.getLength () > getScaledContentWidth () / 2 - m_center->getScaledContentWidth () / 2 ) {
85+ fromCenter = fromCenter.normalize () * (getScaledContentWidth () / 2 - m_center->getScaledContentWidth () / 2 );
86+ pos = fromCenter + getScaledContentSize () / 2 ;
87+ }
88+
89+ CCPoint inp = {0 , 0 };
90+
91+ auto angle = atan2 (fromCenter.y , fromCenter.x );
92+
93+ if (std::abs (fromCenter.x ) > fromCenter.normalize ().x * 15 ) {
94+ if (angle > -M_PI / 3 && angle < M_PI / 3 ) {
95+ inp.x = 1 ;
96+ } else if (angle > 2 * M_PI / 3 || angle < 2 * -M_PI / 3 ) {
97+ inp.x = -1 ;
98+ }
99+ }
100+
101+ if (std::abs (fromCenter.y ) > fromCenter.normalize ().y * 15 ) {
102+ if (angle > M_PI / 6 && angle < 5 * M_PI / 6 ) {
103+ inp.y = 1 ;
104+ } else if (angle < -M_PI / 6 && angle > -5 * M_PI / 6 ) {
105+ inp.y = -1 ;
106+ }
107+ }
108+
109+ if (inp != m_currentInput) {
110+ if (auto pl = PlayLayer::get ()) {
111+ handleInput (pl, inp, m_currentInput);
112+ }
113+ }
114+ m_currentInput = inp;
115+
116+ m_center->setPosition (pos);
117+ }
118+
119+ void JoystickNode::ccTouchCancelled (CCTouch *touch, CCEvent *event) {
120+ ccTouchEnded (touch, event);
121+ }
122+
123+ void JoystickNode::registerWithTouchDispatcher () {
124+ CCTouchDispatcher::get ()->addTargetedDelegate (this , -512 , true );
125+ }
0 commit comments