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

feat: FactExplorer can be opened on long touch

parent 226d92b1
No related branches found
No related tags found
No related merge requests found
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
}
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