ActivityMessageController.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using KairoEngine.UI;
  8. namespace KairoEngine.UI.ActivityMessages
  9. {
  10. //* This is the simplest possible implementation of this controller
  11. // Todo: Add icons to activity messages
  12. // Todo: Add autohide prorperty to messages
  13. // Todo: Cleanup list of activeMessages that have been destroyed
  14. /// <summary>
  15. /// Controller for showing activity messages to the player.
  16. /// Use the ActivityMessageEvents.Send() to send a message to this controller.
  17. /// </summary>
  18. [HideMonoScript]
  19. public class ActivityMessageController : MonoBehaviour , IUiSystemElement
  20. {
  21. [BoxGroup("Settings")] public string elementTitle = "ActivityMessages";
  22. [BoxGroup("Settings")] public Canvas canvas;
  23. [BoxGroup("Settings")] public bool startVisible = false;
  24. [BoxGroup("Settings")] public int defaultGroupIndex = 0;
  25. [BoxGroup("Settings")] public int defaultPrefabIndex = 0;
  26. [BoxGroup("Settings")] public bool removeOldMessages = true;
  27. [PropertySpace(4,4)]
  28. public List<ActivityMessageGroup> groups = new List<ActivityMessageGroup>();
  29. [PropertySpace(4,4), Tooltip("The GameObject prefab for the ActivityMessage.")]
  30. public List<ActivityMessagePrefabData> prefabs;
  31. [ReadOnly, PropertySpace(4,0)] public List<ActivityMessageUi> activeMessages;
  32. private bool isVisible = true;
  33. private void Start()
  34. {
  35. if(startVisible) UiManager.ShowElement(this);
  36. else UiManager.HideElement(this);
  37. }
  38. private void OnEnable()
  39. {
  40. UiManager.RegisterElement(elementTitle, this, this.transform, isVisible);
  41. ActivityMessageEvents.OnActivityMessage += NewActivityMessage;
  42. ActivityMessageEvents.OnTimedActivityMessage += NewTimedActivityMessage;
  43. ActivityMessageEvents.OnStickyActivityMessage += NewStickyActivityMessage;
  44. ActivityMessageEvents.OnFullMessage += NewFullMessage;
  45. ActivityMessageEvents.OnUpdateActivityMessage += UpdateActivityMessage;
  46. ActivityMessageEvents.OnRemoveActivityMessage += DestroyActivityMessage;
  47. }
  48. private void OnDisable()
  49. {
  50. UiManager.UnregisterElement(this);
  51. ActivityMessageEvents.OnActivityMessage -= NewActivityMessage;
  52. ActivityMessageEvents.OnTimedActivityMessage -= NewTimedActivityMessage;
  53. ActivityMessageEvents.OnStickyActivityMessage -= NewStickyActivityMessage;
  54. ActivityMessageEvents.OnFullMessage -= NewFullMessage;
  55. ActivityMessageEvents.OnUpdateActivityMessage -= UpdateActivityMessage;
  56. ActivityMessageEvents.OnRemoveActivityMessage -= DestroyActivityMessage;
  57. }
  58. public void Hide()
  59. {
  60. isVisible = false;
  61. canvas.gameObject.SetActive(false);
  62. }
  63. public void Show()
  64. {
  65. isVisible = true;
  66. canvas.gameObject.SetActive(true);
  67. }
  68. public bool IsVisible() => isVisible;
  69. private void NewActivityMessage(string msg)
  70. {
  71. ActivityMessage message = new ActivityMessage(msg);
  72. CreateMessage(message, defaultGroupIndex, defaultPrefabIndex).Setup(this, message);
  73. }
  74. private void NewTimedActivityMessage(string msg, float time)
  75. {
  76. ActivityMessage message = new ActivityMessage(msg, "", "", "", null, time, false, true, null, "", "");
  77. CreateMessage(message, defaultGroupIndex, defaultPrefabIndex).Setup(this, message);
  78. }
  79. private void NewStickyActivityMessage(string msg, bool sticky)
  80. {
  81. ActivityMessage message = new ActivityMessage(msg, "", "", "", null, 0f, true, true, null, "", "");
  82. CreateMessage(message, defaultGroupIndex, defaultPrefabIndex).Setup(this, message);
  83. }
  84. private void NewFullMessage(string text, string id, string group, string prefab, Sprite icon, float time, bool sticky, bool hideOnClick,
  85. Action action, string tooltipHeader, string tooltipBody, string tooltipType)
  86. {
  87. ActivityMessage message = new ActivityMessage(text, id, group, prefab, icon, time, sticky, hideOnClick, action, tooltipHeader, tooltipBody, tooltipType);
  88. int groupIndex = GetGroupIndexByTitle(group);
  89. int prefabIndex = GetPrefabIndexByTitle(prefab);
  90. CreateMessage(message, groupIndex, prefabIndex).Setup(this, message);
  91. }
  92. private void UpdateActivityMessage(string id, string newText, Sprite newSprite, float time, bool sticky, bool hideOnClick,
  93. Action action, string tooltipHeader, string tooltipBody, string tooltipType)
  94. {
  95. for (int i = 0; i < activeMessages.Count; i++)
  96. {
  97. if(activeMessages[i].message.text == id || activeMessages[i].message.id == id)
  98. {
  99. activeMessages[i].message.text = newText;
  100. if(newSprite != null) activeMessages[i].message.icon = newSprite;
  101. activeMessages[i].message.time = time;
  102. activeMessages[i].message.sticky = sticky;
  103. activeMessages[i].message.hideOnClick = hideOnClick;
  104. activeMessages[i].message.tooltipHeader = tooltipHeader;
  105. activeMessages[i].message.tooltipBody = tooltipBody;
  106. activeMessages[i].message.tooltipType = tooltipType;
  107. if(action != null) activeMessages[i].message.action = action;
  108. activeMessages[i].Setup(this, activeMessages[i].message);
  109. return;
  110. }
  111. }
  112. }
  113. private void DestroyActivityMessage(string text)
  114. {
  115. for (int i = 0; i < activeMessages.Count; i++)
  116. {
  117. if(activeMessages[i].message.text == text || activeMessages[i].message.id == text)
  118. {
  119. activeMessages[i].Remove();
  120. }
  121. }
  122. }
  123. public void RemoveActivityMessageFromList(ActivityMessage message)
  124. {
  125. for (int i = 0; i < activeMessages.Count; i++)
  126. {
  127. if(activeMessages[i].message.text == message.text || activeMessages[i].message.id == message.id)
  128. {
  129. activeMessages.RemoveAt(i);
  130. }
  131. }
  132. }
  133. private ActivityMessageUi CreateMessage(ActivityMessage message, int groupIndex, int prefabIndex)
  134. {
  135. groups[groupIndex].messages.Add(message);
  136. GameObject obj = Instantiate(prefabs[prefabIndex].prefab, groups[groupIndex].container);
  137. ActivityMessageUi msgUi = obj.GetComponent<ActivityMessageUi>();
  138. activeMessages.Add(msgUi);
  139. if(removeOldMessages) RemoveOldMessages(groupIndex);
  140. return msgUi;
  141. }
  142. public bool GroupExits(string title)
  143. {
  144. for (int i = 0; i < groups.Count; i++)
  145. {
  146. if(groups[i].title == title) return true;
  147. }
  148. return false;
  149. }
  150. public int GetGroupIndexByTitle(string title)
  151. {
  152. for (int i = 0; i < groups.Count; i++)
  153. {
  154. if(groups[i].title == title) return i;
  155. }
  156. return defaultGroupIndex;
  157. }
  158. public bool PrefabExits(string title)
  159. {
  160. for (int i = 0; i < prefabs.Count; i++)
  161. {
  162. if(prefabs[i].title == title) return true;
  163. }
  164. return false;
  165. }
  166. public int GetPrefabIndexByTitle(string title)
  167. {
  168. for (int i = 0; i < prefabs.Count; i++)
  169. {
  170. if(prefabs[i].title == title) return i;
  171. }
  172. return defaultPrefabIndex;
  173. }
  174. public void RemoveOldMessages(int groupIndex)
  175. {
  176. if(groups[groupIndex].container.childCount > groups[groupIndex].maxVisibleMessages)
  177. {
  178. int n = groups[groupIndex].container.childCount - groups[groupIndex].maxVisibleMessages;
  179. for (int i = 0; i < n; i++)
  180. {
  181. Destroy(groups[groupIndex].container.GetChild(0).gameObject);
  182. }
  183. }
  184. if(groups[groupIndex].messages.Count > groups[groupIndex].maxHistoryMessages)
  185. {
  186. int n = groups[groupIndex].messages.Count - groups[groupIndex].maxHistoryMessages;
  187. groups[groupIndex].messages.RemoveRange(0, n);
  188. }
  189. }
  190. }
  191. }