/* INFINITY CODE 2018-2019 */
/* http://www.infinity-code.com */
using System;
using InfinityCode.uPano.HotSpots;
using InfinityCode.uPano.InteractiveElements;
using UnityEngine;
namespace InfinityCode.uPano.Actions.HotSpots
{
///
/// Shows and hides tooltips on canvas
///
[AddComponentMenu("uPano/Actions/For HotSpots/Show TextMesh Tooltip")]
public class ShowTextMeshTooltip : HotSpotTooltipAction
{
///
/// Tilt offset relative to HotSpot center
///
public float tiltOffset = 5;
public bool getTextFromElement = false;
///
/// Hides the tooltip for the current HotSpot
///
/// HotSpot
public override void Hide(InteractiveElement element)
{
if (owner != element || instance == null) return;
Destroy(instance);
instance = null;
owner = null;
}
public override void Invoke(HotSpot hotSpot)
{
Show(hotSpot);
}
///
/// Shows the tooltip for the current HotSpot
///
/// HotSpot
public override void Show(InteractiveElement element)
{
Show(element as HotSpot);
}
///
/// Shows the tooltip for the current HotSpot
///
/// HotSpot
public void Show(HotSpot hotSpot)
{
if (tooltipPrefab == null) throw new Exception("Tooltip prefab can not be null");
if (instance != null) Destroy(instance);
string currentText = getTextFromElement ? hotSpot.tooltip : text;
if (string.IsNullOrEmpty(currentText)) return;
instance = Instantiate(tooltipPrefab);
instance.transform.parent = transform;
owner = hotSpot;
instance.transform.position = owner.manager.panoRenderer.GetWorldPosition(owner.pan, owner.tilt + tiltOffset);
instance.transform.LookAt(owner.manager.pano.transform);
Vector3 a = instance.transform.eulerAngles;
instance.transform.eulerAngles = new Vector3(a.x, a.y - 180, a.z);
TextMesh textMesh = instance.GetComponentInChildren();
if (textMesh != null) textMesh.text = currentText;
}
private void Update()
{
if (instance == null || owner == null) return;
instance.transform.position = owner.manager.panoRenderer.GetWorldPosition(owner.pan, owner.tilt + tiltOffset);
instance.transform.LookAt(owner.manager.pano.transform);
Vector3 a = instance.transform.eulerAngles;
instance.transform.eulerAngles = new Vector3(a.x, a.y - 180, a.z);
}
}
}