AniTouchRecognizerMapControl.cpp 9.2 KB
//
//  AniTouchRecognizerMapControl.cppb
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 17.05.2017.
//
//

#include "AniTouchRecognizerMapControl.h"
#include "AniMapLayer.h"

void AniTouchRecognizerMapControl::install(AniMapLayer* p_AniMapLayer, IMapCharacter* p_controlledCharacter, std::vector<IMapControlInterface*> p_observers){
    IMapControl::install(p_AniMapLayer, p_controlledCharacter, p_observers);
    configureTouchAniGestureRecognizer(p_AniMapLayer);
    configureAniFastSwipeGestureRecognizer(p_controlledCharacter);
    configureAniTickleGestureRecognizer(p_controlledCharacter); //TODO TICKLE MAYBE IN A FURTHER DERIVED CLASS??
    configureAniTapGestureRecognizer(p_AniMapLayer);
    configureAniLongPressGestureRecognizer(p_controlledCharacter);
    setEnabled(m_enabled);
}

//void AniTouchRecognizerMapControl::uninstall(){
//    IMapControl::uninstall();
//    deleteAniGestureRecognizers();
//}

void AniTouchRecognizerMapControl::setEnabled(bool p_enabled){
    m_enabled = p_enabled;
    if(m_multipleTouchAniGestureRecognizer) {
        m_multipleTouchAniGestureRecognizer->setEnabled(p_enabled);
    }
    if(m_AniLongPressGestureRecognizer != nullptr){
        m_AniLongPressGestureRecognizer->setEnabled(p_enabled);
    }
    if(m_tickleAniGestureRecognizer != nullptr){
        m_tickleAniGestureRecognizer->setEnabled(p_enabled);
    }
    if(m_AniFastSwipeGestureRecognizer != nullptr){
        m_AniFastSwipeGestureRecognizer->setEnabled(p_enabled);
    }
    if(m_AniTapGestureRecognizer != nullptr){
        m_AniTapGestureRecognizer->setEnabled(p_enabled);
    }
}

void AniTouchRecognizerMapControl::configureTouchAniGestureRecognizer(AniMapLayer* p_AniMapLayer)
{
    m_multipleTouchAniGestureRecognizer = new AniMultipleTouchGestureRecognizer();
    m_multipleTouchAniGestureRecognizer->install(p_AniMapLayer);
    m_multipleTouchAniGestureRecognizer->changeTouchListenerPriorityToSceneGraphPriority();
    
    m_multipleTouchAniGestureRecognizer->setOnSingleTouchDetectedCallback([&](AniGestureRecognizer::TouchData touchData){
        IMapControlSingleXYEventData eventData {touchData.touchId, touchData.currentTouchLocation, touchData.previousTouchLocation, touchData.initialTouchLocation};
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandSingleXYPress(eventData);
        }
//        cocos2d::log("touch point on screen: %f\n", touchData.currentTouchLocation.x);
    });
  
    m_multipleTouchAniGestureRecognizer->setOnSingleTouchMovedCallback([&](AniGestureRecognizer::TouchData touchData){
        IMapControlSingleXYEventData eventData {touchData.touchId, touchData.currentTouchLocation, touchData.previousTouchLocation, touchData.initialTouchLocation};
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandSingleXYMove(eventData);
        }
    });

    m_multipleTouchAniGestureRecognizer->setOnSingleTouchEndedCallback([&](AniGestureRecognizer::TouchData touchData){
        IMapControlSingleXYEventData eventData {touchData.touchId, touchData.currentTouchLocation, touchData.previousTouchLocation, touchData.initialTouchLocation};
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandSingleXYRelease(eventData);
        }
    });

    m_multipleTouchAniGestureRecognizer->setOnMultipleTouchDetectedCallback([&](std::vector<AniGestureRecognizer::TouchData> touches){
        std::vector<IMapControlSingleXYEventData> touchesData;
        for(auto touchIt = touches.begin(); touchIt != touches.end(); ++touchIt){
            touchesData.push_back({touchIt->touchId, touchIt->currentTouchLocation, touchIt->previousTouchLocation, touchIt->initialTouchLocation});
        }
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandMultipleXYPress(touchesData);
        }
    });

    m_multipleTouchAniGestureRecognizer->setOnMultipleTouchMovedCallback([&](std::vector<AniGestureRecognizer::TouchData> touches){
        std::vector<IMapControlSingleXYEventData> touchesData;
        for(auto touchIt = touches.begin(); touchIt != touches.end(); ++touchIt){
            touchesData.push_back({touchIt->touchId, touchIt->currentTouchLocation, touchIt->previousTouchLocation, touchIt->initialTouchLocation});
        }
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandMultipleXYMove(touchesData);
        }
    });

    m_multipleTouchAniGestureRecognizer->setOnMultipleTouchEndedCallback([&](std::vector<AniGestureRecognizer::TouchData> touches){
        std::vector<IMapControlSingleXYEventData> touchesData;
        for(auto touchIt = touches.begin(); touchIt != touches.end(); ++touchIt){
            touchesData.push_back({touchIt->touchId, touchIt->currentTouchLocation, touchIt->previousTouchLocation, touchIt->initialTouchLocation});
        }
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandMultipleXYRelease(touchesData);
        }
    });
}

