2019-11-16 14:53:14 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using SanAndreasUnity.Utilities;
|
2020-04-25 22:16:48 +00:00
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using System.Linq;
|
2019-11-16 14:53:14 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.UI
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class ChatDisplay : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
Queue<Chat.ChatMessage> m_chatMessages = new Queue<Chat.ChatMessage>();
|
|
|
|
|
|
|
|
|
|
public int maxNumChatMessages = 5;
|
2019-11-16 16:09:43 +00:00
|
|
|
|
public float timeToRemoveMessage = 3f;
|
2019-11-16 14:53:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
2021-02-27 22:17:56 +00:00
|
|
|
|
if (!F.IsInHeadlessMode)
|
|
|
|
|
Chat.ChatManager.onChatMessage += OnChatMsg;
|
2019-11-16 14:53:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnChatMsg(Chat.ChatMessage chatMsg)
|
|
|
|
|
{
|
|
|
|
|
if (m_chatMessages.Count >= this.maxNumChatMessages)
|
|
|
|
|
m_chatMessages.Dequeue();
|
|
|
|
|
|
|
|
|
|
m_chatMessages.Enqueue(chatMsg);
|
2019-11-16 16:09:43 +00:00
|
|
|
|
|
|
|
|
|
if (!this.IsInvoking(nameof(RemoveMessage)))
|
|
|
|
|
this.Invoke(nameof(RemoveMessage), this.timeToRemoveMessage);
|
2020-04-25 22:16:48 +00:00
|
|
|
|
|
|
|
|
|
this.UpdateUI();
|
2019-11-16 14:53:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 16:09:43 +00:00
|
|
|
|
void RemoveMessage()
|
|
|
|
|
{
|
|
|
|
|
if (m_chatMessages.Count > 0)
|
|
|
|
|
m_chatMessages.Dequeue();
|
|
|
|
|
|
|
|
|
|
// invoke again if there are more messages
|
|
|
|
|
if (m_chatMessages.Count > 0)
|
|
|
|
|
this.Invoke(nameof(RemoveMessage), this.timeToRemoveMessage);
|
2020-04-25 22:16:48 +00:00
|
|
|
|
|
|
|
|
|
this.UpdateUI();
|
2019-11-16 16:09:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 22:16:48 +00:00
|
|
|
|
void UpdateUI()
|
2019-11-16 14:53:14 +00:00
|
|
|
|
{
|
2020-04-25 22:16:48 +00:00
|
|
|
|
Text[] texts = this.gameObject.GetFirstLevelChildrenComponents<Text>().ToArray();
|
|
|
|
|
Chat.ChatMessage[] chatMessages = m_chatMessages.ToArray();
|
2019-11-16 14:53:14 +00:00
|
|
|
|
|
2020-04-25 22:16:48 +00:00
|
|
|
|
for (int i = 0; i < texts.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i < chatMessages.Length)
|
|
|
|
|
texts[i].text = GetDisplayTextForChatMessage(chatMessages[i]);
|
|
|
|
|
else
|
|
|
|
|
texts[i].text = "";
|
|
|
|
|
}
|
2019-11-16 14:53:14 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 22:16:48 +00:00
|
|
|
|
string GetDisplayTextForChatMessage(Chat.ChatMessage chatMessage)
|
|
|
|
|
{
|
|
|
|
|
return "<color=blue>" + chatMessage.sender + "</color> : " + chatMessage.msg;
|
|
|
|
|
}
|
2019-11-16 14:53:14 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|