// // 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 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(); }