Select Git revision
WorldFactInteraction.cs
Tobias Schöner authored
The FactExplorer Window shows other facts that reference the selected facts and also shows facts referenced by the selected fact.
WorldFactInteraction.cs 2.66 KiB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// This class handles displaying Fact tooltips, when hovering over a fact in the Gameworld
/// </summary>
public class WorldFactInteraction : MonoBehaviour
{
public LayerMask factLayerMask;
public Transform HidingCanvas;
private GameObject currentDisplay;
private Transform lastHit = null;
void LateUpdate()
{
if (currentDisplay != null && currentDisplay.GetComponent<DragHandling>().dragged)
{
// currently dragging -> remove transparency to indicate dragging and let DragHandling.cs take over
ChangeImageAlpha(currentDisplay.GetComponent<Image>(), 1);
return;
}
UpdateDisplay();
}
private void UpdateDisplay()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, factLayerMask)) // check if fact was hit
{
lastHit = null;
Destroy(currentDisplay); // nothing was hit -> destroy currentDisplay if it exists
return;
}
FactObject factObj = hit.transform.gameObject.GetComponentInChildren<FactObject>();
if (factObj == null)
{
// should never happen, if the layerMask is set up correctly
//Debug.LogError("WorldFactInteraction Raycast collided with object in factLayerMask, that did not contain a FactObject script: " + hit.transform.gameObject.name);
lastHit = null;
return;
}
if (hit.transform != lastHit) // a fact has been hit for the first time -> delete old display and instantiate new one
{
InstantiateNewDisplay(factObj);
}
currentDisplay.transform.position = Input.mousePosition; // move currentDisplay to mousePosition
ChangeImageAlpha(currentDisplay.GetComponent<Image>(), 0.5f); // ensure that image alpha is correct, since it could have changed due to dragging
lastHit = hit.transform;
}
private void InstantiateNewDisplay(FactObject factObj)
{
if (currentDisplay)
Destroy(currentDisplay);
Fact fact = StageStatic.stage.factState[factObj.URI];
// TODO: this link to DisplayFacts is not ideal: maybe refactor to SciptableObject or such
currentDisplay = fact.instantiateDisplay(DisplayFacts.prefabDictionary[fact.GetType()], HidingCanvas);
}
#region Helper
private static void ChangeImageAlpha(Image img, float alpha)
{
img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
}
#endregion Helper
}