TwoStateButton.cpp
4.37 KB
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
//
// TwoStateButton.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 25.06.2017.
//
//
#include <stdio.h>
#include "TwoStateButton.h"
#include "PlainSprite.h"
#include "MiscConfig.h"
TwoStateButton* TwoStateButton::create(const std::string& inactiveImagePath, const std::string& activeImagePath)
{
TwoStateButton * button = new (std::nothrow) TwoStateButton();
if(button && button->init(inactiveImagePath, activeImagePath))
{
button->autorelease();
return button;
}
CC_SAFE_DELETE(button);
return nullptr;
}
bool TwoStateButton::init(const std::string& inactiveImagePath, const std::string& activeImagePath)
{
if(!SimpleButton::init()){
return false;
}
_inactiveBgImagePath = inactiveImagePath;
_activeBgImagePath = activeImagePath;
_activeSprite = nullptr;
_inactiveSprite = nullptr;
_active = false;
_onStateChangedCallback = [](bool){};
this->loadTextureNormal(inactiveImagePath);
return true;
}
//void TwoStateButton::onEnter()
//{
// SimpleButton::onEnter();
// this->adjustTextures();
//}
void TwoStateButton::setActive(bool active)
{
_active = active;
this->adjustTextures();
};
void TwoStateButton::addChild(cocos2d::Node* child){
SimpleButton::addChild(child);
auto childAsLayoutNode = dynamic_cast<LayoutObject*>(child);
if(childAsLayoutNode){
if(childAsLayoutNode->objectName == "inactiveImage"){
_inactiveSprite = dynamic_cast<PlainSprite*>(child);
} else if(childAsLayoutNode->objectName == "activeImage"){
_activeSprite = dynamic_cast<PlainSprite*>(child);
}
}
}
void TwoStateButton::configureTouchListeners()
{
Widget::ccWidgetTouchCallback touchListener = [&](Ref* button,Widget::TouchEventType touchEventType){
if(touchEventType == Widget::TouchEventType::BEGAN){
this->setScale(MiscConfig::HighlightedButtonScale*_originalScale);
this->_buttonOwnTouchBehaviour.onTouchBegan();
this->_touchBeganCallback(this->objectName, cocos2d::ui::Widget::TouchEventType::BEGAN);
}
else if(touchEventType == Widget::TouchEventType::ENDED){
this->setScale(_originalScale);
bool newActive = this->switchStates();
this->adjustTextures();
this->_onStateChangedCallback(newActive);
this->_buttonOwnTouchBehaviour.onTouchEnded();
this->_touchEndedCallback(this->objectName, cocos2d::ui::Widget::TouchEventType::ENDED);
}
else if(touchEventType == Widget::TouchEventType::CANCELED){
this->setScale(_originalScale);
this->adjustTextures();
this->_buttonOwnTouchBehaviour.onTouchCancelled();
this->_touchCancelledCallback(this->objectName, cocos2d::ui::Widget::TouchEventType::CANCELED);
}
};;
this->addTouchEventListener(touchListener);
}
void TwoStateButton::adjustTextures()
{
if(_active){
this->loadTextureNormal(_activeBgImagePath);
this->loadTexturePressed(_activeBgImagePath);
if(this->_activeSprite){
this->_activeSprite->setVisible(true);
}
if(this->_inactiveSprite){
this->_inactiveSprite->setVisible(false);
}
} else {
this->loadTextureNormal(_inactiveBgImagePath);
this->loadTexturePressed(_inactiveBgImagePath);
if(this->_activeSprite){
this->_activeSprite->setVisible(false);
}
if(this->_inactiveSprite){
this->_inactiveSprite->setVisible(true);
}
}
}
//void TwoStateButton::configureSpritesIfNeeded(){
// if(!_activeSprite && !_inactiveSprite){
// auto children = this->getChildren();
// for(const auto& child : children){
// auto childAsLayoutNode = dynamic_cast<LayoutObject*>(child);
// if(childAsLayoutNode){
// if(childAsLayoutNode->objectName == "inactiveImage"){
// _inactiveSprite = dynamic_cast<PlainSprite*>(child);
// } else if(childAsLayoutNode->objectName == "activeImage"){
// _activeSprite = dynamic_cast<PlainSprite*>(child);
// }
// }
// }
// }
//}
//void TwoStateButton::adjustScale()
//{
// if(_active){
//
// } else {
//
// }
//}