mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2025-02-20 14:58:29 +00:00
extract ChatDisplay class
This commit is contained in:
parent
36c96062aa
commit
d703a8cae9
3 changed files with 72 additions and 41 deletions
Assets/Scripts/UI
61
Assets/Scripts/UI/ChatDisplay.cs
Normal file
61
Assets/Scripts/UI/ChatDisplay.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
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;
|
||||
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
Rect rect = GUIUtils.GetCornerRect(ScreenCorner.BottomLeft, new Vector2(width, height), Vector2.one * 50);
|
||||
|
||||
GUILayout.BeginArea(rect);
|
||||
|
||||
foreach (var chatMsg in m_chatMessages)
|
||||
{
|
||||
GUILayout.Label("<color=blue>" + chatMsg.sender + "</color> : " + chatMsg.msg);
|
||||
}
|
||||
|
||||
GUILayout.EndArea();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
11
Assets/Scripts/UI/ChatDisplay.cs.meta
Normal file
11
Assets/Scripts/UI/ChatDisplay.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 812fd88a08aca424f9fb291bb5d7e409
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -26,29 +26,12 @@ namespace SanAndreasUnity.UI {
|
|||
public static Texture2D UpArrowTexture { get; set; }
|
||||
public static Texture2D DownArrowTexture { get; set; }
|
||||
|
||||
public int maxNumChatMessages = 5;
|
||||
|
||||
Queue<Chat.ChatMessage> m_chatMessages = new Queue<Chat.ChatMessage>();
|
||||
|
||||
|
||||
|
||||
void Awake () {
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Chat.ChatManager.onChatMessage += OnChatMsg;
|
||||
}
|
||||
|
||||
void OnChatMsg(Chat.ChatMessage chatMsg)
|
||||
{
|
||||
if (m_chatMessages.Count >= this.maxNumChatMessages)
|
||||
m_chatMessages.Dequeue();
|
||||
|
||||
m_chatMessages.Enqueue(chatMsg);
|
||||
}
|
||||
|
||||
void OnGUI () {
|
||||
|
||||
if (!Loader.HasLoaded)
|
||||
|
@ -67,10 +50,6 @@ namespace SanAndreasUnity.UI {
|
|||
// draw hud
|
||||
DrawHud( this.hudScreenCorner, this.hudSize, this.hudPadding, this.healthColor, this.healthBackgroundColor );
|
||||
|
||||
// draw chat
|
||||
if (! GameManager.IsInStartupScene)
|
||||
DrawChat(m_chatMessages);
|
||||
|
||||
// draw dot in the middle of screen
|
||||
if (this.drawRedDotOnScreenCenter)
|
||||
GUIUtils.DrawRect (GUIUtils.GetCenteredRect (new Vector2 (2f, 2f)), Color.red);
|
||||
|
@ -188,26 +167,6 @@ namespace SanAndreasUnity.UI {
|
|||
GUIUtils.DrawBar (rect, fillPerc, fillColor, backgroundColor, borderWidth);
|
||||
}
|
||||
|
||||
static void DrawChat(Queue<Chat.ChatMessage> chatMessages)
|
||||
{
|
||||
if (chatMessages.Count < 1)
|
||||
return;
|
||||
|
||||
float width = Screen.width * 0.25f;
|
||||
float height = Screen.height * 0.33f;
|
||||
Rect rect = GUIUtils.GetCornerRect(ScreenCorner.BottomLeft, new Vector2(width, height), Vector2.one * 50);
|
||||
|
||||
GUILayout.BeginArea(rect);
|
||||
|
||||
foreach (var chatMsg in chatMessages)
|
||||
{
|
||||
GUILayout.Label("<color=blue>" + chatMsg.sender + "</color> : " + chatMsg.msg);
|
||||
}
|
||||
|
||||
GUILayout.EndArea();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue