TooltipSystem.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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) tooltip = tooltipList[""];
  19. if(tooltip == null) Debug.LogError("No tooltip is assigned in TooltipSystem", this.gameObject);
  20. else tooltip.gameObject.SetActive(false);
  21. }
  22. public static void Show(string content, string header = "", string tooltipType = "")
  23. {
  24. if(!HasInstance()) return;
  25. foreach(KeyValuePair<string, Tooltip> keyValue in instance.tooltipList)
  26. {
  27. string key = keyValue.Key;
  28. if(key == tooltipType)
  29. {
  30. instance.tooltip = keyValue.Value;
  31. }
  32. }
  33. if(instance.tooltip != null)
  34. {
  35. instance.tooltip.SetText(content, header);
  36. instance.tooltip.gameObject.SetActive(true);
  37. }
  38. else Debug.LogError($"The Tooltip type named \'{tooltipType}\' could not be found.", instance.gameObject);
  39. }
  40. public static void Hide()
  41. {
  42. if(!HasInstance()) return;
  43. if(instance.tooltip == null) return;
  44. instance.tooltip.gameObject.SetActive(false);
  45. instance.tooltip = null;
  46. }
  47. public static bool HasInstance()
  48. {
  49. if(instance == null)
  50. {
  51. Debug.LogWarning("There is no active TooltipSystem");
  52. return false;
  53. }
  54. else return true;
  55. }
  56. public static float GetDelay()
  57. {
  58. if(!HasInstance()) return 0.5f;
  59. else return instance.delayTime;
  60. }
  61. }
  62. }