TwoStateButton.cpp 4.37 KB
//
//  TwoStateButton.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 25.06.2017.
//
//

#include <stdio.h>
#include "TwoStateButton.h"
#include "PlainSprite.h"
#include "MiscConfig.h"

TwoStateButton* TwoStateButton::create(const std::string& inactiveImagePath, const std::string& activeImagePath)
{
    TwoStateButton * button = new (std::nothrow) TwoStateButton();
    if(button && button->init(inactiveImagePath, activeImagePath))
    {
        button->autorelease();
        return button;
    }
    CC_SAFE_DELETE(button);
    return nullptr;
}

bool TwoStateButton::init(const std::string& inactiveImagePath, const std::string& activeImagePath)
{
    if(!SimpleButton::init()){
        return false;
    }

    _inactiveBgImagePath = inactiveImagePath;
    _activeBgImagePath = activeImagePath;
    _activeSprite = nullptr;
    _inactiveSprite = nullptr;
    
    _active = false;
    _onStateChangedCallback = [](bool){};
    this->loadTextureNormal(inactiveImagePath);
    
    return true;
}

//void TwoStateButton::onEnter()
//{
//    SimpleButton::onEnter();
//    this->adjustTextures();
//}

void TwoStateButton::setActive(bool active)
{
    _active = active;
    this->adjustTextures();
};

void TwoStateButton::addChild(cocos2d::Node* child){
    SimpleButton::addChild(child);
    auto childAsLayoutNode = dynamic_cast<LayoutObject*>(child);
    if(childAsLayoutNode){
       if(childAsLayoutNode->objectName == "inactiveImage"){
           _inactiveSprite = dynamic_cast<PlainSprite*>(child);
       } else if(childAsLayoutNode->objectName == "activeImage"){
           _activeSprite = dynamic_cast<PlainSprite*>(child);
       }
   }
}

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

           this->setScale(MiscConfig::HighlightedButtonScale*_originalScale);
           this->_buttonOwnTouchBehaviour.onTouchBegan();
           this->_touchBeganCallback(this->objectName, cocos2d::ui::Widget::TouchEventType::BEGAN);
        
        }
        
        else if(touchEventType == Widget::TouchEventType::ENDED){
            this->setScale(_originalScale);
            
            bool newActive = this->switchStates();
            this->adjustTextures();
            this->_onStateChangedCallback(newActive);
            
            this->_buttonOwnTouchBehaviour.onTouchEnded();
            this->_touchEndedCallback(this->objectName, cocos2d::ui::Widget::TouchEventType::ENDED);
        }
        
        else if(touchEventType == Widget::TouchEventType::CANCELED){
            this->setScale(_originalScale);
            this->adjustTextures();
            this->_buttonOwnTouchBehaviour.onTouchCancelled();
            this->_touchCancelledCallback(this->objectName, cocos2d::ui::Widget::TouchEventType::CANCELED);
        }
        
    };;

    this->addTouchEventListener(touchListener);
}

void TwoStateButton::adjustTextures()
{
    if(_active){
        this->loadTextureNormal(_activeBgImagePath);
        this->loadTexturePressed(_activeBgImagePath);
        if(this->_activeSprite){
            this->_activeSprite->setVisible(true);
        }
        if(this->_inactiveSprite){
            this->_inactiveSprite->setVisible(false);
        }
    } else {
        this->loadTextureNormal(_inactiveBgImagePath);
        this->loadTexturePressed(_inactiveBgImagePath);
        if(this->_activeSprite){
            this->_activeSprite->setVisible(false);
        }
        if(this->_inactiveSprite){
            this->_inactiveSprite->setVisible(true);
        }
    }
}

//void TwoStateButton::configureSpritesIfNeeded(){
//    if(!_activeSprite && !_inactiveSprite){
//        auto children = this->getChildren();
//        for(const auto& child : children){
//            auto childAsLayoutNode = dynamic_cast<LayoutObject*>(child);
//            if(childAsLayoutNode){
//                if(childAsLayoutNode->objectName == "inactiveImage"){
//                    _inactiveSprite = dynamic_cast<PlainSprite*>(child);
//                } else if(childAsLayoutNode->objectName == "activeImage"){
//                    _activeSprite = dynamic_cast<PlainSprite*>(child);
//                }
//            }
//        }
//    }
//}

//void TwoStateButton::adjustScale()
//{
//    if(_active){
//        
//    } else {
//        
//    }
//}