// // ToySimpleButton.cpp // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 17.05.2017. // // #include #include "ToySimpleButton.h" #include "ToyJSONParseUtils.h" #include "ToyValueStorage.h" #include "ToyGeometryUtils.h" #include "ToyMiscConfig.h" #include "ToyScalingUtils.h" #include "ToyStaticActionParser.h" ToySimpleButton* ToySimpleButton::create() { ToySimpleButton * button = new (std::nothrow) ToySimpleButton(); if(button && button->init()) { button->autorelease(); return button; } CC_SAFE_DELETE(button); return nullptr; } bool ToySimpleButton::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 ToySimpleButton::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, ToyLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder) { this->loadCommonPropertiesFromJSON(jsonValue); if(jsonValue.HasMember("imagePath")){ std::string imagePath = jsonValue["imagePath"].GetString(); if(ToyJSONParseUtils::checkMemberBool(jsonValue, "useAlternativePath", true)) { imagePath = altResFolder + imagePath; } else { imagePath = resFolder + imagePath; } this->loadTextures(imagePath, imagePath, imagePath); } if(ToyJSONParseUtils::hasMemberFloat(jsonValue, "scale")){ _originalScale = jsonValue["scale"].GetFloat(); } if(ToyScalingUtils::isSmallDevice()){ if(ToyScalingUtils::isElementTooSmallForSmallDevice(getContentSize().width) || (ToyJSONParseUtils::hasMemberBool(jsonValue, "scaleUpForSmallDevices") && jsonValue["scaleUpForSmallDevices"].GetBool() == true)){ _originalScale *= ToyScalingUtils::getScaleForSmallDevice(); setScale(_originalScale); } } if(ToyJSONParseUtils::hasMemberPoint(jsonValue, "anchorPoint")){ this->setAnchorPoint(ToyJSONParseUtils::getMemberPoint(jsonValue, "anchorPoint")); } if(ToyJSONParseUtils::hasMemberBool(jsonValue, "adjustScaleOnPress")){ _adjustScaleOnPress = jsonValue["adjustScaleOnPress"].GetBool(); } if(ToyJSONParseUtils::hasMemberObject(jsonValue, "buttonToyActionData")){ const auto& buttonToyActionData = jsonValue["buttonToyActionData"]; static int containerNumber = 0; std::string Container = "SimpleButtonContainer" + std::to_string((containerNumber++)%100); if(ToyJSONParseUtils::hasMemberObject(buttonToyActionData, "onTouchBegan")){ const auto& onTouchBeganJson = buttonToyActionData["onTouchBegan"]; auto key = ToyValueStorage::getInstance().storeValue(onTouchBeganJson, Container); _buttonOwnTouchBehaviour.onTouchBegan = std::bind([&](std::string storedValueKey, std::string container) { auto storedOnTouchBeganJson = ToyValueStorage::getInstance().getStoredValue(storedValueKey, container); ToyStaticActionParser::parseStaticAction(*storedOnTouchBeganJson); }, key, Container); } if(ToyJSONParseUtils::hasMemberObject(buttonToyActionData, "onTouchEnded")){ const auto& onTouchEndedJson = buttonToyActionData["onTouchEnded"]; auto key = ToyValueStorage::getInstance().storeValue(onTouchEndedJson, Container); _buttonOwnTouchBehaviour.onTouchEnded = std::bind([&](std::string storedValueKey, std::string container) { ToyStaticActionParser::parseStaticAction(*ToyValueStorage::getInstance().getStoredValue(storedValueKey, container)); }, key, Container); } //TODO onTouchCancelled, if required } } void ToySimpleButton::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height) { auto size = this->getNormalTextureSize(); width = size.width*_originalScale; height = size.height*_originalScale; } bool ToySimpleButton::isWidget() { return true; } void ToySimpleButton::setOnTouchBeganCallback(std::function callback) { _touchBeganCallback = callback; } void ToySimpleButton::setOnTouchEndedCallback(std::function callback) { _touchEndedCallback = callback; } void ToySimpleButton::setOnTouchCancelledCallback(std::function callback) { _touchCancelledCallback = callback; } void ToySimpleButton::setOriginalScale(float scale) { _originalScale = scale; } float ToySimpleButton::getOriginalScale() { return _originalScale; } void ToySimpleButton::imitateTouchDown() { if (_adjustScaleOnPress) { this->setScale(ToyMiscConfig::HighlightedButtonScale*_originalScale); } } void ToySimpleButton::imitateTouchUp() { if (_adjustScaleOnPress) { this->setScale(_originalScale); } } void ToySimpleButton::configureTouchListeners() { Widget::ccWidgetTouchCallback touchListener = [&](Ref* button,Widget::TouchEventType touchEventType){ auto simpleButton = dynamic_cast(button); if(touchEventType == Widget::TouchEventType::BEGAN){ if (_adjustScaleOnPress) { simpleButton->setScale(ToyMiscConfig::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); }