SanAndreasUnity/Assets/Scripts/UI/ChatDisplay.cs

80 lines
1.9 KiB
C#
Raw Normal View History

2019-11-16 14:53:14 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Utilities;
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
2019-11-16 15:02:49 +00:00
public ScreenCorner chatAreaCorner = ScreenCorner.BottomLeft;
public Vector2 chatAreaPadding = new Vector2(50, 50);
2019-11-16 14:53:14 +00:00
void Start()
{
Chat.ChatManager.onChatMessage += OnChatMsg;
Behaviours.UIManager.onGUI += OnGUICustom;
}
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);
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);
}
2019-11-16 14:53:14 +00:00
void OnGUICustom()
{
if (! Behaviours.GameManager.IsInStartupScene)
DrawChat();
}
void DrawChat()
{
if (m_chatMessages.Count < 1)
return;
float width = Screen.width * 0.25f;
float height = Screen.height * 0.33f;
2019-11-16 15:02:49 +00:00
Rect rect = GUIUtils.GetCornerRect(this.chatAreaCorner, new Vector2(width, height), this.chatAreaPadding);
2019-11-16 14:53:14 +00:00
GUILayout.BeginArea(rect);
foreach (var chatMsg in m_chatMessages)
{
GUILayout.Label("<color=blue>" + chatMsg.sender + "</color> : " + chatMsg.msg);
}
GUILayout.EndArea();
}
}
}