ProgressSliderNode.cpp
5.28 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
//
// TimeSliderNode.cpp
// WattsenglishToyApp-mobile
//
// Created by Katarzyna Kalinowska-Górska on 27/12/2019.
//
#include "ProgressSliderNode.h"
ProgressSliderNode* ProgressSliderNode::create(const std::string& sliderImageFilePath, const std::string& thumbImageFilePath, float minVal, float maxVal, ProgressSliderNodeOrientation p_orientation)
{
ProgressSliderNode * node = new (std::nothrow) ProgressSliderNode();
if(node && node->init(sliderImageFilePath, thumbImageFilePath, minVal, maxVal, p_orientation))
{
node->autorelease();
return node;
}
CC_SAFE_DELETE(node);
return nullptr;
}
bool ProgressSliderNode::init(const std::string& sliderImageFilePath, const std::string& thumbImageFilePath, float minVal, float maxVal, ProgressSliderNodeOrientation p_orientation){
if(!cocos2d::Node::init()){
return false;
}
m_orientation = p_orientation;
if(sliderImageFilePath != ""){
_sliderSprite = cocos2d::Sprite::create(sliderImageFilePath);
addChild(_sliderSprite);
_sliderSprite->setPosition(cocos2d::Vec2(_sliderSprite->getContentSize().width/2, _sliderSprite->getContentSize().height/2));
setContentSize(_sliderSprite->getBoundingBox().size);
}
_thumbSprite = cocos2d::Sprite::create(thumbImageFilePath);
addChild(_thumbSprite);
adjustThumbSpritePosition();
setCascadeOpacityEnabled(true);
_minValue = minVal;
_maxValue = maxVal;
_currentValue = minVal;
return true;
}
void ProgressSliderNode::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, LayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
{
this->loadCommonPropertiesFromJSON(jsonValue);
if(JSONParseUtils::hasMemberColor3B(jsonValue, "colour")){
setColor(JSONParseUtils::getColor3B(jsonValue["colour"]));
}
}
void ProgressSliderNode::setContentSize(const cocos2d::Size& contentSize){
cocos2d::Node::setContentSize(contentSize);
adjustThumbSpritePosition();
}
void ProgressSliderNode::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height)
{
if(_sliderSprite != nullptr){
width = _sliderSprite->getBoundingBox().size.width;
height = _sliderSprite->getBoundingBox().size.height;
} else {
width = this->getContentSize().width;
height = this->getContentSize().height;
}
}
void ProgressSliderNode::adjustThumbSpritePosition(){
if(m_orientation == ProgressSliderNodeOrientationVertical){
_thumbSprite->setPosition(cocos2d::Vec2(getContentSize().width/2, _thumbSprite->getContentSize().height/2));
} else {
_thumbSprite->setPosition(cocos2d::Vec2(_thumbSprite->getContentSize().width/2, getContentSize().height/2));
}
updateProgressUI(_currentValue);
}
void ProgressSliderNode::setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate)
{
if(propertyName == "opacity"){
auto value = newValue.GetInt();
this->setOpacity(value);
}
}
void ProgressSliderNode::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function<void()> callback)
{
LayoutObject::callFunctionByName(methodName, arguments, parseDelegate);
}
void ProgressSliderNode::setProgressFrac(float progressFrac) {
float progress = progressFrac*_maxValue;
setProgress(progress);
}
void ProgressSliderNode::setProgress(float progress) {
float newValue = MIN((MAX(progress, _minValue)),_maxValue);
_currentValue = newValue;
updateProgressUI(newValue);
}
void ProgressSliderNode::updateProgressUI(float value, bool animated){
// 0 - _minValue
// contentSize().width - thumbWidht/2 - _maxValue
float newThumbPosition;
if(m_orientation == ProgressSliderNodeOrientationVertical){
newThumbPosition = (getContentSize().height - _thumbSprite->getContentSize().height/2)*value/(_maxValue-_minValue) - _thumbSprite->getContentSize().height/2;
_thumbSprite->setPositionY(newThumbPosition);
} else {
newThumbPosition = (getContentSize().width - _thumbSprite->getContentSize().width/2)*value/(_maxValue-_minValue) + _thumbSprite->getContentSize().width/2;
_thumbSprite->setPositionX(newThumbPosition);
}
}
void ProgressSliderNode::startTimeAnimation(float timeDuration){
cocos2d::Point finalPoint;
if(m_orientation == ProgressSliderNodeOrientationVertical){
finalPoint = cocos2d::Point(_thumbSprite->getPositionX(), getContentSize().height + _thumbSprite->getContentSize().height/2);
} else {
finalPoint = cocos2d::Point(getContentSize().width - _thumbSprite->getContentSize().width/2, _thumbSprite->getPositionY());
}
_thumbSprite->runAction(cocos2d::MoveTo::create(timeDuration, finalPoint));
// _thumbSprite->runAction(cocos2d::RepeatForever::create(cocos2d::RotateBy::create(timeDuration/(getContentSize().width-_thumbSprite->getContentSize().width - _thumbSprite->getPositionX())*(_thumbSprite->getContentSize().width*3.14), 360)));
}
void ProgressSliderNode::pauseTimeAnimation(){
_thumbSprite->pause();
}
void ProgressSliderNode::resumeTimeAnimation(){
_thumbSprite->resume();
}
void ProgressSliderNode::stopTimeAnimation(){
_thumbSprite->stopAllActions();
}