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 @@ ...@@ -4,7 +4,7 @@
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
[RequireComponent(typeof(FactWrapper))] [RequireComponent(typeof(FactWrapper), typeof(RectTransform))]
public class FactFavorisation : MonoBehaviour, IPointerClickHandler public class FactFavorisation : MonoBehaviour, IPointerClickHandler
{ {
#region InspectorVariables #region InspectorVariables
...@@ -20,6 +20,8 @@ public class FactFavorisation : MonoBehaviour, IPointerClickHandler ...@@ -20,6 +20,8 @@ public class FactFavorisation : MonoBehaviour, IPointerClickHandler
#region Variables #region Variables
private GameObject favoriteDisplay; private GameObject favoriteDisplay;
private Fact fact; private Fact fact;
private const float COOLDOWN_DURATION = 0.15f; // cooldown of the double touch
private bool touchOnCooldown = false;
#endregion Variables #endregion Variables
#region Properties #region Properties
...@@ -34,20 +36,18 @@ public bool IsFavorite ...@@ -34,20 +36,18 @@ public bool IsFavorite
#region UnityMethods #region UnityMethods
public void OnPointerClick(PointerEventData eventData) public void OnPointerClick(PointerEventData eventData)
{ {
// TODO: add support for other input systems
if (eventData.button == PointerEventData.InputButton.Middle) if (eventData.button == PointerEventData.InputButton.Middle)
{ {
// write to property to invoke event ToggleFavorite();
IsFavorite = !IsFavorite;
// update favorites list
if (isFavorite)
favorites.Add(fact);
else
favorites.Remove(fact);
} }
} }
private void Update()
{
if (!touchOnCooldown)
HandleTouches();
}
private void Start() private void Start()
{ {
fact = transform.GetComponent<FactWrapper>().fact; fact = transform.GetComponent<FactWrapper>().fact;
...@@ -68,6 +68,28 @@ private void Start() ...@@ -68,6 +68,28 @@ private void Start()
} }
#endregion UnityMethods #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 #region Implementation
private void OnFavoriteChange(Fact changedFact, bool isFavorite) private void OnFavoriteChange(Fact changedFact, bool isFavorite)
{ {
...@@ -82,5 +104,17 @@ private void UpdateDisplay() ...@@ -82,5 +104,17 @@ private void UpdateDisplay()
{ {
favoriteDisplay.SetActive(isFavorite); 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 #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