Skip to content
Snippets Groups Projects
Commit 710fd613 authored by Tobias Schöner's avatar Tobias Schöner
Browse files

feat: Android: double-tap fact to favorise it

parent cdca1e3b
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment