KKGLabelButton.cpp 4.59 KB
//
//  KKGLabelButton.cpp
//  SteveAndMaggieWorld
//
//  Created by Katarzyna Kalinowska-Górska on 08/12/2021.
//

#include <stdio.h>
#include "HScalingUtils.h"
#include "KKGLabelButton.h"
#include "HMiscConfig.h"

KKGLabelButton* KKGLabelButton::create(const std::string& backgroundPath, const std::string& text,
                                       const std::string& fontFile, float fontSize,
                                       const cocos2d::Color4B& textColor){
    KKGLabelButton * button = new (std::nothrow) KKGLabelButton();
        if(button && button->init(backgroundPath, text, fontFile, fontSize, textColor))
        {
            button->autorelease();
//            button->loadTextures(backgroundPath, backgroundPath);
            return button;
        }
        CC_SAFE_DELETE(button);
        return nullptr;
}

bool KKGLabelButton::init(const std::string& backgroundPath, const std::string& text,
                          const std::string& fontFile, float fontSize,
                          const cocos2d::Color4B& textColor)
{
    if(!cocos2d::ui::Button::init()){
        return false;
    }
    
    _originalScale = 1;
    if(HScalingUtils::isSmallDevice()){
        _originalScale = HScalingUtils::getScaleForSmallDevice();
    }

    _adjustScaleOnPress = true;
    _touchBeganCallback = [](KKGLabelButton* object, cocos2d::ui::Widget::TouchEventType){};
    _touchEndedCallback = [](KKGLabelButton* object,cocos2d::ui::Widget::TouchEventType){};
    _touchCancelledCallback = [](KKGLabelButton* object,cocos2d::ui::Widget::TouchEventType){};
    _buttonOwnTouchBehaviour.onTouchBegan = [](){};
    _buttonOwnTouchBehaviour.onTouchEnded = [](){};
    _buttonOwnTouchBehaviour.onTouchCancelled = [](){};
    
    this->configureTouchListeners();
    
    loadTextures(backgroundPath, backgroundPath, backgroundPath);
    m_textLabel = cocos2d::Label::createWithTTF(text, fontFile, fontSize);
    addChild(m_textLabel);
    m_textLabel->setAnchorPoint(cocos2d::Vec2(0.5, 0.5));
    m_textLabel->setPosition(getContentSize()/2);
    m_textLabel->setTextColor(textColor);

    setScale(_originalScale);
    
    return true;
}

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

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

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

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

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

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

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

void KKGLabelButton::configureTouchListeners()
{
    Widget::ccWidgetTouchCallback touchListener = [&](Ref* button,Widget::TouchEventType touchEventType){
        
        auto HalloweenSimpleButton = dynamic_cast<KKGLabelButton*>(button);
        
        if(touchEventType == Widget::TouchEventType::BEGAN){

           if (_adjustScaleOnPress) {
               HalloweenSimpleButton->setScale(MiscConfig::HighlightedButtonScale*_originalScale);
           }
           HalloweenSimpleButton->_buttonOwnTouchBehaviour.onTouchBegan();
           HalloweenSimpleButton->_touchBeganCallback(HalloweenSimpleButton, cocos2d::ui::Widget::TouchEventType::BEGAN);
        
        }
        
        else if(touchEventType == Widget::TouchEventType::ENDED){
            if (_adjustScaleOnPress) {
                HalloweenSimpleButton->setScale(_originalScale);
            }
            HalloweenSimpleButton->_buttonOwnTouchBehaviour.onTouchEnded();
            HalloweenSimpleButton->_touchEndedCallback(HalloweenSimpleButton, cocos2d::ui::Widget::TouchEventType::ENDED);
        }
        
        else if(touchEventType == Widget::TouchEventType::CANCELED){
            if (_adjustScaleOnPress) {
                HalloweenSimpleButton->setScale(_originalScale);
            }
            HalloweenSimpleButton->_buttonOwnTouchBehaviour.onTouchCancelled();
            HalloweenSimpleButton->_touchCancelledCallback(HalloweenSimpleButton, cocos2d::ui::Widget::TouchEventType::CANCELED);
        }
        
    };

    this->addTouchEventListener(touchListener);
}