1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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<string, Tooltip> tooltipList = new Dictionary<string, Tooltip>();
- 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<string, Tooltip> keyValue in instance.tooltipList)
- {
- string key = keyValue.Key;
- if(key == tooltipType || (key == "" && tooltipType == ""))
- {
- currentTooltip = keyValue.Value;
- }
- }
- foreach(KeyValuePair<string, Tooltip> 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;
- }
- }
- }
|