using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using KairoEngine.UI; using Sirenix.OdinInspector; namespace KairoEngine.UI.Tooltips { [HideMonoScript, ExecuteInEditMode()] public class Tooltip : MonoBehaviour { public TextMeshProUGUI headerField; public TextMeshProUGUI contentField; public LayoutElement layoutElement; public RectTransform rectTransform; public int characterWrapLimit; public Vector2 positionOffset = new Vector2(10, 10); public string header = ""; private void Awake() { if(rectTransform == null) rectTransform = GetComponent(); } private void Update() { if(headerField == null || contentField == null || layoutElement == null || rectTransform == null) return; if(Application.isEditor) UpdateLayout(); Vector2 position = Input.mousePosition; // Todo: Position tooltip left/right/up/donw the mouse pointer dependending on size and screen position. float pivotX = position.x / Screen.width; float pivotY = position.y / Screen.height; rectTransform.pivot = new Vector2(pivotX, pivotY); position += positionOffset; transform.position = position; } public void SetText(string content, string header = "") { if(string.IsNullOrEmpty(header)) headerField.gameObject.SetActive(false); else { this.header = header; headerField.text = header; headerField.gameObject.SetActive(true); } contentField.text = content; UpdateLayout(); } public void UpdateLayout() { int headerLength = headerField.text.Length; int contentLength = contentField.text.Length; layoutElement.enabled = (headerLength > characterWrapLimit || contentLength > characterWrapLimit) ? true : false; } } }