2019-04-07 16:03:16 +02:00
|
|
|
using System;
|
2019-03-17 23:43:20 +01:00
|
|
|
using UnityEngine;
|
2019-04-07 16:03:16 +02:00
|
|
|
using UnityEngine.Serialization;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
|
|
|
namespace UnityAtoms.Mobile
|
|
|
|
{
|
|
|
|
[Serializable]
|
|
|
|
public class DetectTap : IGameEventListener<TouchUserInput>
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
[FormerlySerializedAs("FirstTapTimer")]
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private Timer _firstTapTimer;
|
|
|
|
|
|
|
|
[FormerlySerializedAs("SecondTapTimer")]
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private Timer _secondTapTimer;
|
|
|
|
|
|
|
|
[FormerlySerializedAs("MaxTimeBetweenTaps")]
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private FloatReference _maxTimeBetweenTaps;
|
|
|
|
|
|
|
|
[FormerlySerializedAs("MaxDistanceBetweenTaps")]
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private FloatReference _maxDistanceBetweenTaps;
|
|
|
|
|
|
|
|
[FormerlySerializedAs("MaxMovementToCountAsTap")]
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private FloatReference _maxMovementToCountAsTap;
|
|
|
|
|
|
|
|
[FormerlySerializedAs("OnTapDetectedEvent")]
|
2019-03-17 23:43:20 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private TouchUserInputGameEvent _onTapDetectedEvent;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
[FormerlySerializedAs("OnDoubleTapDetectedEvent")]
|
|
|
|
[SerializeField]
|
|
|
|
private TouchUserInputGameEvent _onDoubleTapDetectedEvent;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
private Vector2 _inputPosFirstTapDown;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
|
|
|
public void OnEventRaised(TouchUserInput touchUserInput)
|
|
|
|
{
|
|
|
|
if (!IsPotentialDoubleTapInProgress())
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_firstTapTimer.Stop();
|
|
|
|
_secondTapTimer.Stop();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (touchUserInput.InputState == TouchUserInput.State.Down && CanStartDoubleTap())
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputPosFirstTapDown = touchUserInput.InputPos;
|
|
|
|
_firstTapTimer.Start();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
2019-04-07 16:03:16 +02:00
|
|
|
else if (touchUserInput.InputState == TouchUserInput.State.Drag && _firstTapTimer.IsStarted() && Vector2.Distance(touchUserInput.InputPos, _inputPosFirstTapDown) > _maxMovementToCountAsTap.Value)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_firstTapTimer.Stop();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
else if (touchUserInput.InputState == TouchUserInput.State.Up && WaitingForFinishingFirstTap())
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_firstTapTimer.GetElapsedTime() <= _maxTimeBetweenTaps.Value)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_onTapDetectedEvent != null)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_onTapDetectedEvent.Raise(touchUserInput);
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
2019-04-07 16:03:16 +02:00
|
|
|
_secondTapTimer.Start();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
2019-04-07 16:03:16 +02:00
|
|
|
_firstTapTimer.Stop();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
else if (touchUserInput.InputState == TouchUserInput.State.Down && WaitingForSecondTap())
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (Vector2.Distance(touchUserInput.InputPos, _inputPosFirstTapDown) <= _maxDistanceBetweenTaps.Value && _secondTapTimer.GetElapsedTime() <= _maxTimeBetweenTaps.Value)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_onDoubleTapDetectedEvent != null)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_onDoubleTapDetectedEvent.Raise(touchUserInput); // OPEN POINT: Should we raise event on state up or down?
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 16:03:16 +02:00
|
|
|
_secondTapTimer.Stop();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool CanStartDoubleTap()
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
return !_secondTapTimer.IsStarted();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool WaitingForFinishingFirstTap()
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
return _firstTapTimer.IsStarted();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool WaitingForSecondTap()
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
return _secondTapTimer.IsStarted();
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsPotentialDoubleTapInProgress()
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
return (_firstTapTimer.IsStarted() && _firstTapTimer.GetElapsedTime() <= _maxTimeBetweenTaps.Value) || (_secondTapTimer.IsStarted() && _secondTapTimer.GetElapsedTime() <= _maxTimeBetweenTaps.Value);
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool InUse()
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
return _firstTapTimer != null && _secondTapTimer != null && _onDoubleTapDetectedEvent != null;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|