TooltipSystem.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using KairoEngine.UI;
  5. using Sirenix.OdinInspector;
  6. namespace KairoEngine.UI.Tooltips
  7. {
  8. public class TooltipSystem : SerializedMonoBehaviour
  9. {
  10. public static TooltipSystem instance;
  11. [ReadOnly] public Tooltip tooltip;
  12. public Dictionary<string, Tooltip> tooltipList = new Dictionary<string, Tooltip>();
  13. public float delayTime = 0.8f;
  14. public void Awake()
  15. {
  16. if(instance == null) instance = this;
  17. else Destroy(this.gameObject);
  18. if(tooltip == null) Debug.LogError("No tooltip is assigned in TooltipSystem", this.gameObject);
  19. else tooltip.gameObject.SetActive(false);
  20. }
  21. public static void Show(string content, string header = "", string tooltipType = "")
  22. {
  23. if(!HasInstance()) return;
  24. foreach(KeyValuePair<string, Tooltip> keyValue in instance.tooltipList)
  25. {
  26. string key = keyValue.Key;
  27. if(key == tooltipType)
  28. {
  29. instance.tooltip = keyValue.Value;
  30. }
  31. }
  32. if(instance.tooltip != null)
  33. {
  34. instance.tooltip.SetText(content, header);
  35. instance.tooltip.gameObject.SetActive(true);
  36. }
  37. else Debug.LogError($"The Tooltip type named \'{tooltipType}\' could not be found.", instance.gameObject);
  38. }
  39. public static void Hide()
  40. {
  41. if(!HasInstance()) return;
  42. if(instance.tooltip == null) return;
  43. instance.tooltip.gameObject.SetActive(false);
  44. instance.tooltip = null;
  45. }
  46. public static bool HasInstance()
  47. {
  48. if(instance == null)
  49. {
  50. Debug.LogWarning("There is no active TooltipSystem");
  51. return false;
  52. }
  53. else return true;
  54. }
  55. public static float GetDelay()
  56. {
  57. if(!HasInstance()) return 0.5f;
  58. else return instance.delayTime;
  59. }
  60. }
  61. }