ActivityMessageUi.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. using KairoEngine.UI.Tooltips;
  9. namespace KairoEngine.UI.ActivityMessages
  10. {
  11. public class ActivityMessageUi : MonoBehaviour
  12. {
  13. public TextMeshProUGUI textUi;
  14. public Image iconImage;
  15. public TooltipTrigger tooltipTrigger;
  16. public Image buttonImage;
  17. public ActivityMessage message;
  18. public bool autoHide = true;
  19. public float hideTimer = 5f;
  20. private float hideCounter = 0f;
  21. public bool fadeIn = true;
  22. public bool fadeOut = true;
  23. public bool hideOnClick = true;
  24. public float fadeTimer = 0.5f;
  25. private float fadeCounter = 0f;
  26. private bool isFading = false;
  27. private bool hasFadedIn = false;
  28. private ActivityMessageController controller;
  29. // private void Update()
  30. // {
  31. // if (fadeIn && !hasFadedIn)
  32. // {
  33. // fadeCounter += Time.deltaTime;
  34. // Color color = textUi.color;
  35. // color.a = fadeCounter / fadeTimer;
  36. // textUi.color = color;
  37. // buttonImage.color = color;
  38. // if (fadeCounter >= fadeTimer)
  39. // {
  40. // fadeCounter = 0f;
  41. // hasFadedIn = true;
  42. // }
  43. // }
  44. // if (autoHide && hasFadedIn || fadeIn == false)
  45. // {
  46. // hideCounter += Time.deltaTime;
  47. // if(hideCounter >= hideTimer)
  48. // {
  49. // hideCounter = 0f;
  50. // isFading = true;
  51. // }
  52. // }
  53. // if (fadeOut && isFading)
  54. // {
  55. // fadeCounter += Time.deltaTime;
  56. // Color color = textUi.color;
  57. // color.a = 1f - (fadeCounter / fadeTimer);
  58. // textUi.color = color;
  59. // buttonImage.color = color;
  60. // if (fadeCounter >= fadeTimer)
  61. // {
  62. // fadeCounter = 0f;
  63. // if(controller != null)
  64. // {
  65. // //controller.messages.Remove(message);
  66. // controller.activeMessages.Remove(this);
  67. // }
  68. // Destroy(this.gameObject);
  69. // }
  70. // }
  71. // }
  72. public void OnClick()
  73. {
  74. if(message.action != null) message.action();
  75. if(hideOnClick)
  76. {
  77. //isFading = true;
  78. Remove();
  79. }
  80. }
  81. public void Setup(ActivityMessageController controller, ActivityMessage newMessage)
  82. {
  83. this.controller = controller;
  84. message = newMessage;
  85. textUi.text = message.text;
  86. hideTimer = message.time;
  87. autoHide = !message.sticky;
  88. hideOnClick = message.hideOnClick;
  89. if(iconImage != null && message.icon != null)
  90. {
  91. iconImage.sprite = message.icon;
  92. iconImage.gameObject.SetActive(true);
  93. }
  94. else if(iconImage != null && message.icon == null) iconImage.gameObject.SetActive(false);
  95. if(!message.sticky)
  96. {
  97. Timer.ExecuteRealTime(message.time * 1000, () => {
  98. Remove();
  99. });
  100. }
  101. if(tooltipTrigger != null && message.tooltipBody != "")
  102. {
  103. tooltipTrigger.header = message.tooltipHeader;
  104. tooltipTrigger.content = message.tooltipBody;
  105. tooltipTrigger.tooltipType = message.tooltipType;
  106. }
  107. else if (tooltipTrigger != null && message.tooltipBody == "")
  108. {
  109. tooltipTrigger.enabled = false;
  110. }
  111. }
  112. public void Remove()
  113. {
  114. controller.RemoveActivityMessageFromList(message);
  115. if(this == null) return;
  116. UiAnimator uiAnimator = gameObject.GetComponent<UiAnimator>();
  117. if(uiAnimator != null)
  118. {
  119. uiAnimator.Disable();
  120. Timer.ExecuteRealTime(uiAnimator.animationTime * 1000, () => {
  121. if(this != null) Destroy(this.gameObject);
  122. });
  123. }
  124. else if(this != null) Destroy(this.gameObject);
  125. }
  126. }
  127. }