123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using KairoEngine.Core;
- using KairoEngine.UI;
- namespace KairoEngine.UI.ActivityMessages
- {
- //* This is the simplest possible implementation of this controller
- // Todo: Add icons to activity messages
- // Todo: Add autohide prorperty to messages
- // Todo: Cleanup list of activeMessages that have been destroyed
- /// <summary>
- /// Controller for showing activity messages to the player.
- /// Use the ActivityMessageEvents.Send() to send a message to this controller.
- /// </summary>
- [HideMonoScript]
- public class ActivityMessageController : MonoBehaviour , IUiSystemElement
- {
- [BoxGroup("Settings")] public string elementTitle = "ActivityMessages";
- [BoxGroup("Settings")] public Canvas canvas;
- [BoxGroup("Settings")] public bool startVisible = false;
- [BoxGroup("Settings")] public int defaultGroupIndex = 0;
- [BoxGroup("Settings")] public int defaultPrefabIndex = 0;
- [BoxGroup("Settings")] public bool removeOldMessages = true;
- [PropertySpace(4,4)]
- public List<ActivityMessageGroup> groups = new List<ActivityMessageGroup>();
- [PropertySpace(4,4), Tooltip("The GameObject prefab for the ActivityMessage.")]
- public List<ActivityMessagePrefabData> prefabs;
- [ReadOnly, PropertySpace(4,0)] public List<ActivityMessageUi> activeMessages;
- private bool isVisible = true;
- private void Start()
- {
- if(startVisible) UiManager.ShowElement(this);
- else UiManager.HideElement(this);
- }
- private void OnEnable()
- {
- UiManager.RegisterElement(elementTitle, this, this.transform, isVisible);
- ActivityMessageEvents.OnActivityMessage += NewActivityMessage;
- ActivityMessageEvents.OnTimedActivityMessage += NewTimedActivityMessage;
- ActivityMessageEvents.OnStickyActivityMessage += NewStickyActivityMessage;
- ActivityMessageEvents.OnFullMessage += NewFullMessage;
- ActivityMessageEvents.OnUpdateActivityMessage += UpdateActivityMessage;
- ActivityMessageEvents.OnRemoveActivityMessage += DestroyActivityMessage;
- }
- private void OnDisable()
- {
- UiManager.UnregisterElement(this);
- ActivityMessageEvents.OnActivityMessage -= NewActivityMessage;
- ActivityMessageEvents.OnTimedActivityMessage -= NewTimedActivityMessage;
- ActivityMessageEvents.OnStickyActivityMessage -= NewStickyActivityMessage;
- ActivityMessageEvents.OnFullMessage -= NewFullMessage;
- ActivityMessageEvents.OnUpdateActivityMessage -= UpdateActivityMessage;
- ActivityMessageEvents.OnRemoveActivityMessage -= DestroyActivityMessage;
- }
- public void Hide()
- {
- isVisible = false;
- canvas.gameObject.SetActive(false);
- }
- public void Show()
- {
- isVisible = true;
- canvas.gameObject.SetActive(true);
- }
- public bool IsVisible() => isVisible;
- private void NewActivityMessage(string msg)
- {
- ActivityMessage message = new ActivityMessage(msg);
- CreateMessage(message, defaultGroupIndex, defaultPrefabIndex).Setup(this, message);
- }
- private void NewTimedActivityMessage(string msg, float time)
- {
- ActivityMessage message = new ActivityMessage(msg, "", "", "", null, time, false, true, null, "", "");
- CreateMessage(message, defaultGroupIndex, defaultPrefabIndex).Setup(this, message);
- }
- private void NewStickyActivityMessage(string msg, bool sticky)
- {
- ActivityMessage message = new ActivityMessage(msg, "", "", "", null, 0f, true, true, null, "", "");
- CreateMessage(message, defaultGroupIndex, defaultPrefabIndex).Setup(this, message);
- }
- private void NewFullMessage(string text, string id, string group, string prefab, Sprite icon, float time, bool sticky, bool hideOnClick,
- Action action, string tooltipHeader, string tooltipBody, string tooltipType)
- {
- ActivityMessage message = new ActivityMessage(text, id, group, prefab, icon, time, sticky, hideOnClick, action, tooltipHeader, tooltipBody, tooltipType);
- int groupIndex = GetGroupIndexByTitle(group);
- int prefabIndex = GetPrefabIndexByTitle(prefab);
- CreateMessage(message, groupIndex, prefabIndex).Setup(this, message);
- }
- private void UpdateActivityMessage(string id, string newText, Sprite newSprite, float time, bool sticky, bool hideOnClick,
- Action action, string tooltipHeader, string tooltipBody, string tooltipType)
- {
- for (int i = 0; i < activeMessages.Count; i++)
- {
- if(activeMessages[i].message.text == id || activeMessages[i].message.id == id)
- {
- activeMessages[i].message.text = newText;
- if(newSprite != null) activeMessages[i].message.icon = newSprite;
- activeMessages[i].message.time = time;
- activeMessages[i].message.sticky = sticky;
- activeMessages[i].message.hideOnClick = hideOnClick;
- activeMessages[i].message.tooltipHeader = tooltipHeader;
- activeMessages[i].message.tooltipBody = tooltipBody;
- activeMessages[i].message.tooltipType = tooltipType;
- if(action != null) activeMessages[i].message.action = action;
- activeMessages[i].Setup(this, activeMessages[i].message);
- return;
- }
- }
- }
- private void DestroyActivityMessage(string text)
- {
- for (int i = 0; i < activeMessages.Count; i++)
- {
- if(activeMessages[i].message.text == text || activeMessages[i].message.id == text)
- {
- activeMessages[i].Remove();
- }
- }
- }
- public void RemoveActivityMessageFromList(ActivityMessage message)
- {
- for (int i = 0; i < activeMessages.Count; i++)
- {
- if(activeMessages[i].message.text == message.text || activeMessages[i].message.id == message.id)
- {
- activeMessages.RemoveAt(i);
- }
- }
- }
- private ActivityMessageUi CreateMessage(ActivityMessage message, int groupIndex, int prefabIndex)
- {
- groups[groupIndex].messages.Add(message);
- GameObject obj = Instantiate(prefabs[prefabIndex].prefab, groups[groupIndex].container);
- ActivityMessageUi msgUi = obj.GetComponent<ActivityMessageUi>();
- activeMessages.Add(msgUi);
- if(removeOldMessages) RemoveOldMessages(groupIndex);
- return msgUi;
- }
- public bool GroupExits(string title)
- {
- for (int i = 0; i < groups.Count; i++)
- {
- if(groups[i].title == title) return true;
- }
- return false;
- }
- public int GetGroupIndexByTitle(string title)
- {
- for (int i = 0; i < groups.Count; i++)
- {
- if(groups[i].title == title) return i;
- }
- return defaultGroupIndex;
- }
- public bool PrefabExits(string title)
- {
- for (int i = 0; i < prefabs.Count; i++)
- {
- if(prefabs[i].title == title) return true;
- }
- return false;
- }
- public int GetPrefabIndexByTitle(string title)
- {
- for (int i = 0; i < prefabs.Count; i++)
- {
- if(prefabs[i].title == title) return i;
- }
- return defaultPrefabIndex;
- }
- public void RemoveOldMessages(int groupIndex)
- {
- if(groups[groupIndex].container.childCount > groups[groupIndex].maxVisibleMessages)
- {
- int n = groups[groupIndex].container.childCount - groups[groupIndex].maxVisibleMessages;
- for (int i = 0; i < n; i++)
- {
- Destroy(groups[groupIndex].container.GetChild(0).gameObject);
- }
- }
- if(groups[groupIndex].messages.Count > groups[groupIndex].maxHistoryMessages)
- {
- int n = groups[groupIndex].messages.Count - groups[groupIndex].maxHistoryMessages;
- groups[groupIndex].messages.RemoveRange(0, n);
- }
- }
- }
- }
|