diff --git a/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs b/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs index b439daf1376f1993c7ed3143c9e4f9d49b327930..662cf3bf27ad631fdbf3b5bb010d0dc95b2d8091 100644 --- a/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs +++ b/Assets/Scripts/UI/FactExplorer/FactFavorisation.cs @@ -4,7 +4,7 @@ using UnityEngine.Events; using UnityEngine.EventSystems; -[RequireComponent(typeof(FactWrapper))] +[RequireComponent(typeof(FactWrapper), typeof(RectTransform))] public class FactFavorisation : MonoBehaviour, IPointerClickHandler { #region InspectorVariables @@ -20,6 +20,8 @@ public class FactFavorisation : MonoBehaviour, IPointerClickHandler #region Variables private GameObject favoriteDisplay; private Fact fact; + private const float COOLDOWN_DURATION = 0.15f; // cooldown of the double touch + private bool touchOnCooldown = false; #endregion Variables #region Properties @@ -34,20 +36,18 @@ public bool IsFavorite #region UnityMethods public void OnPointerClick(PointerEventData eventData) { - // TODO: add support for other input systems if (eventData.button == PointerEventData.InputButton.Middle) { - // write to property to invoke event - IsFavorite = !IsFavorite; - - // update favorites list - if (isFavorite) - favorites.Add(fact); - else - favorites.Remove(fact); + ToggleFavorite(); } } + private void Update() + { + if (!touchOnCooldown) + HandleTouches(); + } + private void Start() { fact = transform.GetComponent<FactWrapper>().fact; @@ -68,6 +68,28 @@ private void Start() } #endregion UnityMethods + #region TouchControls + private void HandleTouches() + { + if (Input.touchCount != 1) + return; + + var touch = Input.touches[0]; + if (RectTransformUtility.RectangleContainsScreenPoint(transform.GetComponent<RectTransform>(), touch.position) && touch.tapCount == 2) + { + StartCoroutine(Cooldown()); + ToggleFavorite(); + } + } + + private IEnumerator Cooldown() + { + touchOnCooldown = true; + yield return new WaitForSeconds(COOLDOWN_DURATION); + touchOnCooldown = false; + } + #endregion TouchControls + #region Implementation private void OnFavoriteChange(Fact changedFact, bool isFavorite) { @@ -82,5 +104,17 @@ private void UpdateDisplay() { favoriteDisplay.SetActive(isFavorite); } + + private void ToggleFavorite() + { + // write to property to invoke event + IsFavorite = !IsFavorite; + + // update favorites list + if (isFavorite) + favorites.Add(fact); + else + favorites.Remove(fact); + } #endregion Implementation }