TooltipSystem.cs 2.5 KB

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