// // TimeSliderNode.cpp // WattsenglishToyApp-mobile // // Created by Katarzyna Kalinowska-Górska on 27/12/2019. // #include "AniProgressSliderNode.h" #include "AniScalingUtils.h" AniProgressSliderNode* AniProgressSliderNode::create(const std::string& sliderImageFilePath, const std::string& thumbImageFilePath, float minVal, float maxVal, AniProgressSliderNodeOrientation p_orientation) { AniProgressSliderNode * node = new (std::nothrow) AniProgressSliderNode(); if(node && node->init(sliderImageFilePath, thumbImageFilePath, minVal, maxVal, p_orientation)) { node->autorelease(); return node; } CC_SAFE_DELETE(node); return nullptr; } bool AniProgressSliderNode::init(const std::string& sliderImageFilePath, const std::string& thumbImageFilePath, float minVal, float maxVal, AniProgressSliderNodeOrientation 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 AniProgressSliderNode::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, AniLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder) { this->loadCommonPropertiesFromJSON(jsonValue); if(AniJSONParseUtils::hasMemberColor3B(jsonValue, "colour")){ setColor(AniJSONParseUtils::getColor3B(jsonValue["colour"])); } if(AniJSONParseUtils::hasMemberFloat(jsonValue, "indicatorInset")){ _indicatorInset = jsonValue["indicatorInset"].GetFloat(); } } void AniProgressSliderNode::setContentSize(const cocos2d::Size& contentSize){ cocos2d::Node::setContentSize(contentSize); adjustThumbSpritePosition(); } void AniProgressSliderNode::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 AniProgressSliderNode::adjustThumbSpritePosition(){ if(m_orientation == AniProgressSliderNodeOrientationVertical){ _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 AniProgressSliderNode::setProperty(std::string propertyName, const rapidjson::Value& newValue, ActionParseDelegate* parseDelegate) { if(propertyName == "opacity"){ auto value = newValue.GetInt(); this->setOpacity(value); } } void AniProgressSliderNode::callFunctionByName(std::string methodName, const rapidjson::Value* arguments, ActionParseDelegate* parseDelegate, std::function callback) { AniLayoutObject::callFunctionByName(methodName, arguments, parseDelegate); } void AniProgressSliderNode::setProgressFrac(float progressFrac) { float progress = progressFrac*_maxValue; setProgress(progress); } void AniProgressSliderNode::setProgress(float progress) { float newValue = MIN((MAX(progress, _minValue)),_maxValue); _currentValue = newValue; updateProgressUI(newValue); } void AniProgressSliderNode::updateProgressUI(float value, bool animated){ // 0 - _minValue // contentSize().width - thumbWidht/2 - _maxValue float newThumbPosition; if(m_orientation == AniProgressSliderNodeOrientationVertical){ auto inset = _indicatorInset * AniScalingUtils::scaleAspectFitToDesignIpadProSize(); newThumbPosition = (getContentSize().height - _thumbSprite->getContentSize().height)*value/(_maxValue-_minValue) + inset; _thumbSprite->setPositionY(newThumbPosition); } else { newThumbPosition = (getContentSize().width - _thumbSprite->getContentSize().width/2)*value/(_maxValue-_minValue) + _thumbSprite->getContentSize().width/2; _thumbSprite->setPositionX(newThumbPosition); } } void AniProgressSliderNode::startTimeAnimation(float timeDuration){ cocos2d::Point finalPoint; if(m_orientation == AniProgressSliderNodeOrientationVertical){ 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 AniProgressSliderNode::pauseTimeAnimation(){ _thumbSprite->pause(); } void AniProgressSliderNode::resumeTimeAnimation(){ _thumbSprite->resume(); } void AniProgressSliderNode::stopTimeAnimation(){ _thumbSprite->stopAllActions(); }