AniTapGestureRecognizer.cpp 1.49 KB
//
//  AniTapGestureRecognizer.cpp
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 02.07.2017.
//
//

#include <stdio.h>
#include "AniTapGestureRecognizer.h"

AniTapGestureRecognizer::AniTapGestureRecognizer() : AniGestureRecognizer({cocos2d::EventListener::Type::TOUCH_ONE_BY_ONE})
{
    _startTime = -1;
    _onTapDetectedCallback = [](cocos2d::Point){
//        cocos2d::log("on tap detected");
    };
}

void AniTapGestureRecognizer::setOnTapDetectedCallback(std::function<void(cocos2d::Point touchLocation)> callback)
{
    _onTapDetectedCallback = callback;
}

///////////////////////////////////////////////////////////////////////////
// internal
///////////////////////////////////////////////////////////////////////////
    
// touch handlers

bool AniTapGestureRecognizer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
{
    _startTime = cocos2d::utils::getTimeInMilliseconds();
    _startLocation = touch->getLocation();
    return true;
}

void AniTapGestureRecognizer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event)
{
    auto newLocation = touch->getLocation();
    if(newLocation.distance(_startLocation) > MAX_DELTA_LOCATION){
         _startTime = -1;
    }
}

void AniTapGestureRecognizer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event)
{
    double currentTime = cocos2d::utils::getTimeInMilliseconds();
    if(_startTime != -1 && (currentTime - _startTime)/1000 < MAX_DELTA_TIME){
        _onTapDetectedCallback(touch->getLocation());
    }
}