using UnityEngine;
using System.Collections.Generic;
using Lean.Common;
using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute;
namespace Lean.Touch
{
/// This component works like LeanFingerSwipe, but you must manually add fingers from components like LeanFingerDown, LeanFingerDownCanvas, etc.
[HelpURL(LeanTouch.HelpUrlPrefix + "LeanManualSwipe")]
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Manual Swipe")]
public class LeanManualSwipe : LeanSwipeBase
{
/// Ignore fingers with OverGui?
public bool IgnoreIsOverGui { set { ignoreIsOverGui = value; } get { return ignoreIsOverGui; } } [FSA("IgnoreIsOverGui")] [SerializeField] private bool ignoreIsOverGui;
/// If the specified object is set and isn't selected, then this component will do nothing.
public LeanSelectable RequiredSelectable { set { requiredSelectable = value; } get { return requiredSelectable; } } [FSA("RequiredSelectable")] [SerializeField] private LeanSelectable requiredSelectable;
[System.NonSerialized]
private List fingers;
/// This method allows you to manually add a finger.
public void AddFinger(LeanFinger finger)
{
if (fingers == null)
{
fingers = new List();
}
for (var i = fingers.Count - 1; i >= 0; i--)
{
if (fingers[i] == finger)
{
return;
}
}
fingers.Add(finger);
}
/// This method allows you to manually remove a finger.
public void RemoveFinger(LeanFinger finger)
{
fingers.Remove(finger);
}
#if UNITY_EDITOR
protected virtual void Reset()
{
requiredSelectable = GetComponentInParent();
}
#endif
protected virtual void Start()
{
if (requiredSelectable == null)
{
requiredSelectable = GetComponentInParent();
}
}
protected virtual void OnEnable()
{
LeanTouch.OnFingerSwipe += HandleFingerSwipe;
}
protected virtual void OnDisable()
{
LeanTouch.OnFingerSwipe -= HandleFingerSwipe;
}
private void HandleFingerSwipe(LeanFinger finger)
{
// Make sure this finger was manually added
if (fingers != null && fingers.Remove(finger) == true)
{
if (ignoreIsOverGui == true && finger.IsOverGui == true)
{
return;
}
if (requiredSelectable != null && requiredSelectable.IsSelected == false)
{
return;
}
HandleFingerSwipe(finger, finger.StartScreenPosition, finger.ScreenPosition);
}
}
}
}
#if UNITY_EDITOR
namespace Lean.Touch.Editor
{
using TARGET = LeanManualSwipe;
[UnityEditor.CanEditMultipleObjects]
[UnityEditor.CustomEditor(typeof(TARGET))]
public class LeanManualSwipe_Editor : LeanSwipeBase_Editor
{
protected override void OnInspector()
{
TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
Draw("ignoreIsOverGui", "Ignore fingers with OverGui?");
Draw("requiredSelectable", "If the specified object is set and isn't selected, then this component will do nothing.");
base.OnInspector();
}
}
}
#endif