HalloweenSimpleButton.cpp 6.84 KB
//
//  HalloweenSimpleButton.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include <stdio.h>
#include "HalloweenSimpleButton.h"
#include "HJSONParseUtils.h"
#include "HValueStorage.h"
#include "HGeometryUtils.h"
#include "HMiscConfig.h"
#include "HScalingUtils.h"

HalloweenSimpleButton* HalloweenSimpleButton::create()
{
    HalloweenSimpleButton * button = new (std::nothrow) HalloweenSimpleButton();
    if(button && button->init())
    {
        button->autorelease();
        return button;
    }
    CC_SAFE_DELETE(button);
    return nullptr;
}

bool HalloweenSimpleButton::init()
{
    if(!cocos2d::ui::Button::init()){
        return false;
    }
    
    _originalScale = 1;
    if(HScalingUtils::isSmallDevice()){
        _originalScale = HScalingUtils::getScaleForSmallDevice();
    }
    _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 = [](){};
    
    this->configureTouchListeners();
    
    setScale(_originalScale);
    
    return true;
}

void HalloweenSimpleButton::loadPropertiesFromJSON(const rapidjson::Value& jsonValue, HLayoutViewInterface* scene, const std::string resFolder, const std::string altResFolder)
{
    this->loadCommonPropertiesFromJSON(jsonValue);
    
    if(jsonValue.HasMember("imagePath")){
        std::string imagePath = jsonValue["imagePath"].GetString();
        if(HJSONParseUtils::checkMemberBool(jsonValue, "useAlternativePath", true))
        {
            imagePath = altResFolder + imagePath;
        } else {
            imagePath = resFolder + imagePath;
        }
        
        this->loadTextures(imagePath, imagePath, imagePath);
    }
    
    if(HJSONParseUtils::hasMemberFloat(jsonValue, "scale")){
        _originalScale = jsonValue["scale"].GetFloat();
        if(HScalingUtils::isSmallDevice()){
            _originalScale *= 1.4;
        }
    }
    
    if(HJSONParseUtils::hasMemberPoint(jsonValue, "anchorPoint")){
        this->setAnchorPoint(HJSONParseUtils::getMemberPoint(jsonValue, "anchorPoint"));
    }
    
    if(HJSONParseUtils::hasMemberBool(jsonValue, "adjustScaleOnPress")){
        _adjustScaleOnPress = jsonValue["adjustScaleOnPress"].GetBool();
    }
    
    if(HJSONParseUtils::hasMemberObject(jsonValue, "buttonActionData")){
        
        const auto& buttonActionData = jsonValue["buttonActionData"];
        
        static int containerNumber = 0;
        std::string Container = "HalloweenSimpleButtonContainer" + std::to_string((containerNumber++)%100);
        
        if(HJSONParseUtils::hasMemberObject(buttonActionData, "onTouchBegan")){
            
            const auto& onTouchBeganJson = buttonActionData["onTouchBegan"];
            
            auto key = HValueStorage::getInstance().storeValue(onTouchBeganJson, Container);
            
//            _buttonOwnTouchBehaviour.onTouchBegan = std::bind([&](std::string storedValueKey, std::string container) {
//
//                auto storedOnTouchBeganJson = HValueStorage::getInstance().getStoredValue(storedValueKey, container);
//                StaticActionParser::parseStaticAction(*storedOnTouchBeganJson);
//            }, key, Container);
            
        }
        
        if(HJSONParseUtils::hasMemberObject(buttonActionData, "onTouchEnded")){
            
            const auto& onTouchEndedJson = buttonActionData["onTouchEnded"];
            auto key = HValueStorage::getInstance().storeValue(onTouchEndedJson, Container);
            
//            _buttonOwnTouchBehaviour.onTouchEnded = std::bind([&](std::string storedValueKey, std::string container) {
//                StaticActionParser::parseStaticAction(*HValueStorage::getInstance().getStoredValue(storedValueKey, container));
//            }, key, Container);
            
        }
        
        //TODO onTouchCancelled, if required
    }
}

void HalloweenSimpleButton::prepareSize(const rapidjson::Value& jsonValue, float& width, float& height)
{
    auto size = this->getNormalTextureSize();
    width = size.width*_originalScale;
    height = size.height*_originalScale;
}

bool HalloweenSimpleButton::isWidget()
{
    return true;
}

void HalloweenSimpleButton::setOnTouchBeganCallback(std::function<void(std::string, cocos2d::ui::Widget::TouchEventType)> callback)
{
    _touchBeganCallback = callback;
}

void HalloweenSimpleButton::setOnTouchEndedCallback(std::function<void(std::string, cocos2d::ui::Widget::TouchEventType)> callback)
{
    _touchEndedCallback = callback;
}

void HalloweenSimpleButton::setOnTouchCancelledCallback(std::function<void(std::string, cocos2d::ui::Widget::TouchEventType)> callback)
{
    _touchCancelledCallback = callback;
}

void HalloweenSimpleButton::setOriginalScale(float scale)
{
    _originalScale = scale;
}

float HalloweenSimpleButton::getOriginalScale()
{
    return _originalScale;
}

void HalloweenSimpleButton::imitateTouchDown()
{
    if (_adjustScaleOnPress)
    {
        this->setScale(MiscConfig::HighlightedButtonScale*_originalScale);
    }
}

void HalloweenSimpleButton::imitateTouchUp()
{
    if (_adjustScaleOnPress)
    {
        this->setScale(_originalScale);
    }
}

void HalloweenSimpleButton::configureTouchListeners()
{
    Widget::ccWidgetTouchCallback touchListener = [&](Ref* button, Widget::TouchEventType touchEventType) {

        auto simpleButton = dynamic_cast<HalloweenSimpleButton*>(button);

        if (touchEventType == Widget::TouchEventType::BEGAN) {

            if (_adjustScaleOnPress) {
                simpleButton->setScale(MiscConfig::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);
}