using System.Collections; using System.Collections.Generic; using UnityEngine; using KairoEngine.UI; using Sirenix.OdinInspector; namespace KairoEngine.UI.Tooltips { public class TooltipSystem : SerializedMonoBehaviour { public static TooltipSystem instance; [ReadOnly] public Tooltip tooltip; public Dictionary tooltipList = new Dictionary(); public float delayTime = 0.8f; public void Awake() { if(instance == null) instance = this; else Destroy(this.gameObject); if(tooltip == null) tooltip = tooltipList[""]; if(tooltip == null) Debug.LogError("No tooltip is assigned in TooltipSystem", this.gameObject); else tooltip.gameObject.SetActive(false); } public static void Show(string content, string header = "", string tooltipType = "") { if(!HasInstance()) return; Tooltip currentTooltip = null; foreach(KeyValuePair keyValue in instance.tooltipList) { string key = keyValue.Key; if(key == tooltipType || (key == "" && tooltipType == "")) { currentTooltip = keyValue.Value; } } foreach(KeyValuePair keyValue in instance.tooltipList) Hide(keyValue.Value); if(currentTooltip != null) { currentTooltip.SetText(content, header); currentTooltip.gameObject.SetActive(true); instance.tooltip = currentTooltip; } else Debug.LogError($"The Tooltip type named \'{tooltipType}\' could not be found.", instance.gameObject); } public static void Hide(Tooltip tooltip = null) { if(!HasInstance()) return; if(tooltip == null && instance.tooltip == null) return; if(tooltip == null) tooltip = instance.tooltip; tooltip.gameObject.SetActive(false); instance.tooltip = null; } public static bool HasInstance() { if(instance == null) { Debug.LogWarning("There is no active TooltipSystem"); return false; } else return true; } public static float GetDelay() { if(!HasInstance()) return 0.5f; else return instance.delayTime; } } }