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