AniSimpleButton.cpp
7.07 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// AniSimpleButton.cpp
// SteveMaggieCpp
//
// Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//
#include <stdio.h>
#include "AniSimpleButton.h"
#include "AniJSONParseUtils.h"
#include "AniValueStorage.h"
#include "AniGeometryUtils.h"
#include "AniMiscConfig.h"
#include "AniScalingUtils.h"
#include "AniStaticActionParser.h"
AniSimpleButton* AniSimpleButton::create()
{
AniSimpleButton * button = new (std::nothrow) AniSimpleButton();
if(button && button->init())
{
button->autorelease();
return button;
}
CC_SAFE_DELETE(button);
return nullptr;
}
bool AniSimpleButton::init()
{
if(!cocos2d::ui::Button::init()){
return false;
}
_originalScale = 1;
_adjustScaleOnPress = true;
_touchBeganCallback = [](std::string objectName, cocos2d::ui::Widget::TouchEventType){};
_touchEndedCallback = [](std::string objectName,cocos2d::ui::Widget::TouchEventType){};
_touchCancelledCallback = [](std::string objectName,cocos2d::ui::Widget::TouchEventType){};
_buttonOwnTouchBehaviour.onTouchBegan = [](){};
_buttonOwnTouchBehaviour.onTouchEnded = [](){};
_buttonOwnTouchBehaviour.onTouchCancelled = [](){};
_cascadeOpacityEnabled = true;
this->configureTouchListeners();
setScale(_originalScale);
return true;
}
void AniSimpleButton::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, AniLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
{
this->loadCommonPropertiesFromJSON(jsonValue);
if(jsonValue.HasMember("imagePath")){
std::string imagePath = jsonValue["imagePath"].GetString();
if(AniJSONParseUtils::checkMemberBool(jsonValue, "useAlternativePath", true))
{
imagePath = altResFolder + imagePath;
} else {
imagePath = resFolder + imagePath;
}
this->loadTextures(imagePath, imagePath, imagePath);
}
if(AniJSONParseUtils::hasMemberFloat(jsonValue, "scale")){
_originalScale = jsonValue["scale"].GetFloat();
}
if(AniScalingUtils::isSmallDevice()){
if(AniScalingUtils::isElementTooSmallForSmallDevice(getContentSize().width) || (AniJSONParseUtils::hasMemberBool(jsonValue, "scaleUpForSmallDevices") && jsonValue["scaleUpForSmallDevices"].GetBool() == true)){
_originalScale *= AniScalingUtils::getScaleForSmallDevice();
setScale(_originalScale);
}
}
if(AniJSONParseUtils::hasMemberPoint(jsonValue, "anchorPoint")){
this->setAnchorPoint(AniJSONParseUtils::getMemberPoint(jsonValue, "anchorPoint"));
}
if(AniJSONParseUtils::hasMemberBool(jsonValue, "adjustScaleOnPress")){
_adjustScaleOnPress = jsonValue["adjustScaleOnPress"].GetBool();
}
if(AniJSONParseUtils::hasMemberObject(jsonValue, "buttonAniActionData")){
const auto& buttonAniActionData = jsonValue["buttonAniActionData"];
static int containerNumber = 0;
std::string Container = "AniSimpleButtonContainer" + std::to_string((containerNumber++)%100);
if(AniJSONParseUtils::hasMemberObject(buttonAniActionData, "onTouchBegan")){
const auto& onTouchBeganJson = buttonAniActionData["onTouchBegan"];
auto key = AniValueStorage::getInstance().storeValue(onTouchBeganJson, Container);
_buttonOwnTouchBehaviour.onTouchBegan = std::bind([&](std::string storedValueKey, std::string container) {
auto storedOnTouchBeganJson = AniValueStorage::getInstance().getStoredValue(storedValueKey, container);
AniStaticActionParser::parseStaticAction(*storedOnTouchBeganJson);
}, key, Container);
}
if(AniJSONParseUtils::hasMemberObject(buttonAniActionData, "onTouchEnded")){
const auto& onTouchEndedJson = buttonAniActionData["onTouchEnded"];
auto key = AniValueStorage::getInstance().storeValue(onTouchEndedJson, Container);
_buttonOwnTouchBehaviour.onTouchEnded = std::bind([&](std::string storedValueKey, std::string container) {
AniStaticActionParser::parseStaticAction(*AniValueStorage::getInstance().getStoredValue(storedValueKey, container));
}, key, Container);
}
//TODO onTouchCancelled, if required
}
}
void AniSimpleButton::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height)
{
auto size = this->getNormalTextureSize();
width = size.width*_originalScale;
height = size.height*_originalScale;
}
bool AniSimpleButton::isWidget()
{
return true;
}
void AniSimpleButton::setOnTouchBeganCallback(std::function<void(std::string, cocos2d::ui::Widget::TouchEventType)> callback)
{
_touchBeganCallback = callback;
}
void AniSimpleButton::setOnTouchEndedCallback(std::function<void(std::string, cocos2d::ui::Widget::TouchEventType)> callback)
{
_touchEndedCallback = callback;
}
void AniSimpleButton::setOnTouchCancelledCallback(std::function<void(std::string, cocos2d::ui::Widget::TouchEventType)> callback)
{
_touchCancelledCallback = callback;
}
void AniSimpleButton::setOriginalScale(float scale)
{
_originalScale = scale;
}
float AniSimpleButton::getOriginalScale()
{
return _originalScale;
}
void AniSimpleButton::imitateTouchDown()
{
if (_adjustScaleOnPress)
{
this->setScale(AniMiscConfig::HighlightedButtonScale*_originalScale);
}
}
void AniSimpleButton::imitateTouchUp()
{
if (_adjustScaleOnPress)
{
this->setScale(_originalScale);
}
}
void AniSimpleButton::configureTouchListeners()
{
Widget::ccWidgetTouchCallback touchListener = [&](Ref* button,Widget::TouchEventType touchEventType){
auto simpleButton = dynamic_cast<AniSimpleButton*>(button);
if(touchEventType == Widget::TouchEventType::BEGAN){
if (_adjustScaleOnPress) {
simpleButton->setScale(AniMiscConfig::HighlightedButtonScale*_originalScale);
}
simpleButton->_buttonOwnTouchBehaviour.onTouchBegan();
simpleButton->_touchBeganCallback(simpleButton->objectName, cocos2d::ui::Widget::TouchEventType::BEGAN);
}
else if(touchEventType == Widget::TouchEventType::ENDED){
if (_adjustScaleOnPress) {
simpleButton->setScale(_originalScale);
}
simpleButton->_buttonOwnTouchBehaviour.onTouchEnded();
simpleButton->_touchEndedCallback(simpleButton->objectName, cocos2d::ui::Widget::TouchEventType::ENDED);
}
else if(touchEventType == Widget::TouchEventType::CANCELED){
if (_adjustScaleOnPress) {
simpleButton->setScale(_originalScale);
}
simpleButton->_buttonOwnTouchBehaviour.onTouchCancelled();
simpleButton->_touchCancelledCallback(simpleButton->objectName, cocos2d::ui::Widget::TouchEventType::CANCELED);
}
};
this->addTouchEventListener(touchListener);
}