using AniGestureRecognizerDirection = AniFastSwipeGestureRecognizer::AniGestureRecognizerDirection;
void AniTouchRecognizerMapControl::configureAniFastSwipeGestureRecognizer(cocos2d::Node* p_controlledCharacter)
{
    m_AniFastSwipeGestureRecognizer = new AniFastSwipeGestureRecognizer();
    m_AniFastSwipeGestureRecognizer->install(p_controlledCharacter);
    m_AniFastSwipeGestureRecognizer->changeTouchListenerPriorityToSceneGraphPriority();
    m_AniFastSwipeGestureRecognizer->setOnFastSwipeDetectedCallback([&](AniGestureRecognizerDirection directionX, AniGestureRecognizerDirection directionY, cocos2d::Point startLocation, cocos2d::Point endLocation, float velocityX, float velocityY){
        int intDirX = 0;
        int intDirY = 0;
        if(directionY == AniGestureRecognizerDirection::UP) intDirY = 1;
        else if (directionY == AniGestureRecognizerDirection::DOWN) intDirY = -1;
        if(directionX == AniGestureRecognizerDirection::RIGHT) intDirX = 1;
        else if (directionX == AniGestureRecognizerDirection::LEFT) intDirX = -1;
        IMapControlSwipeEventData eventData {1, startLocation, endLocation, {velocityX, velocityY}, intDirX, intDirY};
      for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
          (*it)->handleCommandFastSwipe(eventData);
      }
    });
}

void AniTouchRecognizerMapControl::configureAniTickleGestureRecognizer(IMapCharacter* p_controlledCharacter)
{
    m_tickleAniGestureRecognizer = new Tickle2GestureRecognizer();
    m_tickleAniGestureRecognizer->install(p_controlledCharacter);
    m_tickleAniGestureRecognizer->changeTouchListenerPriorityToSceneGraphPriority();
    m_tickleAniGestureRecognizer->setOnTicklingStartedCallback([&](){
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandTicklingStarted();
        }
    });
    m_tickleAniGestureRecognizer->setOnTicklingCallback([&](){
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandTicklingContinued();
        }
    });
    m_tickleAniGestureRecognizer->setOnTicklingEndedCallback([&](){
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandTicklingEnded();
        }
    });
}

void AniTouchRecognizerMapControl::configureAniTapGestureRecognizer(AniMapLayer* p_AniMapLayer)
{
    m_AniTapGestureRecognizer = new AniTapGestureRecognizer();
    m_AniTapGestureRecognizer->install(p_AniMapLayer);
    m_AniTapGestureRecognizer->changeTouchListenerPriorityToSceneGraphPriority();
    m_AniTapGestureRecognizer->setOnTapDetectedCallback([&](cocos2d::Point tapLocation){
        IMapControlSingleXYEventData eventData {1, tapLocation, {-1,-1}, tapLocation};
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandSingleXYTap(eventData);
        }
    });
}

void AniTouchRecognizerMapControl::configureAniLongPressGestureRecognizer(IMapCharacter* p_controlledCharacter)
{
    m_AniLongPressGestureRecognizer = new AniLongPressGestureRecognizer(1.2);
    m_AniLongPressGestureRecognizer->install(p_controlledCharacter);
    m_AniLongPressGestureRecognizer->changeTouchListenerPriorityToSceneGraphPriority();
    m_AniLongPressGestureRecognizer->setOnLongPressDetectedCallback([&](cocos2d::Point touchLocation){
        IMapControlSingleXYEventData eventData {1, touchLocation, {-1,-1}, touchLocation};
        for(auto it = m_observers.begin(); it != m_observers.end(); ++it){
            (*it)->handleCommandSingleXYLongPress(eventData);
        }
    });
}

void AniTouchRecognizerMapControl::deleteAniGestureRecognizers(){
    if(m_multipleTouchAniGestureRecognizer){
        delete m_multipleTouchAniGestureRecognizer;
    }
    
    if(m_AniFastSwipeGestureRecognizer){
        delete m_AniFastSwipeGestureRecognizer;
    }
    
    if(m_AniTapGestureRecognizer){
        delete m_AniTapGestureRecognizer;
    }
    
    if(m_tickleAniGestureRecognizer){
        delete m_tickleAniGestureRecognizer;
    }
    
    if(m_AniLongPressGestureRecognizer){
        delete m_AniLongPressGestureRecognizer;
    }
}

AniTouchRecognizerMapControl::~AniTouchRecognizerMapControl(){
    deleteAniGestureRecognizers();
}