12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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<RectTransform>();
- }
- 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;
- }
- }
- }
|