From 8022f5a871a9eb4581942ca01ddce75f87f6cc0c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20Sch=C3=B6ner?= <tobias.stonehead@gmail.com>
Date: Tue, 14 Feb 2023 17:43:37 +0100
Subject: [PATCH] feat: FactExplorer can be opened on long touch

---
 .../UI/FactExplorer/OpenFactExplorer.cs       | 64 +++++++++++++++++--
 1 file changed, 58 insertions(+), 6 deletions(-)

diff --git a/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs b/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs
index ddd11571..c427ec04 100644
--- a/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs
+++ b/Assets/Scripts/UI/FactExplorer/OpenFactExplorer.cs
@@ -1,3 +1,4 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
@@ -6,23 +7,74 @@
 [RequireComponent(typeof(FactWrapper))]
 public class OpenFactExplorer : MonoBehaviour, IPointerClickHandler
 {
+    #region Variables
     public GameObject factExplorerPrefab;
 
     private static Transform factExplorer;
+    private float pressTime = 0f;
+    private const float LONG_PRESS_DURATION = 0.5f;
+    #endregion Variables
 
+    #region UnityMethods
     public void OnPointerClick(PointerEventData eventData)
     {
-        // TODO: add support for other input systems
+        // open FactExplorer on right click on PC
         if (eventData.button == PointerEventData.InputButton.Right)
         {
-            Destroy(factExplorer != null ? factExplorer.gameObject : null);
+            DoOpenFactExplorer();
+        }
+    }
+
+    private void Update()
+    {
+        // open FactExplorer on press on fact longer than LONG_PRESS_DURATION
+        HandleTouches();
+    }
+    #endregion UnityMethods
+
+    #region Implementation
+    private void HandleTouches()
+    {
+        if (Input.touchCount != 1)
+        {
+            pressTime = 0;
+            return;
+        }
 
-            var parent = transform.GetComponentInParent<Canvas>().transform;
-            var fact = transform.GetComponent<FactWrapper>().fact;
+        var touch = Input.GetTouch(0);
+        if (!RectTransformUtility.RectangleContainsScreenPoint(transform.GetComponent<RectTransform>(), touch.position))
+        {
+            pressTime = 0;
+            return;
+        }
 
-            factExplorer = Instantiate(factExplorerPrefab.transform, Input.mousePosition, Quaternion.identity, parent);
-            factExplorer.GetComponent<FactExplorer>().Initialize(fact, transform.position);
+        switch (touch.phase)
+        {
+            case TouchPhase.Moved:
+            case TouchPhase.Began:
+            case TouchPhase.Ended:
+            case TouchPhase.Canceled:
+                pressTime = 0;
+                break;
+
+            case TouchPhase.Stationary:
+                pressTime += Time.deltaTime;
+                if (pressTime >= LONG_PRESS_DURATION)
+                    DoOpenFactExplorer();
+                break;
         }
     }
+
+    private void DoOpenFactExplorer()
+    {
+        Destroy(factExplorer != null ? factExplorer.gameObject : null);
+
+        var parent = transform.GetComponentInParent<Canvas>().transform;
+        var fact = transform.GetComponent<FactWrapper>().fact;
+
+        factExplorer = Instantiate(factExplorerPrefab.transform, Input.mousePosition, Quaternion.identity, parent);
+        factExplorer.GetComponent<FactExplorer>().Initialize(fact, transform.position);
+    }
+    #endregion Implementation
 }
 
-- 
GitLab